91 lines
2.2 KiB
TypeScript
91 lines
2.2 KiB
TypeScript
/**
|
|
* @author Martin Karkowski
|
|
* @email m.karkowski@zema.de
|
|
* @create date 2020-11-06 08:52:48
|
|
* @modify date 2022-01-10 14:17:09
|
|
* @desc [description]
|
|
*/
|
|
|
|
import { getSingleton } from "../helpers/singletonMethod";
|
|
import { NopeGenericWrapper } from "../module/GenericWrapper";
|
|
import { NopeObservable } from "../observables/nopeObservable";
|
|
import {
|
|
IDispatcherConstructor,
|
|
INopeDispatcher,
|
|
INopeDispatcherOptions,
|
|
} from "../types/nope/nopeDispatcher.interface";
|
|
import { addAllBaseServices } from "./baseServices";
|
|
import { NopeDispatcher } from "./nopeDispatcher";
|
|
|
|
/**
|
|
* Function to extract a Singleton Dispatcher
|
|
* @param dispatcherOptions The provided options for the Dispatcher
|
|
*
|
|
* ```typescript
|
|
*
|
|
* ```
|
|
*/
|
|
export function getDispatcher(
|
|
dispatcherOptions: INopeDispatcherOptions,
|
|
options: {
|
|
constructorClass?: IDispatcherConstructor;
|
|
singleton?: boolean;
|
|
useBaseServices?: boolean;
|
|
} = {}
|
|
): INopeDispatcher {
|
|
if (
|
|
options.constructorClass === null ||
|
|
options.constructorClass === undefined
|
|
) {
|
|
options.constructorClass = NopeDispatcher;
|
|
}
|
|
|
|
options = Object.assign(
|
|
{
|
|
constructorClass: null,
|
|
singleton: true,
|
|
useBaseServices: true,
|
|
},
|
|
options
|
|
);
|
|
|
|
const create = () => {
|
|
const dispatcher = new options.constructorClass(
|
|
dispatcherOptions,
|
|
() => new NopeObservable()
|
|
);
|
|
|
|
// Register a default instance generator:
|
|
// Defaultly generate a NopeGenericModule
|
|
dispatcher.instanceManager.registerInternalWrapperGenerator(
|
|
"*",
|
|
async (core, description) => {
|
|
const mod = new NopeGenericWrapper(core, () => new NopeObservable());
|
|
await mod.fromDescription(description, "overwrite");
|
|
// await mod.init();
|
|
return mod;
|
|
}
|
|
);
|
|
|
|
if (options.useBaseServices) {
|
|
addAllBaseServices(dispatcher);
|
|
}
|
|
|
|
// Return the Dispathcer
|
|
return dispatcher as INopeDispatcher;
|
|
};
|
|
|
|
if (options.singleton) {
|
|
// Create a singaleton if required.
|
|
// use the container to receive the
|
|
// singleton object
|
|
const container = getSingleton("nopeBackendDispatcher.instance", create);
|
|
|
|
return container.instance;
|
|
}
|
|
|
|
// No singleton is required =>
|
|
// create a new instance.
|
|
return create();
|
|
}
|