nope/lib/loader/getPackageLoader.browser.ts

55 lines
1.3 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
*/
import { getSingleton } from "../helpers/singletonMethod";
import {
INopeDispatcherOptions,
INopePackageLoader,
INopePackageLoaderConstructor,
} from "../types/nope";
import { generateNopeBasicPackage } from "./generateNopeBasicPackage";
import { NopePackageLoader } from "./nopePackageLoader";
/**
* Function to extract a Singleton Dispatcher
* @param options The provided options for the Dispatcher
*/
export function getPackageLoader(
options: INopeDispatcherOptions,
singleton = true,
constructorClass: INopePackageLoaderConstructor = null
): INopePackageLoader {
if (constructorClass === null || constructorClass === undefined) {
constructorClass = NopePackageLoader;
}
const create = () => {
// Create a loader
const loader = new constructorClass();
// load the default Package:
loader
.addPackage(generateNopeBasicPackage(options, singleton))
.catch((e) => {
throw e;
});
return loader;
};
if (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();
}