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

File last commit:

r13:dc3d64c43573 default
r13:dc3d64c43573 default
Show More
DescriptorImpl.ts
104 lines | 2.9 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
working on fluent configuration
r1 lifetime: ILifetime<T>;
cin
initial commit
r0
cin
almost woking typings
r9 factory: (refs: Record<key, unknown>) => NonNullable<T>;
cin
initial commit
r0
cin
almost woking typings
r9 cleanup?: (item: NonNullable<T>) => void;
cin
initial commit
r0
cin
almost woking typings
r9 overrides?: DescriptorMap<S>;
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4
cin
almost woking typings
r9 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
working on fluent configuration
r1 private readonly _lifetime: ILifetime<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();
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 services, change target to ES2018
r11 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;
} catch(err) {
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 }
}