nope-js-browser
Version:
NoPE Runtime for the Browser. For nodejs please use nope-js-node
50 lines (49 loc) • 1.91 kB
JavaScript
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
*/
import { getSingleton } from "../helpers/singletonMethod";
import { generateNopeBasicPackage } from "./generateNopeBasicPackage";
import { NopePackageLoader } from "./nopePackageLoader";
/**
* Function to extract the Package-Loader. This Package-Loader omits methods, that depends on a file-system.
*
* This method should be used in a browser enviroment, otherwise use the function created for the Backend.
*
* @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, options = {}) {
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();
}