/** * @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 { NopeGenericWrapper } from "../module/GenericWrapper"; import { NopeObservable } from "../observables/nopeObservable"; import { IDispatcherConstructor, 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: IDispatcherConstructor = 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.registerInternalWrapperGenerator( "*", async (dispather, description) => { const mod = new NopeGenericWrapper( 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(); }