Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing of options in resolveContainer through to the container registrations condition and create callbacks #139

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});