VersaWhiteboard/scripts/services/ReadOnlyBackendService.js

74 lines
2.0 KiB
JavaScript
Raw Normal View History

const { v4: uuidv4 } = require("uuid");
class ReadOnlyBackendService {
/**
* Mapping from an editable whiteboard id to the matching read-only whiteboard id
* @type {Map<string, string>}
* @private
*/
#idToReadOnlyId = new Map();
/**
* Mapping from a read-only whiteboard id to the matching editable whiteboard id
*
* @type {Map<string, string>}
* @private
*/
#readOnlyIdToId = new Map();
/**
* Make sure a whiteboardId is ignited in the service
*
* If it's not found in the service, we assume that it's an editable whiteboard
*
* @param {string} whiteboardId
*/
init(whiteboardId) {
const idToReadOnlyId = this.#idToReadOnlyId;
const readOnlyIdToId = this.#readOnlyIdToId;
if (!idToReadOnlyId.has(whiteboardId) && !readOnlyIdToId.has(whiteboardId)) {
const readOnlyId = uuidv4();
idToReadOnlyId.set(whiteboardId, readOnlyId);
readOnlyIdToId.set(readOnlyId, whiteboardId);
}
}
/**
* Get the read-only id corresponding to a whiteboard id
*
* @param {string} whiteboardId
* @return {string}
*/
getReadOnlyId(whiteboardId) {
// make sure it's inited
2020-05-12 22:00:44 +02:00
if (this.isReadOnly(whiteboardId)) return whiteboardId;
// run in isReadOnly
// this.init(whiteboardId);
return this.#idToReadOnlyId.get(whiteboardId);
}
/**
* Get the id corresponding to readonly id
*
* @param {string} readOnlyId
* @return {string}
*/
getIdFromReadOnlyId(readOnlyId) {
return this.#readOnlyIdToId.get(readOnlyId);
}
/**
* Tell is whiteboard id corresponds to a read-only whiteboard
*
* @param whiteboardId
* @return {boolean}
*/
isReadOnly(whiteboardId) {
this.init(whiteboardId);
return this.#readOnlyIdToId.has(whiteboardId);
}
}
module.exports = new ReadOnlyBackendService();