| @@ -50,11 +50,13 export class Container<S extends object | |||||
| 50 | return this._lifetimeManager; |
|
50 | return this._lifetimeManager; | |
| 51 | } |
|
51 | } | |
| 52 |
|
52 | |||
| 53 |
resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K> |
|
53 | resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K>; | |
|
|
54 | resolve<K extends ContainerKeys<S>>(name: K, def: undefined): TypeOfService<S, K> | undefined; | |||
|
|
55 | resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K> | undefined { | |||
| 54 | trace.debug("resolve {0}", name); |
|
56 | trace.debug("resolve {0}", name); | |
| 55 | const d = this._services[name]; |
|
57 | const d = this._services[name]; | |
| 56 | if (d === undefined) { |
|
58 | if (d === undefined) { | |
| 57 | if (def !== undefined) |
|
59 | if (arguments.length > 1) | |
| 58 | return def; |
|
60 | return def; | |
| 59 | else |
|
61 | else | |
| 60 | throw new Error("Service '" + name + "' isn't found"); |
|
62 | throw new Error("Service '" + name + "' isn't found"); | |
| @@ -73,7 +75,7 export class Container<S extends object | |||||
| 73 | * @deprecated use resolve() method |
|
75 | * @deprecated use resolve() method | |
| 74 | */ |
|
76 | */ | |
| 75 | getService<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>) { |
|
77 | getService<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>) { | |
| 76 | return this.resolve(name, def); |
|
78 | return arguments.length === 1 ? this.resolve(name) : this.resolve(name, def); | |
| 77 | } |
|
79 | } | |
| 78 |
|
80 | |||
| 79 | register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>): this; |
|
81 | register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>): this; | |
| @@ -108,6 +110,7 export class Container<S extends object | |||||
| 108 | this._disposed = true; |
|
110 | this._disposed = true; | |
| 109 | for (const f of this._cleanup) |
|
111 | for (const f of this._cleanup) | |
| 110 | f(); |
|
112 | f(); | |
|
|
113 | this._lifetimeManager.destroy(); | |||
| 111 | } |
|
114 | } | |
| 112 |
|
115 | |||
| 113 | /** |
|
116 | /** | |
| @@ -1,5 +1,5 | |||||
| 1 | import { IDestroyable, MapOf } from "../interfaces"; |
|
1 | import { IDestroyable, MapOf } from "../interfaces"; | |
| 2 | import { argumentNotNull, isDestroyable, argumentNotEmptyString } from "../safe"; |
|
2 | import { argumentNotNull, isDestroyable, argumentNotEmptyString, isRemovable } from "../safe"; | |
| 3 | import { ILifetime, ServiceContainer } from "./interfaces"; |
|
3 | import { ILifetime, ServiceContainer } from "./interfaces"; | |
| 4 | import { ActivationContext } from "./ActivationContext"; |
|
4 | import { ActivationContext } from "./ActivationContext"; | |
| 5 |
|
5 | |||
| @@ -93,6 +93,8 export class LifetimeManager implements | |||||
| 93 | self._cleanup.push(() => cleanup(item)); |
|
93 | self._cleanup.push(() => cleanup(item)); | |
| 94 | } else if (isDestroyable(item)) { |
|
94 | } else if (isDestroyable(item)) { | |
| 95 | self._cleanup.push(() => item.destroy()); |
|
95 | self._cleanup.push(() => item.destroy()); | |
|
|
96 | } else if (isRemovable(item)) { | |||
|
|
97 | self._cleanup.push(() => item.remove()); | |||
| 96 | } |
|
98 | } | |
| 97 | } |
|
99 | } | |
| 98 | }; |
|
100 | }; | |
| @@ -26,6 +26,7 export type PartialServiceMap<S extends | |||||
| 26 |
|
26 | |||
| 27 | export interface ServiceLocator<S extends object> { |
|
27 | export interface ServiceLocator<S extends object> { | |
| 28 | resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K>; |
|
28 | resolve<K extends ContainerKeys<S>>(name: K, def?: TypeOfService<S, K>): TypeOfService<S, K>; | |
|
|
29 | resolve<K extends ContainerKeys<S>>(name: K, def?: undefined): TypeOfService<S, K> | undefined; | |||
| 29 | } |
|
30 | } | |
| 30 |
|
31 | |||
| 31 | export interface ServiceContainer<S extends object> extends ServiceLocator<S>, IDestroyable { |
|
32 | export interface ServiceContainer<S extends object> extends ServiceLocator<S>, IDestroyable { | |
| @@ -1,4 +1,4 | |||||
| 1 | import { ICancellable, Constructor, IDestroyable, ICancellation } from "./interfaces"; |
|
1 | import { ICancellable, Constructor, IDestroyable, ICancellation, IRemovable } from "./interfaces"; | |
| 2 |
|
2 | |||
| 3 | let _nextOid = 0; |
|
3 | let _nextOid = 0; | |
| 4 | const _oid = typeof Symbol === "function" ? |
|
4 | const _oid = typeof Symbol === "function" ? | |
| @@ -62,10 +62,6 export function argumentOfType(arg: any, | |||||
| 62 | throw new Error("The argument '" + name + "' type doesn't match"); |
|
62 | throw new Error("The argument '" + name + "' type doesn't match"); | |
| 63 | } |
|
63 | } | |
| 64 |
|
64 | |||
| 65 | export function isObject(val: any): val is object { |
|
|||
| 66 | return typeof val === "object"; |
|
|||
| 67 | } |
|
|||
| 68 |
|
||||
| 69 | export function isNull(val: any): val is null | undefined { |
|
65 | export function isNull(val: any): val is null | undefined { | |
| 70 | return (val === null || val === undefined); |
|
66 | return (val === null || val === undefined); | |
| 71 | } |
|
67 | } | |
| @@ -77,6 +73,10 export function isPrimitive(val: any): v | |||||
| 77 | typeof (val) === "number" || typeof (val) === "boolean"); |
|
73 | typeof (val) === "number" || typeof (val) === "boolean"); | |
| 78 | } |
|
74 | } | |
| 79 |
|
75 | |||
|
|
76 | export function isObject<T>(value: T): value is Exclude<T & object, primitive> { | |||
|
|
77 | return !!(value && typeof value === "object"); | |||
|
|
78 | } | |||
|
|
79 | ||||
| 80 | export function isInteger(val: any): val is number { |
|
80 | export function isInteger(val: any): val is number { | |
| 81 | return parseInt(val, 10) === val; |
|
81 | return parseInt(val, 10) === val; | |
| 82 | } |
|
82 | } | |
| @@ -86,15 +86,15 export function isNumber(val: any): val | |||||
| 86 | } |
|
86 | } | |
| 87 |
|
87 | |||
| 88 | export function isString(val: any): val is string { |
|
88 | export function isString(val: any): val is string { | |
| 89 |
return typeof (val) === "string" |
|
89 | return typeof (val) === "string"; | |
| 90 | } |
|
90 | } | |
| 91 |
|
91 | |||
| 92 | export function isPromise<T = any>(val: any): val is PromiseLike<T> { |
|
92 | export function isPromise<T = any>(val: any): val is PromiseLike<T> { | |
| 93 | return val && typeof val.then === "function"; |
|
93 | return !!(val && typeof val.then === "function"); | |
| 94 | } |
|
94 | } | |
| 95 |
|
95 | |||
| 96 | export function isCancellable(val: any): val is ICancellable { |
|
96 | export function isCancellable(val: any): val is ICancellable { | |
| 97 | return val && typeof val.cancel === "function"; |
|
97 | return !!(val && typeof val.cancel === "function"); | |
| 98 | } |
|
98 | } | |
| 99 |
|
99 | |||
| 100 | export function isNullOrEmptyString(val: any): val is ("" | null | undefined) { |
|
100 | export function isNullOrEmptyString(val: any): val is ("" | null | undefined) { | |
| @@ -315,7 +315,7 export function fork() { | |||||
| 315 | * assigned later and are required to be not null. |
|
315 | * assigned later and are required to be not null. | |
| 316 | */ |
|
316 | */ | |
| 317 | export function notImplemented(): never { |
|
317 | export function notImplemented(): never { | |
| 318 | throw new Error("Not implemeted"); |
|
318 | throw new Error("Not implemented"); | |
| 319 | } |
|
319 | } | |
| 320 | /** |
|
320 | /** | |
| 321 | * Iterates over the specified array of items and calls the callback `cb`, if |
|
321 | * Iterates over the specified array of items and calls the callback `cb`, if | |
| @@ -491,13 +491,15 export function firstWhere<T>( | |||||
| 491 | } |
|
491 | } | |
| 492 |
|
492 | |||
| 493 | export function isDestroyable(d: any): d is IDestroyable { |
|
493 | export function isDestroyable(d: any): d is IDestroyable { | |
| 494 |
|
|
494 | return !!(d && typeof d.destroy === "function"); | |
| 495 | return true; |
|
495 | } | |
| 496 | return false; |
|
496 | ||
|
|
497 | export function isRemovable(value: any): value is IRemovable { | |||
|
|
498 | return !!(value && typeof value.remove === "function"); | |||
| 497 | } |
|
499 | } | |
| 498 |
|
500 | |||
| 499 | export function destroy(d: any) { |
|
501 | export function destroy(d: any) { | |
| 500 |
if ( |
|
502 | if (isDestroyable(d)) | |
| 501 | d.destroy(); |
|
503 | d.destroy(); | |
| 502 | } |
|
504 | } | |
| 503 |
|
505 | |||
| @@ -4,7 +4,7 | |||||
| 4 | "experimentalDecorators": true, |
|
4 | "experimentalDecorators": true, | |
| 5 | "noEmitOnError": true, |
|
5 | "noEmitOnError": true, | |
| 6 | "listFiles": true, |
|
6 | "listFiles": true, | |
| 7 |
|
|
7 | "strict": true, | |
| 8 | "types": [], |
|
8 | "types": [], | |
| 9 | "target": "ES5", |
|
9 | "target": "ES5", | |
| 10 | "lib": ["es5", "es2015.promise", "es2015.symbol", "es2015.iterable", "dom", "scripthost"] |
|
10 | "lib": ["es5", "es2015.promise", "es2015.symbol", "es2015.iterable", "dom", "scripthost"] | |
General Comments 0
You need to be logged in to leave comments.
Login now
