2020-08-25 22:11:26 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-08-25 23:27:28
|
2020-09-11 07:06:49 +00:00
|
|
|
* @modify date 2020-09-11 09:06:27
|
2020-08-25 22:11:26 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { getSingleton } from "../helpers/singletonMethod";
|
2020-09-11 07:06:49 +00:00
|
|
|
import { nopeObservable } from "../observables/nopeObservable";
|
2020-08-30 08:25:53 +00:00
|
|
|
import { getDispatcher } from "./getDispatcher";
|
2020-09-10 16:21:01 +00:00
|
|
|
import { ICommunicationInterface } from './nopeDispatcher';
|
|
|
|
import { IExportFunctionToDispatcherParameters, IExportMethodToDispatcherParameters, IExportPropertyToDispatcherParameters } from "./nopeDispatcherDecorators";
|
2020-08-25 22:11:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a Dispatcher.
|
|
|
|
* @param communicator
|
|
|
|
*/
|
|
|
|
export function getLinkedDispatcher(communicator: ICommunicationInterface) {
|
2020-09-10 16:21:01 +00:00
|
|
|
const container = getSingleton('nopeBackendDispatcher.container', () => {
|
2020-08-25 22:11:26 +00:00
|
|
|
return {
|
|
|
|
methods: new Map<string, {
|
|
|
|
uri: string,
|
|
|
|
callback: (...args) => Promise<any>,
|
|
|
|
options: IExportMethodToDispatcherParameters
|
|
|
|
}>(),
|
|
|
|
parameters: new Map<string, {
|
|
|
|
uri: string,
|
2020-09-11 07:06:49 +00:00
|
|
|
item: nopeObservable<any>,
|
2020-08-25 22:11:26 +00:00
|
|
|
options: IExportPropertyToDispatcherParameters
|
2020-09-10 16:21:01 +00:00
|
|
|
}>(),
|
|
|
|
functions: new Map<string, {
|
|
|
|
uri: string,
|
|
|
|
callback: (...args) => Promise<any>,
|
|
|
|
options: IExportFunctionToDispatcherParameters
|
2020-08-25 22:11:26 +00:00
|
|
|
}>()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const dispatcher = getDispatcher(communicator);
|
|
|
|
|
2020-09-10 16:21:01 +00:00
|
|
|
// Iterate over the Methods
|
2020-08-25 22:11:26 +00:00
|
|
|
for (const [uri, settings] of container.instance.methods.entries()) {
|
|
|
|
dispatcher.registerFunction(settings.callback, {
|
|
|
|
id: uri
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-10 16:21:01 +00:00
|
|
|
// Iterate over the Functions
|
|
|
|
for (const [uri, settings] of container.instance.functions.entries()) {
|
|
|
|
dispatcher.registerFunction(settings.callback, {
|
|
|
|
id: uri
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the Functions
|
2020-09-11 07:06:49 +00:00
|
|
|
for (const [uri, settings] of container.instance.parameters.entries()) {
|
|
|
|
|
|
|
|
const _pathes = {
|
|
|
|
get: uri + '.get',
|
|
|
|
set: uri + '.set',
|
|
|
|
subscribe: uri + '.subscribe'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register Getter:
|
|
|
|
dispatcher.registerFunction(async () => {
|
|
|
|
return settings.item.getContent()
|
|
|
|
}, {
|
|
|
|
id: _pathes.get
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register Setter:
|
|
|
|
dispatcher.registerFunction(async (content, ...args) => {
|
|
|
|
return settings.item.setContent(content, ...args)
|
|
|
|
}, {
|
|
|
|
id: _pathes.set
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register Subscription:
|
|
|
|
dispatcher.registerFunction(async (oberserver, ...args) => {
|
|
|
|
return settings.item.subscribe(oberserver, ...args)
|
|
|
|
}, {
|
|
|
|
id: _pathes.subscribe
|
|
|
|
});
|
|
|
|
}
|
2020-09-10 16:21:01 +00:00
|
|
|
|
2020-08-25 22:11:26 +00:00
|
|
|
return dispatcher;
|
|
|
|
}
|
|
|
|
|