##// END OF EJS Templates
fixed bug in applying a lifetime while processing the json configuration...
fixed bug in applying a lifetime while processing the json configuration fixed template-compile

File last commit:

r135:03e32ec7c20b ioc ts support
r152:8bee6a6d5f46 v1.4.0-rc10 default
Show More
ServiceDescriptor.ts
154 lines | 4.2 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / di / ServiceDescriptor.ts
cin
changed the project structure
r49 import { ActivationContext } from "./ActivationContext";
cin
Completely removed IoC annotations...
r135 import { Descriptor, ServiceMap, PartialServiceMap, ILifetime } from "./interfaces";
cin
working on fluent configuration
r133 import { isPrimitive, keys, isNull } from "../safe";
cin
changed the project structure
r49 import { TraceSource } from "../log/TraceSource";
cin
configuration interfaces moved to di/Configuration module...
r118 import { isDescriptor } from "./traits";
cin
working on lifetime management
r129 import { LifetimeManager } from "./LifetimeManager";
import { MatchingMemberKeys } from "../interfaces";
cin
changed the project structure
r49
const trace = TraceSource.get("@implab/core/di/ActivationContext");
cin
improved interfaces and more tight type checking
r120 function injectMethod<T, M extends keyof T, S extends object, A>(target: T, method: M, context: ActivationContext<S>, args: A) {
cin
corrected code to support ts strict mode...
r115
cin
changed the project structure
r49 const m = target[method];
cin
corrected code to support ts strict mode...
r115 if (!m || typeof m !== "function")
cin
changed the project structure
r49 throw new Error("Method '" + method + "' not found");
if (args instanceof Array)
cin
fixed "singleton" activation type handling in container configuration...
r65 return m.apply(target, _parse(args, context, "." + method));
cin
changed the project structure
r49 else
cin
fixed "singleton" activation type handling in container configuration...
r65 return m.call(target, _parse(args, context, "." + method));
cin
changed the project structure
r49 }
cin
working on services lifetime
r130 function makeCleanupCallback<T>(method: Cleaner<T>) {
cin
working on lifetime management
r129 if (typeof (method) === "function") {
return (target: T) => {
method(target);
cin
changed the project structure
r49 };
} else {
cin
working on lifetime management
r129 return (target: T) => {
cin
working on services lifetime
r130 const m = target[method] as any;
m.apply(target);
cin
changed the project structure
r49 };
}
}
cin
configuration interfaces moved to di/Configuration module...
r118 function _parse(value: any, context: ActivationContext<any>, path: string): any {
cin
changed the project structure
r49 if (isPrimitive(value))
cin
corrected code to support ts strict mode...
r115 return value as any;
cin
changed the project structure
r49
trace.debug("parse {0}", path);
if (isDescriptor(value))
return context.activate(value, path);
if (value instanceof Array)
cin
corrected code to support ts strict mode...
r115 return value.map((x, i) => _parse(x, context, `${path}[${i}]`)) as any;
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 const t: any = {};
keys(value).forEach(p => t[p] = _parse(value[p], context, `${path}.${p}`));
cin
changed the project structure
r49
return t;
}
cin
working on lifetime management
r129 export type Cleaner<T> = ((x: T) => void) | MatchingMemberKeys<() => void, T>;
cin
corrected code to support ts strict mode...
r115
export type InjectionSpec<T> = {
[m in keyof T]?: any;
};
cin
improved interfaces and more tight type checking
r120 export interface ServiceDescriptorParams<S extends object, T, P extends any[]> {
cin
working on fluent configuration, di annotations removed
r134 lifetime?: ILifetime;
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 params?: P;
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 inject?: InjectionSpec<T>[];
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 services?: PartialServiceMap<S>;
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 cleanup?: Cleaner<T>;
cin
changed the project structure
r49 }
cin
improved interfaces and more tight type checking
r120 export class ServiceDescriptor<S extends object, T, P extends any[]> implements Descriptor<S, T> {
cin
corrected code to support ts strict mode...
r115 _services: ServiceMap<S>;
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 _params: P | undefined;
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 _inject: InjectionSpec<T>[];
cin
changed the project structure
r49
cin
working on lifetime management
r129 _cleanup: ((item: T) => void) | undefined;
cin
changed the project structure
r49
cin
working on fluent configuration, di annotations removed
r134 _lifetime = LifetimeManager.empty();
cin
changed the project structure
r49
cin
working on fluent configuration
r133 _objectLifetime: ILifetime | undefined;
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 constructor(opts: ServiceDescriptorParams<S, T, P>) {
cin
changed the project structure
r49
cin
working on lifetime management
r129 if (opts.lifetime)
cin
working on fluent configuration, di annotations removed
r134 this._lifetime = opts.lifetime;
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 if (!isNull(opts.params))
cin
changed the project structure
r49 this._params = opts.params;
cin
corrected code to support ts strict mode...
r115 this._inject = opts.inject || [];
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 this._services = (opts.services || {}) as ServiceMap<S>;
cin
changed the project structure
r49
if (opts.cleanup) {
if (!(typeof (opts.cleanup) === "string" || opts.cleanup instanceof Function))
throw new Error(
"The cleanup parameter must be either a function or a function name");
cin
working on services lifetime
r130 this._cleanup = makeCleanupCallback(opts.cleanup);
cin
changed the project structure
r49 }
}
cin
corrected code to support ts strict mode...
r115 activate(context: ActivationContext<S>) {
cin
working on fluent configuration, di annotations removed
r134 const lifetime = this._lifetime;
cin
changed the project structure
r49
cin
working on lifetime management
r129 if (lifetime.has()) {
return lifetime.get();
} else {
cin
working on fluent configuration, di annotations removed
r134 lifetime.initialize(context);
cin
working on lifetime management
r129 const instance = this._create(context);
cin
working on fluent configuration
r133 lifetime.store(instance, this._cleanup);
cin
working on lifetime management
r129 return instance;
cin
changed the project structure
r49 }
}
cin
corrected code to support ts strict mode...
r115 _factory(...params: any[]): T {
cin
changed the project structure
r49 throw Error("Not implemented");
}
cin
corrected code to support ts strict mode...
r115 _create(context: ActivationContext<S>) {
cin
changed the project structure
r49 trace.debug(`constructing ${context._name}`);
if (this._services) {
cin
corrected code to support ts strict mode...
r115 keys(this._services).forEach(p => context.register(p, this._services[p]));
cin
changed the project structure
r49 }
cin
corrected code to support ts strict mode...
r115 let instance: T;
cin
changed the project structure
r49
if (this._params === undefined) {
instance = this._factory();
} else if (this._params instanceof Array) {
instance = this._factory.apply(this, _parse(this._params, context, "args"));
} else {
instance = this._factory(_parse(this._params, context, "args"));
}
if (this._inject) {
this._inject.forEach(spec => {
for (const m in spec)
injectMethod(instance, m, context, spec[m]);
});
}
return instance;
}
cin
working on lifetime management
r129
clone() {
return Object.create(this);
}
cin
changed the project structure
r49 }