UNPKG

nope-js-node

Version:

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

122 lines (121 loc) 4.01 kB
"use strict"; /** * @author Martin Karkowski * @email m.karkowski@zema.de */ Object.defineProperty(exports, "__esModule", { value: true }); exports.NopeObservable = void 0; const nopeEventEmitter_1 = require("../eventEmitter/nopeEventEmitter"); const idMethods_1 = require("../helpers/idMethods"); const objectMethods_1 = require("../helpers/objectMethods"); const getLogger_1 = require("../logger/getLogger"); const logger = (0, getLogger_1.getNopeLogger)("obervable"); /** * RsJX based Observable. * * Contains additional Functionalities like: * - property with the current value * - function to publish values. (wrapper for next) * - enables performing a subscription with synced call or a immediate call. */ class NopeObservable extends nopeEventEmitter_1.NopeEventEmitter { get observable() { return this._emitter; } setContent(value, options = {}) { return this._emit(value, options); } /** * Function to set the content of the Observable * @param value * @param sender * @param timeStamp * @param data */ _emit(value, options = {}) { var _a; // Change the Value. if (this.setter !== null) { const adapted = this.setter(value, options); if (!adapted.valid) { return false; } this._value = adapted.data; } else { // Adapt the Value if required. this._value = value; } const valueToPublish = this.getContent(); // Publish the Data, but only if they are different (deeply) if (!this.disablePublishing && (options.forced || !(0, objectMethods_1.deepEqual)((_a = this._emitter.value) === null || _a === void 0 ? void 0 : _a.value, valueToPublish))) { return this._publish(valueToPublish, options); } return false; } /** * Internal Function to Publish content * * @author M.Karkowski * @protected * @param {G} value The value to use. * @param {Partial<AD>} [options={}] * @return {boolean} * @memberof NopeObservable */ _publish(value, options = {}) { // Only Proceed if Publishing is required. if (options.forced || this.disablePublishing === false) { options = this._updateSenderAndTimestamp(options); // Define the value. this._emitter.next({ value, ...options }); return this.hasSubscriptions; } return false; } /** * Function to Force an Update * * @author M.Karkowski * @param {Partial<AD>} options Options which might be relevant * @return {boolean} * @memberof NopeObservable */ forcePublish(options = {}) { options.forced = true; return this._publish(this.getContent(), options); } /** * Function to extract the Content. * If a Getter is provided, the Getter will be used * to Transform the item. */ getContent() { if (this.getter !== null) return this.getter(this._value); return this._value; } /** * A Function to subscribe to updates of the Observable. * @param observer The Observer. Could be a Function or a Partial Observer. * @param mode The Mode of the Subscription * @param options Additional Options. */ subscribe(observer, options = { type: "sync", mode: ["direct", "sub", "super"], skipCurrent: false, }) { return this._subscribe(observer, options); } constructor(_options = {}) { super(Object.assign(_options, { showCurrent: true })); this._options = _options; this.id = (0, idMethods_1.generateId)(); this.options = { generateTimeStamp: true, }; } } exports.NopeObservable = NopeObservable;