Use the modern class syntax on the server

This commit is contained in:
lovasoa 2021-02-07 19:22:12 +01:00
parent 53c61ec16e
commit de9f9725e9
No known key found for this signature in database
GPG key ID: AC8DB8E033B44AB8

View file

@ -32,13 +32,15 @@ var fs = require("./fs_promises.js"),
/**
* Represents a board.
* @class
* @constructor
* @typedef {{[object_id:string]: any}} BoardElem
*/
class BoardData {
/**
* @param {string} name
*/
var BoardData = function (name) {
constructor(name) {
this.name = name;
/** @type {{[name: string]: {[object_id:string]: any}}} */
/** @type {{[name: string]: BoardElem}} */
this.board = {};
this.file = path.join(
config.HISTORY_DIR,
@ -46,24 +48,26 @@ var BoardData = function (name) {
);
this.lastSaveDate = Date.now();
this.users = new Set();
};
}
/** Adds data to the board */
BoardData.prototype.set = function (id, data) {
/** Adds data to the board
* @param {string} id
* @param {BoardElem} data
*/
set(id, data) {
//KISS
data.time = Date.now();
this.validate(data);
this.board[id] = data;
this.delaySave();
};
}
/** Adds a child to an element that is already in the board
* @param {string} id - Identifier of the parent element.
* @param {object} child - Object containing the the values to update.
* @param {boolean} [create=true] - Whether to create an empty parent if it doesn't exist
* @param {string} parentId - Identifier of the parent element.
* @param {BoardElem} child - Object containing the the values to update.
* @returns {boolean} - True if the child was added, else false
*/
BoardData.prototype.addChild = function (parentId, child) {
addChild(parentId, child) {
var obj = this.board[parentId];
if (typeof obj !== "object") return false;
if (Array.isArray(obj._children)) obj._children.push(child);
@ -72,14 +76,14 @@ BoardData.prototype.addChild = function (parentId, child) {
this.validate(obj);
this.delaySave();
return true;
};
}
/** Update the data in the board
* @param {string} id - Identifier of the data to update.
* @param {object} data - Object containing the values to update.
* @param {BoardElem} data - Object containing the values to update.
* @param {boolean} create - True if the object should be created if it's not currently in the DB.
*/
BoardData.prototype.update = function (id, data, create) {
update(id, data, create) {
delete data.type;
delete data.tool;
@ -92,59 +96,48 @@ BoardData.prototype.update = function (id, data, create) {
this.board[id] = data;
}
this.delaySave();
};
}
/** Removes data from the board
* @param {string} id - Identifier of the data to delete.
*/
BoardData.prototype.delete = function (id) {
delete(id) {
//KISS
delete this.board[id];
this.delaySave();
};
}
/** Reads data from the board
* @param {string} id - Identifier of the element to get.
* @returns {object} The element with the given id, or undefined if no element has this id
* @returns {BoardElem} The element with the given id, or undefined if no element has this id
*/
BoardData.prototype.get = function (id, children) {
get(id) {
return this.board[id];
};
}
/** Reads data from the board
* @param {string} [id] - Identifier of the first element to get.
* @param {BoardData~processData} callback - Function to be called with each piece of data read
* @returns {BoardElem[]}
*/
BoardData.prototype.getAll = function (id) {
var results = [];
for (var i in this.board) {
if (!id || i > id) {
results.push(this.board[i]);
getAll(id) {
return Object.entries(this.board)
.filter(([i]) => !id || i > id)
.map(([_, elem]) => elem);
}
}
return results;
};
/**
* This callback is displayed as part of the BoardData class.
* Describes a function that processes data that comes from the board
* @callback BoardData~processData
* @param {object} data
*/
/** Delays the triggering of auto-save by SAVE_INTERVAL seconds
*/
BoardData.prototype.delaySave = function (file) {
delaySave() {
if (this.saveTimeoutId !== undefined) clearTimeout(this.saveTimeoutId);
this.saveTimeoutId = setTimeout(this.save.bind(this), config.SAVE_INTERVAL);
if (Date.now() - this.lastSaveDate > config.MAX_SAVE_DELAY)
setTimeout(this.save.bind(this), 0);
};
}
/** Saves the data in the board to a file.
* @param {string} [file=this.file] - Path to the file where the board data will be saved.
*/
BoardData.prototype.save = async function (file) {
async save(file) {
this.lastSaveDate = Date.now();
this.clean();
if (!file) file = this.file;
@ -178,10 +171,10 @@ BoardData.prototype.save = async function (file) {
return;
}
}
};
}
/** Remove old elements from the board */
BoardData.prototype.clean = function cleanBoard() {
clean() {
var board = this.board;
var ids = Object.keys(board);
if (ids.length > config.MAX_ITEM_COUNT) {
@ -193,13 +186,12 @@ BoardData.prototype.clean = function cleanBoard() {
for (var i = 0; i < toDestroy.length; i++) delete board[toDestroy[i]];
log("cleaned board", { removed: toDestroy.length, board: this.name });
}
};
}
/** Reformats an item if necessary in order to make it follow the boards' policy
* @param {object} item The object to edit
* @param {object} parent The parent of the object to edit
*/
BoardData.prototype.validate = function validate(item, parent) {
validate(item) {
if (item.hasOwnProperty("size")) {
item.size = parseInt(item.size) || 1;
item.size = Math.min(Math.max(item.size, 1), 50);
@ -224,12 +216,12 @@ BoardData.prototype.validate = function validate(item, parent) {
this.validate(item._children[i]);
}
}
};
}
/** Load the data in the board from a file.
* @param {string} name - name of the board
*/
BoardData.load = async function loadBoard(name) {
static async load(name) {
var boardData = new BoardData(name),
data;
try {
@ -256,7 +248,8 @@ BoardData.load = async function loadBoard(name) {
}
}
return boardData;
};
}
}
/**
* Given a board file name, return a name to use for temporary data saving.