From 1c1014c81b0f1287cc00fb0c959103a64338df9d Mon Sep 17 00:00:00 2001 From: Martin Karkowski Date: Tue, 12 Jan 2021 16:40:41 +0100 Subject: [PATCH] Adding the Option of manually added Instances. --- lib/dispatcher/nopeDispatcher.ts | 35 +++++++++++++++------- lib/types/nope/nopeDispatcher.interface.ts | 12 +++++++- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/dispatcher/nopeDispatcher.ts b/lib/dispatcher/nopeDispatcher.ts index 50866a9..aa210e6 100644 --- a/lib/dispatcher/nopeDispatcher.ts +++ b/lib/dispatcher/nopeDispatcher.ts @@ -2,7 +2,7 @@ * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2020-10-12 18:52:00 - * @modify date 2021-01-08 10:56:04 + * @modify date 2021-01-12 16:38:23 * @desc [description] */ @@ -406,6 +406,7 @@ export class nopeDispatcher implements INopeDispatcher { { instance: INopeModule; usedBy: Array; + manual?: boolean; } >; protected _internalInstances: Set; @@ -524,6 +525,17 @@ export class nopeDispatcher implements INopeDispatcher { } } + public async registerInstance(instance: I) { + // Store the Instances. + this._instances.set(instance.identifier, { + instance, + usedBy: [], + manual: true + }); + + return instance; + } + public async deleteInstance( instance: I | string, preventSendingUpdate = false @@ -1042,7 +1054,7 @@ export class nopeDispatcher implements INopeDispatcher { } // Check if the Element isnt required any more => delete it. - if (instance.usedBy.length === 0) { + if (instance.usedBy.length === 0 && !instance.manual) { this._logger.info( "Disposing instance, because it isnt used any more.", instance.instance.identifier @@ -1742,15 +1754,16 @@ export class nopeDispatcher implements INopeDispatcher { // received value if (!options.preventSendingUpdateOnNewSubscription) { // Get the available data. - // First try to get current source-data (which in theory should be) - // Up-to-date. - // If this data isnt available try to use the lastly published data. - const _sourceData = this._lastPublishedEvent.get(_subTopic); - const data = - _sourceData !== null && _sourceData !== undefined - ? _sourceData - : _externalSource.getContent(); - if (data) { + // try to use the lastly published data. + const data = this._lastPublishedEvent.get(_subTopic); + + // This Step is not clear. it should be prevented. Otherwise inconsistent + // bahavoir. Commented out for testing purposes. + // const data = + // _sourceData !== null && _sourceData !== undefined + // ? _sourceData + // : _externalSource.getContent(); + if (typeof data !== "undefined" && data !== null) { if (!observable.setContent(data.data, _this.id, data.timestamp)) { observable.forcePublish(); } diff --git a/lib/types/nope/nopeDispatcher.interface.ts b/lib/types/nope/nopeDispatcher.interface.ts index b3352ae..69c3d8a 100644 --- a/lib/types/nope/nopeDispatcher.interface.ts +++ b/lib/types/nope/nopeDispatcher.interface.ts @@ -2,7 +2,7 @@ * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2020-10-12 18:31:11 - * @modify date 2021-01-04 14:47:20 + * @modify date 2021-01-12 16:39:41 * @desc [description] */ @@ -253,6 +253,16 @@ export interface INopeDispatcher { */ deleteInstance(instance: I): Promise; + /** + * Manually registers an instances. this cannont be removed. + * + * @template I + * @param {I} instance the instance to add + * @return {*} {Promise} + * @memberof INopeDispatcher + */ + registerInstance(instance: I): Promise; + /** * Function used, to register a Generator for Instances. This generator * can be used by other dispatchers, to create an corresponding element.