| @@ -50,11 +50,13 export class Container<S extends object | |||
|
|
50 | 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 | 56 | trace.debug("resolve {0}", name); |
|
|
55 | 57 | const d = this._services[name]; |
|
|
56 | 58 | if (d === undefined) { |
|
|
57 | if (def !== undefined) | |
|
|
59 | if (arguments.length > 1) | |
|
|
58 | 60 | return def; |
|
|
59 | 61 | else |
|
|
60 | 62 | throw new Error("Service '" + name + "' isn't found"); |
| @@ -73,7 +75,7 export class Container<S extends object | |||
|
|
73 | 75 | * @deprecated use resolve() method |
|
|
74 | 76 | */ |
|
|
75 | 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 | 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 | 110 | this._disposed = true; |
|
|
109 | 111 | for (const f of this._cleanup) |
|
|
110 | 112 | f(); |
|
|
113 | this._lifetimeManager.destroy(); | |
|
|
111 | 114 | } |
|
|
112 | 115 | |
|
|
113 | 116 | /** |
| @@ -1,5 +1,5 | |||
|
|
1 | 1 | import { IDestroyable, MapOf } from "../interfaces"; |
|
|
2 | import { argumentNotNull, isDestroyable, argumentNotEmptyString } from "../safe"; | |
|
|
2 | import { argumentNotNull, isDestroyable, argumentNotEmptyString, isRemovable } from "../safe"; | |
|
|
3 | 3 | import { ILifetime, ServiceContainer } from "./interfaces"; |
|
|
4 | 4 | import { ActivationContext } from "./ActivationContext"; |
|
|
5 | 5 | |
| @@ -93,6 +93,8 export class LifetimeManager implements | |||
|
|
93 | 93 | self._cleanup.push(() => cleanup(item)); |
|
|
94 | 94 | } else if (isDestroyable(item)) { |
|
|
95 | 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 | 27 | export interface ServiceLocator<S extends object> { |
|
|
28 | 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 | 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 | 3 | let _nextOid = 0; |
|
|
4 | 4 | const _oid = typeof Symbol === "function" ? |
| @@ -62,10 +62,6 export function argumentOfType(arg: any, | |||
|
|
62 | 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 | 65 | export function isNull(val: any): val is null | undefined { |
|
|
70 | 66 | return (val === null || val === undefined); |
|
|
71 | 67 | } |
| @@ -77,6 +73,10 export function isPrimitive(val: any): v | |||
|
|
77 | 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 | 80 | export function isInteger(val: any): val is number { |
|
|
81 | 81 | return parseInt(val, 10) === val; |
|
|
82 | 82 | } |
| @@ -86,15 +86,15 export function isNumber(val: any): val | |||
|
|
86 | 86 | } |
|
|
87 | 87 | |
|
|
88 | 88 | export function isString(val: any): val is string { |
|
|
89 |
return typeof (val) === "string" |
|
|
|
89 | return typeof (val) === "string"; | |
|
|
90 | 90 | } |
|
|
91 | 91 | |
|
|
92 | 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 | 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 | 100 | export function isNullOrEmptyString(val: any): val is ("" | null | undefined) { |
| @@ -315,7 +315,7 export function fork() { | |||
|
|
315 | 315 | * assigned later and are required to be not null. |
|
|
316 | 316 | */ |
|
|
317 | 317 | export function notImplemented(): never { |
|
|
318 | throw new Error("Not implemeted"); | |
|
|
318 | throw new Error("Not implemented"); | |
|
|
319 | 319 | } |
|
|
320 | 320 | /** |
|
|
321 | 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 | 493 | export function isDestroyable(d: any): d is IDestroyable { |
|
|
494 |
|
|
|
|
495 | return true; | |
|
|
496 | return false; | |
|
|
494 | return !!(d && typeof d.destroy === "function"); | |
|
|
495 | } | |
|
|
496 | ||
|
|
497 | export function isRemovable(value: any): value is IRemovable { | |
|
|
498 | return !!(value && typeof value.remove === "function"); | |
|
|
497 | 499 | } |
|
|
498 | 500 | |
|
|
499 | 501 | export function destroy(d: any) { |
|
|
500 |
if ( |
|
|
|
502 | if (isDestroyable(d)) | |
|
|
501 | 503 | d.destroy(); |
|
|
502 | 504 | } |
|
|
503 | 505 | |
General Comments 0
You need to be logged in to leave comments.
Login now
