Container.ts
177 lines
| 6.5 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r49 | import { ActivationContext } from "./ActivationContext"; | ||
| import { ValueDescriptor } from "./ValueDescriptor"; | ||||
| import { ActivationError } from "./ActivationError"; | ||||
|
|
r144 | import { ServiceMap, Descriptor, PartialServiceMap, ContainerServiceMap, ContainerKeys, TypeOfService, ServiceContainer } from "./interfaces"; | ||
|
|
r49 | import { TraceSource } from "../log/TraceSource"; | ||
|
|
r120 | import { Configuration, RegistrationMap } from "./Configuration"; | ||
|
|
r49 | import { Cancellation } from "../Cancellation"; | ||
|
|
r146 | import { ICancellation } from "../interfaces"; | ||
|
|
r118 | import { isDescriptor } from "./traits"; | ||
|
|
r131 | import { LifetimeManager } from "./LifetimeManager"; | ||
|
|
r142 | import { each, isString } from "../safe"; | ||
| import { ContainerConfiguration, FluentRegistrations } from "./fluent/interfaces"; | ||||
|
|
r135 | import { FluentConfiguration } from "./fluent/FluentConfiguration"; | ||
|
|
r49 | |||
| const trace = TraceSource.get("@implab/core/di/ActivationContext"); | ||||
|
|
r146 | export class Container<S extends object = any> implements ServiceContainer<S> { | ||
|
|
r120 | readonly _services: ContainerServiceMap<S>; | ||
|
|
r49 | |||
|
|
r135 | readonly _lifetimeManager: LifetimeManager; | ||
|
|
r131 | |||
|
|
r114 | readonly _cleanup: (() => void)[]; | ||
|
|
r49 | |||
|
|
r114 | readonly _root: Container<S>; | ||
|
|
r49 | |||
|
|
r114 | readonly _parent?: Container<S>; | ||
|
|
r49 | |||
|
|
r114 | _disposed: boolean; | ||
|
|
r49 | |||
|
|
r114 | constructor(parent?: Container<S>) { | ||
|
|
r49 | this._parent = parent; | ||
|
|
r146 | this._services = Object.create(parent ? parent._services : null); | ||
|
|
r49 | this._cleanup = []; | ||
| this._root = parent ? parent.getRootContainer() : this; | ||||
|
|
r115 | this._services.container = new ValueDescriptor(this) as any; | ||
|
|
r146 | this._services.childContainer = { activate: () => this.createChildContainer() }; | ||
|
|
r114 | this._disposed = false; | ||
|
|
r131 | this._lifetimeManager = new LifetimeManager(); | ||
|
|
r49 | } | ||
| getRootContainer() { | ||||
| return this._root; | ||||
| } | ||||
| getParent() { | ||||
| return this._parent; | ||||
| } | ||||
|
|
r131 | getLifetimeManager() { | ||
| return this._lifetimeManager; | ||||
| } | ||||
|
|
r154 | resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K>; | ||
| resolve<K extends ContainerKeys<S>>(name: K, def: undefined): TypeOfService<S, K> | undefined; | ||||
| resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K> | undefined { | ||||
|
|
r49 | trace.debug("resolve {0}", name); | ||
| const d = this._services[name]; | ||||
| if (d === undefined) { | ||||
|
|
r154 | if (arguments.length > 1) | ||
|
|
r49 | return def; | ||
| else | ||||
| throw new Error("Service '" + name + "' isn't found"); | ||||
|
|
r114 | } else { | ||
|
|
r49 | |||
|
|
r131 | const context = new ActivationContext<S>(this, this._services, String(name), d); | ||
|
|
r114 | try { | ||
|
|
r131 | return d.activate(context); | ||
|
|
r114 | } catch (error) { | ||
| throw new ActivationError(name.toString(), context.getStack(), error); | ||||
| } | ||||
|
|
r49 | } | ||
| } | ||||
| /** | ||||
| * @deprecated use resolve() method | ||||
| */ | ||||
|
|
r133 | getService<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>) { | ||
|
|
r154 | return arguments.length === 1 ? this.resolve(name) : this.resolve(name, def); | ||
|
|
r49 | } | ||
|
|
r115 | register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>): this; | ||
| register(services: PartialServiceMap<S>): this; | ||||
| register<K extends keyof S>(nameOrCollection: K | ServiceMap<S>, service?: Descriptor<S, S[K]>) { | ||||
|
|
r49 | if (arguments.length === 1) { | ||
|
|
r114 | const data = nameOrCollection as ServiceMap<S>; | ||
|
|
r115 | |||
|
|
r131 | each(data, (v, k) => this.register(k, v)); | ||
|
|
r49 | } else { | ||
| if (!isDescriptor(service)) | ||||
| throw new Error("The service parameter must be a descriptor"); | ||||
|
|
r115 | this._services[nameOrCollection as K] = service as any; | ||
|
|
r49 | } | ||
| return this; | ||||
| } | ||||
|
|
r144 | /** @deprecated use getLifetimeManager() */ | ||
|
|
r114 | onDispose(callback: () => void) { | ||
|
|
r49 | if (!(callback instanceof Function)) | ||
| throw new Error("The callback must be a function"); | ||||
| this._cleanup.push(callback); | ||||
| } | ||||
|
|
r129 | destroy() { | ||
| return this.dispose(); | ||||
| } | ||||
|
|
r49 | dispose() { | ||
|
|
r114 | if (this._disposed) | ||
| return; | ||||
| this._disposed = true; | ||||
| for (const f of this._cleanup) | ||||
| f(); | ||||
|
|
r154 | this._lifetimeManager.destroy(); | ||
|
|
r49 | } | ||
| /** | ||||
| * @param{String|Object} config | ||||
|
|
r136 | * The configuration of the container. Can be either a string or an object, | ||
|
|
r49 | * if the configuration is an object it's treated as a collection of | ||
|
|
r136 | * services which will be registered in the container. | ||
|
|
r49 | * | ||
| * @param{Function} opts.contextRequire | ||||
| * The function which will be used to load a configuration or types for services. | ||||
| * | ||||
| */ | ||||
|
|
r136 | async configure(config: string | RegistrationMap<S>, opts?: { contextRequire: any; baseModule?: string }, ct = Cancellation.none) { | ||
| const _opts = Object.create(opts || null); | ||||
|
|
r49 | |||
| if (typeof (config) === "string") { | ||||
|
|
r136 | _opts.baseModule = config; | ||
| const module = await import(config); | ||||
| if (module && module.default && typeof (module.default.apply) === "function") | ||||
| return module.default.apply(this); | ||||
| else | ||||
| return this._applyLegacyConfig(module, _opts, ct); | ||||
|
|
r49 | } else { | ||
|
|
r136 | return this._applyLegacyConfig(config, _opts, ct); | ||
|
|
r49 | } | ||
| } | ||||
|
|
r144 | applyConfig<S2 extends object>(config: Promise<{ default: ContainerConfiguration<S2>; }>, ct?: ICancellation): Promise<ServiceContainer<S & S2>>; | ||
| applyConfig<S2 extends object, P extends string>(config: Promise<{ [p in P]: ContainerConfiguration<S2>; }>, prop: P, ct?: ICancellation): Promise<ServiceContainer<S & S2>>; | ||||
|
|
r142 | async applyConfig<S2 extends object, P extends string>( | ||
| config: Promise<{ [p in P | "default"]: ContainerConfiguration<S2>; }>, | ||||
| propOrCt?: P | ICancellation, | ||||
| ct?: ICancellation | ||||
|
|
r144 | ): Promise<ServiceContainer<S & S2>> { | ||
|
|
r142 | const mod = await config; | ||
| let _ct: ICancellation; | ||||
| let _prop: P | "default"; | ||||
| if (isString(propOrCt)) { | ||||
| _prop = propOrCt; | ||||
| _ct = ct || Cancellation.none; | ||||
| } else { | ||||
| _ct = propOrCt || Cancellation.none; | ||||
| _prop = "default"; | ||||
| } | ||||
| return mod[_prop].apply(this, _ct); | ||||
| } | ||||
|
|
r136 | async _applyLegacyConfig(config: RegistrationMap<S>, opts: { contextRequire: any; baseModule?: string }, ct = Cancellation.none) { | ||
| return new Configuration<S>(this).applyConfiguration(config, opts); | ||||
| } | ||||
|
|
r135 | async fluent<K extends keyof S>(config: FluentRegistrations<K, S>, ct = Cancellation.none): Promise<this> { | ||
| await new FluentConfiguration<S>().register(config).apply(this, ct); | ||||
| return this; | ||||
| } | ||||
|
|
r120 | createChildContainer<S2 extends object = S>(): Container<S & S2> { | ||
|
|
r114 | return new Container<S & S2>(this as any); | ||
|
|
r49 | } | ||
| } | ||||
