UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

135 lines (134 loc) 5.9 kB
"use strict"; /** * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2022-01-06 07:15:22 * @modify date 2022-01-06 09:03:30 * @desc [description] */ Object.defineProperty(exports, "__esModule", { value: true }); exports.NopeCore = void 0; const gc_1 = require("../../helpers/gc"); const idMethods_1 = require("../../helpers/idMethods"); const getLogger_1 = require("../../logger/getLogger"); const index_browser_1 = require("../../logger/index.browser"); const pubSub_1 = require("../../pubSub"); const ConnectivityManager_1 = require("../ConnectivityManager"); const InstanceManager_1 = require("../InstanceManager"); const RpcManager_1 = require("../RpcManager"); class NopeCore { constructor(options, generateEmitter, generateObservable, id = null) { var _a; this.options = options; this.generateEmitter = generateEmitter; this.generateObservable = generateObservable; this.id = id; // Store the communicator: this.communicator = options.communicator; if (id == null) { if (options.id) { this.id = options.id; } else { this.id = (0, idMethods_1.generateId)(); } } this._logger = (0, getLogger_1.defineNopeLogger)(options.logger, `core.rpc-manager`); if ((_a = this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.INFO)) { this._logger.info("setting up sub-systems."); } this.eventDistributor = new pubSub_1.PubSubSystem(); this.dataDistributor = new pubSub_1.DataPubSubSystem(); const defaultSelector = (0, RpcManager_1.generateSelector)(options.defaultSelector || "first", this); // Creating the Connectivity Manager: this.connectivityManager = new ConnectivityManager_1.NopeConnectivityManager(options, generateObservable, this.id); // Create our RPC-Manger. this.rpcManager = new RpcManager_1.NopeRpcManager(options, generateObservable, defaultSelector, this.id, this.connectivityManager); // Create our Instance Manager this.instanceManager = new InstanceManager_1.NopeInstanceManager(options, generateEmitter, generateObservable, defaultSelector, this.id, this.connectivityManager, this.rpcManager, this); this.ready = generateObservable(); this.ready.getter = () => { return (this.connectivityManager.ready.getContent() && this.rpcManager.ready.getContent() && this.instanceManager.ready.getContent()); }; const rcvExternally = (0, idMethods_1.generateId)(); // 1. Subscribe to the events: this.communicator.on("event", (msg) => { if (msg.sender !== this.id) { // split the Message in segments const { path, data, ...options } = msg; // Now, we know, that we have updated the data // so, if there is an update, we will prevent this // by setting the sender to the external id options.sender = rcvExternally; // Push the Data. this.eventDistributor.emit(path, data, options); } }); this.eventDistributor.onIncrementalDataChange.subscribe((item) => { if (item.sender !== rcvExternally) { this.communicator.emit("event", { ...item, sender: this.id, }); } }); // Link the Data-Distributor: // 1. Subscribe to the changes: this.communicator.on("dataChanged", (msg) => { if (msg.sender !== this.id) { // split the Message in segments const { path: name, data, ...options } = msg; // We will prevent to options.sender = rcvExternally; // Push the Data. this.dataDistributor.pushData(name, data, options); } }); // 2. Enable emitting the updates this.dataDistributor.onIncrementalDataChange.subscribe((item) => { if (item.sender !== rcvExternally) { this.communicator.emit("dataChanged", { ...item, sender: this.id, }); } }); this.connectivityManager.ready.subscribe((_) => { this.ready.forcePublish(); }); this.rpcManager.ready.subscribe((_) => { this.ready.forcePublish(); }); this.instanceManager.ready.subscribe((_) => { this.ready.forcePublish(); }); this.disposing = false; (0, gc_1.registerGarbageCallback)(this, this.dispose.bind(this)); } // See interface description async dispose() { this.disposing = true; await this.ready.dispose(); await this.eventDistributor.dispose(); await this.dataDistributor.dispose(); await this.connectivityManager.dispose(); await this.rpcManager.dispose(); await this.instanceManager.dispose(); } toDescription() { const ret = { ...this.connectivityManager.info, ready: this.ready.getContent(), bridge: this.options.communicator.toDescription(), eventDistributor: this.eventDistributor.toDescription(), dataDistributor: this.dataDistributor.toDescription(), connectivityManager: this.connectivityManager.toDescription(), rpcManager: this.rpcManager.toDescription(), instanceManager: this.instanceManager.toDescription(), }; return ret; } } exports.NopeCore = NopeCore;