Adding the Option of manually added Instances.

This commit is contained in:
Martin Karkowski 2021-01-12 16:40:41 +01:00
parent 92d1cfcc1d
commit 1c1014c81b
2 changed files with 35 additions and 12 deletions

View File

@ -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<string>;
manual?: boolean;
}
>;
protected _internalInstances: Set<string>;
@ -524,6 +525,17 @@ export class nopeDispatcher implements INopeDispatcher {
}
}
public async registerInstance<I extends INopeModule>(instance: I) {
// Store the Instances.
this._instances.set(instance.identifier, {
instance,
usedBy: [],
manual: true
});
return instance;
}
public async deleteInstance<I extends INopeModule>(
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();
}

View File

@ -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<I extends INopeModule>(instance: I): Promise<boolean>;
/**
* Manually registers an instances. this cannont be removed.
*
* @template I
* @param {I} instance the instance to add
* @return {*} {Promise<void>}
* @memberof INopeDispatcher
*/
registerInstance<I extends INopeModule>(instance: I): Promise<void>;
/**
* Function used, to register a Generator for Instances. This generator
* can be used by other dispatchers, to create an corresponding element.