##// END OF EJS Templates
improved typings
improved typings

File last commit:

r5:83fa5814462d default
r7:58282d42a47b default
Show More
Container.ts
54 lines | 2.2 KiB | video/mp2t | TypeScriptLexer
cin
initial commit
r0 import { ActivationContext } from "./ActivationContext";
import { ActivationError } from "./ActivationError";
cin
Working on container builder
r5 import { ContainerBuilder } from "./ContainerBuilder";
import { RegistrationMap, ServiceContainer, ContainerServices, ServiceLocator, IContainerBuilder, Configurable, ContainerKeys} from "./interfaces";
cin
initial commit
r0 import { LifetimeManager } from "./LifetimeManager";
cin
Working on container builder
r5 export class Container<S extends Configurable<S>> implements ServiceContainer<S> {
cin
working on fluent configuration
r1 private readonly _services: Partial<RegistrationMap<ContainerServices<S>>>;
cin
initial commit
r0
cin
working on fluent configuration
r1 private readonly _lifetimeManager: LifetimeManager;
cin
initial commit
r0
cin
working on fluent configuration
r1 private _disposed: boolean;
cin
initial commit
r0
cin
Working on container builder
r5 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"] };
cin
initial commit
r0 this._disposed = false;
cin
Working on container builder
r5 this._lifetimeManager = lifetimeManager;
cin
initial commit
r0 }
cin
Working on container builder
r5 createChildContainer(): IContainerBuilder<S> {
return new ContainerBuilder(this._services);
cin
initial commit
r0 }
cin
Working on container builder
r5 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) {
cin
working on fluent configuration
r1 // TODO: add logging
// trace.debug("resolve {0}", name);
cin
initial commit
r0 const d = this._services[name];
if (d === undefined) {
if (arguments.length > 1)
return def;
else
cin
working on fluent configuration
r1 throw new Error(`Service '${String(name)}' isn't found`);
cin
initial commit
r0 } else {
cin
Working on container builder
r5 const context = new ActivationContext(this._lifetimeManager, this._services, String(name), d);
cin
initial commit
r0 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();
}
}