2020-11-06 08:10:30 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
*/
|
|
|
|
|
2020-08-30 08:25:53 +00:00
|
|
|
import { getSingleton } from "../helpers/singletonMethod";
|
2021-11-12 07:57:03 +00:00
|
|
|
import { NopeGenericWrapper } from "../module/GenericWrapper";
|
2020-10-15 09:38:59 +00:00
|
|
|
import { NopeObservable } from "../observables/nopeObservable";
|
2021-09-03 05:42:37 +00:00
|
|
|
import {
|
2021-11-12 07:57:03 +00:00
|
|
|
IDispatcherConstructor,
|
2021-09-03 05:42:37 +00:00
|
|
|
INopeDispatcher,
|
2021-12-04 07:25:26 +00:00
|
|
|
INopeDispatcherOptions,
|
2021-09-03 05:42:37 +00:00
|
|
|
} from "../types/nope/nopeDispatcher.interface";
|
2022-01-16 19:38:45 +00:00
|
|
|
import { addAllBaseServices } from "./baseServices";
|
2022-01-07 17:12:08 +00:00
|
|
|
import { NopeDispatcher } from "./nopeDispatcher";
|
2020-08-30 08:25:53 +00:00
|
|
|
|
|
|
|
/**
|
2022-01-17 18:23:00 +00:00
|
|
|
*
|
2022-04-08 06:10:39 +00:00
|
|
|
* @param {INopeDispatcherOptions} dispatcherOptions The options, that will be used for the dispathcer.
|
|
|
|
* @param options Additional Options. You can provide a different Dispatcher-Class; Controll the scope (Singleton or not.) and define wehter the Base-Services should be added.
|
|
|
|
* @returns {INopeDispatcher} The dispatcher.
|
2022-01-17 18:23:00 +00:00
|
|
|
*
|
2022-04-08 06:10:39 +00:00
|
|
|
* ```typescript
|
|
|
|
* // Create a communication layer:
|
|
|
|
* const communicator = getLayer("event");
|
|
|
|
* // Now create the Dispatcher.
|
|
|
|
* const dispatcher = getDispatcher({communicator});
|
2022-01-17 18:23:00 +00:00
|
|
|
* ```
|
2020-08-30 08:25:53 +00:00
|
|
|
*/
|
2021-09-03 05:42:37 +00:00
|
|
|
export function getDispatcher(
|
2022-01-20 11:59:32 +00:00
|
|
|
dispatcherOptions: INopeDispatcherOptions,
|
|
|
|
options: {
|
2022-04-08 06:10:39 +00:00
|
|
|
dispatcherConstructorClass?: IDispatcherConstructor;
|
2022-01-20 11:59:32 +00:00
|
|
|
singleton?: boolean;
|
|
|
|
useBaseServices?: boolean;
|
|
|
|
} = {}
|
2021-09-03 05:42:37 +00:00
|
|
|
): INopeDispatcher {
|
2022-01-20 11:59:32 +00:00
|
|
|
if (
|
2022-04-08 06:10:39 +00:00
|
|
|
options.dispatcherConstructorClass === null ||
|
|
|
|
options.dispatcherConstructorClass === undefined
|
2022-01-20 11:59:32 +00:00
|
|
|
) {
|
2022-04-08 06:10:39 +00:00
|
|
|
options.dispatcherConstructorClass = NopeDispatcher;
|
2021-08-17 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:59:32 +00:00
|
|
|
options = Object.assign(
|
|
|
|
{
|
|
|
|
constructorClass: null,
|
|
|
|
singleton: true,
|
|
|
|
useBaseServices: true,
|
|
|
|
},
|
|
|
|
options
|
|
|
|
);
|
|
|
|
|
2021-08-17 15:52:46 +00:00
|
|
|
const create = () => {
|
2022-04-08 06:10:39 +00:00
|
|
|
const dispatcher = new options.dispatcherConstructorClass(
|
2022-01-20 11:59:32 +00:00
|
|
|
dispatcherOptions,
|
2021-09-03 05:42:37 +00:00
|
|
|
() => new NopeObservable()
|
|
|
|
);
|
2020-11-07 00:45:20 +00:00
|
|
|
|
|
|
|
// Register a default instance generator:
|
|
|
|
// Defaultly generate a NopeGenericModule
|
2022-01-10 06:52:05 +00:00
|
|
|
dispatcher.instanceManager.registerInternalWrapperGenerator(
|
2020-12-04 18:10:33 +00:00
|
|
|
"*",
|
2022-01-10 06:52:05 +00:00
|
|
|
async (core, description) => {
|
|
|
|
const mod = new NopeGenericWrapper(core, () => new NopeObservable());
|
2020-12-04 18:10:33 +00:00
|
|
|
await mod.fromDescription(description, "overwrite");
|
|
|
|
// await mod.init();
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
);
|
2020-11-07 00:45:20 +00:00
|
|
|
|
2022-01-20 11:59:32 +00:00
|
|
|
if (options.useBaseServices) {
|
|
|
|
addAllBaseServices(dispatcher);
|
|
|
|
}
|
2022-01-16 19:38:45 +00:00
|
|
|
|
2020-11-07 00:45:20 +00:00
|
|
|
// Return the Dispathcer
|
2020-11-11 16:08:11 +00:00
|
|
|
return dispatcher as INopeDispatcher;
|
2021-08-17 15:52:46 +00:00
|
|
|
};
|
|
|
|
|
2022-01-20 11:59:32 +00:00
|
|
|
if (options.singleton) {
|
2021-08-17 15:52:46 +00:00
|
|
|
// Create a singaleton if required.
|
|
|
|
// use the container to receive the
|
|
|
|
// singleton object
|
|
|
|
const container = getSingleton("nopeBackendDispatcher.instance", create);
|
|
|
|
|
|
|
|
return container.instance;
|
|
|
|
}
|
|
|
|
|
2021-09-03 05:42:37 +00:00
|
|
|
// No singleton is required =>
|
2021-08-17 15:52:46 +00:00
|
|
|
// create a new instance.
|
|
|
|
return create();
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|