"use strict"; import {SessionModel} from "../Models/SessionModel.ecma.js"; /** * @typedef {import("../Application/AnP.ecma.js").AnP} AnP */ /** * @class SessionsManager * @constructor * @param {!AnP} anp * @return {void} * @access public * @static */ export const SessionsManager = (function(){ /** * @constructs SessionsManager * @param {!AnP} anp * @return {void} * @access private * @static */ const SessionsManager = function(anp){ /** @type {SessionsManager} */ const self = this, /** @type {Object.} */ sessions = {}, /** @type {Object.} */ keys = {}; /** * @returns {void} * @access private */ const constructor = () => {}; /** * @param {!(string|number)} id * @returns {SessionModel|null} * @access public */ const get = id => sessions[id] || sessions[keys[id]] || null; /** * @param {!(Object.|Array.)} inputs * @return {number|null} * @access public */ this.set = inputs => { const key = Common.get_value("key", inputs, null); if(keys[key]) return null; /** @type {number} */ let i; while(sessions[i = Math.random() * (1 << 28) | 0]); if(key) keys[key] = i; return (sessions[i] = new SessionModel(i, inputs)).i; }; /** * @param {!(number|Array.)} ids * @param {!(string|Array.)} permissions * @returns {boolean} * @access public */ this.check_permissions = (ids, permissions) => Common.get_array(ids).some(id => { /** @type {SessionModel|null} */ const session = get(id); return session && session.check_permissions(permissions) }); /** * @param {!(string|number)} id * @returns {boolean} * @access public */ this.close = id => { if(keys[id]){ id = keys[id]; delete keys[id]; }; if(sessions[id]){ delete sessions[id]; return true; }; return false; }; /** * @param {!(string|number)} id * @param {!string} key * @returns {boolean} * @access public */ this.has_value = (id, key) => { /** @type {SessionModel|null} */ const session = get(id); return session ? session.has(key) : false; }; /** * @param {!(string|number)} id * @param {!string} key * @returns {any|null} * @access public */ this.get_value = (id, key) => { /** @type {SessionModel|null} */ const session = get(id); return session ? session.get(key) : null; }; /** * @param {!(string|number)} id * @param {!string} key * @param {?any} [value = null] * @returns {boolean} * @access public */ this.set_value = (id, key, value) => { /** @type {SessionModel|null} */ const session = get(id); return session ? session.set(key, value) : null; }; constructor(); }; return SessionsManager; })();