diff --git a/src/registry.ts b/src/registry.ts index fa33a990..c2448782 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -3,11 +3,15 @@ import { DefaultContainer } from "./Default/default"; /** Represents the registration details of a container. */ export class ContainerRegistration { - /** Callback used to uniquely identify whether the current scope is for this type of container. */ - condition: () => boolean; + /** Callback used to uniquely identify whether the current scope is for this type of container. + * @param {any} options (Optional) Options provided to {@link resolveContainer}. + */ + condition: (options?: any) => boolean; - /** Factory callback used to create this type of container. */ - create: () => Container; + /** Factory callback used to create this type of container. + * @param {any} options (Optional) Options provided to {@link resolveContainer}. + */ + create: (options?: any) => Container; } const registeredContainers: { [id: string]: ContainerRegistration } = {}; @@ -26,15 +30,39 @@ export let container: Container; * @param {boolean} force (Optional) Determines whether to always create a new instance or returned the cached instance. * @returns {Container} Current concrete container. If no match is found, a DefaultContainer. */ -export function resolveContainer(force?: boolean): Container { +export function resolveContainer(force?: boolean): Container; // tslint:disable-line + +/** Resolve the current container. + * @param {any} options (Optional) Options to pass through to condition and create factory method. + * @returns {Container} Current concrete container. If no match is found, a DefaultContainer. + */ +export function resolveContainer(options?: any): Container; // tslint:disable-line + +/** Resolve the current container. + * @param {boolean} force Determines whether to always create a new instance or returned the cached instance. + * @param {any} options (Optional) Options to pass through to condition and create factory method. + * @returns {Container} Current concrete container. If no match is found, a DefaultContainer. + */ +export function resolveContainer(force: boolean, options? :any): Container; // tslint:disable-line + +export function resolveContainer(param1?: boolean | any, param2?: any): Container { + let force = false; + let options = param2; + + if (typeof param1 === "boolean") { + force = param1; + } else { + options = param1; + } + if (!force && container) { return container; } for (const entry in registeredContainers) { try { - if (registeredContainers[entry].condition()) { - return container = registeredContainers[entry].create(); + if (registeredContainers[entry].condition(options)) { + return container = registeredContainers[entry].create(options); } } catch (e) { console.error("Error resolving container '" + entry + "': " + e.toString()); diff --git a/tests/unit/registry.spec.ts b/tests/unit/registry.spec.ts index 9424190f..9d7b2d45 100644 --- a/tests/unit/registry.spec.ts +++ b/tests/unit/registry.spec.ts @@ -46,5 +46,22 @@ describe("registry", () => { let container: Container = registry.resolveContainer(true); expect(console.error).toHaveBeenCalledWith("Error resolving container 'Test': Error: Forced Error"); }); + + it ("resolveContainer passes options", () => { + const providedOptions = {}; + let condition: boolean = false; + let create: boolean = false; + + registry.registerContainer("Test", + { + condition: (options) => { return condition = (options === providedOptions) }, + create: (options) => { create = (options === providedOptions); return new TestContainer() } + }); + + let container: Container = registry.resolveContainer(true, providedOptions); + + expect(condition).toBeTruthy(); + expect(create).toBeTruthy(); + }); }); });