"use strict"; import {WebSocketsClientsAbstract} from "../Abstracts/WebSocketsClientsAbstract.ecma.js"; import {Common} from "../Utils/Common.ecma.js"; import {Check} from "../Utils/Checks.ecma.js"; /** * @typedef {import("../Application/AnP.ecma.js").AnP} AnP */ /** * @class WebSocketsClientsManager * @constructor * @param {!AnP} anp * @param {!string} key * @param {!(Object.|Array.)} inputs * @returns {void} * @access private * @static */ export const WebSocketsClientsManager = (function(){ /** * @callback continue_callback * @param {boolean} ok * @returns {void} */ /** * @callback simple_callback * @returns {void} */ /** * @callback web_socket_constructor_callback * @param {!AnP} anp * @param {!(Object.|Array.)} inputs * @returns {void} */ /** * @constructs WebSocketsClientsManager * @param {!AnP} anp * @param {!string} key * @param {!(Object.|Array.)} inputs * @returns {void} * @access private * @static */ const WebSocketsClientsManager = function(anp, key, inputs){ /** @type {WebSocketsClientsManager} */ const self = this, /** @type {Object.} */ clients = {}; /** * @returns {void} * @access private */ const constructor = () => {}; /** * @param {?continue_callback} callback * @returns {boolean} * @access public */ this.update = (callback = null) => { Common.execute_array(["default_web_sockets_clients_files", "web_sockets_clients_files", "default_web_sockets_clients", "web_sockets_clients"], (key, next) => { self.add(anp.settings.get(key), true, next); }, callback, true); }; /** * @param {?continue_callback} callback * @returns {boolean} * @access public */ this.reset = (callback = null) => { [settings, secrets].forEach(Common.clear_dictionary); self.update(callback); }; /** * @param {?continue_callback} callback * @returns {boolean} * @access public */ this.start = (callback = null) => { /** @type {continue_callback} */ const end = ok => Common.execute(callback, ok); if(started){ end(false); return false; }; started = true; self.update(() => end(true)); return true; }; /** * @param {?continue_callback} callback * @returns {boolean} * @access public */ this.close = (callback = null) => { /** @type {continue_callback} */ const end = ok => Common.execute(callback, ok); if(!started){ end(false); return false; }; started = false; end(true); return true; }; /** * @param {?any} inputs * @param {!boolean} [overwrite = false] * @param {?simple_callback} [callback = null] * @return {void} * @access public */ this.add = (inputs, overwrite = false, callback = null) => { anp.files.load_json(inputs, data => { for(let subinputs of data) Object.entries(subinputs).filter(([key, _]) => ( overwrite || clients[key] === undefined )).forEach(([key, value]) => { try{ /** @type {string|web_socket_constructor_callback|WebSocketsClientsAbstract} */ const Type = Common.get_value("type", ( Check.is_string(value) ? value = {url : value} : value), "WebSocketsDriver"), /** @type {web_socket_constructor_callback|null} */ Model = Check.is_string(Type) ? anp.models.get(Type) : null, /** @type {WebSocketsClientsAbstract} */ web_socket = ( Model ? new Model(anp, key, value) : Check.is_function(Type) ? new Type(anp, key, value) : Type instanceof WebSocketsClientsAbstract ? Type : null); web_socket && (clients[key] = web_socket); }catch(exception){ anp.exception(exception, "anp_web_sockets_clients_manager_add", { key : key }); }; }); Common.execute(callback); }, true); }; this.remove = key => { if(key in clients){ clients[key].close(); delete clients[key]; return true; }; return false; }; this.send = (key, controller, action, data = null, code = 200) => { if(key in clients){ clients[key].send(controller, action, data, code); return true; }; return false; }; this.on_open = (key, callback) => { if(key in clients){ clients[key].on_open.add(callback); return true; }; return false; }; this.on_message = (key, callback) => { if(key in clients){ clients[key].on_message.add(callback); return true; }; return false; }; this.on_error = (key, callback) => { if(key in clients){ clients[key].on_error.add(callback); return true; }; return false; }; this.on_close = (key, callback) => { if(key in clients){ clients[key].on_close.add(callback); return true; }; return false; }; constructor(); }; return WebSocketsClientsManager; })();