##// END OF EJS Templates
WIP lifetime, service descriptors
WIP lifetime, service descriptors

File last commit:

r14:3f8a82c8ce73 default
r14:3f8a82c8ce73 default
Show More
DescriptorImpl.ts
105 lines | 3.0 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / DescriptorImpl.ts
cin
almost woking typings
r9 import { Descriptor, ILifetime, DepsMap, IActivationContext, DescriptorMap } from "./interfaces";
import { each, key } from "./traits";
cin
working on fluent configuration
r1
cin
almost woking typings
r9 export interface DescriptorImplArgs<S, T> {
cin
initial commit
r0
cin
WIP lifetime, service descriptors
r14 readonly lifetime: ILifetime<NonNullable<T>>;
readonly factory: (refs: Record<key, unknown>) => NonNullable<T>;
cin
initial commit
r0
cin
WIP lifetime, service descriptors
r14 readonly cleanup?: (item: NonNullable<T>) => void;
cin
initial commit
r0
cin
WIP lifetime, service descriptors
r14 readonly overrides?: DescriptorMap<S>;
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4
cin
WIP lifetime, service descriptors
r14 readonly dependencies?: DepsMap<S>;
cin
initial commit
r0 }
cin
WIP service descriptors
r13 export const containerSelfDescriptor = <S>() => Object.freeze({
level: 0,
activate(context: IActivationContext<S>) {
return context.createChildContainer();
}
});
cin
working on fluent configuration
r1
cin
almost woking typings
r9 export class DescriptorImpl<S, T> implements Descriptor<S, T> {
cin
initial commit
r0
cin
almost woking typings
r9 private readonly _overrides?: DescriptorMap<S>;
cin
initial commit
r0
cin
WIP lifetime, service descriptors
r14 private readonly _lifetime: ILifetime<NonNullable<T>>;
cin
initial commit
r0
cin
almost woking typings
r9 private readonly _factory: (refs: Record<key, unknown>) => NonNullable<T>;
private readonly _cleanup?: (item: NonNullable<T>) => void;
cin
initial commit
r0
cin
almost woking typings
r9 private readonly _deps?: DepsMap<S>;
cin
initial commit
r0
cin
almost woking typings
r9 readonly hasOverrides: boolean;
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4
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;
cin
almost woking typings
r9
this.hasOverrides = !!overrides;
cin
initial commit
r0 }
cin
almost woking typings
r9 activate(context: IActivationContext<S>): NonNullable<T> {
cin
initial commit
r0
cin
WIP lifetime services
r12 const { has, get, initialize, store } = this._lifetime(context);
cin
initial commit
r0
cin
WIP lifetime services
r12 if (has())
return get();
cin
WIP lifetime, service descriptors
r14
cin
WIP lifetime services
r12 initialize();
cin
initial commit
r0
if (this._overrides)
each(this._overrides, (v, k) => context.register(k, v));
cin
almost woking typings
r9 const resolve = <K extends keyof S>({ name, lazy, ...opts }: { name: K; lazy?: boolean; default?: S[K] | null; }) => {
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 if (lazy) {
cin
WIP lifetime services, change target to ES2018
r11 return "default" in opts ?
() => context.resolve(name, opts.default) :
() => context.resolve(name);
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 } else {
cin
WIP lifetime services, change target to ES2018
r11 return "default" in opts ?
context.resolve(name, opts.default) :
context.resolve(name);
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 }
};
cin
initial commit
r0
cin
WIP lifetime services, change target to ES2018
r11 const deps = this._deps;
const refs = deps ?
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 Object.keys(deps)
.map(k => {
const ref = deps[k];
cin
almost woking typings
r9 return typeof ref !== "object" ?
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 { [k]: resolve({ name: ref }) } :
{ [k]: resolve(ref) };
})
cin
WIP lifetime services, change target to ES2018
r11 .reduce((a, p) => ({ ...a, ...p }), {}) :
cin
almost woking typings
r9 {};
cin
working on fluent configuration
r1
cin
WIP lifetime services, change target to ES2018
r11 try {
cin
WIP lifetime services
r12 // call the factory method
cin
WIP lifetime, service descriptors
r14 const instance = (0, this._factory)(refs);
cin
WIP lifetime services
r12 // store the instance
store(instance, this._cleanup);
cin
WIP lifetime services, change target to ES2018
r11 return instance;
cin
WIP lifetime, service descriptors
r14 } catch (err) {
cin
WIP lifetime services, change target to ES2018
r11 context.fail(err);
}
cin
initial commit
r0 }
cin
working on fluent configuration
r1
cin
initial commit
r0 toString() {
cin
working on fluent configuration
r1 return `[object DescriptorImpl, lifetime=${String(this._lifetime)}]`;
cin
initial commit
r0 }
}