diff --git a/src/main/ts/di/Container.ts b/src/main/ts/di/Container.ts --- a/src/main/ts/di/Container.ts +++ b/src/main/ts/di/Container.ts @@ -50,11 +50,13 @@ export class Container>(name: K, def?: TypeOfService): TypeOfService { + resolve>(name: K, def?: TypeOfService): TypeOfService; + resolve>(name: K, def: undefined): TypeOfService | undefined; + resolve>(name: K, def?: TypeOfService): TypeOfService | undefined { trace.debug("resolve {0}", name); const d = this._services[name]; if (d === undefined) { - if (def !== undefined) + if (arguments.length > 1) return def; else throw new Error("Service '" + name + "' isn't found"); @@ -73,7 +75,7 @@ export class Container>(name: K, def?: TypeOfService) { - return this.resolve(name, def); + return arguments.length === 1 ? this.resolve(name) : this.resolve(name, def); } register(name: K, service: Descriptor): this; @@ -108,6 +110,7 @@ export class Container cleanup(item)); } else if (isDestroyable(item)) { self._cleanup.push(() => item.destroy()); + } else if (isRemovable(item)) { + self._cleanup.push(() => item.remove()); } } }; diff --git a/src/main/ts/di/interfaces.ts b/src/main/ts/di/interfaces.ts --- a/src/main/ts/di/interfaces.ts +++ b/src/main/ts/di/interfaces.ts @@ -26,6 +26,7 @@ export type PartialServiceMap { resolve>(name: K, def?: TypeOfService): TypeOfService; + resolve>(name: K, def?: undefined): TypeOfService | undefined; } export interface ServiceContainer extends ServiceLocator, IDestroyable { diff --git a/src/main/ts/safe.ts b/src/main/ts/safe.ts --- a/src/main/ts/safe.ts +++ b/src/main/ts/safe.ts @@ -1,4 +1,4 @@ -import { ICancellable, Constructor, IDestroyable, ICancellation } from "./interfaces"; +import { ICancellable, Constructor, IDestroyable, ICancellation, IRemovable } from "./interfaces"; let _nextOid = 0; const _oid = typeof Symbol === "function" ? @@ -62,10 +62,6 @@ export function argumentOfType(arg: any, throw new Error("The argument '" + name + "' type doesn't match"); } -export function isObject(val: any): val is object { - return typeof val === "object"; -} - export function isNull(val: any): val is null | undefined { return (val === null || val === undefined); } @@ -77,6 +73,10 @@ export function isPrimitive(val: any): v typeof (val) === "number" || typeof (val) === "boolean"); } +export function isObject(value: T): value is Exclude { + return !!(value && typeof value === "object"); +} + export function isInteger(val: any): val is number { return parseInt(val, 10) === val; } @@ -86,15 +86,15 @@ export function isNumber(val: any): val } export function isString(val: any): val is string { - return typeof (val) === "string" || val instanceof String; + return typeof (val) === "string"; } export function isPromise(val: any): val is PromiseLike { - return val && typeof val.then === "function"; + return !!(val && typeof val.then === "function"); } export function isCancellable(val: any): val is ICancellable { - return val && typeof val.cancel === "function"; + return !!(val && typeof val.cancel === "function"); } export function isNullOrEmptyString(val: any): val is ("" | null | undefined) { @@ -315,7 +315,7 @@ export function fork() { * assigned later and are required to be not null. */ export function notImplemented(): never { - throw new Error("Not implemeted"); + throw new Error("Not implemented"); } /** * Iterates over the specified array of items and calls the callback `cb`, if @@ -491,13 +491,15 @@ export function firstWhere( } export function isDestroyable(d: any): d is IDestroyable { - if (d && "destroy" in d && typeof (destroy) === "function") - return true; - return false; + return !!(d && typeof d.destroy === "function"); +} + +export function isRemovable(value: any): value is IRemovable { + return !!(value && typeof value.remove === "function"); } export function destroy(d: any) { - if (d && "destroy" in d) + if (isDestroyable(d)) d.destroy(); } diff --git a/src/tsconfig.json b/src/tsconfig.json --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -4,7 +4,7 @@ "experimentalDecorators": true, "noEmitOnError": true, "listFiles": true, - //"strict": true, + "strict": true, "types": [], "target": "ES5", "lib": ["es5", "es2015.promise", "es2015.symbol", "es2015.iterable", "dom", "scripthost"]