ditox 2.4.1 → 3.1.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/README.md +35 -13
- package/dist/browser/index.d.ts +9 -8
- package/dist/browser/index.js +1 -1
- package/dist/cjs/container.d.ts +6 -8
- package/dist/cjs/container.js +47 -26
- package/dist/cjs/container.js.map +1 -1
- package/dist/cjs/modules.d.ts +4 -3
- package/dist/cjs/modules.js +54 -20
- package/dist/cjs/modules.js.map +1 -1
- package/dist/esm/container.d.ts +6 -8
- package/dist/esm/container.js +46 -24
- package/dist/esm/container.js.map +1 -1
- package/dist/esm/modules.d.ts +4 -3
- package/dist/esm/modules.js +53 -19
- package/dist/esm/modules.js.map +1 -1
- package/dist/umd/index.d.ts +9 -8
- package/dist/umd/index.js +1 -1
- package/package.json +2 -3
- package/src/container.test.ts +132 -52
- package/src/container.ts +62 -29
- package/src/modules.test.ts +69 -6
- package/src/modules.ts +69 -27
package/README.md
CHANGED
|
@@ -31,14 +31,6 @@ The package can be used as [UMD](https://github.com/umdjs/umd) module. Use
|
|
|
31
31
|
</script>
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
Using in Deno environment:
|
|
35
|
-
|
|
36
|
-
```ts
|
|
37
|
-
import {createContainer} from 'https://deno.land/x/ditox/mod.ts';
|
|
38
|
-
|
|
39
|
-
const container = createContainer();
|
|
40
|
-
```
|
|
41
|
-
|
|
42
34
|
## General description
|
|
43
35
|
|
|
44
36
|
DI pattern in general allows to declare and construct a code graph of an
|
|
@@ -79,7 +71,7 @@ class UserService {
|
|
|
79
71
|
}
|
|
80
72
|
|
|
81
73
|
// Define tokens for injections.
|
|
82
|
-
const STORAGE_TOKEN = token<UserService>
|
|
74
|
+
const STORAGE_TOKEN = token < UserService > 'Token description for debugging';
|
|
83
75
|
const LOGGER_TOKEN = token();
|
|
84
76
|
const USER_SERVICE_TOKEN = token();
|
|
85
77
|
|
|
@@ -158,21 +150,51 @@ container.resolve(V1_TOKEN); // 10
|
|
|
158
150
|
container.resolve(V2_TOKEN); // 21
|
|
159
151
|
```
|
|
160
152
|
|
|
153
|
+
### Multiple parent containers
|
|
154
|
+
|
|
155
|
+
A container can have multiple parent containers. Pass an array of parents when
|
|
156
|
+
creating a container. During resolution, parents are queried from left to right,
|
|
157
|
+
and the first parent that provides the token wins.
|
|
158
|
+
|
|
159
|
+
```js
|
|
160
|
+
import {createContainer, token} from 'ditox';
|
|
161
|
+
|
|
162
|
+
const VALUE = token();
|
|
163
|
+
|
|
164
|
+
// Create two parents
|
|
165
|
+
const p1 = createContainer();
|
|
166
|
+
const p2 = createContainer();
|
|
167
|
+
|
|
168
|
+
// Case 1: Only the second parent provides the token
|
|
169
|
+
p2.bindValue(VALUE, 'from-p2');
|
|
170
|
+
const child = createContainer([p1, p2]);
|
|
171
|
+
child.get(VALUE); // 'from-p2' (found in the second parent)
|
|
172
|
+
|
|
173
|
+
// Case 2: Both parents provide the token — order matters (left-to-right)
|
|
174
|
+
p1.bindValue(VALUE, 'from-p1');
|
|
175
|
+
const child2 = createContainer([p1, p2]);
|
|
176
|
+
child2.resolve(VALUE); // 'from-p1' (first parent wins)
|
|
177
|
+
|
|
178
|
+
// The child can still override parents
|
|
179
|
+
child2.bindValue(VALUE, 'from-child');
|
|
180
|
+
child2.resolve(VALUE); // 'from-child'
|
|
181
|
+
```
|
|
182
|
+
|
|
161
183
|
## Factory Lifetimes
|
|
162
184
|
|
|
163
185
|
Ditox.js supports managing the lifetime of values which are produced by
|
|
164
186
|
factories. There are the following types:
|
|
165
187
|
|
|
166
188
|
- `singleton` - **This is the default**. The value is created and cached by the
|
|
167
|
-
container which
|
|
168
|
-
- `scoped` - The value is created and cached by the container which
|
|
169
|
-
|
|
189
|
+
most distant parent container which owns the factory function.
|
|
190
|
+
- `scoped` - The value is created and cached by the nearest container which owns
|
|
191
|
+
the factory function.
|
|
170
192
|
- `transient` - The value is created every time it is resolved.
|
|
171
193
|
|
|
172
194
|
### `singleton`
|
|
173
195
|
|
|
174
196
|
**This is the default scope**. "Singleton" allows to cache a produced value by a
|
|
175
|
-
parent container which registered the factory:
|
|
197
|
+
most distant parent container which registered the factory function:
|
|
176
198
|
|
|
177
199
|
```js
|
|
178
200
|
import {creatContainer, token} from 'ditox';
|
package/dist/browser/index.d.ts
CHANGED
|
@@ -69,8 +69,8 @@ type FactoryScope = 'scoped' | 'singleton' | 'transient';
|
|
|
69
69
|
* Options for factory binding.
|
|
70
70
|
*
|
|
71
71
|
* `scope` types:
|
|
72
|
-
* - `singleton` - **This is the default**. The value is created and cached by the container which
|
|
73
|
-
* - `scoped` - The value is created and cached by the container which
|
|
72
|
+
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
73
|
+
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
74
74
|
* - `transient` - The value is created every time it is resolved.
|
|
75
75
|
*
|
|
76
76
|
* `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
|
|
@@ -102,7 +102,7 @@ type Container = {
|
|
|
102
102
|
*/
|
|
103
103
|
get<T>(token: Token<T>): T | undefined;
|
|
104
104
|
/**
|
|
105
|
-
* Returns a resolved value by the token
|
|
105
|
+
* Returns a resolved value by the token or throws `ResolverError` in case the token is not found.
|
|
106
106
|
*/
|
|
107
107
|
resolve<T>(token: Token<T>): T;
|
|
108
108
|
/**
|
|
@@ -119,9 +119,9 @@ type Container = {
|
|
|
119
119
|
*
|
|
120
120
|
* Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token.
|
|
121
121
|
*
|
|
122
|
-
* @param
|
|
122
|
+
* @param parentArg - Optional parent container or an array of containers.
|
|
123
123
|
*/
|
|
124
|
-
declare function createContainer(
|
|
124
|
+
declare function createContainer(parentArg?: Container | ReadonlyArray<Container>): Container;
|
|
125
125
|
|
|
126
126
|
type ValuesProps = {
|
|
127
127
|
[key: string]: unknown;
|
|
@@ -316,16 +316,17 @@ type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
|
316
316
|
* Options for module binding.
|
|
317
317
|
*
|
|
318
318
|
* `scope` types:
|
|
319
|
-
* - `singleton` - **This is the default**. The
|
|
320
|
-
* - `scoped` - The
|
|
319
|
+
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
320
|
+
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
321
321
|
*/
|
|
322
322
|
type BindModuleOptions = {
|
|
323
323
|
scope?: 'scoped' | 'singleton';
|
|
324
324
|
};
|
|
325
|
-
type
|
|
325
|
+
type ModuleDeclarationWithOptions = {
|
|
326
326
|
module: ModuleDeclaration<AnyObject>;
|
|
327
327
|
options: BindModuleOptions;
|
|
328
328
|
};
|
|
329
|
+
type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
|
|
329
330
|
/**
|
|
330
331
|
* Binds the dependency module to the container
|
|
331
332
|
* @param container - Dependency container.
|
package/dist/browser/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function o(o){var n="string"==typeof o?{description:o}:o;return{symbol:(null==n?void 0:n.key)?Symbol.for(n.key):Symbol(null==n?void 0:n.description)}}function n(o,n){return{symbol:o.symbol,isOptional:!0,optionalValue:n}}var t=function(o,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(o,n){o.__proto__=n}||function(o,n){for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(o[t]=n[t])},t(o,n)};var r=function(){return r=Object.assign||function(o){for(var n,t=1,r=arguments.length;t<r;t++)for(var e in n=arguments[t])Object.prototype.hasOwnProperty.call(n,e)&&(o[e]=n[e]);return o},r.apply(this,arguments)};function e(o,n,t){if(t||2===arguments.length)for(var r,e=0,i=n.length;e<i;e++)!r&&e in n||(r||(r=Array.prototype.slice.call(n,0,e)),r[e]=n[e]);return o.concat(r||Array.prototype.slice.call(n))}var i=function(o){function n(n){var t=this.constructor,r=o.call(this,n)||this;return Object.setPrototypeOf(r,t.prototype),r.name="ResolverError",r}return function(o,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=o}t(o,n),o.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}(n,o),n}(Error),u=o("ditox.Container"),l=o("ditox.ParentContainer"),
|
|
1
|
+
function o(o){var n="string"==typeof o?{description:o}:o;return{symbol:(null==n?void 0:n.key)?Symbol.for(n.key):Symbol(null==n?void 0:n.description)}}function n(o,n){return{symbol:o.symbol,isOptional:!0,optionalValue:n}}var t=function(o,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(o,n){o.__proto__=n}||function(o,n){for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(o[t]=n[t])},t(o,n)};var r=function(){return r=Object.assign||function(o){for(var n,t=1,r=arguments.length;t<r;t++)for(var e in n=arguments[t])Object.prototype.hasOwnProperty.call(n,e)&&(o[e]=n[e]);return o},r.apply(this,arguments)};function e(o,n,t){if(t||2===arguments.length)for(var r,e=0,i=n.length;e<i;e++)!r&&e in n||(r||(r=Array.prototype.slice.call(n,0,e)),r[e]=n[e]);return o.concat(r||Array.prototype.slice.call(n))}var i=function(o){function n(n){var t=this.constructor,r=o.call(this,n)||this;return Object.setPrototypeOf(r,t.prototype),r.name="ResolverError",r}return function(o,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=o}t(o,n),o.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}(n,o),n}(Error),u=o("ditox.Container"),l=o("ditox.ParentContainer"),a=o("ditox.Resolver"),c=Symbol(),s="singleton",f=o("ditox.FactoriesMap");function p(o){return o.symbol===u.symbol||o.symbol===l.symbol||o.symbol===a.symbol}function v(o){var n=o?Array.isArray(o)?e([],o,!0):[o]:void 0,t=new Map,r=new Map,v={bindValue:function(o,n){p(o)||t.set(o.symbol,n)},bindFactory:function(o,n,t){p(o)||r.set(o.symbol,{factory:n,options:t})},remove:function(o){var n;if(!p(o)){var e=null===(n=r.get(o.symbol))||void 0===n?void 0:n.options;e&&m(o.symbol,e),t.delete(o.symbol),r.delete(o.symbol)}},removeAll:function(){r.forEach((function(o,n){o.options&&m(n,o.options)})),t.clear(),r.clear(),b()},hasToken:function(o){return t.has(o.symbol)||r.has(o.symbol)||y(n,o)},get:function(o){var n=d(o,v);return n!==c?n:o.isOptional?o.optionalValue:void 0},resolve:function(o){var n,t=d(o,v);if(t!==c)return t;if(o.isOptional)return o.optionalValue;throw new i('Token "'.concat(null!==(n=o.symbol.description)&&void 0!==n?n:"",'" is not provided'))}};function d(o,e){var i=t.get(o.symbol),u=void 0!==i||t.has(o.symbol);if(u&&e===v)return i;var l,f,p=r.get(o.symbol);if(p)switch(l=p.options,null!==(f=null==l?void 0:l.scope)&&void 0!==f?f:s){case"singleton":if(u)return i;if(y(n,o))break;var d=p.factory(v);return v.bindValue(o,d),d;case"scoped":if(u)return i;var m=p.factory(v);return v.bindValue(o,m),m;case"transient":return p.factory(e)}return u?i:n?function(o,n,t){for(var r=0,e=o;r<e.length;r++){var i=e[r].get(a);if(i){var u=i(n,t);if(u!==c)return u}}return c}(n,o,e):c}function m(o,n){var r=function(o){return void 0===o.scope||"scoped"===o.scope||"singleton"===o.scope?o.onRemoved:void 0}(n);if(r){var e=t.get(o);(void 0!==e||t.has(o))&&r(e)}}function b(){t.set(u.symbol,v),t.set(a.symbol,d),t.set(f.symbol,r),n&&t.set(l.symbol,n)}return b(),v}function y(o,n){if(!o)return!1;for(var t=0;t<o.length;t++)if(o[t].hasToken(n))return!0;return!1}function d(o){return null!=o&&"object"==typeof o&&"symbol"in o&&"symbol"==typeof o.symbol}function m(o,n,t){var r,i=e(e([],null!==(r=o.get(n))&&void 0!==r?r:[],!0),[t],!1);o.bindValue(n,i)}function b(o,n){if(d(n))return o.get(n);var t={};return Object.keys(n).forEach((function(r){return t[r]=o.get(n[r])})),t}function h(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return b(o,n)}))}function g(o,n){if(d(n))return o.resolve(n);var t={};return Object.keys(n).forEach((function(r){return t[r]=o.resolve(n[r])})),t}function O(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return g(o,n)}))}function k(o){for(var n=this,t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return function(r){var i=O.apply(void 0,e([r],t,!1));return o.apply(n,i)}}function w(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return k.apply(void 0,e([function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return new(o.bind.apply(o,e([void 0],n,!1)))}],n,!1))}function j(o,n,t){for(var r,e,i,u={module:n,options:null!=t?t:{}},l=new Set([u]),a=[u],c=0;c<a.length;){null===(r=(p="module"in(f=a[c])?f.module:f).imports)||void 0===r||r.forEach((function(o){l.has(o)||(l.add(o),a.push(o))})),c++}for(var s=0;s<a.length;s++){null===(e=(p="module"in(f=a[s])?f.module:f).beforeBinding)||void 0===e||e.call(p,o)}for(s=0;s<a.length;s++){E(o,f=a[s])}for(s=a.length-1;s>=0;s--){var f,p;null===(i=(p="module"in(f=a[s])?f.module:f).afterBinding)||void 0===i||i.call(p,o)}}function E(o,n){var t,r;"module"in n?(t=n.module,r=n.options):t=n;var e=null==r?void 0:r.scope,i=new Set,u=t.exports;u&&Object.keys(u).forEach((function(n){var r=u[n];r&&(i.add(r),o.bindFactory(r,k((function(o){return o[n]}),t.token),{scope:e}))}));o.bindFactory(t.token,t.factory,{scope:e,onRemoved:function(n){n.destroy&&n.destroy(),i.forEach((function(n){return o.remove(n)})),i.clear()}})}function _(o,n){n.forEach((function(n){"module"in n?j(o,n.module,n.options):j(o,n)}))}function x(n){var t;return r(r({},n),{token:null!==(t=n.token)&&void 0!==t?t:o()})}function V(o){return x({factory:function(){return{}},imports:o})}export{i as ResolverError,j as bindModule,_ as bindModules,m as bindMultiValue,v as createContainer,x as declareModule,V as declareModuleBindings,k as injectable,w as injectableClass,d as isToken,n as optional,g as resolveValue,O as resolveValues,o as token,b as tryResolveValue,h as tryResolveValues};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/container.d.ts
CHANGED
|
@@ -13,8 +13,8 @@ export type FactoryScope = 'scoped' | 'singleton' | 'transient';
|
|
|
13
13
|
* Options for factory binding.
|
|
14
14
|
*
|
|
15
15
|
* `scope` types:
|
|
16
|
-
* - `singleton` - **This is the default**. The value is created and cached by the container which
|
|
17
|
-
* - `scoped` - The value is created and cached by the container which
|
|
16
|
+
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
17
|
+
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
18
18
|
* - `transient` - The value is created every time it is resolved.
|
|
19
19
|
*
|
|
20
20
|
* `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
|
|
@@ -46,7 +46,7 @@ export type Container = {
|
|
|
46
46
|
*/
|
|
47
47
|
get<T>(token: Token<T>): T | undefined;
|
|
48
48
|
/**
|
|
49
|
-
* Returns a resolved value by the token
|
|
49
|
+
* Returns a resolved value by the token or throws `ResolverError` in case the token is not found.
|
|
50
50
|
*/
|
|
51
51
|
resolve<T>(token: Token<T>): T;
|
|
52
52
|
/**
|
|
@@ -61,14 +61,12 @@ export type Container = {
|
|
|
61
61
|
/** @internal */
|
|
62
62
|
export declare const CONTAINER: Token<Container>;
|
|
63
63
|
/** @internal */
|
|
64
|
-
export declare const
|
|
64
|
+
export declare const PARENT_CONTAINERS: Token<ReadonlyArray<Container>>;
|
|
65
65
|
/** @internal */
|
|
66
66
|
export declare const RESOLVER: Token<Resolver>;
|
|
67
67
|
/** @internal */
|
|
68
68
|
declare const NOT_FOUND: unique symbol;
|
|
69
69
|
/** @internal */
|
|
70
|
-
export declare const FAKE_FACTORY: () => never;
|
|
71
|
-
/** @internal */
|
|
72
70
|
type FactoryContext<T> = {
|
|
73
71
|
factory: (container: Container) => T;
|
|
74
72
|
options?: FactoryOptions<T>;
|
|
@@ -84,7 +82,7 @@ type Resolver = <T>(token: Token<T>, origin: Container) => T | typeof NOT_FOUND;
|
|
|
84
82
|
*
|
|
85
83
|
* Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token.
|
|
86
84
|
*
|
|
87
|
-
* @param
|
|
85
|
+
* @param parentArg - Optional parent container or an array of containers.
|
|
88
86
|
*/
|
|
89
|
-
export declare function createContainer(
|
|
87
|
+
export declare function createContainer(parentArg?: Container | ReadonlyArray<Container>): Container;
|
|
90
88
|
export {};
|
package/dist/cjs/container.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createContainer = exports.FACTORIES_MAP = exports.
|
|
3
|
+
exports.createContainer = exports.FACTORIES_MAP = exports.RESOLVER = exports.PARENT_CONTAINERS = exports.CONTAINER = exports.ResolverError = void 0;
|
|
4
4
|
const tokens_1 = require("./tokens");
|
|
5
5
|
/**
|
|
6
6
|
* ResolverError is thrown by the resolver when a token is not found in a container.
|
|
@@ -16,17 +16,12 @@ exports.ResolverError = ResolverError;
|
|
|
16
16
|
/** @internal */
|
|
17
17
|
exports.CONTAINER = (0, tokens_1.token)('ditox.Container');
|
|
18
18
|
/** @internal */
|
|
19
|
-
exports.
|
|
19
|
+
exports.PARENT_CONTAINERS = (0, tokens_1.token)('ditox.ParentContainer');
|
|
20
20
|
/** @internal */
|
|
21
21
|
exports.RESOLVER = (0, tokens_1.token)('ditox.Resolver');
|
|
22
22
|
/** @internal */
|
|
23
23
|
const NOT_FOUND = Symbol();
|
|
24
24
|
/** @internal */
|
|
25
|
-
const FAKE_FACTORY = () => {
|
|
26
|
-
throw new Error('FAKE_FACTORY');
|
|
27
|
-
};
|
|
28
|
-
exports.FAKE_FACTORY = FAKE_FACTORY;
|
|
29
|
-
/** @internal */
|
|
30
25
|
const DEFAULT_SCOPE = 'singleton';
|
|
31
26
|
/** @internal */
|
|
32
27
|
exports.FACTORIES_MAP = (0, tokens_1.token)('ditox.FactoriesMap');
|
|
@@ -46,7 +41,7 @@ function getOnRemoved(options) {
|
|
|
46
41
|
/** @internal */
|
|
47
42
|
function isInternalToken(token) {
|
|
48
43
|
return (token.symbol === exports.CONTAINER.symbol ||
|
|
49
|
-
token.symbol === exports.
|
|
44
|
+
token.symbol === exports.PARENT_CONTAINERS.symbol ||
|
|
50
45
|
token.symbol === exports.RESOLVER.symbol);
|
|
51
46
|
}
|
|
52
47
|
/**
|
|
@@ -54,9 +49,14 @@ function isInternalToken(token) {
|
|
|
54
49
|
*
|
|
55
50
|
* Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token.
|
|
56
51
|
*
|
|
57
|
-
* @param
|
|
52
|
+
* @param parentArg - Optional parent container or an array of containers.
|
|
58
53
|
*/
|
|
59
|
-
function createContainer(
|
|
54
|
+
function createContainer(parentArg) {
|
|
55
|
+
const parents = parentArg
|
|
56
|
+
? Array.isArray(parentArg)
|
|
57
|
+
? [...parentArg]
|
|
58
|
+
: [parentArg]
|
|
59
|
+
: undefined;
|
|
60
60
|
const values = new Map();
|
|
61
61
|
const factories = new Map();
|
|
62
62
|
const container = {
|
|
@@ -95,10 +95,9 @@ function createContainer(parentContainer) {
|
|
|
95
95
|
bindInternalTokens();
|
|
96
96
|
},
|
|
97
97
|
hasToken(token) {
|
|
98
|
-
var _a;
|
|
99
98
|
return (values.has(token.symbol) ||
|
|
100
99
|
factories.has(token.symbol) ||
|
|
101
|
-
(
|
|
100
|
+
parentContainersHaveToken(parents, token));
|
|
102
101
|
},
|
|
103
102
|
get(token) {
|
|
104
103
|
const value = resolver(token, container);
|
|
@@ -129,14 +128,14 @@ function createContainer(parentContainer) {
|
|
|
129
128
|
return value;
|
|
130
129
|
}
|
|
131
130
|
const factoryContext = factories.get(token.symbol);
|
|
132
|
-
if (factoryContext
|
|
131
|
+
if (factoryContext) {
|
|
133
132
|
const scope = getScope(factoryContext.options);
|
|
134
133
|
switch (scope) {
|
|
135
134
|
case 'singleton': {
|
|
136
135
|
if (hasValue) {
|
|
137
136
|
return value;
|
|
138
137
|
}
|
|
139
|
-
else if (
|
|
138
|
+
else if (parentContainersHaveToken(parents, token)) {
|
|
140
139
|
break;
|
|
141
140
|
}
|
|
142
141
|
else {
|
|
@@ -147,14 +146,15 @@ function createContainer(parentContainer) {
|
|
|
147
146
|
}
|
|
148
147
|
}
|
|
149
148
|
case 'scoped': {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
//
|
|
155
|
-
|
|
149
|
+
if (hasValue) {
|
|
150
|
+
return value;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
// Create a value within the factory's container and cache it.
|
|
154
|
+
const value = factoryContext.factory(container);
|
|
155
|
+
container.bindValue(token, value);
|
|
156
|
+
return value;
|
|
156
157
|
}
|
|
157
|
-
return value;
|
|
158
158
|
}
|
|
159
159
|
case 'transient': {
|
|
160
160
|
// Create a value within the origin container and don't cache it.
|
|
@@ -165,9 +165,8 @@ function createContainer(parentContainer) {
|
|
|
165
165
|
if (hasValue) {
|
|
166
166
|
return value;
|
|
167
167
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return parentResolver(token, origin);
|
|
168
|
+
if (parents) {
|
|
169
|
+
return parentContainersResolveToken(parents, token, origin);
|
|
171
170
|
}
|
|
172
171
|
return NOT_FOUND;
|
|
173
172
|
}
|
|
@@ -184,12 +183,34 @@ function createContainer(parentContainer) {
|
|
|
184
183
|
values.set(exports.CONTAINER.symbol, container);
|
|
185
184
|
values.set(exports.RESOLVER.symbol, resolver);
|
|
186
185
|
values.set(exports.FACTORIES_MAP.symbol, factories);
|
|
187
|
-
if (
|
|
188
|
-
values.set(exports.
|
|
186
|
+
if (parents) {
|
|
187
|
+
values.set(exports.PARENT_CONTAINERS.symbol, parents);
|
|
189
188
|
}
|
|
190
189
|
}
|
|
191
190
|
bindInternalTokens();
|
|
192
191
|
return container;
|
|
193
192
|
}
|
|
194
193
|
exports.createContainer = createContainer;
|
|
194
|
+
function parentContainersHaveToken(parents, token) {
|
|
195
|
+
if (!parents)
|
|
196
|
+
return false;
|
|
197
|
+
for (let i = 0; i < parents.length; i++) {
|
|
198
|
+
if (parents[i].hasToken(token)) {
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
function parentContainersResolveToken(parents, token, origin) {
|
|
205
|
+
for (const parentContainer of parents) {
|
|
206
|
+
const parentResolver = parentContainer.get(exports.RESOLVER);
|
|
207
|
+
if (parentResolver) {
|
|
208
|
+
const resolved = parentResolver(token, origin);
|
|
209
|
+
if (resolved !== NOT_FOUND) {
|
|
210
|
+
return resolved;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return NOT_FOUND;
|
|
215
|
+
}
|
|
195
216
|
//# sourceMappingURL=container.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"container.js","sourceRoot":"","sources":["../../src/container.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AAEtC;;GAEG;AACH,MAAa,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AAND,sCAMC;AAsED,gBAAgB;AACH,QAAA,SAAS,GAAqB,IAAA,cAAK,EAAC,iBAAiB,CAAC,CAAC;AACpE,gBAAgB;AACH,QAAA,
|
|
1
|
+
{"version":3,"file":"container.js","sourceRoot":"","sources":["../../src/container.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AAEtC;;GAEG;AACH,MAAa,aAAc,SAAQ,KAAK;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,EACf,MAAiB;IAEjB,KAAK,MAAM,eAAe,IAAI,OAAO,EAAE;QACrC,MAAM,cAAc,GAAG,eAAe,CAAC,GAAG,CAAC,gBAAQ,CAAC,CAAC;QACrD,IAAI,cAAc,EAAE;YAClB,MAAM,QAAQ,GAAG,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAC/C,IAAI,QAAQ,KAAK,SAAS,EAAE;gBAC1B,OAAO,QAAQ,CAAC;aACjB;SACF;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/dist/cjs/modules.d.ts
CHANGED
|
@@ -59,16 +59,17 @@ export type AnyModuleDeclaration = ModuleDeclaration<Module<AnyObject>>;
|
|
|
59
59
|
* Options for module binding.
|
|
60
60
|
*
|
|
61
61
|
* `scope` types:
|
|
62
|
-
* - `singleton` - **This is the default**. The
|
|
63
|
-
* - `scoped` - The
|
|
62
|
+
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
63
|
+
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
64
64
|
*/
|
|
65
65
|
export type BindModuleOptions = {
|
|
66
66
|
scope?: 'scoped' | 'singleton';
|
|
67
67
|
};
|
|
68
|
-
|
|
68
|
+
type ModuleDeclarationWithOptions = {
|
|
69
69
|
module: ModuleDeclaration<AnyObject>;
|
|
70
70
|
options: BindModuleOptions;
|
|
71
71
|
};
|
|
72
|
+
export type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
|
|
72
73
|
/**
|
|
73
74
|
* Binds the dependency module to the container
|
|
74
75
|
* @param container - Dependency container.
|
package/dist/cjs/modules.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.declareModuleBindings = exports.declareModule = exports.bindModules = exports.bindModule = void 0;
|
|
4
|
-
const utils_1 = require("./utils");
|
|
5
4
|
const tokens_1 = require("./tokens");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
6
|
/**
|
|
7
7
|
* Binds the dependency module to the container
|
|
8
8
|
* @param container - Dependency container.
|
|
@@ -15,41 +15,75 @@ const tokens_1 = require("./tokens");
|
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
17
|
function bindModule(container, moduleDeclaration, options) {
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
var _a, _b, _c;
|
|
19
|
+
const rootEntry = {
|
|
20
|
+
module: moduleDeclaration,
|
|
21
|
+
options: options !== null && options !== void 0 ? options : {},
|
|
22
|
+
};
|
|
23
|
+
const bfsVisits = new Set([rootEntry]);
|
|
24
|
+
const bfsQueue = [rootEntry];
|
|
25
|
+
let bfsIndex = 0;
|
|
26
|
+
while (bfsIndex < bfsQueue.length) {
|
|
27
|
+
const entry = bfsQueue[bfsIndex];
|
|
28
|
+
const m = 'module' in entry ? entry.module : entry;
|
|
29
|
+
(_a = m.imports) === null || _a === void 0 ? void 0 : _a.forEach((depEntry) => {
|
|
30
|
+
if (!bfsVisits.has(depEntry)) {
|
|
31
|
+
bfsVisits.add(depEntry);
|
|
32
|
+
bfsQueue.push(depEntry);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
bfsIndex++;
|
|
36
|
+
}
|
|
37
|
+
for (let i = 0; i < bfsQueue.length; i++) {
|
|
38
|
+
const entry = bfsQueue[i];
|
|
39
|
+
const m = 'module' in entry ? entry.module : entry;
|
|
40
|
+
(_b = m.beforeBinding) === null || _b === void 0 ? void 0 : _b.call(m, container);
|
|
41
|
+
}
|
|
42
|
+
for (let i = 0; i < bfsQueue.length; i++) {
|
|
43
|
+
const entry = bfsQueue[i];
|
|
44
|
+
bindModuleEntry(container, entry);
|
|
23
45
|
}
|
|
24
|
-
|
|
25
|
-
|
|
46
|
+
for (let i = bfsQueue.length - 1; i >= 0; i--) {
|
|
47
|
+
const entry = bfsQueue[i];
|
|
48
|
+
const m = 'module' in entry ? entry.module : entry;
|
|
49
|
+
(_c = m.afterBinding) === null || _c === void 0 ? void 0 : _c.call(m, container);
|
|
26
50
|
}
|
|
51
|
+
}
|
|
52
|
+
exports.bindModule = bindModule;
|
|
53
|
+
function bindModuleEntry(container, entry) {
|
|
54
|
+
let module;
|
|
55
|
+
let options;
|
|
56
|
+
if ('module' in entry) {
|
|
57
|
+
module = entry.module;
|
|
58
|
+
options = entry.options;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
module = entry;
|
|
62
|
+
}
|
|
63
|
+
const scope = options === null || options === void 0 ? void 0 : options.scope;
|
|
27
64
|
const exportedValueTokens = new Set();
|
|
28
|
-
|
|
29
|
-
|
|
65
|
+
const moduleExports = module.exports;
|
|
66
|
+
if (moduleExports) {
|
|
67
|
+
const keys = Object.keys(moduleExports);
|
|
30
68
|
keys.forEach((valueKey) => {
|
|
31
|
-
const valueToken =
|
|
69
|
+
const valueToken = moduleExports[valueKey];
|
|
32
70
|
if (valueToken) {
|
|
33
71
|
exportedValueTokens.add(valueToken);
|
|
34
|
-
container.bindFactory(valueToken, (0, utils_1.injectable)((module) => module[valueKey], token), { scope });
|
|
72
|
+
container.bindFactory(valueToken, (0, utils_1.injectable)((module) => module[valueKey], module.token), { scope });
|
|
35
73
|
}
|
|
36
74
|
});
|
|
37
75
|
}
|
|
38
|
-
container.bindFactory(token, factory, {
|
|
76
|
+
container.bindFactory(module.token, module.factory, {
|
|
39
77
|
scope,
|
|
40
|
-
onRemoved: (
|
|
41
|
-
if (
|
|
42
|
-
|
|
78
|
+
onRemoved: (moduleInstance) => {
|
|
79
|
+
if (moduleInstance.destroy) {
|
|
80
|
+
moduleInstance.destroy();
|
|
43
81
|
}
|
|
44
82
|
exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
|
|
45
83
|
exportedValueTokens.clear();
|
|
46
84
|
},
|
|
47
85
|
});
|
|
48
|
-
if (afterBinding) {
|
|
49
|
-
afterBinding(container);
|
|
50
|
-
}
|
|
51
86
|
}
|
|
52
|
-
exports.bindModule = bindModule;
|
|
53
87
|
/**
|
|
54
88
|
* Binds dependency modules to the container
|
|
55
89
|
*
|
package/dist/cjs/modules.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"modules.js","sourceRoot":"","sources":["../../src/modules.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"modules.js","sourceRoot":"","sources":["../../src/modules.ts"],"names":[],"mappings":";;;AACA,qCAAsC;AACtC,mCAAmC;AAyFnC;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CACxB,SAAoB,EACpB,iBAAuC,EACvC,OAA2B;;IAE3B,MAAM,SAAS,GAAuB;QACpC,MAAM,EAAE,iBAAiB;QACzB,OAAO,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE;KACvB,CAAC;IAEF,MAAM,SAAS,GAAG,IAAI,GAAG,CAAqB,CAAC,SAAS,CAAC,CAAC,CAAC;IAC3D,MAAM,QAAQ,GAAyB,CAAC,SAAS,CAAC,CAAC;IAEnD,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,OAAO,QAAQ,GAAG,QAAQ,CAAC,MAAM,EAAE;QACjC,MAAM,KAAK,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAEjC,MAAM,CAAC,GAAG,QAAQ,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;QAEnD,MAAA,CAAC,CAAC,OAAO,0CAAE,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YAC9B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;gBAC5B,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBACxB,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;aACzB;QACH,CAAC,CAAC,CAAC;QAEH,QAAQ,EAAE,CA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,CAAC,CAAC;SACpD;aAAM;YACL,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;SAC9B;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAXD,kCAWC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,aAAa,CAC3B,WAC8C;;IAE9C,OAAO,EAAC,GAAG,WAAW,EAAE,KAAK,EAAE,MAAA,WAAW,CAAC,KAAK,mCAAI,IAAA,cAAK,GAAE,EAAC,CAAC;AAC/D,CAAC;AALD,sCAKC;AAED;;;;GAIG;AACH,SAAgB,qBAAqB,CACnC,OAA0C;IAE1C,OAAO,aAAa,CAAC;QACnB,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;QACnB,OAAO,EAAE,OAAO;KACjB,CAAC,CAAC;AACL,CAAC;AAPD,sDAOC"}
|
package/dist/esm/container.d.ts
CHANGED
|
@@ -13,8 +13,8 @@ export type FactoryScope = 'scoped' | 'singleton' | 'transient';
|
|
|
13
13
|
* Options for factory binding.
|
|
14
14
|
*
|
|
15
15
|
* `scope` types:
|
|
16
|
-
* - `singleton` - **This is the default**. The value is created and cached by the container which
|
|
17
|
-
* - `scoped` - The value is created and cached by the container which
|
|
16
|
+
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
17
|
+
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
18
18
|
* - `transient` - The value is created every time it is resolved.
|
|
19
19
|
*
|
|
20
20
|
* `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
|
|
@@ -46,7 +46,7 @@ export type Container = {
|
|
|
46
46
|
*/
|
|
47
47
|
get<T>(token: Token<T>): T | undefined;
|
|
48
48
|
/**
|
|
49
|
-
* Returns a resolved value by the token
|
|
49
|
+
* Returns a resolved value by the token or throws `ResolverError` in case the token is not found.
|
|
50
50
|
*/
|
|
51
51
|
resolve<T>(token: Token<T>): T;
|
|
52
52
|
/**
|
|
@@ -61,14 +61,12 @@ export type Container = {
|
|
|
61
61
|
/** @internal */
|
|
62
62
|
export declare const CONTAINER: Token<Container>;
|
|
63
63
|
/** @internal */
|
|
64
|
-
export declare const
|
|
64
|
+
export declare const PARENT_CONTAINERS: Token<ReadonlyArray<Container>>;
|
|
65
65
|
/** @internal */
|
|
66
66
|
export declare const RESOLVER: Token<Resolver>;
|
|
67
67
|
/** @internal */
|
|
68
68
|
declare const NOT_FOUND: unique symbol;
|
|
69
69
|
/** @internal */
|
|
70
|
-
export declare const FAKE_FACTORY: () => never;
|
|
71
|
-
/** @internal */
|
|
72
70
|
type FactoryContext<T> = {
|
|
73
71
|
factory: (container: Container) => T;
|
|
74
72
|
options?: FactoryOptions<T>;
|
|
@@ -84,7 +82,7 @@ type Resolver = <T>(token: Token<T>, origin: Container) => T | typeof NOT_FOUND;
|
|
|
84
82
|
*
|
|
85
83
|
* Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token.
|
|
86
84
|
*
|
|
87
|
-
* @param
|
|
85
|
+
* @param parentArg - Optional parent container or an array of containers.
|
|
88
86
|
*/
|
|
89
|
-
export declare function createContainer(
|
|
87
|
+
export declare function createContainer(parentArg?: Container | ReadonlyArray<Container>): Container;
|
|
90
88
|
export {};
|