ditox 3.2.0 → 3.3.1
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 +76 -27
- package/dist/index.cjs +531 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{umd/index.d.ts → index.d.cts} +191 -196
- package/dist/{browser/index.d.ts → index.d.ts} +191 -196
- package/dist/index.js +514 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +537 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +22 -20
- package/src/container.test.ts +77 -33
- package/src/container.ts +26 -11
- package/src/index.ts +22 -24
- package/src/modules.test.ts +190 -37
- package/src/modules.ts +22 -5
- package/src/removal-compatibility.test.ts +259 -0
- package/src/removal.test.ts +190 -0
- package/src/tokens.test.ts +9 -8
- package/src/tokens.ts +2 -2
- package/src/utils.test.ts +18 -17
- package/src/utils.ts +21 -21
- package/dist/browser/index.js +0 -2
- package/dist/browser/index.js.map +0 -1
- package/dist/cjs/container.d.ts +0 -93
- package/dist/cjs/container.js +0 -216
- package/dist/cjs/container.js.map +0 -1
- package/dist/cjs/index.d.ts +0 -7
- package/dist/cjs/index.js +0 -24
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/modules.d.ts +0 -121
- package/dist/cjs/modules.js +0 -143
- package/dist/cjs/modules.js.map +0 -1
- package/dist/cjs/tokens.d.ts +0 -56
- package/dist/cjs/tokens.js +0 -20
- package/dist/cjs/tokens.js.map +0 -1
- package/dist/cjs/utils.d.ts +0 -137
- package/dist/cjs/utils.js +0 -156
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/container.d.ts +0 -93
- package/dist/esm/container.js +0 -211
- package/dist/esm/container.js.map +0 -1
- package/dist/esm/index.d.ts +0 -7
- package/dist/esm/index.js +0 -5
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/modules.d.ts +0 -121
- package/dist/esm/modules.js +0 -136
- package/dist/esm/modules.js.map +0 -1
- package/dist/esm/tokens.d.ts +0 -56
- package/dist/esm/tokens.js +0 -15
- package/dist/esm/tokens.js.map +0 -1
- package/dist/esm/utils.d.ts +0 -137
- package/dist/esm/utils.js +0 -145
- package/dist/esm/utils.js.map +0 -1
- package/dist/umd/index.js +0 -2
- package/dist/umd/index.js.map +0 -1
package/src/utils.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {Container} from './container';
|
|
2
|
-
import {Token} from './tokens';
|
|
1
|
+
import type { Container } from './container';
|
|
2
|
+
import { Token } from './tokens';
|
|
3
3
|
|
|
4
|
-
type ValuesProps = {[key: string]: unknown};
|
|
4
|
+
type ValuesProps = { [key: string]: unknown };
|
|
5
5
|
type TokenProps<Props extends ValuesProps> = {
|
|
6
6
|
[K in keyof Props]: Token<Props[K]>;
|
|
7
7
|
};
|
|
@@ -53,12 +53,12 @@ export function bindMultiValue<T>(
|
|
|
53
53
|
* ```
|
|
54
54
|
*/
|
|
55
55
|
export function tryResolveValue<
|
|
56
|
-
Tokens extends Token<unknown> | {[key: string]: Token<unknown>},
|
|
56
|
+
Tokens extends Token<unknown> | { [key: string]: Token<unknown> },
|
|
57
57
|
Values extends Tokens extends Token<infer V>
|
|
58
58
|
? V | undefined
|
|
59
59
|
: Tokens extends TokenProps<infer Props>
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
? Partial<Props>
|
|
61
|
+
: never,
|
|
62
62
|
>(container: Container, token: Tokens): Values {
|
|
63
63
|
if (isToken(token)) {
|
|
64
64
|
return container.get(token) as Values;
|
|
@@ -87,13 +87,13 @@ export function tryResolveValue<
|
|
|
87
87
|
* ```
|
|
88
88
|
*/
|
|
89
89
|
export function tryResolveValues<
|
|
90
|
-
Tokens extends (Token<unknown> | {[key: string]: Token<unknown>})[],
|
|
90
|
+
Tokens extends (Token<unknown> | { [key: string]: Token<unknown> })[],
|
|
91
91
|
Values extends {
|
|
92
92
|
[K in keyof Tokens]: Tokens[K] extends Token<infer V>
|
|
93
93
|
? V | undefined
|
|
94
94
|
: Tokens[K] extends TokenProps<infer Props>
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
? Partial<Props>
|
|
96
|
+
: never;
|
|
97
97
|
},
|
|
98
98
|
>(container: Container, ...tokens: Tokens): Values {
|
|
99
99
|
return tokens.map((item) => tryResolveValue(container, item)) as Values;
|
|
@@ -117,12 +117,12 @@ export function tryResolveValues<
|
|
|
117
117
|
* ```
|
|
118
118
|
*/
|
|
119
119
|
export function resolveValue<
|
|
120
|
-
Tokens extends Token<unknown> | {[key: string]: Token<unknown>},
|
|
120
|
+
Tokens extends Token<unknown> | { [key: string]: Token<unknown> },
|
|
121
121
|
Values extends Tokens extends Token<infer V>
|
|
122
122
|
? V
|
|
123
123
|
: Tokens extends TokenProps<infer Props>
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
? Props
|
|
125
|
+
: never,
|
|
126
126
|
>(container: Container, token: Tokens): Values {
|
|
127
127
|
if (isToken(token)) {
|
|
128
128
|
return container.resolve(token) as Values;
|
|
@@ -153,13 +153,13 @@ export function resolveValue<
|
|
|
153
153
|
* ```
|
|
154
154
|
*/
|
|
155
155
|
export function resolveValues<
|
|
156
|
-
Tokens extends (Token<unknown> | {[key: string]: Token<unknown>})[],
|
|
156
|
+
Tokens extends (Token<unknown> | { [key: string]: Token<unknown> })[],
|
|
157
157
|
Values extends {
|
|
158
158
|
[K in keyof Tokens]: Tokens[K] extends Token<infer V>
|
|
159
159
|
? V
|
|
160
160
|
: Tokens[K] extends TokenProps<infer Props>
|
|
161
|
-
|
|
162
|
-
|
|
161
|
+
? Props
|
|
162
|
+
: never;
|
|
163
163
|
},
|
|
164
164
|
>(container: Container, ...tokens: Tokens): Values {
|
|
165
165
|
return tokens.map((item) => resolveValue(container, item)) as Values;
|
|
@@ -177,13 +177,13 @@ export function resolveValues<
|
|
|
177
177
|
* @return Decorated factory which takes a dependency container as a single argument.
|
|
178
178
|
*/
|
|
179
179
|
export function injectable<
|
|
180
|
-
Tokens extends (Token<unknown> | {[key: string]: Token<unknown>})[],
|
|
180
|
+
Tokens extends (Token<unknown> | { [key: string]: Token<unknown> })[],
|
|
181
181
|
Values extends {
|
|
182
182
|
[K in keyof Tokens]: Tokens[K] extends Token<infer V>
|
|
183
183
|
? V
|
|
184
184
|
: Tokens[K] extends TokenProps<infer Props>
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
? Props
|
|
186
|
+
: never;
|
|
187
187
|
},
|
|
188
188
|
Result,
|
|
189
189
|
>(
|
|
@@ -210,13 +210,13 @@ export function injectable<
|
|
|
210
210
|
* and returns a new created class.
|
|
211
211
|
*/
|
|
212
212
|
export function injectableClass<
|
|
213
|
-
Tokens extends (Token<unknown> | {[key: string]: Token<unknown>})[],
|
|
213
|
+
Tokens extends (Token<unknown> | { [key: string]: Token<unknown> })[],
|
|
214
214
|
Values extends {
|
|
215
215
|
[K in keyof Tokens]: Tokens[K] extends Token<infer V>
|
|
216
216
|
? V
|
|
217
217
|
: Tokens[K] extends TokenProps<infer Props>
|
|
218
|
-
|
|
219
|
-
|
|
218
|
+
? Props
|
|
219
|
+
: never;
|
|
220
220
|
},
|
|
221
221
|
Result,
|
|
222
222
|
>(
|
package/dist/browser/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
package/dist/cjs/container.d.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { Token } from './tokens';
|
|
2
|
-
/**
|
|
3
|
-
* ResolverError is thrown by the resolver when a token is not found in a container.
|
|
4
|
-
*/
|
|
5
|
-
export declare class ResolverError extends Error {
|
|
6
|
-
constructor(message: string);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* @see https://github.com/mnasyrov/ditox#factory-lifetimes
|
|
10
|
-
*/
|
|
11
|
-
export type FactoryScope = 'scoped' | 'singleton' | 'transient';
|
|
12
|
-
/**
|
|
13
|
-
* Options for factory binding.
|
|
14
|
-
*
|
|
15
|
-
* `scope` types:
|
|
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
|
-
* - `transient` - The value is created every time it is resolved.
|
|
19
|
-
*
|
|
20
|
-
* `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
|
|
21
|
-
*/
|
|
22
|
-
export type FactoryOptions<T> = {
|
|
23
|
-
scope?: 'scoped' | 'singleton';
|
|
24
|
-
onRemoved?: (value: T) => void;
|
|
25
|
-
} | {
|
|
26
|
-
scope: 'transient';
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Dependency container.
|
|
30
|
-
*/
|
|
31
|
-
export type Container = {
|
|
32
|
-
/**
|
|
33
|
-
* Binds a value for the token
|
|
34
|
-
*/
|
|
35
|
-
bindValue<T>(token: Token<T>, value: T): void;
|
|
36
|
-
/**
|
|
37
|
-
* Binds a factory for the token.
|
|
38
|
-
*/
|
|
39
|
-
bindFactory<T>(token: Token<T>, factory: (container: Container) => T, options?: FactoryOptions<T>): void;
|
|
40
|
-
/**
|
|
41
|
-
* Checks if the token is registered in the container hierarchy.
|
|
42
|
-
*/
|
|
43
|
-
hasToken(token: Token<unknown>): boolean;
|
|
44
|
-
/**
|
|
45
|
-
* Returns a resolved value by the token, or returns `undefined` in case the token is not found.
|
|
46
|
-
*/
|
|
47
|
-
get<T>(token: Token<T>): T | undefined;
|
|
48
|
-
/**
|
|
49
|
-
* Returns a resolved value by the token or throws `ResolverError` in case the token is not found.
|
|
50
|
-
*/
|
|
51
|
-
resolve<T>(token: Token<T>): T;
|
|
52
|
-
/**
|
|
53
|
-
* Removes a binding for the token.
|
|
54
|
-
*/
|
|
55
|
-
remove<T>(token: Token<T>): void;
|
|
56
|
-
/**
|
|
57
|
-
* Removes all bindings in the container.
|
|
58
|
-
*/
|
|
59
|
-
removeAll(): void;
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* A subset of Container interface that provides read-only access to dependency resolution.
|
|
63
|
-
* This type is used for parent containers to allow token resolution without exposing mutation methods.
|
|
64
|
-
*/
|
|
65
|
-
export type ContainerResolver = Pick<Container, 'hasToken' | 'get' | 'resolve'>;
|
|
66
|
-
/** @internal */
|
|
67
|
-
export declare const CONTAINER: Token<Container>;
|
|
68
|
-
/** @internal */
|
|
69
|
-
export declare const PARENT_CONTAINERS: Token<ReadonlyArray<Container>>;
|
|
70
|
-
/** @internal */
|
|
71
|
-
export declare const RESOLVER: Token<Resolver>;
|
|
72
|
-
/** @internal */
|
|
73
|
-
declare const NOT_FOUND: unique symbol;
|
|
74
|
-
/** @internal */
|
|
75
|
-
type FactoryContext<T> = {
|
|
76
|
-
factory: (container: Container) => T;
|
|
77
|
-
options?: FactoryOptions<T>;
|
|
78
|
-
};
|
|
79
|
-
/** @internal */
|
|
80
|
-
export type FactoriesMap = Map<symbol, FactoryContext<any>>;
|
|
81
|
-
/** @internal */
|
|
82
|
-
export declare const FACTORIES_MAP: Token<FactoriesMap>;
|
|
83
|
-
/** @internal */
|
|
84
|
-
type Resolver = <T>(token: Token<T>, origin: Container) => T | typeof NOT_FOUND;
|
|
85
|
-
/**
|
|
86
|
-
* Creates a new dependency container.
|
|
87
|
-
*
|
|
88
|
-
* 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.
|
|
89
|
-
*
|
|
90
|
-
* @param parentArg - Optional parent container or an array of containers.
|
|
91
|
-
*/
|
|
92
|
-
export declare function createContainer(parentArg?: ContainerResolver | ReadonlyArray<ContainerResolver>): Container;
|
|
93
|
-
export {};
|
package/dist/cjs/container.js
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createContainer = exports.FACTORIES_MAP = exports.RESOLVER = exports.PARENT_CONTAINERS = exports.CONTAINER = exports.ResolverError = void 0;
|
|
4
|
-
const tokens_1 = require("./tokens");
|
|
5
|
-
/**
|
|
6
|
-
* ResolverError is thrown by the resolver when a token is not found in a container.
|
|
7
|
-
*/
|
|
8
|
-
class ResolverError extends Error {
|
|
9
|
-
constructor(message) {
|
|
10
|
-
super(message);
|
|
11
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
12
|
-
this.name = 'ResolverError';
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.ResolverError = ResolverError;
|
|
16
|
-
/** @internal */
|
|
17
|
-
exports.CONTAINER = (0, tokens_1.token)('ditox.Container');
|
|
18
|
-
/** @internal */
|
|
19
|
-
exports.PARENT_CONTAINERS = (0, tokens_1.token)('ditox.ParentContainer');
|
|
20
|
-
/** @internal */
|
|
21
|
-
exports.RESOLVER = (0, tokens_1.token)('ditox.Resolver');
|
|
22
|
-
/** @internal */
|
|
23
|
-
const NOT_FOUND = Symbol();
|
|
24
|
-
/** @internal */
|
|
25
|
-
const DEFAULT_SCOPE = 'singleton';
|
|
26
|
-
/** @internal */
|
|
27
|
-
exports.FACTORIES_MAP = (0, tokens_1.token)('ditox.FactoriesMap');
|
|
28
|
-
/** @internal */
|
|
29
|
-
function getScope(options) {
|
|
30
|
-
var _a;
|
|
31
|
-
return (_a = options === null || options === void 0 ? void 0 : options.scope) !== null && _a !== void 0 ? _a : DEFAULT_SCOPE;
|
|
32
|
-
}
|
|
33
|
-
/** @internal */
|
|
34
|
-
function getOnRemoved(options) {
|
|
35
|
-
return options.scope === undefined ||
|
|
36
|
-
options.scope === 'scoped' ||
|
|
37
|
-
options.scope === 'singleton'
|
|
38
|
-
? options.onRemoved
|
|
39
|
-
: undefined;
|
|
40
|
-
}
|
|
41
|
-
/** @internal */
|
|
42
|
-
function isInternalToken(token) {
|
|
43
|
-
return (token.symbol === exports.CONTAINER.symbol ||
|
|
44
|
-
token.symbol === exports.PARENT_CONTAINERS.symbol ||
|
|
45
|
-
token.symbol === exports.RESOLVER.symbol);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Creates a new dependency container.
|
|
49
|
-
*
|
|
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.
|
|
51
|
-
*
|
|
52
|
-
* @param parentArg - Optional parent container or an array of containers.
|
|
53
|
-
*/
|
|
54
|
-
function createContainer(parentArg) {
|
|
55
|
-
const parents = parentArg
|
|
56
|
-
? Array.isArray(parentArg)
|
|
57
|
-
? [...parentArg]
|
|
58
|
-
: [parentArg]
|
|
59
|
-
: undefined;
|
|
60
|
-
const values = new Map();
|
|
61
|
-
const factories = new Map();
|
|
62
|
-
const container = {
|
|
63
|
-
bindValue(token, value) {
|
|
64
|
-
if (isInternalToken(token)) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
values.set(token.symbol, value);
|
|
68
|
-
},
|
|
69
|
-
bindFactory(token, factory, options) {
|
|
70
|
-
if (isInternalToken(token)) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
factories.set(token.symbol, { factory, options });
|
|
74
|
-
},
|
|
75
|
-
remove(token) {
|
|
76
|
-
var _a;
|
|
77
|
-
if (isInternalToken(token)) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const options = (_a = factories.get(token.symbol)) === null || _a === void 0 ? void 0 : _a.options;
|
|
81
|
-
if (options) {
|
|
82
|
-
executeOnRemoved(token.symbol, options);
|
|
83
|
-
}
|
|
84
|
-
values.delete(token.symbol);
|
|
85
|
-
factories.delete(token.symbol);
|
|
86
|
-
},
|
|
87
|
-
removeAll() {
|
|
88
|
-
factories.forEach((context, tokenSymbol) => {
|
|
89
|
-
if (context.options) {
|
|
90
|
-
executeOnRemoved(tokenSymbol, context.options);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
values.clear();
|
|
94
|
-
factories.clear();
|
|
95
|
-
bindInternalTokens();
|
|
96
|
-
},
|
|
97
|
-
hasToken(token) {
|
|
98
|
-
return (values.has(token.symbol) ||
|
|
99
|
-
factories.has(token.symbol) ||
|
|
100
|
-
parentContainersHaveToken(parents, token));
|
|
101
|
-
},
|
|
102
|
-
get(token) {
|
|
103
|
-
const value = resolver(token, container);
|
|
104
|
-
if (value !== NOT_FOUND) {
|
|
105
|
-
return value;
|
|
106
|
-
}
|
|
107
|
-
if (token.isOptional) {
|
|
108
|
-
return token.optionalValue;
|
|
109
|
-
}
|
|
110
|
-
return undefined;
|
|
111
|
-
},
|
|
112
|
-
resolve(token) {
|
|
113
|
-
var _a;
|
|
114
|
-
const value = resolver(token, container);
|
|
115
|
-
if (value !== NOT_FOUND) {
|
|
116
|
-
return value;
|
|
117
|
-
}
|
|
118
|
-
if (token.isOptional) {
|
|
119
|
-
return token.optionalValue;
|
|
120
|
-
}
|
|
121
|
-
throw new ResolverError(`Token "${(_a = token.symbol.description) !== null && _a !== void 0 ? _a : ''}" is not provided`);
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
function resolver(token, origin) {
|
|
125
|
-
const value = values.get(token.symbol);
|
|
126
|
-
const hasValue = value !== undefined || values.has(token.symbol);
|
|
127
|
-
if (hasValue && origin === container) {
|
|
128
|
-
return value;
|
|
129
|
-
}
|
|
130
|
-
const factoryContext = factories.get(token.symbol);
|
|
131
|
-
if (factoryContext) {
|
|
132
|
-
const scope = getScope(factoryContext.options);
|
|
133
|
-
switch (scope) {
|
|
134
|
-
case 'singleton': {
|
|
135
|
-
if (hasValue) {
|
|
136
|
-
return value;
|
|
137
|
-
}
|
|
138
|
-
else if (parentContainersHaveToken(parents, token)) {
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
// Cache the value in the same container where the factory is registered.
|
|
143
|
-
const value = factoryContext.factory(container);
|
|
144
|
-
container.bindValue(token, value);
|
|
145
|
-
return value;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
case 'scoped': {
|
|
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;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
case 'transient': {
|
|
160
|
-
// Create a value within the origin container and don't cache it.
|
|
161
|
-
return factoryContext.factory(origin);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
if (hasValue) {
|
|
166
|
-
return value;
|
|
167
|
-
}
|
|
168
|
-
if (parents) {
|
|
169
|
-
return parentContainersResolveToken(parents, token, origin);
|
|
170
|
-
}
|
|
171
|
-
return NOT_FOUND;
|
|
172
|
-
}
|
|
173
|
-
function executeOnRemoved(tokenSymbol, options) {
|
|
174
|
-
const onRemoved = getOnRemoved(options);
|
|
175
|
-
if (onRemoved) {
|
|
176
|
-
const value = values.get(tokenSymbol);
|
|
177
|
-
if (value !== undefined || values.has(tokenSymbol)) {
|
|
178
|
-
onRemoved(value);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
function bindInternalTokens() {
|
|
183
|
-
values.set(exports.CONTAINER.symbol, container);
|
|
184
|
-
values.set(exports.RESOLVER.symbol, resolver);
|
|
185
|
-
values.set(exports.FACTORIES_MAP.symbol, factories);
|
|
186
|
-
if (parents) {
|
|
187
|
-
values.set(exports.PARENT_CONTAINERS.symbol, parents);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
bindInternalTokens();
|
|
191
|
-
return container;
|
|
192
|
-
}
|
|
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
|
-
}
|
|
216
|
-
//# sourceMappingURL=container.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AA4ED,gBAAgB;AACH,QAAA,SAAS,GAAqB,IAAA,cAAK,EAAC,iBAAiB,CAAC,CAAC;AACpE,gBAAgB;AACH,QAAA,iBAAiB,GAAoC,IAAA,cAAK,EACrE,uBAAuB,CACxB,CAAC;AACF,gBAAgB;AACH,QAAA,QAAQ,GAAoB,IAAA,cAAK,EAAC,gBAAgB,CAAC,CAAC;AAEjE,gBAAgB;AAChB,MAAM,SAAS,GAAG,MAAM,EAAE,CAAC;AAE3B,gBAAgB;AAChB,MAAM,aAAa,GAAiB,WAAW,CAAC;AAchD,gBAAgB;AACH,QAAA,aAAa,GAAwB,IAAA,cAAK,EAAC,oBAAoB,CAAC,CAAC;AAK9E,gBAAgB;AAChB,SAAS,QAAQ,CAAI,OAA2B;;IAC9C,OAAO,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,mCAAI,aAAa,CAAC;AACzC,CAAC;AAED,gBAAgB;AAChB,SAAS,YAAY,CAAI,OAA0B;IACjD,OAAO,OAAO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AC;AAnLD,0CAmLC;AAED,SAAS,yBAAyB,CAChC,OAAqD,EACrD,KAAe;IAEf,IAAI,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC;IAE3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACvC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;YAC9B,OAAO,IAAI,CAAC;SACb;KACF;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,4BAA4B,CACnC,OAAyC,EACzC,KAAe,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/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { token, optional } from './tokens';
|
|
2
|
-
export type { RequiredToken, OptionalToken, Token } from './tokens';
|
|
3
|
-
export { ResolverError, createContainer } from './container';
|
|
4
|
-
export type { FactoryScope, FactoryOptions, Container, ContainerResolver, } from './container';
|
|
5
|
-
export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
|
|
6
|
-
export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
|
|
7
|
-
export type { Module, ModuleDeclaration, BindModuleOptions, ModuleBindingEntry, } from './modules';
|
package/dist/cjs/index.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.declareModuleBindings = exports.declareModule = exports.bindModules = exports.bindModule = exports.injectableClass = exports.injectable = exports.resolveValues = exports.resolveValue = exports.tryResolveValues = exports.tryResolveValue = exports.bindMultiValue = exports.isToken = exports.createContainer = exports.ResolverError = exports.optional = exports.token = void 0;
|
|
4
|
-
var tokens_1 = require("./tokens");
|
|
5
|
-
Object.defineProperty(exports, "token", { enumerable: true, get: function () { return tokens_1.token; } });
|
|
6
|
-
Object.defineProperty(exports, "optional", { enumerable: true, get: function () { return tokens_1.optional; } });
|
|
7
|
-
var container_1 = require("./container");
|
|
8
|
-
Object.defineProperty(exports, "ResolverError", { enumerable: true, get: function () { return container_1.ResolverError; } });
|
|
9
|
-
Object.defineProperty(exports, "createContainer", { enumerable: true, get: function () { return container_1.createContainer; } });
|
|
10
|
-
var utils_1 = require("./utils");
|
|
11
|
-
Object.defineProperty(exports, "isToken", { enumerable: true, get: function () { return utils_1.isToken; } });
|
|
12
|
-
Object.defineProperty(exports, "bindMultiValue", { enumerable: true, get: function () { return utils_1.bindMultiValue; } });
|
|
13
|
-
Object.defineProperty(exports, "tryResolveValue", { enumerable: true, get: function () { return utils_1.tryResolveValue; } });
|
|
14
|
-
Object.defineProperty(exports, "tryResolveValues", { enumerable: true, get: function () { return utils_1.tryResolveValues; } });
|
|
15
|
-
Object.defineProperty(exports, "resolveValue", { enumerable: true, get: function () { return utils_1.resolveValue; } });
|
|
16
|
-
Object.defineProperty(exports, "resolveValues", { enumerable: true, get: function () { return utils_1.resolveValues; } });
|
|
17
|
-
Object.defineProperty(exports, "injectable", { enumerable: true, get: function () { return utils_1.injectable; } });
|
|
18
|
-
Object.defineProperty(exports, "injectableClass", { enumerable: true, get: function () { return utils_1.injectableClass; } });
|
|
19
|
-
var modules_1 = require("./modules");
|
|
20
|
-
Object.defineProperty(exports, "bindModule", { enumerable: true, get: function () { return modules_1.bindModule; } });
|
|
21
|
-
Object.defineProperty(exports, "bindModules", { enumerable: true, get: function () { return modules_1.bindModules; } });
|
|
22
|
-
Object.defineProperty(exports, "declareModule", { enumerable: true, get: function () { return modules_1.declareModule; } });
|
|
23
|
-
Object.defineProperty(exports, "declareModuleBindings", { enumerable: true, get: function () { return modules_1.declareModuleBindings; } });
|
|
24
|
-
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAyC;AAAjC,+FAAA,KAAK,OAAA;AAAE,kGAAA,QAAQ,OAAA;AAGvB,yCAA2D;AAAnD,0GAAA,aAAa,OAAA;AAAE,4GAAA,eAAe,OAAA;AAQtC,iCASiB;AARf,gGAAA,OAAO,OAAA;AACP,uGAAA,cAAc,OAAA;AACd,wGAAA,eAAe,OAAA;AACf,yGAAA,gBAAgB,OAAA;AAChB,qGAAA,YAAY,OAAA;AACZ,sGAAA,aAAa,OAAA;AACb,mGAAA,UAAU,OAAA;AACV,wGAAA,eAAe,OAAA;AAGjB,qCAKmB;AAJjB,qGAAA,UAAU,OAAA;AACV,sGAAA,WAAW,OAAA;AACX,wGAAA,aAAa,OAAA;AACb,gHAAA,qBAAqB,OAAA"}
|
package/dist/cjs/modules.d.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { Container } from './container';
|
|
2
|
-
import { Token } from './tokens';
|
|
3
|
-
type AnyObject = Record<string, any>;
|
|
4
|
-
type EmptyObject = Record<string, never>;
|
|
5
|
-
type ModuleController = {
|
|
6
|
-
/** Dispose the module and clean its resources */
|
|
7
|
-
destroy?: () => void;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Dependency module
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* type LoggerModule = Module<{
|
|
15
|
-
* logger: Logger;
|
|
16
|
-
* }>;
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
|
|
20
|
-
type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
|
|
21
|
-
/**
|
|
22
|
-
* Description how to bind the module in declarative way.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
27
|
-
* token: LOGGER_MODULE_TOKEN,
|
|
28
|
-
* factory: (container) => {
|
|
29
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
30
|
-
* return {
|
|
31
|
-
* logger: { log: (message) => transport.write(message) },
|
|
32
|
-
* destroy: () => transport.close(),
|
|
33
|
-
* }
|
|
34
|
-
* },
|
|
35
|
-
* exports: {
|
|
36
|
-
* logger: LOGGER_TOKEN,
|
|
37
|
-
* },
|
|
38
|
-
* };
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
export type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
42
|
-
/** Token for the module */
|
|
43
|
-
token: Token<T>;
|
|
44
|
-
/** Modules for binding */
|
|
45
|
-
imports?: ReadonlyArray<ModuleBindingEntry>;
|
|
46
|
-
/** Factory of the module */
|
|
47
|
-
factory: (container: Container) => T;
|
|
48
|
-
/** Dictionary of module properties which are bound to tokens. */
|
|
49
|
-
exports?: {
|
|
50
|
-
[K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
|
|
51
|
-
};
|
|
52
|
-
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
53
|
-
beforeBinding?: (container: Container) => void;
|
|
54
|
-
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
55
|
-
afterBinding?: (container: Container) => void;
|
|
56
|
-
};
|
|
57
|
-
export type AnyModuleDeclaration = ModuleDeclaration<Module<AnyObject>>;
|
|
58
|
-
/**
|
|
59
|
-
* Options for module binding.
|
|
60
|
-
*
|
|
61
|
-
* `scope` types:
|
|
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
|
-
*/
|
|
65
|
-
export type BindModuleOptions = {
|
|
66
|
-
scope?: 'scoped' | 'singleton';
|
|
67
|
-
};
|
|
68
|
-
type ModuleDeclarationWithOptions = {
|
|
69
|
-
module: ModuleDeclaration<AnyObject>;
|
|
70
|
-
options: BindModuleOptions;
|
|
71
|
-
};
|
|
72
|
-
export type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
|
|
73
|
-
/**
|
|
74
|
-
* Binds the dependency module to the container
|
|
75
|
-
* @param container - Dependency container.
|
|
76
|
-
* @param moduleDeclaration - Declaration of the dependency module.
|
|
77
|
-
* @param options - Options for module binding.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* bindModule(container, LOGGER_MODULE);
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
export declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
85
|
-
/**
|
|
86
|
-
* Binds dependency modules to the container
|
|
87
|
-
*
|
|
88
|
-
* @param container - Dependency container for binding
|
|
89
|
-
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
90
|
-
*/
|
|
91
|
-
export declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
92
|
-
/**
|
|
93
|
-
* Declares a module binding
|
|
94
|
-
*
|
|
95
|
-
* @param declaration - a module declaration
|
|
96
|
-
* @param declaration.token - optional field
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```ts
|
|
100
|
-
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
101
|
-
* factory: (container) => {
|
|
102
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
103
|
-
* return {
|
|
104
|
-
* logger: { log: (message) => transport.write(message) },
|
|
105
|
-
* destroy: () => transport.close(),
|
|
106
|
-
* }
|
|
107
|
-
* },
|
|
108
|
-
* exports: {
|
|
109
|
-
* logger: LOGGER_TOKEN,
|
|
110
|
-
* },
|
|
111
|
-
* });
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
export declare function declareModule<T extends Module<AnyObject>>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
115
|
-
/**
|
|
116
|
-
* Declares bindings of several modules
|
|
117
|
-
*
|
|
118
|
-
* @param modules - module declaration entries
|
|
119
|
-
*/
|
|
120
|
-
export declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
121
|
-
export {};
|