|
|
import { Descriptor, ILifetime, ConfigurableKeys, DepsMap, Ref } from "./interfaces";
|
|
|
import { ActivationContext } from "./ActivationContext";
|
|
|
import { each, isKey, key } from "./traits";
|
|
|
|
|
|
export type RegistrationOverridesMap<S extends object> = { [k in ConfigurableKeys<S>]?: Descriptor<S, NonNullable<S[k]>> };
|
|
|
|
|
|
export interface DescriptorImplArgs<S extends object, T> {
|
|
|
lifetime: ILifetime<T>;
|
|
|
|
|
|
factory: (refs: Record<key, never>) => T;
|
|
|
|
|
|
cleanup?: (item: T) => void;
|
|
|
|
|
|
overrides?: RegistrationOverridesMap<S>;
|
|
|
|
|
|
dependencies?: DepsMap<key, keyof S>;
|
|
|
}
|
|
|
|
|
|
|
|
|
export class DescriptorImpl<S extends object, T> implements Descriptor<S, T> {
|
|
|
|
|
|
private readonly _overrides?: RegistrationOverridesMap<S>;
|
|
|
|
|
|
private readonly _lifetime: ILifetime<T>;
|
|
|
|
|
|
private readonly _factory: (refs: Record<key, never>) => T;
|
|
|
|
|
|
private readonly _cleanup?: (item: T) => void;
|
|
|
|
|
|
private readonly _deps?: DepsMap<key, keyof S>;
|
|
|
|
|
|
constructor({ lifetime, factory, cleanup, overrides, dependencies }: DescriptorImplArgs<S, T>) {
|
|
|
this._lifetime = lifetime;
|
|
|
this._factory = factory;
|
|
|
if (cleanup)
|
|
|
this._cleanup = cleanup;
|
|
|
if (overrides)
|
|
|
this._overrides = overrides;
|
|
|
if (dependencies)
|
|
|
this._deps = dependencies;
|
|
|
}
|
|
|
|
|
|
activate(context: ActivationContext<S>): T {
|
|
|
|
|
|
if (this._lifetime.has())
|
|
|
return this._lifetime.get();
|
|
|
|
|
|
this._lifetime.initialize(context);
|
|
|
|
|
|
if (this._overrides)
|
|
|
each(this._overrides, (v, k) => context.register(k, v));
|
|
|
|
|
|
const resolve = <K extends keyof S, L extends boolean, D = never>({ name, lazy, ...opts }: Ref<K, L, D>) => {
|
|
|
if (lazy) {
|
|
|
return () => "default" in opts ? context.resolve(name, opts.default) : context.resolve(name);
|
|
|
} else {
|
|
|
return "default" in opts ? context.resolve(name, opts.default) : context.resolve(name);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const makeRefs = (deps?: DepsMap<key, keyof S>) => deps ?
|
|
|
Object.keys(deps)
|
|
|
.map(k => {
|
|
|
const ref = deps[k];
|
|
|
return isKey(ref) ?
|
|
|
{ [k]: resolve({ name: ref }) } :
|
|
|
{ [k]: resolve(ref) };
|
|
|
})
|
|
|
.reduce((a, p) => ({ ...a, ...p }), {} ) as Record<key, never>:
|
|
|
{} as Record<key, never>;
|
|
|
|
|
|
const instance = this._factory.call(undefined, makeRefs(this._deps));
|
|
|
|
|
|
this._lifetime.store(instance, this._cleanup);
|
|
|
|
|
|
return instance;
|
|
|
}
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
return `[object DescriptorImpl, lifetime=${String(this._lifetime)}]`;
|
|
|
}
|
|
|
}
|
|
|
|