##// END OF EJS Templates
WIP lifetime services, change target to ES2018
WIP lifetime services, change target to ES2018

File last commit:

r11:dd37d4287c45 default
r11:dd37d4287c45 default
Show More
DescriptorImpl.ts
93 lines | 2.7 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / DescriptorImpl.ts
cin
WIP lifetime services, change target to ES2018
r11 import { ActivationError } from "./ActivationError";
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
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
if (this._lifetime.has())
return this._lifetime.get();
this._lifetime.initialize(context);
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 {
const instance = (0,this._factory)(refs);
this._lifetime.store(instance, this._cleanup);
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 }
}