##// END OF EJS Templates
sync
sync

File last commit:

r15:3985e8405319 tip default
r15:3985e8405319 tip default
Show More
DescriptorImpl.ts
89 lines | 2.9 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / DescriptorImpl.ts
cin
sync
r15 import { Descriptor, ILifetime, IActivationContext, DescriptorMap, DescriptorDepsMap } from "../typings/interfaces";
import { ActivationError, ActivationItem } from "./ActivationError";
cin
almost woking typings
r9 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
sync
r15 readonly lifetime: ILifetime<T>;
cin
WIP lifetime, service descriptors
r14
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
sync
r15 readonly dependencies?: DescriptorDepsMap<S>;
cin
initial commit
r0 }
cin
almost woking typings
r9 export class DescriptorImpl<S, T> implements Descriptor<S, T> {
cin
initial commit
r0
cin
sync
r15 private readonly _overrides: DescriptorMap<S> | undefined;
cin
initial commit
r0
cin
sync
r15 private readonly _lifetime: ILifetime<T>;
cin
initial commit
r0
cin
almost woking typings
r9 private readonly _factory: (refs: Record<key, unknown>) => NonNullable<T>;
cin
sync
r15 private readonly _cleanup: (item: NonNullable<T>) => void;
cin
initial commit
r0
cin
sync
r15 private readonly _dependencies: DescriptorDepsMap<S> | undefined;
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;
cin
sync
r15 this._cleanup = cleanup ? cleanup : () => { };
this._overrides = overrides;
this._dependencies = dependencies;
cin
initial commit
r0 }
cin
sync
r15 activate(context: IActivationContext<S>, stack: ActivationItem[]): NonNullable<T> {
cin
initial commit
r0
cin
sync
r15 const slot = this._lifetime(context);
if (slot.has())
return slot.get();
if (!slot.initialize())
throw new Error("Cyclic reference detected");
cin
initial commit
r0
cin
sync
r15 const instance = this._overrides ?
context.withOverrides(this._overrides, () => this._activate(context, stack)) :
this._activate(context, stack);
cin
WIP lifetime, service descriptors
r14
cin
sync
r15 slot.store(instance, this._cleanup);
cin
initial commit
r0
cin
sync
r15 return instance;
}
cin
initial commit
r0
cin
sync
r15 private _activate(context: IActivationContext<S>, stack: ActivationItem[]) {
const refs: Record<key, unknown> = {};
if (this._dependencies) {
const resolve = <K extends keyof S>({ name, lazy, ...opts }: { name: K; lazy?: boolean; default?: S[K]; }) => {
if (lazy) {
return "default" in opts ?
() => context.resolve(name, stack, opts.default) :
() => context.resolve(name, stack);
} else {
return "default" in opts ?
context.resolve(name, stack, opts.default) :
context.resolve(name, stack);
}
};
cin
initial commit
r0
cin
sync
r15 // can throw activation exception
each(this._dependencies, (v, k) => {
refs[k] = resolve(v);
});
}
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
sync
r15 return (0, this._factory)(refs);
} catch (e) {
throw new ActivationError("Error creating instance", stack, e);
cin
WIP lifetime services, change target to ES2018
r11 }
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 }
}