##// END OF EJS Templates
working on multi-platform support
working on multi-platform support

File last commit:

r44:7a410676c874 di-typescript
r48:2ef1241803ee di-typescript
Show More
Container.ts
128 lines | 3.4 KiB | video/mp2t | TypeScriptLexer
/ src / ts / di / Container.ts
cin
minor fixes, code cleanup...
r33 import { ActivationContext } from "./ActivationContext";
cin
ported IoC container to typescript...
r34 import { ValueDescriptor } from "./ValueDescriptor";
import { ActivationError } from "./ActivationError";
cin
fixes, tests...
r44 import { isDescriptor, ServiceMap } from "./interfaces";
cin
Container.configure sync/async tests
r42 import { TraceSource } from "../log/TraceSource";
cin
fixes, tests...
r44 import { Configuration } from "./Configuration";
import { Cancellation } from "../Cancellation";
cin
Container.configure sync/async tests
r42
const trace = TraceSource.get("@implab/core/di/ActivationContext");
cin
minor fixes, code cleanup...
r33
export class Container {
cin
working on IoC container
r38 _services: ServiceMap;
cin
minor fixes, code cleanup...
r33
cin
working on IoC container
r38 _cache: object;
_cleanup: (() => void)[];
cin
minor fixes, code cleanup...
r33
cin
working on IoC container
r38 _root: Container;
cin
minor fixes, code cleanup...
r33
cin
working on IoC container
r38 _parent: Container;
cin
minor fixes, code cleanup...
r33
constructor(parent?: Container) {
this._parent = parent;
this._services = parent ? Object.create(parent._services) : {};
this._cache = {};
this._cleanup = [];
this._root = parent ? parent.getRootContainer() : this;
this._services.container = new ValueDescriptor(this);
}
getRootContainer() {
return this._root;
}
getParent() {
return this._parent;
}
cin
tests
r41 resolve(name: string, def?) {
cin
fixes, tests...
r44 trace.debug("resolve {0}", name);
cin
working on IoC container
r38 const d = this._services[name];
cin
tests
r41 if (d === undefined) {
cin
minor fixes, code cleanup...
r33 if (arguments.length > 1)
return def;
else
throw new Error("Service '" + name + "' isn't found");
cin
tests
r41 }
cin
minor fixes, code cleanup...
r33
cin
working on IoC container
r38 const context = new ActivationContext(this, this._services);
cin
minor fixes, code cleanup...
r33 try {
cin
tests
r41 return context.activate(d, name);
cin
minor fixes, code cleanup...
r33 } catch (error) {
throw new ActivationError(name, context.getStack(), error);
}
}
cin
Container.configure sync/async tests
r42 /**
* @deprecated use resolve() method
*/
cin
fixes, tests...
r44 getService() {
cin
tests
r41 return this.resolve.apply(this, arguments);
}
cin
minor fixes, code cleanup...
r33 register(nameOrCollection, service?) {
cin
working on IoC container
r38 if (arguments.length === 1) {
const data = nameOrCollection;
for (const name in data)
cin
minor fixes, code cleanup...
r33 this.register(name, data[name]);
} else {
cin
tests
r41 if (!isDescriptor(service))
throw new Error("The service parameter must be a descriptor");
cin
minor fixes, code cleanup...
r33 this._services[nameOrCollection] = service;
}
return this;
}
onDispose(callback) {
if (!(callback instanceof Function))
throw new Error("The callback must be a function");
this._cleanup.push(callback);
}
dispose() {
if (this._cleanup) {
cin
working on IoC container
r38 for (const f of this._cleanup)
f();
cin
minor fixes, code cleanup...
r33 this._cleanup = null;
}
}
/**
* @param{String|Object} config
* The configuration of the contaier. Can be either a string or an object,
* if the configuration is an object it's treated as a collection of
* services which will be registed in the contaier.
cin
working on IoC container
r38 *
cin
minor fixes, code cleanup...
r33 * @param{Function} opts.contextRequire
* The function which will be used to load a configuration or types for services.
cin
working on IoC container
r38 *
cin
minor fixes, code cleanup...
r33 */
cin
fixes, tests...
r44 async configure(config: string | object, opts?: any, ct = Cancellation.none) {
const c = new Configuration(this);
cin
minor fixes, code cleanup...
r33 if (typeof (config) === "string") {
cin
fixes, tests...
r44 return c.loadConfiguration(config, ct);
cin
minor fixes, code cleanup...
r33 } else {
cin
fixes, tests...
r44 return c.applyConfiguration(config, opts && opts.contextRequire, ct);
cin
minor fixes, code cleanup...
r33 }
}
createChildContainer() {
return new Container(this);
}
has(id) {
return id in this._cache;
}
get(id) {
return this._cache[id];
}
store(id, value) {
return (this._cache[id] = value);
}
cin
working on IoC container
r38 }