nope/modules/mod-Publish-And-Subscribe-System/test/Obeservable-Test.ts

141 lines
4.0 KiB
TypeScript
Raw Normal View History

2020-08-30 07:45:44 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2018-05-22 01:13:13
2020-09-01 15:05:06 +00:00
* @modify date 2020-09-01 17:04:38
2020-08-30 07:45:44 +00:00
* @desc [description]
*/
/** Clear the Screen */
declare const process: any;
import { inject, injectable } from 'inversify';
import { Builder } from '../../mod-Assembly-Builder/src/Container-Builder.FileLoader';
import * as PUBSUB from '../assembly/manual-assembly';
/** Trigger Loading the Modules */
Builder.load();
Builder.on('loaded', () => {
/** Define a Class containing an observable @see attribute_01 */
@injectable()
class TestClass implements PUBSUB.ContainsSharedObservables {
path: string = '';
observables: Map<string, PUBSUB.Observable<any>>;
shareConfig: { [id: string]: PUBSUB.ShareConfig };
initObservables(): void {
/** Set a Value */
this.attribute_01.value = 0;
}
/**
* Defining the Attribute as observable
*
* @type {PUBSUB.Observable<number>}
* @memberof TestClass
*/
@inject(PUBSUB.TYPES.Observable)
public attribute_01: PUBSUB.Observable<number>;
/**
* Creates an instance of TestClass.
* @memberof TestClass
*/
constructor() {
/** Set a Values - That must be done during the construction */
this.observables = new Map<string, PUBSUB.Observable<any>>();
this.shareConfig = {};
this.shareConfig.attribute_01 = {
useAutoPathForPublishing: true,
useAutoPathForSubscribing: true,
};
}
}
/** Create a Definition for the Builder */
const _definition = [{
selector: 'Test',
type: TestClass
}];
/** Add the Definition to the Builder */
Builder.instance.addElements(_definition);
/** Generate an Object of the Test-Class */
const _test = Builder.instance.container.get<TestClass>("Test");
let _counter = 0;
/** Subscribe tot the attribute */
_test.attribute_01.subscribe((_num, _sender, _topic) => {
console.log('Calling Subscription of', _topic, ' the ' + _counter + '\'th time. Value changed to:', _num);
_counter++;
}, 'sync');
_test.path = 'test';
/** Set the value to trigger the execution of the Subscription */
console.log('Changing Value to 1337');
_test.attribute_01.value = 1337;
console.log('Adding Setter');
/** Define a specialized Setter */
_test.attribute_01.setter = (value) => {
console.log('In Setter of the attribute_01');
/** The value should be limeted to Min 0 or Max 100 */
if (value > 100) {
return 100;
}
if (value < 0) {
return 0;
}
return value;
}
/** Setting the Attribute to change the Value and trigger the setter and subscription */
console.log('Changing Value to 133123');
2020-09-01 15:05:06 +00:00
_test.attribute_01.setContent(133123);
console.log('Reading after setter', _test.attribute_01.getContent());
2020-08-30 07:45:44 +00:00
/** Get the underlying Publish-and-Subscribe System */
const _pubSub = Builder.instance.container.get<PUBSUB.PubSubSystem>(PUBSUB.TYPES.PubSubSystem);
const _sub = _pubSub.createSubscription('test.attribute_01.value.done', content => {
console.log(content);
})
/** Publish data to change the Attribute */
_pubSub.createSubscription(_test.shareConfig.attribute_01.publishToPath as string, (value) => console.log('Channel "test.path.to.publish" received', value));
console.log('plublish 1336 on', _test.shareConfig.attribute_01.publishToPath as string)
_pubSub.createPublisher<number>(_test.shareConfig.attribute_01.subscribeToPath as string).publishData(1336);
/** Publish data to change the Attribute */
console.log(_test.shareConfig.attribute_01.subscribeToPath, 'EXTERNALLY')
_pubSub.createPublisher<number>(_test.shareConfig.attribute_01.subscribeToPath as string).publishData(12, 'ME');
});