ditox 3.1.0 → 3.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/README.md +135 -32
- package/dist/index.cjs +518 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{umd/index.d.ts → index.d.cts} +195 -197
- package/dist/{browser/index.d.ts → index.d.ts} +195 -197
- package/dist/index.js +501 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +524 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +22 -19
- package/src/container.test.ts +34 -33
- package/src/container.ts +12 -6
- package/src/index.ts +26 -23
- package/src/modules.test.ts +172 -37
- package/src/modules.ts +22 -5
- 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 -88
- 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 -88
- 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/modules.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {Container} from './container';
|
|
2
|
-
import {Token, token} from './tokens';
|
|
3
|
-
import {injectable} from './utils';
|
|
1
|
+
import { Container } from './container';
|
|
2
|
+
import { Token, token } from './tokens';
|
|
3
|
+
import { injectable } from './utils';
|
|
4
4
|
|
|
5
5
|
type AnyObject = Record<string, any>;
|
|
6
6
|
type EmptyObject = Record<string, never>;
|
|
@@ -65,6 +65,13 @@ export type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
|
65
65
|
|
|
66
66
|
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
67
67
|
afterBinding?: (container: Container) => void;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Strategy for executing the factory:
|
|
71
|
+
* - `lazy` - **This is the default**. The factory is called when the module is resolved.
|
|
72
|
+
* - `eager` - The factory is called immediately after the module is bound to the container.
|
|
73
|
+
*/
|
|
74
|
+
strategy?: 'eager' | 'lazy';
|
|
68
75
|
};
|
|
69
76
|
|
|
70
77
|
export type AnyModuleDeclaration = ModuleDeclaration<Module<AnyObject>>;
|
|
@@ -145,6 +152,14 @@ export function bindModule<T extends Module<AnyObject>>(
|
|
|
145
152
|
const m = 'module' in entry ? entry.module : entry;
|
|
146
153
|
m.afterBinding?.(container);
|
|
147
154
|
}
|
|
155
|
+
|
|
156
|
+
for (let i = bfsQueue.length - 1; i >= 0; i--) {
|
|
157
|
+
const entry = bfsQueue[i];
|
|
158
|
+
const m = 'module' in entry ? entry.module : entry;
|
|
159
|
+
if (m.strategy === 'eager') {
|
|
160
|
+
container.resolve(m.token);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
148
163
|
}
|
|
149
164
|
|
|
150
165
|
function bindModuleEntry(
|
|
@@ -176,7 +191,7 @@ function bindModuleEntry(
|
|
|
176
191
|
container.bindFactory(
|
|
177
192
|
valueToken,
|
|
178
193
|
injectable((module) => module[valueKey], module.token),
|
|
179
|
-
{scope},
|
|
194
|
+
{ scope },
|
|
180
195
|
);
|
|
181
196
|
}
|
|
182
197
|
});
|
|
@@ -240,10 +255,12 @@ export function declareModule<T extends Module<AnyObject>>(
|
|
|
240
255
|
declaration: Omit<ModuleDeclaration<T>, 'token'> &
|
|
241
256
|
Partial<Pick<ModuleDeclaration<T>, 'token'>>,
|
|
242
257
|
): ModuleDeclaration<T> {
|
|
243
|
-
return {...declaration, token: declaration.token ?? token()};
|
|
258
|
+
return { ...declaration, token: declaration.token ?? token() };
|
|
244
259
|
}
|
|
245
260
|
|
|
246
261
|
/**
|
|
262
|
+
* @deprecated Use `declareModule` instead
|
|
263
|
+
*
|
|
247
264
|
* Declares bindings of several modules
|
|
248
265
|
*
|
|
249
266
|
* @param modules - module declaration entries
|
package/src/tokens.test.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { optional, token } from './tokens';
|
|
2
3
|
|
|
3
4
|
describe('token()', () => {
|
|
4
5
|
it('should return a token with description it is specified', () => {
|
|
5
6
|
expect(token().symbol.description).toBeUndefined();
|
|
6
7
|
expect(token('text1').symbol.description).toBe('text1');
|
|
7
|
-
expect(token({description: 'text2'}).symbol.description).toBe('text2');
|
|
8
|
+
expect(token({ description: 'text2' }).symbol.description).toBe('text2');
|
|
8
9
|
});
|
|
9
10
|
|
|
10
11
|
it('should return independent tokens if key is not specified', () => {
|
|
@@ -17,9 +18,9 @@ describe('token()', () => {
|
|
|
17
18
|
});
|
|
18
19
|
|
|
19
20
|
it('should return tokens with the same symbol if key is specified', () => {
|
|
20
|
-
const source = token({key: 'test-token'});
|
|
21
|
-
const clone = token({key: 'test-token'});
|
|
22
|
-
const something = token({key: 'something-else'});
|
|
21
|
+
const source = token({ key: 'test-token' });
|
|
22
|
+
const clone = token({ key: 'test-token' });
|
|
23
|
+
const something = token({ key: 'something-else' });
|
|
23
24
|
|
|
24
25
|
expect(source).not.toBe(clone);
|
|
25
26
|
expect(source.symbol).toBe(clone.symbol);
|
|
@@ -44,9 +45,9 @@ describe('optional()', () => {
|
|
|
44
45
|
expect(o2.optionalValue).toBe(-1);
|
|
45
46
|
});
|
|
46
47
|
|
|
47
|
-
it('should reuse symbol from a source token
|
|
48
|
-
const source = token({key: 'token-key'});
|
|
49
|
-
const clone = token({key: 'token-key'});
|
|
48
|
+
it('should reuse symbol from a source token if its key is specified', () => {
|
|
49
|
+
const source = token({ key: 'token-key' });
|
|
50
|
+
const clone = token({ key: 'token-key' });
|
|
50
51
|
const optClone = optional(clone);
|
|
51
52
|
|
|
52
53
|
expect(optClone.symbol).toBe(source.symbol);
|
package/src/tokens.ts
CHANGED
|
@@ -56,13 +56,13 @@ export function token<T>(description?: string): Token<T>;
|
|
|
56
56
|
*/ export function token<T>(options?: TokenOptions): Token<T>;
|
|
57
57
|
export function token<T>(options?: TokenOptions | string): Token<T> {
|
|
58
58
|
const normalized: TokenOptions | undefined =
|
|
59
|
-
typeof options === 'string' ? {description: options} : options;
|
|
59
|
+
typeof options === 'string' ? { description: options } : options;
|
|
60
60
|
|
|
61
61
|
const symbol: symbol = normalized?.key
|
|
62
62
|
? Symbol.for(normalized.key)
|
|
63
63
|
: Symbol(normalized?.description);
|
|
64
64
|
|
|
65
|
-
return {symbol};
|
|
65
|
+
return { symbol };
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
/**
|
package/src/utils.test.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createContainer, ResolverError } from './container';
|
|
3
|
+
import { optional, token } from './tokens';
|
|
2
4
|
import {
|
|
3
5
|
bindMultiValue,
|
|
4
6
|
injectable,
|
|
@@ -8,7 +10,6 @@ import {
|
|
|
8
10
|
tryResolveValue,
|
|
9
11
|
tryResolveValues,
|
|
10
12
|
} from './utils';
|
|
11
|
-
import {optional, token} from './tokens';
|
|
12
13
|
|
|
13
14
|
const NUMBER = token<number>('number');
|
|
14
15
|
const STRING = token<string>('string');
|
|
@@ -60,19 +61,19 @@ describe('tryResolveValue()', () => {
|
|
|
60
61
|
container.bindValue(NUMBER, 1);
|
|
61
62
|
container.bindValue(STRING, 'abc');
|
|
62
63
|
|
|
63
|
-
const props: {a: number; b: string} = tryResolveValue(container, {
|
|
64
|
+
const props: { a: number; b: string } = tryResolveValue(container, {
|
|
64
65
|
a: NUMBER,
|
|
65
66
|
b: STRING,
|
|
66
67
|
});
|
|
67
|
-
expect(props).toEqual({a: 1, b: 'abc'});
|
|
68
|
+
expect(props).toEqual({ a: 1, b: 'abc' });
|
|
68
69
|
});
|
|
69
70
|
|
|
70
71
|
it('should return "undefined" item in case a value is not provided', () => {
|
|
71
72
|
const container = createContainer();
|
|
72
73
|
container.bindValue(NUMBER, 1);
|
|
73
74
|
|
|
74
|
-
const props = tryResolveValue(container, {a: NUMBER, b: STRING});
|
|
75
|
-
expect(props).toEqual({a: 1, b: undefined});
|
|
75
|
+
const props = tryResolveValue(container, { a: NUMBER, b: STRING });
|
|
76
|
+
expect(props).toEqual({ a: 1, b: undefined });
|
|
76
77
|
});
|
|
77
78
|
|
|
78
79
|
it('should return values of optional tokens in case they are not provided', () => {
|
|
@@ -86,7 +87,7 @@ describe('tryResolveValue()', () => {
|
|
|
86
87
|
b: STRING,
|
|
87
88
|
c: OPTIONAL_STRING,
|
|
88
89
|
});
|
|
89
|
-
expect(props).toEqual({a: 1, b: undefined, c: 'value'});
|
|
90
|
+
expect(props).toEqual({ a: 1, b: undefined, c: 'value' });
|
|
90
91
|
});
|
|
91
92
|
});
|
|
92
93
|
|
|
@@ -149,11 +150,11 @@ describe('resolveValue()', () => {
|
|
|
149
150
|
container.bindValue(NUMBER, 1);
|
|
150
151
|
container.bindValue(STRING, 'abc');
|
|
151
152
|
|
|
152
|
-
const props: {a: number; b: string} = resolveValue(container, {
|
|
153
|
+
const props: { a: number; b: string } = resolveValue(container, {
|
|
153
154
|
a: NUMBER,
|
|
154
155
|
b: STRING,
|
|
155
156
|
});
|
|
156
|
-
expect(props).toEqual({a: 1, b: 'abc'});
|
|
157
|
+
expect(props).toEqual({ a: 1, b: 'abc' });
|
|
157
158
|
});
|
|
158
159
|
|
|
159
160
|
it('should throw an error in case a value is not provided', () => {
|
|
@@ -161,7 +162,7 @@ describe('resolveValue()', () => {
|
|
|
161
162
|
container.bindValue(NUMBER, 1);
|
|
162
163
|
|
|
163
164
|
expect(() => {
|
|
164
|
-
resolveValue(container, {a: NUMBER, b: STRING});
|
|
165
|
+
resolveValue(container, { a: NUMBER, b: STRING });
|
|
165
166
|
}).toThrowError(
|
|
166
167
|
new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
|
|
167
168
|
);
|
|
@@ -175,7 +176,7 @@ describe('resolveValue()', () => {
|
|
|
175
176
|
a: NUMBER,
|
|
176
177
|
b: optional(STRING, 'value'),
|
|
177
178
|
});
|
|
178
|
-
expect(props).toEqual({a: 1, b: 'value'});
|
|
179
|
+
expect(props).toEqual({ a: 1, b: 'value' });
|
|
179
180
|
});
|
|
180
181
|
});
|
|
181
182
|
|
|
@@ -224,7 +225,7 @@ describe('resolveValues()', () => {
|
|
|
224
225
|
});
|
|
225
226
|
|
|
226
227
|
describe('injectable()', () => {
|
|
227
|
-
function join({a, b}: {a: number; b: string}): string {
|
|
228
|
+
function join({ a, b }: { a: number; b: string }): string {
|
|
228
229
|
return `${a}/${b}`;
|
|
229
230
|
}
|
|
230
231
|
|
|
@@ -275,7 +276,7 @@ describe('injectable()', () => {
|
|
|
275
276
|
container.bindValue(NUMBER, 1);
|
|
276
277
|
container.bindValue(STRING, '2');
|
|
277
278
|
|
|
278
|
-
const decoratedFactory = injectable(join, {a: NUMBER, b: STRING});
|
|
279
|
+
const decoratedFactory = injectable(join, { a: NUMBER, b: STRING });
|
|
279
280
|
|
|
280
281
|
expect(decoratedFactory(container)).toBe('1/2');
|
|
281
282
|
});
|
|
@@ -284,7 +285,7 @@ describe('injectable()', () => {
|
|
|
284
285
|
const container = createContainer();
|
|
285
286
|
container.bindValue(NUMBER, 1);
|
|
286
287
|
|
|
287
|
-
const decoratedFactory = injectable(join, {a: NUMBER, b: STRING});
|
|
288
|
+
const decoratedFactory = injectable(join, { a: NUMBER, b: STRING });
|
|
288
289
|
|
|
289
290
|
expect(() => decoratedFactory(container)).toThrowError(
|
|
290
291
|
new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
|
|
@@ -359,7 +360,7 @@ describe('injectableClass()', () => {
|
|
|
359
360
|
a: number;
|
|
360
361
|
b: string;
|
|
361
362
|
|
|
362
|
-
constructor(props: {a: number; b: string}) {
|
|
363
|
+
constructor(props: { a: number; b: string }) {
|
|
363
364
|
this.a = props.a;
|
|
364
365
|
this.b = props.b;
|
|
365
366
|
}
|
|
@@ -374,7 +375,7 @@ describe('injectableClass()', () => {
|
|
|
374
375
|
container.bindValue(NUMBER, 1);
|
|
375
376
|
container.bindValue(STRING, '2');
|
|
376
377
|
|
|
377
|
-
const factory = injectableClass(TestClass, {a: NUMBER, b: STRING});
|
|
378
|
+
const factory = injectableClass(TestClass, { a: NUMBER, b: STRING });
|
|
378
379
|
|
|
379
380
|
expect(factory(container).join()).toBe('1/2');
|
|
380
381
|
});
|
|
@@ -383,7 +384,7 @@ describe('injectableClass()', () => {
|
|
|
383
384
|
const container = createContainer();
|
|
384
385
|
container.bindValue(NUMBER, 1);
|
|
385
386
|
|
|
386
|
-
const factory = injectableClass(TestClass, {a: NUMBER, b: STRING});
|
|
387
|
+
const factory = injectableClass(TestClass, { a: NUMBER, b: STRING });
|
|
387
388
|
|
|
388
389
|
expect(() => factory(container)).toThrowError(
|
|
389
390
|
new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
|
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,88 +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
|
-
/** @internal */
|
|
62
|
-
export declare const CONTAINER: Token<Container>;
|
|
63
|
-
/** @internal */
|
|
64
|
-
export declare const PARENT_CONTAINERS: Token<ReadonlyArray<Container>>;
|
|
65
|
-
/** @internal */
|
|
66
|
-
export declare const RESOLVER: Token<Resolver>;
|
|
67
|
-
/** @internal */
|
|
68
|
-
declare const NOT_FOUND: unique symbol;
|
|
69
|
-
/** @internal */
|
|
70
|
-
type FactoryContext<T> = {
|
|
71
|
-
factory: (container: Container) => T;
|
|
72
|
-
options?: FactoryOptions<T>;
|
|
73
|
-
};
|
|
74
|
-
/** @internal */
|
|
75
|
-
export type FactoriesMap = Map<symbol, FactoryContext<any>>;
|
|
76
|
-
/** @internal */
|
|
77
|
-
export declare const FACTORIES_MAP: Token<FactoriesMap>;
|
|
78
|
-
/** @internal */
|
|
79
|
-
type Resolver = <T>(token: Token<T>, origin: Container) => T | typeof NOT_FOUND;
|
|
80
|
-
/**
|
|
81
|
-
* Creates a new dependency container.
|
|
82
|
-
*
|
|
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.
|
|
84
|
-
*
|
|
85
|
-
* @param parentArg - Optional parent container or an array of containers.
|
|
86
|
-
*/
|
|
87
|
-
export declare function createContainer(parentArg?: Container | ReadonlyArray<Container>): Container;
|
|
88
|
-
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
|