nope/lib/loader/getPackageLoader.browser.ts

77 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-11-07 00:45:20 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
*/
import { getSingleton } from "../helpers/singletonMethod";
2022-01-21 15:17:40 +00:00
import {
IDispatcherConstructor,
2022-01-21 15:17:40 +00:00
INopeDispatcherOptions,
INopePackageLoader,
INopePackageLoaderConstructor,
} from "../types/nope";
import { generateNopeBasicPackage } from "./generateNopeBasicPackage";
2020-11-07 00:45:20 +00:00
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.
2020-11-07 00:45:20 +00:00
*/
2021-12-04 07:25:26 +00:00
export function getPackageLoader(
dispatcherOptions: INopeDispatcherOptions,
options: {
packageLoaderConstructorClass?: INopePackageLoaderConstructor;
dispatcherConstructorClass?: IDispatcherConstructor;
singleton?: boolean;
useBaseServices?: boolean;
} = {}
2022-01-21 15:17:40 +00:00
): INopePackageLoader {
if (
options.packageLoaderConstructorClass === null ||
options.packageLoaderConstructorClass === undefined
) {
options.packageLoaderConstructorClass = NopePackageLoader;
2022-01-17 17:06:10 +00:00
}
options = Object.assign(
{
packageLoaderConstructorClass: null,
dispatcherConstructorClass: null,
singleton: true,
useBaseServices: true,
},
options
);
2021-08-17 15:52:46 +00:00
const create = () => {
2020-11-07 00:45:20 +00:00
// Create a loader
const loader = new options.packageLoaderConstructorClass();
2020-11-07 00:45:20 +00:00
// load the default Package:
2021-12-04 07:25:26 +00:00
loader
.addPackage(
generateNopeBasicPackage(dispatcherOptions, options.singleton)
)
2021-12-04 07:25:26 +00:00
.catch((e) => {
throw e;
});
2020-11-07 00:45:20 +00:00
return loader;
2021-08-17 15:52:46 +00:00
};
2020-11-07 00:45:20 +00:00
if (options.singleton) {
2021-08-17 15:52:46 +00:00
// Create a singaleton if required.
// use the container to receive the
// singleton object
const container = getSingleton("nopeBackendPackageLoader.instance", create);
return container.instance;
}
2021-12-04 07:25:26 +00:00
// No singleton is required =>
2021-08-17 15:52:46 +00:00
// create a new instance.
return create();
}