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

Pull Request for Container onActivation #1125

Closed
wants to merge 7 commits into from
Closed
100 changes: 99 additions & 1 deletion src/container/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,105 @@ class Container implements interfaces.Container {
tempContainer.bind<T>(constructorFunction).toSelf();
return tempContainer.get<T>(constructorFunction);
}

private _lateBoundActivationHandlers: ({
handler: interfaces.OnActivationHandler<any>
}&Required<Omit<interfaces.GlobalOnActivationOptions<any>, "setAncestors">>)[] = [];

private _filterAndApplyOnActivation(
filter: interfaces.GlobalOnActivationFilter<any>,
binding: interfaces.Binding<any>,
removeExisting: boolean,
handler: interfaces.OnActivationHandler<any>) {
const metadata = binding.constraint.metaData;
if (filter(
binding.serviceIdentifier,
binding.scope,
binding.type,
this._getBindingValue(binding),
metadata ? metadata.key : undefined,
metadata ? metadata.value : undefined )
) {
if (binding.onActivation && !removeExisting) {
const existingHandler = binding.onActivation;
binding.onActivation = (context, injectable) => {
return handler(context, existingHandler(context, injectable) );
};
} else {
binding.onActivation = handler;
}
}
}
public onActivation<T>(handler: interfaces.OnActivationHandler<T>, {
removeExisting = false,
filter = () => true,
setAncestors = false
}: interfaces.GlobalOnActivationOptions<T> = {}) {
const setAncestorLevels = typeof setAncestors === "number" ? setAncestors : 0;
let ancestorLevel = 0;
let parentContainer: interfaces.Container|null = this;
while (parentContainer) {
this._lateBoundActivationHandlers.push({
filter,
handler,
removeExisting
});
getBindingDictionary(parentContainer).traverse((sid, bindings) => {
bindings.forEach((b) => {
const metadata = b.constraint.metaData;
if (filter(
b.serviceIdentifier,
b.scope,
b.type,
this._getBindingValue(b),
metadata ? metadata.key : undefined,
metadata ? metadata.value : undefined )
) {
if (b.onActivation && !removeExisting) {
const existingHandler = b.onActivation;
b.onActivation = (context, injectable) => {
return handler(context, existingHandler(context, injectable) );
};
} else {
b.onActivation = handler;
}
}
});
});
if (setAncestors === true || ancestorLevel < setAncestorLevels) {
parentContainer = parentContainer.parent;
ancestorLevel++;
} else {
parentContainer = null;
}
}
}
public autoBind(serviceIdentifier: interfaces.Newable<any>) {
this.bind(serviceIdentifier).toSelf();
const binding = this._bindingDictionary.get(serviceIdentifier).slice(-1)[0];
this._lateBoundActivationHandlers.forEach((h) => {
this._filterAndApplyOnActivation(h.filter, binding, h.removeExisting, h.handler);
});
}
private _getBindingValue(binding: interfaces.Binding<any>) {
switch (binding.type) {
case "ConstantValue":
return binding.cache!;
case "Constructor":
return binding.implementationType!;
case "DynamicValue":
return binding.dynamicValue!;
case "Factory":
return binding.factory!;
case "Function":
return binding.cache!;
case "Instance":
return binding.implementationType!;
case "Provider":
return binding.provider!;
default:
throw new Error("Binding has invalid type");
}
}
private _getContainerModuleHelpersFactory() {

const setModuleId = (bindingToSyntax: any, moduleId: number) => {
Expand Down
18 changes: 16 additions & 2 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace interfaces {
implementationType: Newable<T> | null;
factory: FactoryCreator<any> | null;
provider: ProviderCreator<any> | null;
onActivation: ((context: interfaces.Context, injectable: T) => T) | null;
onActivation: OnActivationHandler<T> | null;
cache: T | null;
}

Expand Down Expand Up @@ -164,6 +164,7 @@ namespace interfaces {
parent: Container | null;
options: ContainerOptions;
bind<T>(serviceIdentifier: ServiceIdentifier<T>): BindingToSyntax<T>;
autoBind(serviceIdentifier: interfaces.Newable<any>): void;
rebind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): interfaces.BindingToSyntax<T>;
unbind(serviceIdentifier: ServiceIdentifier<any>): void;
unbindAll(): void;
Expand All @@ -185,8 +186,21 @@ namespace interfaces {
snapshot(): void;
restore(): void;
createChild(): Container;
onActivation<T>(handler: OnActivationHandler<T>, options?: interfaces.GlobalOnActivationOptions<T>): void;
}
export type OnActivationHandler<T> = (context: interfaces.Context, injectable: T) => T;
export type GlobalOnActivationFilter<T> = (
serviceIdentifier: interfaces.ServiceIdentifier<T>,
scope: interfaces.BindingScope,
type: interfaces.BindingType,
value: any,
metadataKey: number|string|symbol|undefined,
metadataValue: any|undefined) => boolean;
export interface GlobalOnActivationOptions<T> {
removeExisting?: boolean;
filter?: GlobalOnActivationFilter<T>;
setAncestors?: number|boolean;
}

export type Bind = <T>(serviceIdentifier: ServiceIdentifier<T>) => BindingToSyntax<T>;

export type Rebind = <T>(serviceIdentifier: ServiceIdentifier<T>) => BindingToSyntax<T>;
Expand Down
2 changes: 1 addition & 1 deletion src/planning/planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function _getActiveBindings(
typeof target.serviceIdentifier === "function" &&
metadataReader.getConstructorMetadata(target.serviceIdentifier).compilerGeneratedMetadata
) {
context.container.bind(target.serviceIdentifier).toSelf();
context.container.autoBind(target.serviceIdentifier as interfaces.Newable<any>);
bindings = getBindings(context.container, target.serviceIdentifier);
}

Expand Down
2 changes: 1 addition & 1 deletion src/syntax/binding_on_syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BindingOnSyntax<T> implements interfaces.BindingOnSyntax<T> {
this._binding = binding;
}

public onActivation(handler: (context: interfaces.Context, injectable: T) => T): interfaces.BindingWhenSyntax<T> {
public onActivation(handler: interfaces.OnActivationHandler<T>): interfaces.BindingWhenSyntax<T> {
this._binding.onActivation = handler;
return new BindingWhenSyntax<T>(this._binding);
}
Expand Down
Loading