##// END OF EJS Templates
Corrected method signatures
Corrected method signatures

File last commit:

r5:83fa5814462d default
r6:5683fe88e2a5 default
Show More
Container.ts
54 lines | 2.2 KiB | video/mp2t | TypeScriptLexer
import { ActivationContext } from "./ActivationContext";
import { ActivationError } from "./ActivationError";
import { ContainerBuilder } from "./ContainerBuilder";
import { RegistrationMap, ServiceContainer, ContainerServices, ServiceLocator, IContainerBuilder, Configurable, ContainerKeys} from "./interfaces";
import { LifetimeManager } from "./LifetimeManager";
export class Container<S extends Configurable<S>> implements ServiceContainer<S> {
private readonly _services: Partial<RegistrationMap<ContainerServices<S>>>;
private readonly _lifetimeManager: LifetimeManager;
private _disposed: boolean;
constructor(services: Partial<RegistrationMap<ContainerServices<S>>>, lifetimeManager: LifetimeManager) {
this._services = services;
this._services.container = { activate: () => this as ContainerServices<S>["container"]};
this._services.childContainer = { activate: () => this.createChildContainer() as ContainerServices<S>["childContainer"] };
this._disposed = false;
this._lifetimeManager = lifetimeManager;
}
createChildContainer(): IContainerBuilder<S> {
return new ContainerBuilder(this._services);
}
resolve<K extends keyof ContainerServices<S>>(name: K): NonNullable<ContainerServices<S>[K]>;
resolve<K extends keyof ContainerServices<S>, T>(name: K, def: T): NonNullable<ContainerServices<S>[K]> | T;
resolve<K extends keyof ContainerServices<S>, T>(name: K, def?: T) {
// TODO: add logging
// trace.debug("resolve {0}", name);
const d = this._services[name];
if (d === undefined) {
if (arguments.length > 1)
return def;
else
throw new Error(`Service '${String(name)}' isn't found`);
} else {
const context = new ActivationContext(this._lifetimeManager, this._services, String(name), d);
try {
return d.activate(context);
} catch (error) {
throw new ActivationError(name.toString(), context.getStack(), error);
}
}
}
destroy() {
if (this._disposed)
return;
this._disposed = true;
this._lifetimeManager.destroy();
}
}