Skip to content

Commit

Permalink
Allow passing of options in resolveContainer through to the container…
Browse files Browse the repository at this point in the history
… registrations condition and create callbacks (#139)
  • Loading branch information
bingenito authored Mar 29, 2018
1 parent 55ea990 commit e6b54cc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {};
Expand All @@ -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());
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit e6b54cc

Please sign in to comment.