async-injection 1.2.7 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Changelog.md CHANGED
@@ -42,12 +42,16 @@ Update tsc devDependency from 4.3.3 to 4.3.4.
42
42
  Update the ChangeLog to properly reflect recent GitHub releases.
43
43
 
44
44
  **1.2.6 / 2021-07-14**
45
- Merge PR [https://github.com/pcafstockf/async-injection/pull/9](ESLINT integration + Improvements).
45
+ Merge PR #9 [ESLINT integration + Improvements](https://github.com/pcafstockf/async-injection/pull/9).
46
46
  Update devDependencies.
47
47
  Resolved a couple of eslint warnings.
48
48
  tsc no longer removes comments in generated code. This can cause problems with post-processing tools such as istanbul. If file size is of concern to you, you should probably be minifying anyway.
49
49
 
50
50
  **1.2.7 / 2021-08-02**
51
- Revert type declaration for AbstractConstructor which was broken during eslint integration.
51
+ Revert type declaration for AbstractConstructor which was broken during eslint integration.
52
52
  Update eslint related dev-dependencies.
53
53
 
54
+ **1.3.0 / 2021-11-27**
55
+ Support Container driven release of Singleton allocated resources (see Container.releaseSingletons).
56
+ Update devDependencies.
57
+ Minor updates to ReadMe.
package/ReadMe.md CHANGED
@@ -18,7 +18,8 @@ You can get the latest release using npm:
18
18
  $ npm install async-injection --save
19
19
  ```
20
20
 
21
- Add a polyfill for the Reflect API (examples below use reflect-metadata). You can use:
21
+ To enhance flexibility, Async-Injection does not dictate which Reflect API implementation you use. However, you will need to explicitly choose and load one.
22
+ You can use:
22
23
 
23
24
  - [reflect-metadata](https://www.npmjs.com/package/reflect-metadata)
24
25
  - [core-js (core-js/es7/reflect)](https://www.npmjs.com/package/core-js)
@@ -32,6 +33,9 @@ import "reflect-metadata";
32
33
  // Your code here...
33
34
  ```
34
35
 
36
+ Please note that this library supports a wide variety of runtimes and is distributed as both esm and cjs modules, side by side.
37
+ Please see this [link](https://github.com/pcafstockf/async-injection/issues/10) if you experience module errors when compiling.
38
+
35
39
  ## Basic Usage (synchronous)
36
40
  Here we 'get' a new transaction handling object, that itself, relies on a shared service:
37
41
 
@@ -233,7 +237,7 @@ Thanks to Carlos Delgado for the idea of a ["QuerablePromise"](https://ourcodewo
233
237
 
234
238
  ## MIT License
235
239
 
236
- Copyright (c) 2020 Frank Stock
240
+ Copyright (c) 2021 Frank Stock
237
241
 
238
242
  Permission is hereby granted, free of charge, to any person obtaining a copy
239
243
  of this software and associated documentation files (the "Software"), to deal
@@ -2,6 +2,7 @@ export declare const INJECTABLE_METADATA_KEY: unique symbol;
2
2
  export declare const INJECT_METADATA_KEY: unique symbol;
3
3
  export declare const POSTCONSTRUCT_SYNC_METADATA_KEY: unique symbol;
4
4
  export declare const POSTCONSTRUCT_ASYNC_METADATA_KEY: unique symbol;
5
+ export declare const RELEASE_METADATA_KEY: unique symbol;
5
6
  export declare const OPTIONAL_METADATA_KEY: unique symbol;
6
7
  export declare const REFLECT_PARAMS = "design:paramtypes";
7
8
  export declare const REFLECT_RETURN = "design:returntype";
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.REFLECT_RETURN = exports.REFLECT_PARAMS = exports.OPTIONAL_METADATA_KEY = exports.POSTCONSTRUCT_ASYNC_METADATA_KEY = exports.POSTCONSTRUCT_SYNC_METADATA_KEY = exports.INJECT_METADATA_KEY = exports.INJECTABLE_METADATA_KEY = void 0;
3
+ exports.REFLECT_RETURN = exports.REFLECT_PARAMS = exports.OPTIONAL_METADATA_KEY = exports.RELEASE_METADATA_KEY = exports.POSTCONSTRUCT_ASYNC_METADATA_KEY = exports.POSTCONSTRUCT_SYNC_METADATA_KEY = exports.INJECT_METADATA_KEY = exports.INJECTABLE_METADATA_KEY = void 0;
4
4
  exports.INJECTABLE_METADATA_KEY = Symbol('INJECTABLE_KEY');
5
5
  exports.INJECT_METADATA_KEY = Symbol('INJECT_KEY');
6
6
  exports.POSTCONSTRUCT_SYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_SYNC_KEY');
7
7
  exports.POSTCONSTRUCT_ASYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_ASYNC_KEY');
8
+ exports.RELEASE_METADATA_KEY = Symbol('RELEASE_KEY');
8
9
  exports.OPTIONAL_METADATA_KEY = Symbol('OPTIONAL_KEY');
9
10
  exports.REFLECT_PARAMS = 'design:paramtypes';
10
11
  exports.REFLECT_RETURN = 'design:returntype';
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,uBAAuB,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AACnD,QAAA,mBAAmB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAC3C,QAAA,+BAA+B,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AACnE,QAAA,gCAAgC,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AACrE,QAAA,qBAAqB,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAE/C,QAAA,cAAc,GAAG,mBAAmB,CAAC;AACrC,QAAA,cAAc,GAAG,mBAAmB,CAAC","sourcesContent":["export const INJECTABLE_METADATA_KEY = Symbol('INJECTABLE_KEY');\nexport const INJECT_METADATA_KEY = Symbol('INJECT_KEY');\nexport const POSTCONSTRUCT_SYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_SYNC_KEY');\nexport const POSTCONSTRUCT_ASYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_ASYNC_KEY');\nexport const OPTIONAL_METADATA_KEY = Symbol('OPTIONAL_KEY');\n\nexport const REFLECT_PARAMS = 'design:paramtypes';\nexport const REFLECT_RETURN = 'design:returntype';\n"]}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,uBAAuB,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AACnD,QAAA,mBAAmB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAC3C,QAAA,+BAA+B,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AACnE,QAAA,gCAAgC,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AACrE,QAAA,oBAAoB,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AAC7C,QAAA,qBAAqB,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAE/C,QAAA,cAAc,GAAG,mBAAmB,CAAC;AACrC,QAAA,cAAc,GAAG,mBAAmB,CAAC","sourcesContent":["export const INJECTABLE_METADATA_KEY = Symbol('INJECTABLE_KEY');\nexport const INJECT_METADATA_KEY = Symbol('INJECT_KEY');\nexport const POSTCONSTRUCT_SYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_SYNC_KEY');\nexport const POSTCONSTRUCT_ASYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_ASYNC_KEY');\nexport const RELEASE_METADATA_KEY = Symbol('RELEASE_KEY');\nexport const OPTIONAL_METADATA_KEY = Symbol('OPTIONAL_KEY');\n\nexport const REFLECT_PARAMS = 'design:paramtypes';\nexport const REFLECT_RETURN = 'design:returntype';\n"]}
@@ -30,8 +30,9 @@ export declare class Container implements Binder {
30
30
  *
31
31
  * @param id The id to be removed.
32
32
  * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).
33
+ * @param releaseIfSingleton If true, @Provider.releaseIfSingleton will be invoked before the binding is removed.
33
34
  */
34
- removeBinding<T>(id: InjectableId<T>, ascending?: boolean): void;
35
+ removeBinding<T>(id: InjectableId<T>, ascending?: boolean, releaseIfSingleton?: boolean): void;
35
36
  /**
36
37
  * @inheritDoc
37
38
  */
@@ -58,4 +59,15 @@ export declare class Container implements Binder {
58
59
  * It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container.
59
60
  */
60
61
  protected resolveState<T>(id: InjectableId<T>): State<T>;
62
+ /**
63
+ * Convenience method to assist in releasing non-garbage-collectable resources that Singletons in this Container may have allocated.
64
+ * It will walk through all registered Providers (of this Container only), and invoke their @see Provider.releaseIfSingleton method.
65
+ * This method is not part of the Binding interface, because you normally only create (and release) Containers.
66
+ * NOTE:
67
+ * This *only* releases active/pending Singleton's that have already been created by this Container.
68
+ * The most likely use of this method would be when you have created a new child Container for a limited duration transaction, and you want to easily cleanup temporary resources.
69
+ * For example, your service object may need to know when it should unsubscribe from an RxJs stream (failure to do so can result in your Singleton not being garbage collected at the end of a transaction).
70
+ * In theory, you could handle all unsubscription and cleanup yourself, but the @Release decorator and this method are meant to simply make that easier.
71
+ */
72
+ releaseSingletons(): void;
61
73
  }
@@ -76,13 +76,19 @@ class Container {
76
76
  *
77
77
  * @param id The id to be removed.
78
78
  * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).
79
+ * @param releaseIfSingleton If true, @Provider.releaseIfSingleton will be invoked before the binding is removed.
79
80
  */
80
- removeBinding(id, ascending) {
81
+ removeBinding(id, ascending, releaseIfSingleton) {
81
82
  var _a;
83
+ if (releaseIfSingleton) {
84
+ const p = this.providers.get(id);
85
+ if (p)
86
+ p.releaseIfSingleton();
87
+ }
82
88
  this.providers.delete(id);
83
89
  if (ascending && this.parent) {
84
90
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
85
- (_a = this.parent) === null || _a === void 0 ? void 0 : _a.removeBinding(id, true);
91
+ (_a = this.parent) === null || _a === void 0 ? void 0 : _a.removeBinding(id, true, releaseIfSingleton);
86
92
  }
87
93
  }
88
94
  /**
@@ -190,6 +196,22 @@ class Container {
190
196
  }
191
197
  return provider.provideAsState();
192
198
  }
199
+ // noinspection JSUnusedGlobalSymbols
200
+ /**
201
+ * Convenience method to assist in releasing non-garbage-collectable resources that Singletons in this Container may have allocated.
202
+ * It will walk through all registered Providers (of this Container only), and invoke their @see Provider.releaseIfSingleton method.
203
+ * This method is not part of the Binding interface, because you normally only create (and release) Containers.
204
+ * NOTE:
205
+ * This *only* releases active/pending Singleton's that have already been created by this Container.
206
+ * The most likely use of this method would be when you have created a new child Container for a limited duration transaction, and you want to easily cleanup temporary resources.
207
+ * For example, your service object may need to know when it should unsubscribe from an RxJs stream (failure to do so can result in your Singleton not being garbage collected at the end of a transaction).
208
+ * In theory, you could handle all unsubscription and cleanup yourself, but the @Release decorator and this method are meant to simply make that easier.
209
+ */
210
+ releaseSingletons() {
211
+ this.providers.forEach((value) => {
212
+ value.releaseIfSingleton();
213
+ });
214
+ }
193
215
  }
194
216
  exports.Container = Container;
195
217
  //# sourceMappingURL=container.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"container.js","sourceRoot":"","sources":["../../src/container.ts"],"names":[],"mappings":";;;AAAA,qEAAqE;AAErE,qDAAsD;AACtD,2DAAuD;AACvD,2CAAsD;AAGtD,mCAAgC;AAChC,mEAA+D;AAC/D,mCAAoC;AAEpC;;;;GAIG;AACH,MAAM,aAAa;IAClB,YAAmB,MAAW;QAAX,WAAM,GAAN,MAAM,CAAK;IAC9B,CAAC;CACD;AAED;;GAEG;AACH,MAAa,SAAS;IAErB;;OAEG;IACH,YAA6B,MAAiB;QAAjB,WAAM,GAAN,MAAM,CAAW;QAEpC,cAAS,GAAG,IAAI,GAAG,EAA+B,CAAC;IAD7D,CAAC;IAGD;;OAEG;IACI,SAAS,CAAI,EAAmB,EAAE,SAAmB;QAC3D,IAAI,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YAC3B,OAAO,IAAI,CAAC;QACb,IAAI,SAAS,IAAI,IAAI,CAAC,MAAM;YAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC;IACd,CAAC;IAED;;OAEG;IACI,GAAG,CAAI,EAAmB;QAChC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,EAAE;YACd,IAAI,IAAI,C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AAG,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;SACjF;QACD,OAAO,QAAQ,CAAC,cAAc,EAAc,CAAC;IAC9C,CAAC;CACD;AAzLD,8BAyLC","sourcesContent":["import { AsyncFactoryBasedProvider } from './async-factory-provider';\nimport { AsyncFactory, BindAs, Binder, SyncFactory } from './binder';\nimport { ClassBasedProvider } from './class-provider';\nimport { ConstantProvider } from './constant-provider';\nimport { INJECTABLE_METADATA_KEY } from './constants';\nimport { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector';\nimport { Provider } from './provider';\nimport { State } from './state';\nimport { FactoryBasedProvider } from './sync-factory-provider';\nimport { isPromise } from './utils';\n\n/**\n * Helper class to ensure we can distinguish between Error instances legitimately returned from Providers, and Errors thrown by Providers.\n *\n * @see resolveSingletons.\n */\nclass ReasonWrapper {\n\tconstructor(public reason: any) {\n\t}\n}\n\n/**\n * Binder and Injector (aka Container) to handle (a)synchronous dependency management.\n */\nexport class Container implements Binder {\n\n\t/**\n\t * Create a new Container, with an optional parent Injector which will be searched if any given InjectableId is not bound within this Container.\n\t */\n\tpublic constructor(protected parent?: Injector) {\n\t}\n\tprotected providers = new Map<InjectableId<any>, Provider>();\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic isIdKnown<T>(id: InjectableId<T>, ascending?: boolean): boolean {\n\t\tif (!!this.providers.get(id))\n\t\t\treturn true;\n\t\tif (ascending && this.parent)\n\t\t\treturn this.parent.isIdKnown(id, true);\n\t\treturn false;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic get<T>(id: InjectableId<T>): T {\n\t\tconst provider = this.providers.get(id);\n\t\tif (!provider) {\n\t\t\tif (this.parent)\n\t\t\t\treturn this.parent.get<T>(id);\n\t\t\tthrow new Error('Symbol not bound: ' + id.toString());\n\t\t}\n\t\tconst state = provider.provideAsState();\n\t\tif (state.pending)\n\t\t\tthrow new Error('Synchronous request on unresolved asynchronous dependency tree: ' + id.toString());\n\t\tif (state.rejected)\n\t\t\tthrow state.rejected;\n\t\treturn state.fulfilled as T;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic resolve<T>(id: InjectableId<T>): Promise<T> {\n\t\tconst state = this.resolveState(id);\n\t\tif (isPromise(state.promise)) {\n\t\t\treturn state.promise;\n\t\t}\n\n\t\tif (state.rejected) {\n\t\t\treturn Promise.reject(state.rejected);\n\t\t}\n\n\t\treturn Promise.resolve(state.fulfilled);\n\t}\n\n\t// noinspection JSUnusedGlobalSymbols\n\t/**\n\t * This method is not part of the Binding interface, because it is highly unusual.\n\t * But that doesn't mean we can't imagine scenarios where you might require it.\n\t *\n\t * @param id The id to be removed.\n\t * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).\n\t */\n\tpublic removeBinding<T>(id: InjectableId<T>, ascending?: boolean): void {\n\t\tthis.providers.delete(id);\n\n\t\tif (ascending && this.parent) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call\n\t\t\t(this.parent as any)?.removeBinding(id, true);\n\t\t}\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindConstant<T>(id: InjectableId<T>, value: T): void {\n\t\tthis.providers.set(id, new ConstantProvider(value));\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\tpublic bindClass<T>(id: string | symbol | AbstractConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\tpublic bindClass<T>(id: string | symbol | AbstractConstructor<T> | ClassConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>> {\n\t\tif (typeof constructor === 'undefined') {\n\t\t\tconstructor = id as new (...args: any[]) => T;\n\t\t}\n\t\tif (!Reflect.getMetadata(INJECTABLE_METADATA_KEY, constructor)) {\n\t\t\tthrow new Error('Class not decorated with @Injectable [' + constructor.toString() + ']');\n\t\t}\n\t\tconst provider = new ClassBasedProvider(this, id, constructor, (i: InjectableId<any>) => {\n\t\t\treturn this.resolveState(i);\n\t\t});\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>> {\n\t\tconst provider = new FactoryBasedProvider(this, id, factory);\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>> {\n\t\tconst provider = new AsyncFactoryBasedProvider(this, id, factory);\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<void> {\n\t\tconst makePromiseToResolve = () => {\n\t\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\t\tconst pending = new Map<InjectableId<any>, Promise<void>>();\n\t\t\t\t// Ask each provider to resolve itself *IF* it is a singleton.\n\t\t\t\tthis.providers.forEach((value: Provider, key: InjectableId<any>) => {\n\t\t\t\t\t// If the provider is a singleton *and* if resolution is being handled asynchronously, the provider will return a completion promise.\n\t\t\t\t\tconst p = value.resolveIfSingleton(asyncOnly);\n\t\t\t\t\tif (p !== null && typeof p !== 'undefined')\n\t\t\t\t\t\tpending.set(key, p);\n\t\t\t\t});\n\t\t\t\t// The contract for this method is that it behaves somewhat like Promise.allSettled (e.g. won't complete until all pending Singletons have settled).\n\t\t\t\t// Further the contract states that if any of the asynchronous Singletons rejected, that we will also return a rejected Promise, and that the rejection reason will be a Map of the InjectableId's that did not resolve, and the Error they emitted.\n\t\t\t\tconst pp = Array.from(pending.values());\n\t\t\t\tconst keys = Array.from(pending.keys());\n\t\t\t\t// Mapping the catch is an alternate version of Promise.allSettled (e.g. keeps Promise.all from short-circuiting).\n\t\t\t\tPromise.all(pp\n\t\t\t\t\t.map(p => p.catch(e => new ReasonWrapper(e))))\n\t\t\t\t\t.then((results) => {\n\t\t\t\t\t\tconst rejects = new Map<InjectableId<any>, Error>();\n\t\t\t\t\t\t// Check the results. Since we don't export ReasonWrapper, it is safe to assume that an instance of that was produced by our map => catch code above, so it's a rejected Singleton error.\n\t\t\t\t\t\tresults.forEach((result, idx) => {\n\t\t\t\t\t\t\tif (result instanceof ReasonWrapper) {\n\t\t\t\t\t\t\t\trejects.set(keys[idx], result.reason);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t\t// If we had rejections, notify our caller what they were.\n\t\t\t\t\t\tif (rejects.size > 0)\n\t\t\t\t\t\t\treject(rejects);\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\tresolve(); // All good.\n\t\t\t\t\t});\n\t\t\t});\n\t\t};\n\t\tif (parentRecursion && typeof (this.parent as Binder)?.resolveSingletons === \"function\") {\n\t\t\tconst pb: Binder = this.parent as Binder;\n\t\t\treturn pb.resolveSingletons(asyncOnly, parentRecursion).then(() => {\n\t\t\t\treturn makePromiseToResolve();\n\t\t\t});\n\t\t}\n\t\treturn makePromiseToResolve();\n\t}\n\n\t/**\n\t * As implied by the name prefix, this is a factored out method invoked only by the 'resolve' method.\n\t * It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container.\n\t */\n\tprotected resolveState<T>(id: InjectableId<T>): State<T> {\n\t\tconst provider = this.providers.get(id);\n\t\tif (!provider) {\n\t\t\tif (this.parent) {\n\t\t\t\tif (this.parent instanceof Container) {\n\t\t\t\t\treturn this.parent.resolveState<T>(id);\n\t\t\t\t}\n\t\t\t\t// This code (below) will only ever execute if the creator of this container passes in their own implementation of an Injector.\n\t\t\t\t/* istanbul ignore next */\n\t\t\t\ttry {\n\t\t\t\t\treturn State.MakeState<T>(this.parent.resolve<T>(id), undefined, undefined);\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\treturn State.MakeState<T>(null, err);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn State.MakeState<T>(null, new Error('Symbol not bound: ' + id.toString()));\n\t\t}\n\t\treturn provider.provideAsState() as State<T>;\n\t}\n}\n"]}
1
+ {"version":3,"file":"container.js","sourceRoot":"","sources":["../../src/container.ts"],"names":[],"mappings":";;;AAAA,qEAAqE;AAErE,qDAAsD;AACtD,2DAAuD;AACvD,2CAAsD;AAGtD,mCAAgC;AAChC,mEAA+D;AAC/D,mCAAoC;AAEpC;;;;GAIG;AACH,MAAM,aAAa;IAClB,YAAmB,MAAW;QAAX,WAAM,GAAN,MAAM,CAAK;IAC9B,CAAC;CACD;AAED;;GAEG;AACH,MAAa,SAAS;IAErB;;OAEG;IACH,YAA6B,MAAiB;QAAjB,WAAM,GAAN,MAAM,CAAW;QAEpC,cAAS,GAAG,IAAI,GAAG,EAA+B,CAAC;IAD7D,CAAC;IAGD;;OAEG;IACI,SAAS,CAAI,EAAmB,EAAE,SAAmB;QAC3D,IAAI,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YAC3B,OAAO,IAAI,CAAC;QACb,IAAI,SAAS,IAAI,IAAI,CAAC,MAAM;YAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC;IACd,CAAC;IAED;;OAEG;IACI,GAAG,CAAI,EAAmB;QAChC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,EAAE;YACd,IAAI,IAAI,CAAC,MAAM;gBACd,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAI,EAAE,CAAC,CAAC;YAC/B,MAAM,IAAI,KAAK,CAAC,oBAAoB,GAAG,EAAE,CAA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aAAK,CAAC,SAAS,CAAI,IAAI,EAAE,IAAI,KAAK,CAAC,oBAAoB,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;SACjF;QACD,OAAO,QAAQ,CAAC,cAAc,EAAc,CAAC;IAC9C,CAAC;IAED,qCAAqC;IACrC;;;;;;;;;OASG;IACI,iBAAiB;QACvB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,KAAe,EAAE,EAAE;YAC1C,KAAK,CAAC,kBAAkB,EAAE,CAAC;QAC5B,CAAC,CAAC,CAAC;IACJ,CAAC;CACD;AAhND,8BAgNC","sourcesContent":["import { AsyncFactoryBasedProvider } from './async-factory-provider';\nimport { AsyncFactory, BindAs, Binder, SyncFactory } from './binder';\nimport { ClassBasedProvider } from './class-provider';\nimport { ConstantProvider } from './constant-provider';\nimport { INJECTABLE_METADATA_KEY } from './constants';\nimport { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector';\nimport { Provider } from './provider';\nimport { State } from './state';\nimport { FactoryBasedProvider } from './sync-factory-provider';\nimport { isPromise } from './utils';\n\n/**\n * Helper class to ensure we can distinguish between Error instances legitimately returned from Providers, and Errors thrown by Providers.\n *\n * @see resolveSingletons.\n */\nclass ReasonWrapper {\n\tconstructor(public reason: any) {\n\t}\n}\n\n/**\n * Binder and Injector (aka Container) to handle (a)synchronous dependency management.\n */\nexport class Container implements Binder {\n\n\t/**\n\t * Create a new Container, with an optional parent Injector which will be searched if any given InjectableId is not bound within this Container.\n\t */\n\tpublic constructor(protected parent?: Injector) {\n\t}\n\tprotected providers = new Map<InjectableId<any>, Provider>();\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic isIdKnown<T>(id: InjectableId<T>, ascending?: boolean): boolean {\n\t\tif (!!this.providers.get(id))\n\t\t\treturn true;\n\t\tif (ascending && this.parent)\n\t\t\treturn this.parent.isIdKnown(id, true);\n\t\treturn false;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic get<T>(id: InjectableId<T>): T {\n\t\tconst provider = this.providers.get(id);\n\t\tif (!provider) {\n\t\t\tif (this.parent)\n\t\t\t\treturn this.parent.get<T>(id);\n\t\t\tthrow new Error('Symbol not bound: ' + id.toString());\n\t\t}\n\t\tconst state = provider.provideAsState();\n\t\tif (state.pending)\n\t\t\tthrow new Error('Synchronous request on unresolved asynchronous dependency tree: ' + id.toString());\n\t\tif (state.rejected)\n\t\t\tthrow state.rejected;\n\t\treturn state.fulfilled as T;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic resolve<T>(id: InjectableId<T>): Promise<T> {\n\t\tconst state = this.resolveState(id);\n\t\tif (isPromise(state.promise)) {\n\t\t\treturn state.promise;\n\t\t}\n\n\t\tif (state.rejected) {\n\t\t\treturn Promise.reject(state.rejected);\n\t\t}\n\n\t\treturn Promise.resolve(state.fulfilled);\n\t}\n\n\t// noinspection JSUnusedGlobalSymbols\n\t/**\n\t * This method is not part of the Binding interface, because it is highly unusual.\n\t * But that doesn't mean we can't imagine scenarios where you might require it.\n\t *\n\t * @param id The id to be removed.\n\t * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).\n\t * @param releaseIfSingleton If true, @Provider.releaseIfSingleton will be invoked before the binding is removed.\n\t */\n\tpublic removeBinding<T>(id: InjectableId<T>, ascending?: boolean, releaseIfSingleton?: boolean): void {\n\t\tif (releaseIfSingleton) {\n\t\t\tconst p = this.providers.get(id);\n\t\t\tif (p)\n\t\t\t\tp.releaseIfSingleton();\n\t\t}\n\t\tthis.providers.delete(id);\n\n\t\tif (ascending && this.parent) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call\n\t\t\t(this.parent as any)?.removeBinding(id, true, releaseIfSingleton);\n\t\t}\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindConstant<T>(id: InjectableId<T>, value: T): void {\n\t\tthis.providers.set(id, new ConstantProvider(value));\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\tpublic bindClass<T>(id: string | symbol | AbstractConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\tpublic bindClass<T>(id: string | symbol | AbstractConstructor<T> | ClassConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>> {\n\t\tif (typeof constructor === 'undefined') {\n\t\t\tconstructor = id as new (...args: any[]) => T;\n\t\t}\n\t\tif (!Reflect.getMetadata(INJECTABLE_METADATA_KEY, constructor)) {\n\t\t\tthrow new Error('Class not decorated with @Injectable [' + constructor.toString() + ']');\n\t\t}\n\t\tconst provider = new ClassBasedProvider(this, id, constructor, (i: InjectableId<any>) => {\n\t\t\treturn this.resolveState(i);\n\t\t});\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>> {\n\t\tconst provider = new FactoryBasedProvider(this, id, factory);\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>> {\n\t\tconst provider = new AsyncFactoryBasedProvider(this, id, factory);\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<void> {\n\t\tconst makePromiseToResolve = () => {\n\t\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\t\tconst pending = new Map<InjectableId<any>, Promise<void>>();\n\t\t\t\t// Ask each provider to resolve itself *IF* it is a singleton.\n\t\t\t\tthis.providers.forEach((value: Provider, key: InjectableId<any>) => {\n\t\t\t\t\t// If the provider is a singleton *and* if resolution is being handled asynchronously, the provider will return a completion promise.\n\t\t\t\t\tconst p = value.resolveIfSingleton(asyncOnly);\n\t\t\t\t\tif (p !== null && typeof p !== 'undefined')\n\t\t\t\t\t\tpending.set(key, p);\n\t\t\t\t});\n\t\t\t\t// The contract for this method is that it behaves somewhat like Promise.allSettled (e.g. won't complete until all pending Singletons have settled).\n\t\t\t\t// Further the contract states that if any of the asynchronous Singletons rejected, that we will also return a rejected Promise, and that the rejection reason will be a Map of the InjectableId's that did not resolve, and the Error they emitted.\n\t\t\t\tconst pp = Array.from(pending.values());\n\t\t\t\tconst keys = Array.from(pending.keys());\n\t\t\t\t// Mapping the catch is an alternate version of Promise.allSettled (e.g. keeps Promise.all from short-circuiting).\n\t\t\t\tPromise.all(pp\n\t\t\t\t\t.map(p => p.catch(e => new ReasonWrapper(e))))\n\t\t\t\t\t.then((results) => {\n\t\t\t\t\t\tconst rejects = new Map<InjectableId<any>, Error>();\n\t\t\t\t\t\t// Check the results. Since we don't export ReasonWrapper, it is safe to assume that an instance of that was produced by our map => catch code above, so it's a rejected Singleton error.\n\t\t\t\t\t\tresults.forEach((result, idx) => {\n\t\t\t\t\t\t\tif (result instanceof ReasonWrapper) {\n\t\t\t\t\t\t\t\trejects.set(keys[idx], result.reason);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t\t// If we had rejections, notify our caller what they were.\n\t\t\t\t\t\tif (rejects.size > 0)\n\t\t\t\t\t\t\treject(rejects);\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\tresolve(); // All good.\n\t\t\t\t\t});\n\t\t\t});\n\t\t};\n\t\tif (parentRecursion && typeof (this.parent as Binder)?.resolveSingletons === \"function\") {\n\t\t\tconst pb: Binder = this.parent as Binder;\n\t\t\treturn pb.resolveSingletons(asyncOnly, parentRecursion).then(() => {\n\t\t\t\treturn makePromiseToResolve();\n\t\t\t});\n\t\t}\n\t\treturn makePromiseToResolve();\n\t}\n\n\t/**\n\t * As implied by the name prefix, this is a factored out method invoked only by the 'resolve' method.\n\t * It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container.\n\t */\n\tprotected resolveState<T>(id: InjectableId<T>): State<T> {\n\t\tconst provider = this.providers.get(id);\n\t\tif (!provider) {\n\t\t\tif (this.parent) {\n\t\t\t\tif (this.parent instanceof Container) {\n\t\t\t\t\treturn this.parent.resolveState<T>(id);\n\t\t\t\t}\n\t\t\t\t// This code (below) will only ever execute if the creator of this container passes in their own implementation of an Injector.\n\t\t\t\t/* istanbul ignore next */\n\t\t\t\ttry {\n\t\t\t\t\treturn State.MakeState<T>(this.parent.resolve<T>(id), undefined, undefined);\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\treturn State.MakeState<T>(null, err);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn State.MakeState<T>(null, new Error('Symbol not bound: ' + id.toString()));\n\t\t}\n\t\treturn provider.provideAsState() as State<T>;\n\t}\n\n\t// noinspection JSUnusedGlobalSymbols\n\t/**\n\t * Convenience method to assist in releasing non-garbage-collectable resources that Singletons in this Container may have allocated.\n\t * It will walk through all registered Providers (of this Container only), and invoke their @see Provider.releaseIfSingleton method.\n\t * This method is not part of the Binding interface, because you normally only create (and release) Containers.\n\t * NOTE:\n\t * This *only* releases active/pending Singleton's that have already been created by this Container.\n\t * The most likely use of this method would be when you have created a new child Container for a limited duration transaction, and you want to easily cleanup temporary resources.\n\t * For example, your service object may need to know when it should unsubscribe from an RxJs stream (failure to do so can result in your Singleton not being garbage collected at the end of a transaction).\n\t * In theory, you could handle all unsubscription and cleanup yourself, but the @Release decorator and this method are meant to simply make that easier.\n\t */\n\tpublic releaseSingletons(): void {\n\t\tthis.providers.forEach((value: Provider) => {\n\t\t\tvalue.releaseIfSingleton();\n\t\t});\n\t}\n}\n"]}
@@ -41,3 +41,16 @@ export declare function _getOptionalDefaultAt(target: Function, parameterIndex:
41
41
  * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
42
42
  */
43
43
  export declare function PostConstruct(): MethodDecorator;
44
+ /**
45
+ * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.
46
+ * If invoked by the container, the container will drop any references it has to the object when the method returns.
47
+ * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).
48
+ * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.
49
+ * You should *not* use this decorator as a general "object finalization" method. It has very limited scope and purpose.
50
+ * The decorated method must complete normally (no throwing), as "release" is not an abort-able process.
51
+ * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
52
+ * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.
53
+ * Also @see Container.releaseSingletons for the intended usage of this decorator.
54
+ * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).
55
+ */
56
+ export declare function Release(): MethodDecorator;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PostConstruct = exports._getOptionalDefaultAt = exports.Optional = exports._getInjectedIdAt = exports.Inject = exports.Injectable = void 0;
3
+ exports.Release = exports.PostConstruct = exports._getOptionalDefaultAt = exports.Optional = exports._getInjectedIdAt = exports.Inject = exports.Injectable = void 0;
4
4
  /* eslint-disable @typescript-eslint/ban-types */
5
5
  /**
6
6
  * These decorators all apply the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor
@@ -155,4 +155,36 @@ function PostConstruct() {
155
155
  };
156
156
  }
157
157
  exports.PostConstruct = PostConstruct;
158
+ // noinspection JSUnusedGlobalSymbols
159
+ /**
160
+ * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.
161
+ * If invoked by the container, the container will drop any references it has to the object when the method returns.
162
+ * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).
163
+ * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.
164
+ * You should *not* use this decorator as a general "object finalization" method. It has very limited scope and purpose.
165
+ * The decorated method must complete normally (no throwing), as "release" is not an abort-able process.
166
+ * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
167
+ * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.
168
+ * Also @see Container.releaseSingletons for the intended usage of this decorator.
169
+ * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).
170
+ */
171
+ function Release() {
172
+ /**
173
+ * @param prototypeOrConstructor The prototype of the class (we don't allow @Release on anything other than a class instance method.
174
+ * @param methodName The name of the method.
175
+ * @param descriptor The Property Descriptor for the method.
176
+ * @returns Undefined (nothing), as this decorator does not modify the method in any way.
177
+ */
178
+ // noinspection JSUnusedLocalSymbols
179
+ return function (target, methodName, descriptor) {
180
+ if (typeof target !== 'object' || typeof target.constructor !== 'function') {
181
+ throw new Error('@Release not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');
182
+ }
183
+ if (Reflect.hasOwnMetadata(constants_1.RELEASE_METADATA_KEY, target.constructor)) {
184
+ throw new Error('@Release applied multiple times [' + targetHint(target.constructor) + ']');
185
+ }
186
+ Reflect.defineMetadata(constants_1.RELEASE_METADATA_KEY, methodName, target.constructor);
187
+ };
188
+ }
189
+ exports.Release = Release;
158
190
  //# sourceMappingURL=decorators.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/decorators.ts"],"names":[],"mappings":";;;AAAA,iDAAiD;AACjD;;GAEG;AACH,2CAAqL;AAGrL,0CAA0C;AAC1C,SAAS,UAAU,CAAC,MAAgB;IACnC,IAAI,IAAwB,CAAC;IAC7B,IAAI,MAAM,EAAE;QACX,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACnB,IAAI,CAAC,C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AC,WAAW,CAAC,CAAC;SACxF;IACF,CAAC,CAAC;AACH,CAAC;AAtBD,sCAsBC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/**\n * These decorators all apply the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor\n */\nimport { INJECTABLE_METADATA_KEY, INJECT_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_RETURN } from './constants';\nimport { InjectableId } from './injector';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function) {\n\tlet hint: string | undefined;\n\tif (target) {\n\t\thint = target.name;\n\t\tif ((!hint) && target.constructor) {\n\t\t\thint = target.constructor.name;\n\t\t}\n\t}\n\treturn hint;\n}\n\n// Validate that 'target' is a class constructor function.\nfunction isClassConstructor(target: any) {\n\t// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\treturn target.prototype.constructor === target;\n\t}\n\treturn false;\n}\n\n// Ensure consistency in our meta-data name getting/setting.\nfunction makeParamIdxKey(idx: number): string {\n\treturn `index-${idx}`;\n}\n\n// Validate that the specified target is a parameter of a class constructor\nfunction validateConstructorParam(decorator: string, target: Function, idx: number): string {\n\tif (!isClassConstructor(target)) {\n\t\tthrow new Error('@' + decorator + ' is not valid here [' + targetHint(target) + ']');\n\t}\n\treturn makeParamIdxKey(idx);\n}\n\n// Validate the decorator was only applied once.\nfunction validateSingleConstructorParam(decorator: string, target: Function, idx: number): string {\n\tconst propKey = validateConstructorParam(decorator, target, idx);\n\tif (Reflect.hasOwnMetadata(decorator, target, propKey)) {\n\t\tthrow new Error('@' + decorator + ' applied multiple times [' + target.constructor.name + ']');\n\t}\n\treturn propKey;\n}\n\n/**\n * Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container.\n * This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.\n */\nexport function Injectable(): ClassDecorator {\n\t/**\n\t * @param target The constructor function of the class that is being decorated\n\t * @returns Undefined (nothing), as this decorator does not modify the constructor in any way.\n\t */\n\treturn function (target: Function): void {\n\t\tif (Reflect.hasOwnMetadata(INJECTABLE_METADATA_KEY, target)) {\n\t\t\tthrow new Error('@Injectable applied multiple times [' + targetHint(target) + ']');\n\t\t}\n\t\tReflect.defineMetadata(INJECTABLE_METADATA_KEY, true, target);\n\t};\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of the type of Object that will be injected into the parameter.\n * In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).\n *\n * @param id The identifier of the bound type that should be injected.\n */\nexport function Inject(id: InjectableId<any>): ParameterDecorator {\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Inject on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst hint = targetHint(target);\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + hint + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target, parameterIndex);\n\t\tReflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Inject metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Inject on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Inject\n */\nexport function _getInjectedIdAt(target: Function, parameterIndex: number): InjectableId<any> {\n\treturn Reflect.getMetadata(INJECT_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as InjectableId<any>;\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.\n * WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.\n */\nexport function Optional(alt?: any): ParameterDecorator { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Optional on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target, parameterIndex);\n\t\tReflect.defineMetadata(OPTIONAL_METADATA_KEY, { value: alt }, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Optional metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Optional on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Optional\n * @returns an object containing the value provided in the decorator, or undefined if no annotation was present.\n */\nexport function _getOptionalDefaultAt(target: Function, parameterIndex: number): { value: any } {\n\treturn Reflect.getMetadata(OPTIONAL_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as { value: any }; // See the @Optional decorator before making any changes here.\n}\n\n/**\n * Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.\n * The method will be assumed to be synchronous unless the method signature explicitly declares it's return type to be \": Promise<something>\"\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n */\nexport function PostConstruct(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @PostConstruct on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\t// noinspection JSUnusedLocalSymbols\n\treturn function (target: Object, methodName: string | symbol, descriptor: PropertyDescriptor) { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@PostConstruct not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, target.constructor) || Reflect.hasOwnMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@PostConstruct applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tconst rt = Reflect.getMetadata(REFLECT_RETURN, target, methodName);\n\t\tif (typeof rt === 'function') {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, methodName, target.constructor);\n\t\t} else {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, methodName, target.constructor);\n\t\t}\n\t};\n}\n"]}
1
+ {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/decorators.ts"],"names":[],"mappings":";;;AAAA,iDAAiD;AACjD;;GAEG;AACH,2CAAyM;AAGzM,0CAA0C;AAC1C,SAAS,UAAU,CAAC,MAAgB;IACnC,IAAI,IAAwB,CAAC;IAC7B,IAAI,MAAM,EAAE;QACX,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACnB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE;YAClC,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC;SAC/B;KACD;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,0DAA0D;AAC1D,SAAS,kBAAkB,CAAC,MAAW;IACtC,yGAAyG;IACzG,IAAI,OAAO,MAAM,KAAK,UAAU,IAAI,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE;QACvE,sEAAsE;QACtE,OAAO,MAAM,CAAC,SAAS,CAAC,WAAW,KAAK,MAAM,CAAC;KAC/C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AG,GAAG,UAAU,CAAC,QAAQ,EAAE,GAAG,GAAG,CAAC,CAAC;SACrH;QACD,IAAI,OAAO,CAAC,cAAc,CAAC,gCAAoB,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE;YACrE,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,GAAG,CAAC,CAAC;SAC5F;QACD,OAAO,CAAC,cAAc,CAAC,gCAAoB,EAAE,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC,CAAC;AACH,CAAC;AAjBD,0BAiBC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/**\n * These decorators all apply the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor\n */\nimport {INJECTABLE_METADATA_KEY, INJECT_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_RETURN, RELEASE_METADATA_KEY} from './constants';\nimport { InjectableId } from './injector';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function) {\n\tlet hint: string | undefined;\n\tif (target) {\n\t\thint = target.name;\n\t\tif ((!hint) && target.constructor) {\n\t\t\thint = target.constructor.name;\n\t\t}\n\t}\n\treturn hint;\n}\n\n// Validate that 'target' is a class constructor function.\nfunction isClassConstructor(target: any) {\n\t// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\treturn target.prototype.constructor === target;\n\t}\n\treturn false;\n}\n\n// Ensure consistency in our meta-data name getting/setting.\nfunction makeParamIdxKey(idx: number): string {\n\treturn `index-${idx}`;\n}\n\n// Validate that the specified target is a parameter of a class constructor\nfunction validateConstructorParam(decorator: string, target: Function, idx: number): string {\n\tif (!isClassConstructor(target)) {\n\t\tthrow new Error('@' + decorator + ' is not valid here [' + targetHint(target) + ']');\n\t}\n\treturn makeParamIdxKey(idx);\n}\n\n// Validate the decorator was only applied once.\nfunction validateSingleConstructorParam(decorator: string, target: Function, idx: number): string {\n\tconst propKey = validateConstructorParam(decorator, target, idx);\n\tif (Reflect.hasOwnMetadata(decorator, target, propKey)) {\n\t\tthrow new Error('@' + decorator + ' applied multiple times [' + target.constructor.name + ']');\n\t}\n\treturn propKey;\n}\n\n/**\n * Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container.\n * This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.\n */\nexport function Injectable(): ClassDecorator {\n\t/**\n\t * @param target The constructor function of the class that is being decorated\n\t * @returns Undefined (nothing), as this decorator does not modify the constructor in any way.\n\t */\n\treturn function (target: Function): void {\n\t\tif (Reflect.hasOwnMetadata(INJECTABLE_METADATA_KEY, target)) {\n\t\t\tthrow new Error('@Injectable applied multiple times [' + targetHint(target) + ']');\n\t\t}\n\t\tReflect.defineMetadata(INJECTABLE_METADATA_KEY, true, target);\n\t};\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of the type of Object that will be injected into the parameter.\n * In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).\n *\n * @param id The identifier of the bound type that should be injected.\n */\nexport function Inject(id: InjectableId<any>): ParameterDecorator {\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Inject on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst hint = targetHint(target);\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + hint + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target, parameterIndex);\n\t\tReflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Inject metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Inject on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Inject\n */\nexport function _getInjectedIdAt(target: Function, parameterIndex: number): InjectableId<any> {\n\treturn Reflect.getMetadata(INJECT_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as InjectableId<any>;\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.\n * WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.\n */\nexport function Optional(alt?: any): ParameterDecorator { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Optional on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target, parameterIndex);\n\t\tReflect.defineMetadata(OPTIONAL_METADATA_KEY, { value: alt }, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Optional metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Optional on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Optional\n * @returns an object containing the value provided in the decorator, or undefined if no annotation was present.\n */\nexport function _getOptionalDefaultAt(target: Function, parameterIndex: number): { value: any } {\n\treturn Reflect.getMetadata(OPTIONAL_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as { value: any }; // See the @Optional decorator before making any changes here.\n}\n\n/**\n * Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.\n * The method will be assumed to be synchronous unless the method signature explicitly declares it's return type to be \": Promise<something>\"\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n */\nexport function PostConstruct(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @PostConstruct on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\t// noinspection JSUnusedLocalSymbols\n\treturn function (target: Object, methodName: string | symbol, descriptor: PropertyDescriptor) { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@PostConstruct not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, target.constructor) || Reflect.hasOwnMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@PostConstruct applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tconst rt = Reflect.getMetadata(REFLECT_RETURN, target, methodName);\n\t\tif (typeof rt === 'function') {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, methodName, target.constructor);\n\t\t} else {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, methodName, target.constructor);\n\t\t}\n\t};\n}\n\n// noinspection JSUnusedGlobalSymbols\n/**\n * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.\n * If invoked by the container, the container will drop any references it has to the object when the method returns.\n * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).\n * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.\n * You should *not* use this decorator as a general \"object finalization\" method. It has very limited scope and purpose.\n * The decorated method must complete normally (no throwing), as \"release\" is not an abort-able process.\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.\n * Also @see Container.releaseSingletons for the intended usage of this decorator.\n * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).\n */\nexport function Release(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @Release on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\t// noinspection JSUnusedLocalSymbols\n\treturn function (target: Object, methodName: string | symbol, descriptor: PropertyDescriptor) { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@Release not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(RELEASE_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@Release applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tReflect.defineMetadata(RELEASE_METADATA_KEY, methodName, target.constructor);\n\t};\n}\n"]}
@@ -26,4 +26,12 @@ export declare abstract class Provider<T = any> {
26
26
  * @returns A completion Promise if initialization requires asynchronicity, otherwise the return value is undefined.
27
27
  */
28
28
  resolveIfSingleton(asyncOnly: boolean): Promise<T>;
29
+ /**
30
+ * If (and only if) this Provider has been configured as a Singleton, and if it has been (or is being resolved), find and invoke the @Release decorated method (if there is one).
31
+ * NOTE that if the singleton is actively being resolved when this method is called, this method waits for the resolution to complete and then invokes the @Release decorated method; But in any case this is a synchronous method and returns immediately to it's caller.
32
+ * Also note that invoking this method does not release or invalidate the Provider;
33
+ * Rather, it resets a Singleton Provider to a fresh (unresolved/unqueried) state (aka sets this.singleton to null).
34
+ * It is assumed that the Singleton itself will no longer be used after this method returns.
35
+ */
36
+ releaseIfSingleton(): void;
29
37
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Provider = void 0;
4
+ const utils_1 = require("./utils");
4
5
  /**
5
6
  * Internally all InjectableIds are mapped to an abstract Provider<T>.
6
7
  * A Provider may choose to return a singleton or a new value each time it is queried.
@@ -25,6 +26,31 @@ class Provider {
25
26
  }
26
27
  return undefined;
27
28
  }
29
+ /**
30
+ * If (and only if) this Provider has been configured as a Singleton, and if it has been (or is being resolved), find and invoke the @Release decorated method (if there is one).
31
+ * NOTE that if the singleton is actively being resolved when this method is called, this method waits for the resolution to complete and then invokes the @Release decorated method; But in any case this is a synchronous method and returns immediately to it's caller.
32
+ * Also note that invoking this method does not release or invalidate the Provider;
33
+ * Rather, it resets a Singleton Provider to a fresh (unresolved/unqueried) state (aka sets this.singleton to null).
34
+ * It is assumed that the Singleton itself will no longer be used after this method returns.
35
+ */
36
+ releaseIfSingleton() {
37
+ if (this.singleton) {
38
+ const s = this.provideAsState();
39
+ if (s.pending) {
40
+ s.promise.then((v) => {
41
+ this.singleton = null;
42
+ utils_1.InvokeReleaseMethod(v);
43
+ }).catch(() => {
44
+ this.singleton = null;
45
+ });
46
+ }
47
+ else {
48
+ this.singleton = null;
49
+ if ((!s.rejected) && s.fulfilled)
50
+ utils_1.InvokeReleaseMethod(s.fulfilled);
51
+ }
52
+ }
53
+ }
28
54
  }
29
55
  exports.Provider = Provider;
30
56
  //# sourceMappingURL=provider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/provider.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH,MAAsB,QAAQ;IAC7B;IACA,CAAC;IAiBD;;;;;;OAMG;IACH,kBAAkB,CAAC,SAAkB;QACpC,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI,EAAE;YAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;YAChC,IAAI,CAAC,CAAC,OAAO;gBACZ,OAAO,CAAC,CAAC,OAAO,CAAC;iBACb,IAAI,CAAC,CAAC,QAAQ;gBAClB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;SACnC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;CACD;AApCD,4BAoCC","sourcesContent":["import { State } from './state';\n\n/**\n * Internally all InjectableIds are mapped to an abstract Provider<T>.\n * A Provider may choose to return a singleton or a new value each time it is queried.\n */\nexport abstract class Provider<T = any> {\n\tprotected constructor() {\n\t}\n\n\t/**\n\t * If the provider is configured as a singleton, this property will be the state of that singleton.\n\t * This value will be defined for resolved/resolving Singletons, null for Singletons that have not yet been queried, and will remain undefined for non-Singleton Providers.\n\t * Default value is undefined (e.g. not a Singleton).\n\t */\n\tprotected singleton?: State<T>;\n\n\t/**\n\t * This is the workhorse method of the Provider, and is invoked directly or indirectly by both Injector.get and Injector.resolve.\n\t * This method returns the current State<T> if it is already known (which it might be for Singleton scenarios).\n\t * Otherwise it resolves the State<T>.\n\t * IF the Provider<T> is a Singleton, it's State<T> is updated before returning.\n\t */\n\tabstract provideAsState(): State<T>;\n\n\t/**\n\t * Base method to initialize the state of this Provider *if* (and only if) it has been configured as a Singleton.\n\t * If this Provider has not been configured as a singleton, this method is essentially a noop that returns undefined.\n\t *\n\t * @param asyncOnly This default implementation ignores this parameter.\n\t * @returns A completion Promise if initialization requires asynchronicity, otherwise the return value is undefined.\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (this.singleton === null) {\n\t\t\tconst s = this.provideAsState();\n\t\t\tif (s.pending)\n\t\t\t\treturn s.promise;\n\t\t\telse if (s.rejected)\n\t\t\t\treturn Promise.reject(s.rejected);\n\t\t}\n\t\treturn undefined;\n\t}\n}\n"]}
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/provider.ts"],"names":[],"mappings":";;;AACA,mCAA8C;AAG9C;;;GAGG;AACH,MAAsB,QAAQ;IAC7B;IACA,CAAC;IAiBD;;;;;;OAMG;IACH,kBAAkB,CAAC,SAAkB;QACpC,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI,EAAE;YAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;YAChC,IAAI,CAAC,CAAC,OAAO;gBACZ,OAAO,CAAC,CAAC,OAAO,CAAC;iBACb,IAAI,CAAC,CAAC,QAAQ;gBAClB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;SACnC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;IAED;;;;;;OAMG;IACH,kBAAkB;QACjB,IAAI,IAAI,CAAC,SAAS,EAAE;YACnB,MAAM,CAAC,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;YAChC,IAAI,CAAC,CAAC,OAAO,EAAE;gBACd,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;oBACpB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;oBACtB,2BAAmB,CAAC,CAAC,CAAC,CAAC;gBACxB,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;oBACb,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;gBACvB,CAAC,CAAC,CAAC;aACH;iBACI;gBACJ,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;gBACtB,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,SAAS;oBAC/B,2BAAmB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;aAClC;SACD;IACF,CAAC;CACD;AA9DD,4BA8DC","sourcesContent":["import { State } from './state';\nimport { InvokeReleaseMethod } from './utils';\n\n\n/**\n * Internally all InjectableIds are mapped to an abstract Provider<T>.\n * A Provider may choose to return a singleton or a new value each time it is queried.\n */\nexport abstract class Provider<T = any> {\n\tprotected constructor() {\n\t}\n\n\t/**\n\t * If the provider is configured as a singleton, this property will be the state of that singleton.\n\t * This value will be defined for resolved/resolving Singletons, null for Singletons that have not yet been queried, and will remain undefined for non-Singleton Providers.\n\t * Default value is undefined (e.g. not a Singleton).\n\t */\n\tprotected singleton?: State<T>;\n\n\t/**\n\t * This is the workhorse method of the Provider, and is invoked directly or indirectly by both Injector.get and Injector.resolve.\n\t * This method returns the current State<T> if it is already known (which it might be for Singleton scenarios).\n\t * Otherwise it resolves the State<T>.\n\t * IF the Provider<T> is a Singleton, it's State<T> is updated before returning.\n\t */\n\tabstract provideAsState(): State<T>;\n\n\t/**\n\t * Base method to initialize the state of this Provider *if* (and only if) it has been configured as a Singleton.\n\t * If this Provider has not been configured as a singleton, this method is essentially a noop that returns undefined.\n\t *\n\t * @param asyncOnly This default implementation ignores this parameter.\n\t * @returns A completion Promise if initialization requires asynchronicity, otherwise the return value is undefined.\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (this.singleton === null) {\n\t\t\tconst s = this.provideAsState();\n\t\t\tif (s.pending)\n\t\t\t\treturn s.promise;\n\t\t\telse if (s.rejected)\n\t\t\t\treturn Promise.reject(s.rejected);\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * If (and only if) this Provider has been configured as a Singleton, and if it has been (or is being resolved), find and invoke the @Release decorated method (if there is one).\n\t * NOTE that if the singleton is actively being resolved when this method is called, this method waits for the resolution to complete and then invokes the @Release decorated method; But in any case this is a synchronous method and returns immediately to it's caller.\n\t * Also note that invoking this method does not release or invalidate the Provider;\n\t * Rather, it resets a Singleton Provider to a fresh (unresolved/unqueried) state (aka sets this.singleton to null).\n\t * It is assumed that the Singleton itself will no longer be used after this method returns.\n\t */\n\treleaseIfSingleton(): void {\n\t\tif (this.singleton) {\n\t\t\tconst s = this.provideAsState();\n\t\t\tif (s.pending) {\n\t\t\t\ts.promise.then((v) => {\n\t\t\t\t\tthis.singleton = null;\n\t\t\t\t\tInvokeReleaseMethod(v);\n\t\t\t\t}).catch(() => {\n\t\t\t\t\tthis.singleton = null;\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.singleton = null;\n\t\t\t\tif ((!s.rejected) && s.fulfilled)\n\t\t\t\t\tInvokeReleaseMethod(s.fulfilled);\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
@@ -1,2 +1,14 @@
1
+ /**
2
+ * Returns true if the specified object looks like a JavaScript Error object.
3
+ */
1
4
  export declare function isErrorObj(err: any): err is Error;
5
+ /**
6
+ * Returns true if the specified value is "thenable" (aka a Promise).
7
+ */
2
8
  export declare function isPromise<T>(value: any): value is Promise<T>;
9
+ /**
10
+ * Simple helper function to find the @Release decorated method of an object (if any), and invoke it.
11
+ * This is primarily an internal method as you probably know the exact method, and should invoke it yourself.
12
+ * async-injection uses this helper to allow Singletons to clean up any non-garbage-collectable resources they may have allocated.
13
+ */
14
+ export declare function InvokeReleaseMethod<T = unknown>(obj: T): boolean;
package/lib/cjs/utils.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
- /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.isPromise = exports.isErrorObj = void 0;
3
+ exports.InvokeReleaseMethod = exports.isPromise = exports.isErrorObj = void 0;
4
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */
5
+ const constants_1 = require("./constants");
6
+ /**
7
+ * Returns true if the specified object looks like a JavaScript Error object.
8
+ */
5
9
  function isErrorObj(err) {
6
10
  if (!err)
7
11
  return false;
@@ -10,6 +14,9 @@ function isErrorObj(err) {
10
14
  return err && typeof err.message === 'string' && typeof err.stack === 'string';
11
15
  }
12
16
  exports.isErrorObj = isErrorObj;
17
+ /**
18
+ * Returns true if the specified value is "thenable" (aka a Promise).
19
+ */
13
20
  function isPromise(value) {
14
21
  if (!value)
15
22
  return false;
@@ -18,4 +25,24 @@ function isPromise(value) {
18
25
  return value && typeof value.then === 'function';
19
26
  }
20
27
  exports.isPromise = isPromise;
28
+ /**
29
+ * Simple helper function to find the @Release decorated method of an object (if any), and invoke it.
30
+ * This is primarily an internal method as you probably know the exact method, and should invoke it yourself.
31
+ * async-injection uses this helper to allow Singletons to clean up any non-garbage-collectable resources they may have allocated.
32
+ */
33
+ function InvokeReleaseMethod(obj) {
34
+ var _a, _b;
35
+ const releaseMethod = Reflect.getMetadata(constants_1.RELEASE_METADATA_KEY, obj.constructor);
36
+ /* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
37
+ if (releaseMethod && obj.constructor.prototype[releaseMethod] && typeof obj.constructor.prototype[releaseMethod] === 'function') {
38
+ const releaseFn = (_b = (_a = obj[releaseMethod]).bind) === null || _b === void 0 ? void 0 : _b.call(_a, obj);
39
+ if (releaseFn) {
40
+ releaseFn();
41
+ return true;
42
+ }
43
+ }
44
+ /* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
45
+ return false;
46
+ }
47
+ exports.InvokeReleaseMethod = InvokeReleaseMethod;
21
48
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";AAAA,kHAAkH;;;AAElH,SAAgB,UAAU,CAAC,GAAQ;IAClC,IAAI,CAAC,GAAG;QACD,OAAO,KAAK,CAAC;IAEjB,IAAI,GAAG,YAAY,KAAK;QACpB,OAAO,IAAI,CAAC;IAEhB,OAAO,GAAG,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,CAAC;AACnF,CAAC;AARD,gCAQC;AAED,SAAgB,SAAS,CAAI,KAAU;IACnC,IAAI,CAAC,KAAK;QACN,OAAO,KAAK,CAAC;IAEjB,IAAI,KAAK,YAAY,OAAO;QACxB,OAAO,IAAI,CAAC;IAEhB,OAAO,KAAK,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC;AACrD,CAAC;AARD,8BAQC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */\n\nexport function isErrorObj(err: any): err is Error {\n\tif (!err)\n return false;\n\n if (err instanceof Error)\n return true;\n\n return err && typeof err.message === 'string' && typeof err.stack === 'string';\n}\n\nexport function isPromise<T>(value: any): value is Promise<T> {\n if (!value)\n return false;\n\n if (value instanceof Promise)\n return true;\n\n return value && typeof value.then === 'function';\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAAA,kHAAkH;AAClH,2CAAiD;AAEjD;;GAEG;AACH,SAAgB,UAAU,CAAC,GAAQ;IAClC,IAAI,CAAC,GAAG;QACD,OAAO,KAAK,CAAC;IAEjB,IAAI,GAAG,YAAY,KAAK;QACpB,OAAO,IAAI,CAAC;IAEhB,OAAO,GAAG,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,CAAC;AACnF,CAAC;AARD,gCAQC;AAED;;GAEG;AACH,SAAgB,SAAS,CAAI,KAAU;IACnC,IAAI,CAAC,KAAK;QACN,OAAO,KAAK,CAAC;IAEjB,IAAI,KAAK,YAAY,OAAO;QACxB,OAAO,IAAI,CAAC;IAEhB,OAAO,KAAK,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC;AACrD,CAAC;AARD,8BAQC;AAED;;;;GAIG;AACH,SAAgB,mBAAmB,CAAY,GAAM;;IACpD,MAAM,aAAa,GAAW,OAAO,CAAC,WAAW,CAAC,gCAAoB,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;IACzF,kGAAkG;IAClG,IAAI,aAAa,IAAI,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,aAAa,CAAC,IAAI,OAAO,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,aAAa,CAAC,KAAK,UAAU,EAAE;QAChI,MAAM,SAAS,GAAG,MAAA,MAAA,GAAG,CAAC,aAAa,CAAC,EAAC,IAAI,mDAAG,GAAG,CAAC,CAAC;QACjD,IAAI,SAAS,EAAE;YACd,SAAS,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC;SACZ;KACD;IACD,iGAAiG;IACjG,OAAO,KAAK,CAAC;AACd,CAAC;AAZD,kDAYC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */\nimport {RELEASE_METADATA_KEY} from './constants';\n\n/**\n * Returns true if the specified object looks like a JavaScript Error object.\n */\nexport function isErrorObj(err: any): err is Error {\n\tif (!err)\n return false;\n\n if (err instanceof Error)\n return true;\n\n return err && typeof err.message === 'string' && typeof err.stack === 'string';\n}\n\n/**\n * Returns true if the specified value is \"thenable\" (aka a Promise).\n */\nexport function isPromise<T>(value: any): value is Promise<T> {\n if (!value)\n return false;\n\n if (value instanceof Promise)\n return true;\n\n return value && typeof value.then === 'function';\n}\n\n/**\n * Simple helper function to find the @Release decorated method of an object (if any), and invoke it.\n * This is primarily an internal method as you probably know the exact method, and should invoke it yourself.\n * async-injection uses this helper to allow Singletons to clean up any non-garbage-collectable resources they may have allocated.\n */\nexport function InvokeReleaseMethod<T=unknown>(obj: T) : boolean {\n\tconst releaseMethod: string = Reflect.getMetadata(RELEASE_METADATA_KEY, obj.constructor);\n\t/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\tif (releaseMethod && obj.constructor.prototype[releaseMethod] && typeof obj.constructor.prototype[releaseMethod] === 'function') {\n\t\tconst releaseFn = obj[releaseMethod].bind?.(obj);\n\t\tif (releaseFn) {\n\t\t\treleaseFn();\n\t\t\treturn true;\n\t\t}\n\t}\n\t/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\treturn false;\n}\n"]}
@@ -2,6 +2,7 @@ export declare const INJECTABLE_METADATA_KEY: unique symbol;
2
2
  export declare const INJECT_METADATA_KEY: unique symbol;
3
3
  export declare const POSTCONSTRUCT_SYNC_METADATA_KEY: unique symbol;
4
4
  export declare const POSTCONSTRUCT_ASYNC_METADATA_KEY: unique symbol;
5
+ export declare const RELEASE_METADATA_KEY: unique symbol;
5
6
  export declare const OPTIONAL_METADATA_KEY: unique symbol;
6
7
  export declare const REFLECT_PARAMS = "design:paramtypes";
7
8
  export declare const REFLECT_RETURN = "design:returntype";
@@ -2,6 +2,7 @@ export const INJECTABLE_METADATA_KEY = Symbol('INJECTABLE_KEY');
2
2
  export const INJECT_METADATA_KEY = Symbol('INJECT_KEY');
3
3
  export const POSTCONSTRUCT_SYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_SYNC_KEY');
4
4
  export const POSTCONSTRUCT_ASYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_ASYNC_KEY');
5
+ export const RELEASE_METADATA_KEY = Symbol('RELEASE_KEY');
5
6
  export const OPTIONAL_METADATA_KEY = Symbol('OPTIONAL_KEY');
6
7
  export const REFLECT_PARAMS = 'design:paramtypes';
7
8
  export const REFLECT_RETURN = 'design:returntype';
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAChE,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AACxD,MAAM,CAAC,MAAM,+BAA+B,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAChF,MAAM,CAAC,MAAM,gCAAgC,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAClF,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAE5D,MAAM,CAAC,MAAM,cAAc,GAAG,mBAAmB,CAAC;AAClD,MAAM,CAAC,MAAM,cAAc,GAAG,mBAAmB,CAAC","sourcesContent":["export const INJECTABLE_METADATA_KEY = Symbol('INJECTABLE_KEY');\nexport const INJECT_METADATA_KEY = Symbol('INJECT_KEY');\nexport const POSTCONSTRUCT_SYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_SYNC_KEY');\nexport const POSTCONSTRUCT_ASYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_ASYNC_KEY');\nexport const OPTIONAL_METADATA_KEY = Symbol('OPTIONAL_KEY');\n\nexport const REFLECT_PARAMS = 'design:paramtypes';\nexport const REFLECT_RETURN = 'design:returntype';\n"]}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAChE,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AACxD,MAAM,CAAC,MAAM,+BAA+B,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAChF,MAAM,CAAC,MAAM,gCAAgC,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAClF,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AAC1D,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAE5D,MAAM,CAAC,MAAM,cAAc,GAAG,mBAAmB,CAAC;AAClD,MAAM,CAAC,MAAM,cAAc,GAAG,mBAAmB,CAAC","sourcesContent":["export const INJECTABLE_METADATA_KEY = Symbol('INJECTABLE_KEY');\nexport const INJECT_METADATA_KEY = Symbol('INJECT_KEY');\nexport const POSTCONSTRUCT_SYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_SYNC_KEY');\nexport const POSTCONSTRUCT_ASYNC_METADATA_KEY = Symbol('POSTCONSTRUCT_ASYNC_KEY');\nexport const RELEASE_METADATA_KEY = Symbol('RELEASE_KEY');\nexport const OPTIONAL_METADATA_KEY = Symbol('OPTIONAL_KEY');\n\nexport const REFLECT_PARAMS = 'design:paramtypes';\nexport const REFLECT_RETURN = 'design:returntype';\n"]}
@@ -30,8 +30,9 @@ export declare class Container implements Binder {
30
30
  *
31
31
  * @param id The id to be removed.
32
32
  * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).
33
+ * @param releaseIfSingleton If true, @Provider.releaseIfSingleton will be invoked before the binding is removed.
33
34
  */
34
- removeBinding<T>(id: InjectableId<T>, ascending?: boolean): void;
35
+ removeBinding<T>(id: InjectableId<T>, ascending?: boolean, releaseIfSingleton?: boolean): void;
35
36
  /**
36
37
  * @inheritDoc
37
38
  */
@@ -58,4 +59,15 @@ export declare class Container implements Binder {
58
59
  * It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container.
59
60
  */
60
61
  protected resolveState<T>(id: InjectableId<T>): State<T>;
62
+ /**
63
+ * Convenience method to assist in releasing non-garbage-collectable resources that Singletons in this Container may have allocated.
64
+ * It will walk through all registered Providers (of this Container only), and invoke their @see Provider.releaseIfSingleton method.
65
+ * This method is not part of the Binding interface, because you normally only create (and release) Containers.
66
+ * NOTE:
67
+ * This *only* releases active/pending Singleton's that have already been created by this Container.
68
+ * The most likely use of this method would be when you have created a new child Container for a limited duration transaction, and you want to easily cleanup temporary resources.
69
+ * For example, your service object may need to know when it should unsubscribe from an RxJs stream (failure to do so can result in your Singleton not being garbage collected at the end of a transaction).
70
+ * In theory, you could handle all unsubscription and cleanup yourself, but the @Release decorator and this method are meant to simply make that easier.
71
+ */
72
+ releaseSingletons(): void;
61
73
  }
@@ -73,13 +73,19 @@ export class Container {
73
73
  *
74
74
  * @param id The id to be removed.
75
75
  * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).
76
+ * @param releaseIfSingleton If true, @Provider.releaseIfSingleton will be invoked before the binding is removed.
76
77
  */
77
- removeBinding(id, ascending) {
78
+ removeBinding(id, ascending, releaseIfSingleton) {
78
79
  var _a;
80
+ if (releaseIfSingleton) {
81
+ const p = this.providers.get(id);
82
+ if (p)
83
+ p.releaseIfSingleton();
84
+ }
79
85
  this.providers.delete(id);
80
86
  if (ascending && this.parent) {
81
87
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
82
- (_a = this.parent) === null || _a === void 0 ? void 0 : _a.removeBinding(id, true);
88
+ (_a = this.parent) === null || _a === void 0 ? void 0 : _a.removeBinding(id, true, releaseIfSingleton);
83
89
  }
84
90
  }
85
91
  /**
@@ -187,5 +193,21 @@ export class Container {
187
193
  }
188
194
  return provider.provideAsState();
189
195
  }
196
+ // noinspection JSUnusedGlobalSymbols
197
+ /**
198
+ * Convenience method to assist in releasing non-garbage-collectable resources that Singletons in this Container may have allocated.
199
+ * It will walk through all registered Providers (of this Container only), and invoke their @see Provider.releaseIfSingleton method.
200
+ * This method is not part of the Binding interface, because you normally only create (and release) Containers.
201
+ * NOTE:
202
+ * This *only* releases active/pending Singleton's that have already been created by this Container.
203
+ * The most likely use of this method would be when you have created a new child Container for a limited duration transaction, and you want to easily cleanup temporary resources.
204
+ * For example, your service object may need to know when it should unsubscribe from an RxJs stream (failure to do so can result in your Singleton not being garbage collected at the end of a transaction).
205
+ * In theory, you could handle all unsubscription and cleanup yourself, but the @Release decorator and this method are meant to simply make that easier.
206
+ */
207
+ releaseSingletons() {
208
+ this.providers.forEach((value) => {
209
+ value.releaseIfSingleton();
210
+ });
211
+ }
190
212
  }
191
213
  //# sourceMappingURL=container.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"container.js","sourceRoot":"","sources":["../../src/container.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAErE,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,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IAAI,CAAC,MAAiB,0CAAE,iBAAiB,CAAA,KAAK,UAAU,EAAE;YACxF,MAAM,EAAE,GAAW,IAAI,CAAC,MAAgB,CAAC;YACzC,OAAO,EAAE,CAAC,iBAAiB,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;gBACjE,OAAO,oBAAoB,EAAE,CAAC;YAC/B,CAAC,CAAC,CAAC;SACH;QACD,OAAO,oBAAoB,EAAE,CAAC;IAC/B,CAAC;IAED;;;OAGG;IACO,YAAY,CAAI,EAAmB;QAC5C,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,EAAE;YACd,IAAI,IAAI,CAAC,MAAM,EAAE;gBAChB,IAAI,IAAI,CAAC,MAAM,YAAY,SAAS,EAAE;oBACrC,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAI,EAAE,CAAC,CAAC;iBACvC;gBACD,+HAA+H;gBAC/H,2BAA2B;gBAC3B,IAAI;oBACH,OAAO,KAAK,CAAC,SAAS,CAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAI,EAAE,CAAC,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;iBAC5E;gBACD,OAAO,GAAG,EAAE;oBACX,OAAO,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,GAAG,CAAC,CAAC;iBACrC;aACD;YACD,OAAO,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,IAAI,KAAK,CAAC,oBAAoB,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;SACjF;QACD,OAAO,QAAQ,CAAC,cAAc,EAAc,CAAC;IAC9C,CAAC;CACD","sourcesContent":["import { AsyncFactoryBasedProvider } from './async-factory-provider';\nimport { AsyncFactory, BindAs, Binder, SyncFactory } from './binder';\nimport { ClassBasedProvider } from './class-provider';\nimport { ConstantProvider } from './constant-provider';\nimport { INJECTABLE_METADATA_KEY } from './constants';\nimport { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector';\nimport { Provider } from './provider';\nimport { State } from './state';\nimport { FactoryBasedProvider } from './sync-factory-provider';\nimport { isPromise } from './utils';\n\n/**\n * Helper class to ensure we can distinguish between Error instances legitimately returned from Providers, and Errors thrown by Providers.\n *\n * @see resolveSingletons.\n */\nclass ReasonWrapper {\n\tconstructor(public reason: any) {\n\t}\n}\n\n/**\n * Binder and Injector (aka Container) to handle (a)synchronous dependency management.\n */\nexport class Container implements Binder {\n\n\t/**\n\t * Create a new Container, with an optional parent Injector which will be searched if any given InjectableId is not bound within this Container.\n\t */\n\tpublic constructor(protected parent?: Injector) {\n\t}\n\tprotected providers = new Map<InjectableId<any>, Provider>();\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic isIdKnown<T>(id: InjectableId<T>, ascending?: boolean): boolean {\n\t\tif (!!this.providers.get(id))\n\t\t\treturn true;\n\t\tif (ascending && this.parent)\n\t\t\treturn this.parent.isIdKnown(id, true);\n\t\treturn false;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic get<T>(id: InjectableId<T>): T {\n\t\tconst provider = this.providers.get(id);\n\t\tif (!provider) {\n\t\t\tif (this.parent)\n\t\t\t\treturn this.parent.get<T>(id);\n\t\t\tthrow new Error('Symbol not bound: ' + id.toString());\n\t\t}\n\t\tconst state = provider.provideAsState();\n\t\tif (state.pending)\n\t\t\tthrow new Error('Synchronous request on unresolved asynchronous dependency tree: ' + id.toString());\n\t\tif (state.rejected)\n\t\t\tthrow state.rejected;\n\t\treturn state.fulfilled as T;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic resolve<T>(id: InjectableId<T>): Promise<T> {\n\t\tconst state = this.resolveState(id);\n\t\tif (isPromise(state.promise)) {\n\t\t\treturn state.promise;\n\t\t}\n\n\t\tif (state.rejected) {\n\t\t\treturn Promise.reject(state.rejected);\n\t\t}\n\n\t\treturn Promise.resolve(state.fulfilled);\n\t}\n\n\t// noinspection JSUnusedGlobalSymbols\n\t/**\n\t * This method is not part of the Binding interface, because it is highly unusual.\n\t * But that doesn't mean we can't imagine scenarios where you might require it.\n\t *\n\t * @param id The id to be removed.\n\t * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).\n\t */\n\tpublic removeBinding<T>(id: InjectableId<T>, ascending?: boolean): void {\n\t\tthis.providers.delete(id);\n\n\t\tif (ascending && this.parent) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call\n\t\t\t(this.parent as any)?.removeBinding(id, true);\n\t\t}\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindConstant<T>(id: InjectableId<T>, value: T): void {\n\t\tthis.providers.set(id, new ConstantProvider(value));\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\tpublic bindClass<T>(id: string | symbol | AbstractConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\tpublic bindClass<T>(id: string | symbol | AbstractConstructor<T> | ClassConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>> {\n\t\tif (typeof constructor === 'undefined') {\n\t\t\tconstructor = id as new (...args: any[]) => T;\n\t\t}\n\t\tif (!Reflect.getMetadata(INJECTABLE_METADATA_KEY, constructor)) {\n\t\t\tthrow new Error('Class not decorated with @Injectable [' + constructor.toString() + ']');\n\t\t}\n\t\tconst provider = new ClassBasedProvider(this, id, constructor, (i: InjectableId<any>) => {\n\t\t\treturn this.resolveState(i);\n\t\t});\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>> {\n\t\tconst provider = new FactoryBasedProvider(this, id, factory);\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>> {\n\t\tconst provider = new AsyncFactoryBasedProvider(this, id, factory);\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<void> {\n\t\tconst makePromiseToResolve = () => {\n\t\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\t\tconst pending = new Map<InjectableId<any>, Promise<void>>();\n\t\t\t\t// Ask each provider to resolve itself *IF* it is a singleton.\n\t\t\t\tthis.providers.forEach((value: Provider, key: InjectableId<any>) => {\n\t\t\t\t\t// If the provider is a singleton *and* if resolution is being handled asynchronously, the provider will return a completion promise.\n\t\t\t\t\tconst p = value.resolveIfSingleton(asyncOnly);\n\t\t\t\t\tif (p !== null && typeof p !== 'undefined')\n\t\t\t\t\t\tpending.set(key, p);\n\t\t\t\t});\n\t\t\t\t// The contract for this method is that it behaves somewhat like Promise.allSettled (e.g. won't complete until all pending Singletons have settled).\n\t\t\t\t// Further the contract states that if any of the asynchronous Singletons rejected, that we will also return a rejected Promise, and that the rejection reason will be a Map of the InjectableId's that did not resolve, and the Error they emitted.\n\t\t\t\tconst pp = Array.from(pending.values());\n\t\t\t\tconst keys = Array.from(pending.keys());\n\t\t\t\t// Mapping the catch is an alternate version of Promise.allSettled (e.g. keeps Promise.all from short-circuiting).\n\t\t\t\tPromise.all(pp\n\t\t\t\t\t.map(p => p.catch(e => new ReasonWrapper(e))))\n\t\t\t\t\t.then((results) => {\n\t\t\t\t\t\tconst rejects = new Map<InjectableId<any>, Error>();\n\t\t\t\t\t\t// Check the results. Since we don't export ReasonWrapper, it is safe to assume that an instance of that was produced by our map => catch code above, so it's a rejected Singleton error.\n\t\t\t\t\t\tresults.forEach((result, idx) => {\n\t\t\t\t\t\t\tif (result instanceof ReasonWrapper) {\n\t\t\t\t\t\t\t\trejects.set(keys[idx], result.reason);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t\t// If we had rejections, notify our caller what they were.\n\t\t\t\t\t\tif (rejects.size > 0)\n\t\t\t\t\t\t\treject(rejects);\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\tresolve(); // All good.\n\t\t\t\t\t});\n\t\t\t});\n\t\t};\n\t\tif (parentRecursion && typeof (this.parent as Binder)?.resolveSingletons === \"function\") {\n\t\t\tconst pb: Binder = this.parent as Binder;\n\t\t\treturn pb.resolveSingletons(asyncOnly, parentRecursion).then(() => {\n\t\t\t\treturn makePromiseToResolve();\n\t\t\t});\n\t\t}\n\t\treturn makePromiseToResolve();\n\t}\n\n\t/**\n\t * As implied by the name prefix, this is a factored out method invoked only by the 'resolve' method.\n\t * It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container.\n\t */\n\tprotected resolveState<T>(id: InjectableId<T>): State<T> {\n\t\tconst provider = this.providers.get(id);\n\t\tif (!provider) {\n\t\t\tif (this.parent) {\n\t\t\t\tif (this.parent instanceof Container) {\n\t\t\t\t\treturn this.parent.resolveState<T>(id);\n\t\t\t\t}\n\t\t\t\t// This code (below) will only ever execute if the creator of this container passes in their own implementation of an Injector.\n\t\t\t\t/* istanbul ignore next */\n\t\t\t\ttry {\n\t\t\t\t\treturn State.MakeState<T>(this.parent.resolve<T>(id), undefined, undefined);\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\treturn State.MakeState<T>(null, err);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn State.MakeState<T>(null, new Error('Symbol not bound: ' + id.toString()));\n\t\t}\n\t\treturn provider.provideAsState() as State<T>;\n\t}\n}\n"]}
1
+ {"version":3,"file":"container.js","sourceRoot":"","sources":["../../src/container.ts"],"names":[],"mappings":"AAAA,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AAI,IAAI,EAAE,IAAI,KAAK,CAAC,oBAAoB,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;SACjF;QACD,OAAO,QAAQ,CAAC,cAAc,EAAc,CAAC;IAC9C,CAAC;IAED,qCAAqC;IACrC;;;;;;;;;OASG;IACI,iBAAiB;QACvB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,KAAe,EAAE,EAAE;YAC1C,KAAK,CAAC,kBAAkB,EAAE,CAAC;QAC5B,CAAC,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["import { AsyncFactoryBasedProvider } from './async-factory-provider';\nimport { AsyncFactory, BindAs, Binder, SyncFactory } from './binder';\nimport { ClassBasedProvider } from './class-provider';\nimport { ConstantProvider } from './constant-provider';\nimport { INJECTABLE_METADATA_KEY } from './constants';\nimport { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector';\nimport { Provider } from './provider';\nimport { State } from './state';\nimport { FactoryBasedProvider } from './sync-factory-provider';\nimport { isPromise } from './utils';\n\n/**\n * Helper class to ensure we can distinguish between Error instances legitimately returned from Providers, and Errors thrown by Providers.\n *\n * @see resolveSingletons.\n */\nclass ReasonWrapper {\n\tconstructor(public reason: any) {\n\t}\n}\n\n/**\n * Binder and Injector (aka Container) to handle (a)synchronous dependency management.\n */\nexport class Container implements Binder {\n\n\t/**\n\t * Create a new Container, with an optional parent Injector which will be searched if any given InjectableId is not bound within this Container.\n\t */\n\tpublic constructor(protected parent?: Injector) {\n\t}\n\tprotected providers = new Map<InjectableId<any>, Provider>();\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic isIdKnown<T>(id: InjectableId<T>, ascending?: boolean): boolean {\n\t\tif (!!this.providers.get(id))\n\t\t\treturn true;\n\t\tif (ascending && this.parent)\n\t\t\treturn this.parent.isIdKnown(id, true);\n\t\treturn false;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic get<T>(id: InjectableId<T>): T {\n\t\tconst provider = this.providers.get(id);\n\t\tif (!provider) {\n\t\t\tif (this.parent)\n\t\t\t\treturn this.parent.get<T>(id);\n\t\t\tthrow new Error('Symbol not bound: ' + id.toString());\n\t\t}\n\t\tconst state = provider.provideAsState();\n\t\tif (state.pending)\n\t\t\tthrow new Error('Synchronous request on unresolved asynchronous dependency tree: ' + id.toString());\n\t\tif (state.rejected)\n\t\t\tthrow state.rejected;\n\t\treturn state.fulfilled as T;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic resolve<T>(id: InjectableId<T>): Promise<T> {\n\t\tconst state = this.resolveState(id);\n\t\tif (isPromise(state.promise)) {\n\t\t\treturn state.promise;\n\t\t}\n\n\t\tif (state.rejected) {\n\t\t\treturn Promise.reject(state.rejected);\n\t\t}\n\n\t\treturn Promise.resolve(state.fulfilled);\n\t}\n\n\t// noinspection JSUnusedGlobalSymbols\n\t/**\n\t * This method is not part of the Binding interface, because it is highly unusual.\n\t * But that doesn't mean we can't imagine scenarios where you might require it.\n\t *\n\t * @param id The id to be removed.\n\t * @param ascending If true, this will remove all bindings of the specified id all the way up the parent container chain (if it exists).\n\t * @param releaseIfSingleton If true, @Provider.releaseIfSingleton will be invoked before the binding is removed.\n\t */\n\tpublic removeBinding<T>(id: InjectableId<T>, ascending?: boolean, releaseIfSingleton?: boolean): void {\n\t\tif (releaseIfSingleton) {\n\t\t\tconst p = this.providers.get(id);\n\t\t\tif (p)\n\t\t\t\tp.releaseIfSingleton();\n\t\t}\n\t\tthis.providers.delete(id);\n\n\t\tif (ascending && this.parent) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call\n\t\t\t(this.parent as any)?.removeBinding(id, true, releaseIfSingleton);\n\t\t}\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindConstant<T>(id: InjectableId<T>, value: T): void {\n\t\tthis.providers.set(id, new ConstantProvider(value));\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\tpublic bindClass<T>(id: string | symbol | AbstractConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\tpublic bindClass<T>(id: string | symbol | AbstractConstructor<T> | ClassConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>> {\n\t\tif (typeof constructor === 'undefined') {\n\t\t\tconstructor = id as new (...args: any[]) => T;\n\t\t}\n\t\tif (!Reflect.getMetadata(INJECTABLE_METADATA_KEY, constructor)) {\n\t\t\tthrow new Error('Class not decorated with @Injectable [' + constructor.toString() + ']');\n\t\t}\n\t\tconst provider = new ClassBasedProvider(this, id, constructor, (i: InjectableId<any>) => {\n\t\t\treturn this.resolveState(i);\n\t\t});\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>> {\n\t\tconst provider = new FactoryBasedProvider(this, id, factory);\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic bindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>> {\n\t\tconst provider = new AsyncFactoryBasedProvider(this, id, factory);\n\t\tthis.providers.set(id, provider);\n\t\treturn provider.makeBindAs();\n\t}\n\n\t/**\n\t * @inheritDoc\n\t */\n\tpublic resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<void> {\n\t\tconst makePromiseToResolve = () => {\n\t\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\t\tconst pending = new Map<InjectableId<any>, Promise<void>>();\n\t\t\t\t// Ask each provider to resolve itself *IF* it is a singleton.\n\t\t\t\tthis.providers.forEach((value: Provider, key: InjectableId<any>) => {\n\t\t\t\t\t// If the provider is a singleton *and* if resolution is being handled asynchronously, the provider will return a completion promise.\n\t\t\t\t\tconst p = value.resolveIfSingleton(asyncOnly);\n\t\t\t\t\tif (p !== null && typeof p !== 'undefined')\n\t\t\t\t\t\tpending.set(key, p);\n\t\t\t\t});\n\t\t\t\t// The contract for this method is that it behaves somewhat like Promise.allSettled (e.g. won't complete until all pending Singletons have settled).\n\t\t\t\t// Further the contract states that if any of the asynchronous Singletons rejected, that we will also return a rejected Promise, and that the rejection reason will be a Map of the InjectableId's that did not resolve, and the Error they emitted.\n\t\t\t\tconst pp = Array.from(pending.values());\n\t\t\t\tconst keys = Array.from(pending.keys());\n\t\t\t\t// Mapping the catch is an alternate version of Promise.allSettled (e.g. keeps Promise.all from short-circuiting).\n\t\t\t\tPromise.all(pp\n\t\t\t\t\t.map(p => p.catch(e => new ReasonWrapper(e))))\n\t\t\t\t\t.then((results) => {\n\t\t\t\t\t\tconst rejects = new Map<InjectableId<any>, Error>();\n\t\t\t\t\t\t// Check the results. Since we don't export ReasonWrapper, it is safe to assume that an instance of that was produced by our map => catch code above, so it's a rejected Singleton error.\n\t\t\t\t\t\tresults.forEach((result, idx) => {\n\t\t\t\t\t\t\tif (result instanceof ReasonWrapper) {\n\t\t\t\t\t\t\t\trejects.set(keys[idx], result.reason);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t\t// If we had rejections, notify our caller what they were.\n\t\t\t\t\t\tif (rejects.size > 0)\n\t\t\t\t\t\t\treject(rejects);\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\tresolve(); // All good.\n\t\t\t\t\t});\n\t\t\t});\n\t\t};\n\t\tif (parentRecursion && typeof (this.parent as Binder)?.resolveSingletons === \"function\") {\n\t\t\tconst pb: Binder = this.parent as Binder;\n\t\t\treturn pb.resolveSingletons(asyncOnly, parentRecursion).then(() => {\n\t\t\t\treturn makePromiseToResolve();\n\t\t\t});\n\t\t}\n\t\treturn makePromiseToResolve();\n\t}\n\n\t/**\n\t * As implied by the name prefix, this is a factored out method invoked only by the 'resolve' method.\n\t * It makes searching our parent (if it exists) easier (and quicker) IF our parent is a fellow instance of Container.\n\t */\n\tprotected resolveState<T>(id: InjectableId<T>): State<T> {\n\t\tconst provider = this.providers.get(id);\n\t\tif (!provider) {\n\t\t\tif (this.parent) {\n\t\t\t\tif (this.parent instanceof Container) {\n\t\t\t\t\treturn this.parent.resolveState<T>(id);\n\t\t\t\t}\n\t\t\t\t// This code (below) will only ever execute if the creator of this container passes in their own implementation of an Injector.\n\t\t\t\t/* istanbul ignore next */\n\t\t\t\ttry {\n\t\t\t\t\treturn State.MakeState<T>(this.parent.resolve<T>(id), undefined, undefined);\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\treturn State.MakeState<T>(null, err);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn State.MakeState<T>(null, new Error('Symbol not bound: ' + id.toString()));\n\t\t}\n\t\treturn provider.provideAsState() as State<T>;\n\t}\n\n\t// noinspection JSUnusedGlobalSymbols\n\t/**\n\t * Convenience method to assist in releasing non-garbage-collectable resources that Singletons in this Container may have allocated.\n\t * It will walk through all registered Providers (of this Container only), and invoke their @see Provider.releaseIfSingleton method.\n\t * This method is not part of the Binding interface, because you normally only create (and release) Containers.\n\t * NOTE:\n\t * This *only* releases active/pending Singleton's that have already been created by this Container.\n\t * The most likely use of this method would be when you have created a new child Container for a limited duration transaction, and you want to easily cleanup temporary resources.\n\t * For example, your service object may need to know when it should unsubscribe from an RxJs stream (failure to do so can result in your Singleton not being garbage collected at the end of a transaction).\n\t * In theory, you could handle all unsubscription and cleanup yourself, but the @Release decorator and this method are meant to simply make that easier.\n\t */\n\tpublic releaseSingletons(): void {\n\t\tthis.providers.forEach((value: Provider) => {\n\t\t\tvalue.releaseIfSingleton();\n\t\t});\n\t}\n}\n"]}
@@ -41,3 +41,16 @@ export declare function _getOptionalDefaultAt(target: Function, parameterIndex:
41
41
  * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
42
42
  */
43
43
  export declare function PostConstruct(): MethodDecorator;
44
+ /**
45
+ * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.
46
+ * If invoked by the container, the container will drop any references it has to the object when the method returns.
47
+ * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).
48
+ * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.
49
+ * You should *not* use this decorator as a general "object finalization" method. It has very limited scope and purpose.
50
+ * The decorated method must complete normally (no throwing), as "release" is not an abort-able process.
51
+ * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
52
+ * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.
53
+ * Also @see Container.releaseSingletons for the intended usage of this decorator.
54
+ * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).
55
+ */
56
+ export declare function Release(): MethodDecorator;
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * These decorators all apply the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor
4
4
  */
5
- import { INJECTABLE_METADATA_KEY, INJECT_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_RETURN } from './constants';
5
+ import { INJECTABLE_METADATA_KEY, INJECT_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_RETURN, RELEASE_METADATA_KEY } from './constants';
6
6
  // Help user locate misapplied decorators.
7
7
  function targetHint(target) {
8
8
  let hint;
@@ -146,4 +146,35 @@ export function PostConstruct() {
146
146
  }
147
147
  };
148
148
  }
149
+ // noinspection JSUnusedGlobalSymbols
150
+ /**
151
+ * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.
152
+ * If invoked by the container, the container will drop any references it has to the object when the method returns.
153
+ * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).
154
+ * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.
155
+ * You should *not* use this decorator as a general "object finalization" method. It has very limited scope and purpose.
156
+ * The decorated method must complete normally (no throwing), as "release" is not an abort-able process.
157
+ * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
158
+ * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.
159
+ * Also @see Container.releaseSingletons for the intended usage of this decorator.
160
+ * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).
161
+ */
162
+ export function Release() {
163
+ /**
164
+ * @param prototypeOrConstructor The prototype of the class (we don't allow @Release on anything other than a class instance method.
165
+ * @param methodName The name of the method.
166
+ * @param descriptor The Property Descriptor for the method.
167
+ * @returns Undefined (nothing), as this decorator does not modify the method in any way.
168
+ */
169
+ // noinspection JSUnusedLocalSymbols
170
+ return function (target, methodName, descriptor) {
171
+ if (typeof target !== 'object' || typeof target.constructor !== 'function') {
172
+ throw new Error('@Release not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');
173
+ }
174
+ if (Reflect.hasOwnMetadata(RELEASE_METADATA_KEY, target.constructor)) {
175
+ throw new Error('@Release applied multiple times [' + targetHint(target.constructor) + ']');
176
+ }
177
+ Reflect.defineMetadata(RELEASE_METADATA_KEY, methodName, target.constructor);
178
+ };
179
+ }
149
180
  //# sourceMappingURL=decorators.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/decorators.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD;;GAEG;AACH,OAAO,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,+BAA+B,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAGrL,0CA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AE,MAAM,CAAC,WAAW,CAAC,CAAC;SACzF;aAAM;YACN,OAAO,CAAC,cAAc,CAAC,+BAA+B,EAAE,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;SACxF;IACF,CAAC,CAAC;AACH,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/**\n * These decorators all apply the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor\n */\nimport { INJECTABLE_METADATA_KEY, INJECT_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_RETURN } from './constants';\nimport { InjectableId } from './injector';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function) {\n\tlet hint: string | undefined;\n\tif (target) {\n\t\thint = target.name;\n\t\tif ((!hint) && target.constructor) {\n\t\t\thint = target.constructor.name;\n\t\t}\n\t}\n\treturn hint;\n}\n\n// Validate that 'target' is a class constructor function.\nfunction isClassConstructor(target: any) {\n\t// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\treturn target.prototype.constructor === target;\n\t}\n\treturn false;\n}\n\n// Ensure consistency in our meta-data name getting/setting.\nfunction makeParamIdxKey(idx: number): string {\n\treturn `index-${idx}`;\n}\n\n// Validate that the specified target is a parameter of a class constructor\nfunction validateConstructorParam(decorator: string, target: Function, idx: number): string {\n\tif (!isClassConstructor(target)) {\n\t\tthrow new Error('@' + decorator + ' is not valid here [' + targetHint(target) + ']');\n\t}\n\treturn makeParamIdxKey(idx);\n}\n\n// Validate the decorator was only applied once.\nfunction validateSingleConstructorParam(decorator: string, target: Function, idx: number): string {\n\tconst propKey = validateConstructorParam(decorator, target, idx);\n\tif (Reflect.hasOwnMetadata(decorator, target, propKey)) {\n\t\tthrow new Error('@' + decorator + ' applied multiple times [' + target.constructor.name + ']');\n\t}\n\treturn propKey;\n}\n\n/**\n * Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container.\n * This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.\n */\nexport function Injectable(): ClassDecorator {\n\t/**\n\t * @param target The constructor function of the class that is being decorated\n\t * @returns Undefined (nothing), as this decorator does not modify the constructor in any way.\n\t */\n\treturn function (target: Function): void {\n\t\tif (Reflect.hasOwnMetadata(INJECTABLE_METADATA_KEY, target)) {\n\t\t\tthrow new Error('@Injectable applied multiple times [' + targetHint(target) + ']');\n\t\t}\n\t\tReflect.defineMetadata(INJECTABLE_METADATA_KEY, true, target);\n\t};\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of the type of Object that will be injected into the parameter.\n * In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).\n *\n * @param id The identifier of the bound type that should be injected.\n */\nexport function Inject(id: InjectableId<any>): ParameterDecorator {\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Inject on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst hint = targetHint(target);\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + hint + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target, parameterIndex);\n\t\tReflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Inject metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Inject on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Inject\n */\nexport function _getInjectedIdAt(target: Function, parameterIndex: number): InjectableId<any> {\n\treturn Reflect.getMetadata(INJECT_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as InjectableId<any>;\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.\n * WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.\n */\nexport function Optional(alt?: any): ParameterDecorator { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Optional on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target, parameterIndex);\n\t\tReflect.defineMetadata(OPTIONAL_METADATA_KEY, { value: alt }, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Optional metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Optional on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Optional\n * @returns an object containing the value provided in the decorator, or undefined if no annotation was present.\n */\nexport function _getOptionalDefaultAt(target: Function, parameterIndex: number): { value: any } {\n\treturn Reflect.getMetadata(OPTIONAL_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as { value: any }; // See the @Optional decorator before making any changes here.\n}\n\n/**\n * Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.\n * The method will be assumed to be synchronous unless the method signature explicitly declares it's return type to be \": Promise<something>\"\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n */\nexport function PostConstruct(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @PostConstruct on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\t// noinspection JSUnusedLocalSymbols\n\treturn function (target: Object, methodName: string | symbol, descriptor: PropertyDescriptor) { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@PostConstruct not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, target.constructor) || Reflect.hasOwnMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@PostConstruct applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tconst rt = Reflect.getMetadata(REFLECT_RETURN, target, methodName);\n\t\tif (typeof rt === 'function') {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, methodName, target.constructor);\n\t\t} else {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, methodName, target.constructor);\n\t\t}\n\t};\n}\n"]}
1
+ {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/decorators.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD;;GAEG;AACH,OAAO,EAAC,uBAAuB,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,+BAA+B,EAAE,cAAc,EAAE,oBAAoB,EAAC,MAAM,aAAa,CAAC;AAGzM,0CAA0C;AAC1C,SAAS,UAAU,CAAC,MAAgB;IACnC,IAAI,IAAwB,CAAC;IAC7B,IAAI,MAAM,EAAE;QACX,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACnB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE;YAClC,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC;SAC/B;KACD;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,0DAA0D;A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3F,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,CAAC,WAAW,KAAK,UAAU,EAAE;YAC3E,MAAM,IAAI,KAAK,CAAC,2CAA2C,GAAG,MAAM,CAAC,QAAQ,EAAE,GAAG,GAAG,GAAG,UAAU,CAAC,QAAQ,EAAE,GAAG,GAAG,CAAC,CAAC;SACrH;QACD,IAAI,OAAO,CAAC,cAAc,CAAC,oBAAoB,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE;YACrE,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,GAAG,CAAC,CAAC;SAC5F;QACD,OAAO,CAAC,cAAc,CAAC,oBAAoB,EAAE,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC,CAAC;AACH,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/**\n * These decorators all apply the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor\n */\nimport {INJECTABLE_METADATA_KEY, INJECT_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_RETURN, RELEASE_METADATA_KEY} from './constants';\nimport { InjectableId } from './injector';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function) {\n\tlet hint: string | undefined;\n\tif (target) {\n\t\thint = target.name;\n\t\tif ((!hint) && target.constructor) {\n\t\t\thint = target.constructor.name;\n\t\t}\n\t}\n\treturn hint;\n}\n\n// Validate that 'target' is a class constructor function.\nfunction isClassConstructor(target: any) {\n\t// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\treturn target.prototype.constructor === target;\n\t}\n\treturn false;\n}\n\n// Ensure consistency in our meta-data name getting/setting.\nfunction makeParamIdxKey(idx: number): string {\n\treturn `index-${idx}`;\n}\n\n// Validate that the specified target is a parameter of a class constructor\nfunction validateConstructorParam(decorator: string, target: Function, idx: number): string {\n\tif (!isClassConstructor(target)) {\n\t\tthrow new Error('@' + decorator + ' is not valid here [' + targetHint(target) + ']');\n\t}\n\treturn makeParamIdxKey(idx);\n}\n\n// Validate the decorator was only applied once.\nfunction validateSingleConstructorParam(decorator: string, target: Function, idx: number): string {\n\tconst propKey = validateConstructorParam(decorator, target, idx);\n\tif (Reflect.hasOwnMetadata(decorator, target, propKey)) {\n\t\tthrow new Error('@' + decorator + ' applied multiple times [' + target.constructor.name + ']');\n\t}\n\treturn propKey;\n}\n\n/**\n * Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container.\n * This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.\n */\nexport function Injectable(): ClassDecorator {\n\t/**\n\t * @param target The constructor function of the class that is being decorated\n\t * @returns Undefined (nothing), as this decorator does not modify the constructor in any way.\n\t */\n\treturn function (target: Function): void {\n\t\tif (Reflect.hasOwnMetadata(INJECTABLE_METADATA_KEY, target)) {\n\t\t\tthrow new Error('@Injectable applied multiple times [' + targetHint(target) + ']');\n\t\t}\n\t\tReflect.defineMetadata(INJECTABLE_METADATA_KEY, true, target);\n\t};\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of the type of Object that will be injected into the parameter.\n * In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).\n *\n * @param id The identifier of the bound type that should be injected.\n */\nexport function Inject(id: InjectableId<any>): ParameterDecorator {\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Inject on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst hint = targetHint(target);\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + hint + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target, parameterIndex);\n\t\tReflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Inject metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Inject on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Inject\n */\nexport function _getInjectedIdAt(target: Function, parameterIndex: number): InjectableId<any> {\n\treturn Reflect.getMetadata(INJECT_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as InjectableId<any>;\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.\n * WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.\n */\nexport function Optional(alt?: any): ParameterDecorator { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Optional on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target, parameterIndex);\n\t\tReflect.defineMetadata(OPTIONAL_METADATA_KEY, { value: alt }, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Optional metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Optional on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Optional\n * @returns an object containing the value provided in the decorator, or undefined if no annotation was present.\n */\nexport function _getOptionalDefaultAt(target: Function, parameterIndex: number): { value: any } {\n\treturn Reflect.getMetadata(OPTIONAL_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as { value: any }; // See the @Optional decorator before making any changes here.\n}\n\n/**\n * Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.\n * The method will be assumed to be synchronous unless the method signature explicitly declares it's return type to be \": Promise<something>\"\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n */\nexport function PostConstruct(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @PostConstruct on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\t// noinspection JSUnusedLocalSymbols\n\treturn function (target: Object, methodName: string | symbol, descriptor: PropertyDescriptor) { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@PostConstruct not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, target.constructor) || Reflect.hasOwnMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@PostConstruct applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tconst rt = Reflect.getMetadata(REFLECT_RETURN, target, methodName);\n\t\tif (typeof rt === 'function') {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, methodName, target.constructor);\n\t\t} else {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, methodName, target.constructor);\n\t\t}\n\t};\n}\n\n// noinspection JSUnusedGlobalSymbols\n/**\n * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.\n * If invoked by the container, the container will drop any references it has to the object when the method returns.\n * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).\n * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.\n * You should *not* use this decorator as a general \"object finalization\" method. It has very limited scope and purpose.\n * The decorated method must complete normally (no throwing), as \"release\" is not an abort-able process.\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.\n * Also @see Container.releaseSingletons for the intended usage of this decorator.\n * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).\n */\nexport function Release(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @Release on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\t// noinspection JSUnusedLocalSymbols\n\treturn function (target: Object, methodName: string | symbol, descriptor: PropertyDescriptor) { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@Release not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(RELEASE_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@Release applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tReflect.defineMetadata(RELEASE_METADATA_KEY, methodName, target.constructor);\n\t};\n}\n"]}
@@ -26,4 +26,12 @@ export declare abstract class Provider<T = any> {
26
26
  * @returns A completion Promise if initialization requires asynchronicity, otherwise the return value is undefined.
27
27
  */
28
28
  resolveIfSingleton(asyncOnly: boolean): Promise<T>;
29
+ /**
30
+ * If (and only if) this Provider has been configured as a Singleton, and if it has been (or is being resolved), find and invoke the @Release decorated method (if there is one).
31
+ * NOTE that if the singleton is actively being resolved when this method is called, this method waits for the resolution to complete and then invokes the @Release decorated method; But in any case this is a synchronous method and returns immediately to it's caller.
32
+ * Also note that invoking this method does not release or invalidate the Provider;
33
+ * Rather, it resets a Singleton Provider to a fresh (unresolved/unqueried) state (aka sets this.singleton to null).
34
+ * It is assumed that the Singleton itself will no longer be used after this method returns.
35
+ */
36
+ releaseIfSingleton(): void;
29
37
  }
@@ -1,3 +1,4 @@
1
+ import { InvokeReleaseMethod } from './utils';
1
2
  /**
2
3
  * Internally all InjectableIds are mapped to an abstract Provider<T>.
3
4
  * A Provider may choose to return a singleton or a new value each time it is queried.
@@ -22,5 +23,30 @@ export class Provider {
22
23
  }
23
24
  return undefined;
24
25
  }
26
+ /**
27
+ * If (and only if) this Provider has been configured as a Singleton, and if it has been (or is being resolved), find and invoke the @Release decorated method (if there is one).
28
+ * NOTE that if the singleton is actively being resolved when this method is called, this method waits for the resolution to complete and then invokes the @Release decorated method; But in any case this is a synchronous method and returns immediately to it's caller.
29
+ * Also note that invoking this method does not release or invalidate the Provider;
30
+ * Rather, it resets a Singleton Provider to a fresh (unresolved/unqueried) state (aka sets this.singleton to null).
31
+ * It is assumed that the Singleton itself will no longer be used after this method returns.
32
+ */
33
+ releaseIfSingleton() {
34
+ if (this.singleton) {
35
+ const s = this.provideAsState();
36
+ if (s.pending) {
37
+ s.promise.then((v) => {
38
+ this.singleton = null;
39
+ InvokeReleaseMethod(v);
40
+ }).catch(() => {
41
+ this.singleton = null;
42
+ });
43
+ }
44
+ else {
45
+ this.singleton = null;
46
+ if ((!s.rejected) && s.fulfilled)
47
+ InvokeReleaseMethod(s.fulfilled);
48
+ }
49
+ }
50
+ }
25
51
  }
26
52
  //# sourceMappingURL=provider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/provider.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,OAAgB,QAAQ;IAC7B;IACA,CAAC;IAiBD;;;;;;OAMG;IACH,kBAAkB,CAAC,SAAkB;QACpC,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI,EAAE;YAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;YAChC,IAAI,CAAC,CAAC,OAAO;gBACZ,OAAO,CAAC,CAAC,OAAO,CAAC;iBACb,IAAI,CAAC,CAAC,QAAQ;gBAClB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;SACnC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;CACD","sourcesContent":["import { State } from './state';\n\n/**\n * Internally all InjectableIds are mapped to an abstract Provider<T>.\n * A Provider may choose to return a singleton or a new value each time it is queried.\n */\nexport abstract class Provider<T = any> {\n\tprotected constructor() {\n\t}\n\n\t/**\n\t * If the provider is configured as a singleton, this property will be the state of that singleton.\n\t * This value will be defined for resolved/resolving Singletons, null for Singletons that have not yet been queried, and will remain undefined for non-Singleton Providers.\n\t * Default value is undefined (e.g. not a Singleton).\n\t */\n\tprotected singleton?: State<T>;\n\n\t/**\n\t * This is the workhorse method of the Provider, and is invoked directly or indirectly by both Injector.get and Injector.resolve.\n\t * This method returns the current State<T> if it is already known (which it might be for Singleton scenarios).\n\t * Otherwise it resolves the State<T>.\n\t * IF the Provider<T> is a Singleton, it's State<T> is updated before returning.\n\t */\n\tabstract provideAsState(): State<T>;\n\n\t/**\n\t * Base method to initialize the state of this Provider *if* (and only if) it has been configured as a Singleton.\n\t * If this Provider has not been configured as a singleton, this method is essentially a noop that returns undefined.\n\t *\n\t * @param asyncOnly This default implementation ignores this parameter.\n\t * @returns A completion Promise if initialization requires asynchronicity, otherwise the return value is undefined.\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (this.singleton === null) {\n\t\t\tconst s = this.provideAsState();\n\t\t\tif (s.pending)\n\t\t\t\treturn s.promise;\n\t\t\telse if (s.rejected)\n\t\t\t\treturn Promise.reject(s.rejected);\n\t\t}\n\t\treturn undefined;\n\t}\n}\n"]}
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAG9C;;;GAGG;AACH,MAAM,OAAgB,QAAQ;IAC7B;IACA,CAAC;IAiBD;;;;;;OAMG;IACH,kBAAkB,CAAC,SAAkB;QACpC,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI,EAAE;YAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;YAChC,IAAI,CAAC,CAAC,OAAO;gBACZ,OAAO,CAAC,CAAC,OAAO,CAAC;iBACb,IAAI,CAAC,CAAC,QAAQ;gBAClB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;SACnC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;IAED;;;;;;OAMG;IACH,kBAAkB;QACjB,IAAI,IAAI,CAAC,SAAS,EAAE;YACnB,MAAM,CAAC,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;YAChC,IAAI,CAAC,CAAC,OAAO,EAAE;gBACd,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;oBACpB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;oBACtB,mBAAmB,CAAC,CAAC,CAAC,CAAC;gBACxB,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;oBACb,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;gBACvB,CAAC,CAAC,CAAC;aACH;iBACI;gBACJ,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;gBACtB,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,SAAS;oBAC/B,mBAAmB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;aAClC;SACD;IACF,CAAC;CACD","sourcesContent":["import { State } from './state';\nimport { InvokeReleaseMethod } from './utils';\n\n\n/**\n * Internally all InjectableIds are mapped to an abstract Provider<T>.\n * A Provider may choose to return a singleton or a new value each time it is queried.\n */\nexport abstract class Provider<T = any> {\n\tprotected constructor() {\n\t}\n\n\t/**\n\t * If the provider is configured as a singleton, this property will be the state of that singleton.\n\t * This value will be defined for resolved/resolving Singletons, null for Singletons that have not yet been queried, and will remain undefined for non-Singleton Providers.\n\t * Default value is undefined (e.g. not a Singleton).\n\t */\n\tprotected singleton?: State<T>;\n\n\t/**\n\t * This is the workhorse method of the Provider, and is invoked directly or indirectly by both Injector.get and Injector.resolve.\n\t * This method returns the current State<T> if it is already known (which it might be for Singleton scenarios).\n\t * Otherwise it resolves the State<T>.\n\t * IF the Provider<T> is a Singleton, it's State<T> is updated before returning.\n\t */\n\tabstract provideAsState(): State<T>;\n\n\t/**\n\t * Base method to initialize the state of this Provider *if* (and only if) it has been configured as a Singleton.\n\t * If this Provider has not been configured as a singleton, this method is essentially a noop that returns undefined.\n\t *\n\t * @param asyncOnly This default implementation ignores this parameter.\n\t * @returns A completion Promise if initialization requires asynchronicity, otherwise the return value is undefined.\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> { // eslint-disable-line @typescript-eslint/no-unused-vars\n\t\tif (this.singleton === null) {\n\t\t\tconst s = this.provideAsState();\n\t\t\tif (s.pending)\n\t\t\t\treturn s.promise;\n\t\t\telse if (s.rejected)\n\t\t\t\treturn Promise.reject(s.rejected);\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * If (and only if) this Provider has been configured as a Singleton, and if it has been (or is being resolved), find and invoke the @Release decorated method (if there is one).\n\t * NOTE that if the singleton is actively being resolved when this method is called, this method waits for the resolution to complete and then invokes the @Release decorated method; But in any case this is a synchronous method and returns immediately to it's caller.\n\t * Also note that invoking this method does not release or invalidate the Provider;\n\t * Rather, it resets a Singleton Provider to a fresh (unresolved/unqueried) state (aka sets this.singleton to null).\n\t * It is assumed that the Singleton itself will no longer be used after this method returns.\n\t */\n\treleaseIfSingleton(): void {\n\t\tif (this.singleton) {\n\t\t\tconst s = this.provideAsState();\n\t\t\tif (s.pending) {\n\t\t\t\ts.promise.then((v) => {\n\t\t\t\t\tthis.singleton = null;\n\t\t\t\t\tInvokeReleaseMethod(v);\n\t\t\t\t}).catch(() => {\n\t\t\t\t\tthis.singleton = null;\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.singleton = null;\n\t\t\t\tif ((!s.rejected) && s.fulfilled)\n\t\t\t\t\tInvokeReleaseMethod(s.fulfilled);\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
@@ -1,2 +1,14 @@
1
+ /**
2
+ * Returns true if the specified object looks like a JavaScript Error object.
3
+ */
1
4
  export declare function isErrorObj(err: any): err is Error;
5
+ /**
6
+ * Returns true if the specified value is "thenable" (aka a Promise).
7
+ */
2
8
  export declare function isPromise<T>(value: any): value is Promise<T>;
9
+ /**
10
+ * Simple helper function to find the @Release decorated method of an object (if any), and invoke it.
11
+ * This is primarily an internal method as you probably know the exact method, and should invoke it yourself.
12
+ * async-injection uses this helper to allow Singletons to clean up any non-garbage-collectable resources they may have allocated.
13
+ */
14
+ export declare function InvokeReleaseMethod<T = unknown>(obj: T): boolean;
package/lib/esm/utils.js CHANGED
@@ -1,4 +1,8 @@
1
1
  /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */
2
+ import { RELEASE_METADATA_KEY } from './constants';
3
+ /**
4
+ * Returns true if the specified object looks like a JavaScript Error object.
5
+ */
2
6
  export function isErrorObj(err) {
3
7
  if (!err)
4
8
  return false;
@@ -6,6 +10,9 @@ export function isErrorObj(err) {
6
10
  return true;
7
11
  return err && typeof err.message === 'string' && typeof err.stack === 'string';
8
12
  }
13
+ /**
14
+ * Returns true if the specified value is "thenable" (aka a Promise).
15
+ */
9
16
  export function isPromise(value) {
10
17
  if (!value)
11
18
  return false;
@@ -13,4 +20,23 @@ export function isPromise(value) {
13
20
  return true;
14
21
  return value && typeof value.then === 'function';
15
22
  }
23
+ /**
24
+ * Simple helper function to find the @Release decorated method of an object (if any), and invoke it.
25
+ * This is primarily an internal method as you probably know the exact method, and should invoke it yourself.
26
+ * async-injection uses this helper to allow Singletons to clean up any non-garbage-collectable resources they may have allocated.
27
+ */
28
+ export function InvokeReleaseMethod(obj) {
29
+ var _a, _b;
30
+ const releaseMethod = Reflect.getMetadata(RELEASE_METADATA_KEY, obj.constructor);
31
+ /* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
32
+ if (releaseMethod && obj.constructor.prototype[releaseMethod] && typeof obj.constructor.prototype[releaseMethod] === 'function') {
33
+ const releaseFn = (_b = (_a = obj[releaseMethod]).bind) === null || _b === void 0 ? void 0 : _b.call(_a, obj);
34
+ if (releaseFn) {
35
+ releaseFn();
36
+ return true;
37
+ }
38
+ }
39
+ /* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
40
+ return false;
41
+ }
16
42
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAElH,MAAM,UAAU,UAAU,CAAC,GAAQ;IAClC,IAAI,CAAC,GAAG;QACD,OAAO,KAAK,CAAC;IAEjB,IAAI,GAAG,YAAY,KAAK;QACpB,OAAO,IAAI,CAAC;IAEhB,OAAO,GAAG,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,CAAC;AACnF,CAAC;AAED,MAAM,UAAU,SAAS,CAAI,KAAU;IACnC,IAAI,CAAC,KAAK;QACN,OAAO,KAAK,CAAC;IAEjB,IAAI,KAAK,YAAY,OAAO;QACxB,OAAO,IAAI,CAAC;IAEhB,OAAO,KAAK,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC;AACrD,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */\n\nexport function isErrorObj(err: any): err is Error {\n\tif (!err)\n return false;\n\n if (err instanceof Error)\n return true;\n\n return err && typeof err.message === 'string' && typeof err.stack === 'string';\n}\n\nexport function isPromise<T>(value: any): value is Promise<T> {\n if (!value)\n return false;\n\n if (value instanceof Promise)\n return true;\n\n return value && typeof value.then === 'function';\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,OAAO,EAAC,oBAAoB,EAAC,MAAM,aAAa,CAAC;AAEjD;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,GAAQ;IAClC,IAAI,CAAC,GAAG;QACD,OAAO,KAAK,CAAC;IAEjB,IAAI,GAAG,YAAY,KAAK;QACpB,OAAO,IAAI,CAAC;IAEhB,OAAO,GAAG,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,CAAC;AACnF,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CAAI,KAAU;IACnC,IAAI,CAAC,KAAK;QACN,OAAO,KAAK,CAAC;IAEjB,IAAI,KAAK,YAAY,OAAO;QACxB,OAAO,IAAI,CAAC;IAEhB,OAAO,KAAK,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC;AACrD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAY,GAAM;;IACpD,MAAM,aAAa,GAAW,OAAO,CAAC,WAAW,CAAC,oBAAoB,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;IACzF,kGAAkG;IAClG,IAAI,aAAa,IAAI,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,aAAa,CAAC,IAAI,OAAO,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,aAAa,CAAC,KAAK,UAAU,EAAE;QAChI,MAAM,SAAS,GAAG,MAAA,MAAA,GAAG,CAAC,aAAa,CAAC,EAAC,IAAI,mDAAG,GAAG,CAAC,CAAC;QACjD,IAAI,SAAS,EAAE;YACd,SAAS,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC;SACZ;KACD;IACD,iGAAiG;IACjG,OAAO,KAAK,CAAC;AACd,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/explicit-module-boundary-types */\nimport {RELEASE_METADATA_KEY} from './constants';\n\n/**\n * Returns true if the specified object looks like a JavaScript Error object.\n */\nexport function isErrorObj(err: any): err is Error {\n\tif (!err)\n return false;\n\n if (err instanceof Error)\n return true;\n\n return err && typeof err.message === 'string' && typeof err.stack === 'string';\n}\n\n/**\n * Returns true if the specified value is \"thenable\" (aka a Promise).\n */\nexport function isPromise<T>(value: any): value is Promise<T> {\n if (!value)\n return false;\n\n if (value instanceof Promise)\n return true;\n\n return value && typeof value.then === 'function';\n}\n\n/**\n * Simple helper function to find the @Release decorated method of an object (if any), and invoke it.\n * This is primarily an internal method as you probably know the exact method, and should invoke it yourself.\n * async-injection uses this helper to allow Singletons to clean up any non-garbage-collectable resources they may have allocated.\n */\nexport function InvokeReleaseMethod<T=unknown>(obj: T) : boolean {\n\tconst releaseMethod: string = Reflect.getMetadata(RELEASE_METADATA_KEY, obj.constructor);\n\t/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\tif (releaseMethod && obj.constructor.prototype[releaseMethod] && typeof obj.constructor.prototype[releaseMethod] === 'function') {\n\t\tconst releaseFn = obj[releaseMethod].bind?.(obj);\n\t\tif (releaseFn) {\n\t\t\treleaseFn();\n\t\t\treturn true;\n\t\t}\n\t}\n\t/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\treturn false;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-injection",
3
- "version": "1.2.7",
3
+ "version": "1.3.0",
4
4
  "description": "A robust lightweight dependency injection library for TypeScript.",
5
5
  "author": "Frank Stock",
6
6
  "license": "MIT",
@@ -51,22 +51,22 @@
51
51
  },
52
52
  "devDependencies": {
53
53
  "@istanbuljs/nyc-config-typescript": "~1.0.1",
54
- "@types/jasmine": "~3.8.2",
55
- "@typescript-eslint/eslint-plugin": "~4.29.0",
56
- "@typescript-eslint/parser": "~4.29.0",
54
+ "@types/jasmine": "~3.10.2",
55
+ "@typescript-eslint/eslint-plugin": "~4.33.0",
56
+ "@typescript-eslint/parser": "~4.33.0",
57
57
  "eslint": "~7.32.0",
58
- "eslint-plugin-import": "~2.23.4",
59
- "eslint-plugin-jsdoc": "~36.0.6",
58
+ "eslint-plugin-import": "~2.25.3",
59
+ "eslint-plugin-jsdoc": "~36.1.1",
60
60
  "eslint-plugin-prefer-arrow": "~1.2.3",
61
- "jasmine": "~3.8.0",
61
+ "jasmine": "~3.10.0",
62
62
  "jasmine-console-reporter": "~3.1.0",
63
63
  "nyc": "~15.1.0",
64
64
  "reflect-metadata": "~0.1.13",
65
65
  "rimraf": "~3.0.2",
66
- "source-map-support": "~0.5.19",
67
- "ts-node": "~10.1.0",
68
- "tsconfig-paths": "~3.10.1",
69
- "tslib": "~2.3.0",
66
+ "source-map-support": "~0.5.21",
67
+ "ts-node": "~10.4.0",
68
+ "tsconfig-paths": "~3.12.0",
69
+ "tslib": "~2.3.1",
70
70
  "typescript": "~4.3.5"
71
71
  },
72
72
  "nyc": {