/** * @author Martin Karkowski * @email m.karkowski@zema.de */ import { getSingleton } from "../helpers/singletonMethod"; import { IDispatcherConstructor, INopeDispatcherOptions, INopePackageLoader, INopePackageLoaderConstructor, } from "../types/nope"; import { generateNopeBasicPackage } from "./generateNopeBasicPackage"; import { NopePackageLoader } from "./nopePackageLoader"; /** * Function to extract the Package-Loader. * @param {INopeDispatcherOptions} dispatcherOptions The provided options for the Dispatcher * @param options Settings for the creation of the Dispatcher etc. * @returns {INopePackageLoader} The Package loader. */ export function getPackageLoader( dispatcherOptions: INopeDispatcherOptions, options: { packageLoaderConstructorClass?: INopePackageLoaderConstructor; dispatcherConstructorClass?: IDispatcherConstructor; singleton?: boolean; useBaseServices?: boolean; } = {} ): INopePackageLoader { if ( options.packageLoaderConstructorClass === null || options.packageLoaderConstructorClass === undefined ) { options.packageLoaderConstructorClass = NopePackageLoader; } options = Object.assign( { packageLoaderConstructorClass: null, dispatcherConstructorClass: null, singleton: true, useBaseServices: true, }, options ); const create = () => { // Create a loader const loader = new options.packageLoaderConstructorClass(); // load the default Package: loader .addPackage( generateNopeBasicPackage(dispatcherOptions, options.singleton) ) .catch((e) => { throw e; }); return loader; }; if (options.singleton) { // Create a singaleton if required. // use the container to receive the // singleton object const container = getSingleton("nopeBackendPackageLoader.instance", create); return container.instance; } // No singleton is required => // create a new instance. return create(); }