decide.nolog.cz/mirage/identity-managers/poll.js

57 lines
979 B
JavaScript
Raw Normal View History

import { generatePassphrase } from '../../utils/encryption';
2016-12-19 17:04:09 +01:00
export default class {
constructor() {
this.reset();
}
/**
* Returns an unique identifier.
*
* @method fetch
* @param {Object} data Records attributes hash
* @return {String} Unique identifier
* @public
*/
fetch() {
let id;
while (id === undefined) {
let randomString = generatePassphrase().substring(0, 10);
2016-12-19 17:04:09 +01:00
if (this._ids[randomString] === undefined) {
id = randomString;
}
}
this._ids[id] = true;
return id;
}
/**
* Register an identifier.
* Must throw if identifier is already used.
*
* @method set
* @param {String|Number} id
* @public
*/
set(id) {
if (typeof this._ids[id] !== 'undefined') {
throw new Error(`Id {id} is already used.`);
}
this._ids[id] = true;
}
/**
* Reset identity manager.
*
* @method reset
* @public
*/
reset() {
this._ids = {};
2016-12-19 17:04:09 +01:00
}
}