69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
/**
|
|
* @author Martin Karkowski
|
|
* @email m.karkowski@zema.de
|
|
* @create date 2020-11-06 08:52:48
|
|
* @modify date 2021-08-11 19:59:57
|
|
* @desc [description]
|
|
*/
|
|
|
|
import { getSingleton } from "../helpers/singletonMethod";
|
|
import { NopeGenericModule } from "../module/GenericModule";
|
|
import { NopeObservable } from "../observables/nopeObservable";
|
|
import {
|
|
INopeDispatcher,
|
|
INopeDispatcherOptions
|
|
} from "../types/nope/nopeDispatcher.interface";
|
|
import { nopeDispatcher } from "./nopeDispatcher";
|
|
|
|
/**
|
|
* Function to extract a Singleton Dispatcher
|
|
* @param options The provided options for the Dispatcher
|
|
*/
|
|
export function getDispatcher(
|
|
options: INopeDispatcherOptions,
|
|
constructorClass = null,
|
|
singleton = true
|
|
): INopeDispatcher {
|
|
if (constructorClass === null || constructorClass === undefined) {
|
|
constructorClass = nopeDispatcher;
|
|
}
|
|
|
|
const create = () => {
|
|
const dispatcher = new constructorClass(
|
|
options,
|
|
() => new NopeObservable()
|
|
);
|
|
|
|
// Register a default instance generator:
|
|
// Defaultly generate a NopeGenericModule
|
|
dispatcher.registerInternalInstanceGenerator(
|
|
"*",
|
|
async (dispather, description) => {
|
|
const mod = new NopeGenericModule(
|
|
dispather,
|
|
() => new NopeObservable()
|
|
);
|
|
await mod.fromDescription(description, "overwrite");
|
|
// await mod.init();
|
|
return mod;
|
|
}
|
|
);
|
|
|
|
// Return the Dispathcer
|
|
return dispatcher as INopeDispatcher;
|
|
};
|
|
|
|
if (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();
|
|
}
|