2023-10-28 19:15:06 +02:00
|
|
|
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) {
|
2023-10-28 19:15:06 +02:00
|
|
|
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() {
|
2023-10-15 20:37:03 +02:00
|
|
|
this._ids = {};
|
2016-12-19 17:04:09 +01:00
|
|
|
}
|
|
|
|
}
|