ditox 1.4.1 → 2.2.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 +6 -5
- package/dist/{index.d.ts → browser/index.d.ts} +98 -42
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/cjs/ditox.d.ts +125 -0
- package/dist/cjs/ditox.js +210 -0
- package/dist/cjs/ditox.js.map +1 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/modules.d.ts +118 -0
- package/dist/cjs/modules.js +109 -0
- package/dist/cjs/modules.js.map +1 -0
- package/dist/cjs/utils-deprecated.d.ts +59 -0
- package/dist/cjs/utils-deprecated.js +66 -0
- package/dist/cjs/utils-deprecated.js.map +1 -0
- package/dist/cjs/utils.d.ts +136 -0
- package/dist/cjs/utils.js +156 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/ditox.d.ts +125 -0
- package/{lib/index.js → dist/esm/ditox.js} +10 -201
- package/dist/esm/ditox.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/modules.d.ts +118 -0
- package/dist/esm/modules.js +102 -0
- package/dist/esm/modules.js.map +1 -0
- package/dist/esm/utils-deprecated.d.ts +59 -0
- package/dist/esm/utils-deprecated.js +59 -0
- package/dist/esm/utils-deprecated.js.map +1 -0
- package/dist/esm/utils.d.ts +136 -0
- package/dist/esm/utils.js +145 -0
- package/dist/esm/utils.js.map +1 -0
- package/{lib → dist/umd}/index.d.ts +98 -42
- package/dist/umd/index.js +2 -0
- package/dist/umd/index.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +250 -164
- package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
- package/package.json +15 -16
- package/src/ditox.test.ts +10 -1
- package/src/index.ts +13 -13
- package/src/modules.test.ts +2 -0
- package/src/modules.ts +1 -8
- package/src/utils.test.ts +173 -58
- package/src/utils.ts +164 -66
- package/CHANGELOG.md +0 -121
- package/dist/index.browser.js +0 -2
- package/dist/index.browser.js.flow +0 -4
- package/dist/index.browser.js.map +0 -1
- package/dist/index.js +0 -411
- package/dist/index.js.flow +0 -4
- package/dist/index.js.map +0 -1
- package/dist/index.module.d.ts +0 -301
- package/dist/index.module.js +0 -393
- package/dist/index.module.js.flow +0 -4
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.d.ts +0 -301
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.flow +0 -4
- package/dist/index.umd.js.map +0 -1
- package/lib/index.js.flow +0 -4
- package/lib/index.js.map +0 -1
- package/mod.ts +0 -2
- package/src/ditox.js.flow +0 -131
- package/src/index.js.flow +0 -34
- package/src/modules.js.flow +0 -156
- package/src/utils.js.flow +0 -113
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAOA;;GAEG;AACH,SAAgB,OAAO,CAAI,KAAc;IACvC,OAAO,CACL,KAAK,KAAK,SAAS;QACnB,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,QAAQ,IAAI,KAAK;QACjB,OAAQ,KAAa,CAAC,MAAM,KAAK,QAAQ,CAC1C,CAAC;AACJ,CAAC;AARD,0BAQC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAC5B,SAAoB,EACpB,KAA8B,EAC9B,KAAQ;;IAER,MAAM,UAAU,GAAG,MAAA,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,mCAAI,EAAE,CAAC;IAC9C,MAAM,UAAU,GAAG,CAAC,GAAG,UAAU,EAAE,KAAK,CAAC,CAAC;IAC1C,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AACzC,CAAC;AARD,wCAQC;AAED;;;;;;;;;;;;;;;;G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,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAW,CAAC;AACvE,CAAC;AAXD,sCAWC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CAYxB,OAAsC,EACtC,GAAG,MAAc;IAEjB,OAAO,CAAC,SAAS,EAAE,EAAE;QACnB,MAAM,MAAM,GAAW,aAAa,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,CAAC;QAC3D,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACrC,CAAC,CAAC;AACJ,CAAC;AAnBD,gCAmBC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,eAAe,CAY7B,WAA8C,EAC9C,GAAG,MAAc;IAEjB,OAAO,UAAU,CACf,CAAC,GAAG,MAAM,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,GAAG,MAAM,CAAC,EACzC,GAAG,MAAM,CACV,CAAC;AACJ,CAAC;AAnBD,0CAmBC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ignore
|
|
3
|
+
* Binding token for mandatory value
|
|
4
|
+
*/
|
|
5
|
+
export declare type RequiredToken<T> = {
|
|
6
|
+
symbol: symbol;
|
|
7
|
+
type?: T;
|
|
8
|
+
isOptional?: false;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* @ignore
|
|
12
|
+
* Binding token for optional value
|
|
13
|
+
*/
|
|
14
|
+
export declare type OptionalToken<T> = {
|
|
15
|
+
symbol: symbol;
|
|
16
|
+
type?: T;
|
|
17
|
+
isOptional: true;
|
|
18
|
+
optionalValue: T;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Binding token.
|
|
22
|
+
*/
|
|
23
|
+
export declare type Token<T> = RequiredToken<T> | OptionalToken<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new binding token.
|
|
26
|
+
* @param description - Token description for better error messages.
|
|
27
|
+
*/
|
|
28
|
+
export declare function token<T>(description?: string): Token<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Decorate a token with an optional value.
|
|
31
|
+
* This value is be used as default value in case a container does not have registered token.
|
|
32
|
+
* @param token - Existed token.
|
|
33
|
+
* @param optionalValue - Default value for the resolver.
|
|
34
|
+
*/
|
|
35
|
+
export declare function optional<T>(token: Token<T>, optionalValue: T): OptionalToken<T>;
|
|
36
|
+
export declare function optional<T>(token: Token<T>): OptionalToken<T | undefined>;
|
|
37
|
+
/**
|
|
38
|
+
* ResolverError is thrown by the resolver when a token is not found in a container.
|
|
39
|
+
*/
|
|
40
|
+
export declare class ResolverError extends Error {
|
|
41
|
+
constructor(message: string);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @see https://github.com/mnasyrov/ditox#factory-lifetimes
|
|
45
|
+
*/
|
|
46
|
+
export declare type FactoryScope = 'scoped' | 'singleton' | 'transient';
|
|
47
|
+
/**
|
|
48
|
+
* Options for factory binding.
|
|
49
|
+
*
|
|
50
|
+
* `scope` types:
|
|
51
|
+
* - `singleton` - **This is the default**. The value is created and cached by the container which registered the factory.
|
|
52
|
+
* - `scoped` - The value is created and cached by the container which starts resolving.
|
|
53
|
+
* - `transient` - The value is created every time it is resolved.
|
|
54
|
+
*
|
|
55
|
+
* `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
|
|
56
|
+
*/
|
|
57
|
+
export declare type FactoryOptions<T> = {
|
|
58
|
+
scope?: 'scoped' | 'singleton';
|
|
59
|
+
onRemoved?: (value: T) => void;
|
|
60
|
+
} | {
|
|
61
|
+
scope: 'transient';
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Dependency container.
|
|
65
|
+
*/
|
|
66
|
+
export declare type Container = {
|
|
67
|
+
/**
|
|
68
|
+
* Binds a value for the token
|
|
69
|
+
*/
|
|
70
|
+
bindValue<T>(token: Token<T>, value: T): void;
|
|
71
|
+
/**
|
|
72
|
+
* Binds a factory for the token.
|
|
73
|
+
*/
|
|
74
|
+
bindFactory<T>(token: Token<T>, factory: (container: Container) => T, options?: FactoryOptions<T>): void;
|
|
75
|
+
/**
|
|
76
|
+
* Checks if the token is registered in the container hierarchy.
|
|
77
|
+
*/
|
|
78
|
+
hasToken(token: Token<unknown>): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Returns a resolved value by the token, or returns `undefined` in case the token is not found.
|
|
81
|
+
*/
|
|
82
|
+
get<T>(token: Token<T>): T | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Returns a resolved value by the token, or throws `ResolverError` in case the token is not found.
|
|
85
|
+
*/
|
|
86
|
+
resolve<T>(token: Token<T>): T;
|
|
87
|
+
/**
|
|
88
|
+
* Removes a binding for the token.
|
|
89
|
+
*/
|
|
90
|
+
remove<T>(token: Token<T>): void;
|
|
91
|
+
/**
|
|
92
|
+
* Removes all bindings in the container.
|
|
93
|
+
*/
|
|
94
|
+
removeAll(): void;
|
|
95
|
+
};
|
|
96
|
+
/** @internal */
|
|
97
|
+
export declare const CONTAINER: Token<Container>;
|
|
98
|
+
/** @internal */
|
|
99
|
+
export declare const PARENT_CONTAINER: Token<Container>;
|
|
100
|
+
/** @internal */
|
|
101
|
+
export declare const RESOLVER: Token<Resolver>;
|
|
102
|
+
/** @internal */
|
|
103
|
+
declare const NOT_FOUND: unique symbol;
|
|
104
|
+
/** @internal */
|
|
105
|
+
export declare const FAKE_FACTORY: () => never;
|
|
106
|
+
/** @internal */
|
|
107
|
+
declare type FactoryContext<T> = {
|
|
108
|
+
factory: (container: Container) => T;
|
|
109
|
+
options?: FactoryOptions<T>;
|
|
110
|
+
};
|
|
111
|
+
/** @internal */
|
|
112
|
+
export declare type FactoriesMap = Map<symbol, FactoryContext<any>>;
|
|
113
|
+
/** @internal */
|
|
114
|
+
export declare const FACTORIES_MAP: Token<FactoriesMap>;
|
|
115
|
+
/** @internal */
|
|
116
|
+
declare type Resolver = <T>(token: Token<T>, origin: Container) => T | typeof NOT_FOUND;
|
|
117
|
+
/**
|
|
118
|
+
* Creates a new dependency container.
|
|
119
|
+
*
|
|
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
|
+
*
|
|
122
|
+
* @param parentContainer - Optional parent container.
|
|
123
|
+
*/
|
|
124
|
+
export declare function createContainer(parentContainer?: Container): Container;
|
|
125
|
+
export {};
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* Creates a new binding token.
|
|
3
3
|
* @param description - Token description for better error messages.
|
|
4
4
|
*/
|
|
5
|
-
function token(description) {
|
|
5
|
+
export function token(description) {
|
|
6
6
|
return { symbol: Symbol(description) };
|
|
7
7
|
}
|
|
8
|
-
function optional(token, optionalValue) {
|
|
8
|
+
export function optional(token, optionalValue) {
|
|
9
9
|
return {
|
|
10
10
|
symbol: token.symbol,
|
|
11
11
|
isOptional: true,
|
|
@@ -15,7 +15,7 @@ function optional(token, optionalValue) {
|
|
|
15
15
|
/**
|
|
16
16
|
* ResolverError is thrown by the resolver when a token is not found in a container.
|
|
17
17
|
*/
|
|
18
|
-
class ResolverError extends Error {
|
|
18
|
+
export class ResolverError extends Error {
|
|
19
19
|
constructor(message) {
|
|
20
20
|
super(message);
|
|
21
21
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -23,21 +23,21 @@ class ResolverError extends Error {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
/** @internal */
|
|
26
|
-
const CONTAINER = token('ditox.Container');
|
|
26
|
+
export const CONTAINER = token('ditox.Container');
|
|
27
27
|
/** @internal */
|
|
28
|
-
const PARENT_CONTAINER = token('ditox.ParentContainer');
|
|
28
|
+
export const PARENT_CONTAINER = token('ditox.ParentContainer');
|
|
29
29
|
/** @internal */
|
|
30
|
-
const RESOLVER = token('ditox.Resolver');
|
|
30
|
+
export const RESOLVER = token('ditox.Resolver');
|
|
31
31
|
/** @internal */
|
|
32
32
|
const NOT_FOUND = Symbol();
|
|
33
33
|
/** @internal */
|
|
34
|
-
const FAKE_FACTORY = () => {
|
|
34
|
+
export const FAKE_FACTORY = () => {
|
|
35
35
|
throw new Error('FAKE_FACTORY');
|
|
36
36
|
};
|
|
37
37
|
/** @internal */
|
|
38
38
|
const DEFAULT_SCOPE = 'singleton';
|
|
39
39
|
/** @internal */
|
|
40
|
-
const FACTORIES_MAP = token('ditox.FactoriesMap');
|
|
40
|
+
export const FACTORIES_MAP = token('ditox.FactoriesMap');
|
|
41
41
|
/** @internal */
|
|
42
42
|
function getScope(options) {
|
|
43
43
|
var _a;
|
|
@@ -64,7 +64,7 @@ function isInternalToken(token) {
|
|
|
64
64
|
*
|
|
65
65
|
* @param parentContainer - Optional parent container.
|
|
66
66
|
*/
|
|
67
|
-
function createContainer(parentContainer) {
|
|
67
|
+
export function createContainer(parentContainer) {
|
|
68
68
|
const values = new Map();
|
|
69
69
|
const factories = new Map();
|
|
70
70
|
const container = {
|
|
@@ -199,195 +199,4 @@ function createContainer(parentContainer) {
|
|
|
199
199
|
bindInternalTokens();
|
|
200
200
|
return container;
|
|
201
201
|
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Rebinds the array by the token with added new value.
|
|
205
|
-
* @param container - Dependency container.
|
|
206
|
-
* @param token - Token for an array of values.
|
|
207
|
-
* @param value - New value which is added to the end of the array.
|
|
208
|
-
*/
|
|
209
|
-
function bindMultiValue(container, token, value) {
|
|
210
|
-
var _a;
|
|
211
|
-
const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
|
|
212
|
-
const nextValues = [...prevValues, value];
|
|
213
|
-
container.bindValue(token, nextValues);
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Returns an array of resolved values by the specified token.
|
|
217
|
-
* If a token is not found, then `undefined` value is used.
|
|
218
|
-
*/
|
|
219
|
-
function getValues(container, ...tokens) {
|
|
220
|
-
return tokens.map(container.get);
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Returns an array of resolved values by the specified token.
|
|
224
|
-
* If a token is not found, then `ResolverError` is thrown.
|
|
225
|
-
*/
|
|
226
|
-
function resolveValues(container, ...tokens) {
|
|
227
|
-
return tokens.map(container.resolve);
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Decorates a factory by passing resolved tokens as factory arguments.
|
|
231
|
-
* @param factory - A factory.
|
|
232
|
-
* @param tokens - Tokens which correspond to factory arguments.
|
|
233
|
-
* @return Decorated factory which takes a dependency container as a single argument.
|
|
234
|
-
*/
|
|
235
|
-
function injectable(factory, ...tokens) {
|
|
236
|
-
return (container) => {
|
|
237
|
-
const values = resolveValues(container, ...tokens);
|
|
238
|
-
return factory.apply(this, values);
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Returns an object with resolved properties which are specified by token properties.
|
|
243
|
-
* If a token is not found, then `undefined` value is used.
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* ```ts
|
|
247
|
-
* const props = getProps(container, {a: tokenA, b: tokenB});
|
|
248
|
-
* console.log(props); // {a: 1, b: 2}
|
|
249
|
-
* ```
|
|
250
|
-
*/
|
|
251
|
-
function getProps(container, tokens) {
|
|
252
|
-
const obj = { ...tokens };
|
|
253
|
-
Object.keys(obj).forEach((key) => (obj[key] = container.get(obj[key])));
|
|
254
|
-
return obj;
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Returns an object with resolved properties which are specified by token properties.
|
|
258
|
-
* If a token is not found, then `ResolverError` is thrown.
|
|
259
|
-
*
|
|
260
|
-
* @example
|
|
261
|
-
* ```ts
|
|
262
|
-
* const props = resolveProps(container, {a: tokenA, b: tokenB});
|
|
263
|
-
* console.log(props); // {a: 1, b: 2}
|
|
264
|
-
* ```
|
|
265
|
-
*/
|
|
266
|
-
function resolveProps(container, tokens) {
|
|
267
|
-
const obj = { ...tokens };
|
|
268
|
-
Object.keys(obj).forEach((key) => (obj[key] = container.resolve(obj[key])));
|
|
269
|
-
return obj;
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Decorates a factory by passing a resolved object with tokens as the first argument.
|
|
273
|
-
* @param factory - A factory.
|
|
274
|
-
* @param tokens - Object with tokens.
|
|
275
|
-
* @return Decorated factory which takes a dependency container as a single argument.
|
|
276
|
-
*
|
|
277
|
-
* @example
|
|
278
|
-
* ```ts
|
|
279
|
-
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
280
|
-
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
281
|
-
* const result = decoratedFactory(container);
|
|
282
|
-
* ```
|
|
283
|
-
*/
|
|
284
|
-
function injectableProps(factory, tokens) {
|
|
285
|
-
return (container) => {
|
|
286
|
-
const values = resolveProps(container, tokens);
|
|
287
|
-
return factory(values);
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Binds the dependency module to the container
|
|
293
|
-
* @param container - Dependency container.
|
|
294
|
-
* @param moduleDeclaration - Declaration of the dependency module.
|
|
295
|
-
* @param options - Options for module binding.
|
|
296
|
-
*
|
|
297
|
-
* @example
|
|
298
|
-
* ```ts
|
|
299
|
-
* bindModule(container, LOGGER_MODULE);
|
|
300
|
-
* ```
|
|
301
|
-
*/
|
|
302
|
-
function bindModule(container, moduleDeclaration, options) {
|
|
303
|
-
var _a;
|
|
304
|
-
const { token, imports, factory, beforeBinding, afterBinding } = moduleDeclaration;
|
|
305
|
-
const exports = (_a = moduleDeclaration.exports) !== null && _a !== void 0 ? _a : moduleDeclaration.exportedProps;
|
|
306
|
-
const scope = options === null || options === void 0 ? void 0 : options.scope;
|
|
307
|
-
if (beforeBinding) {
|
|
308
|
-
beforeBinding(container);
|
|
309
|
-
}
|
|
310
|
-
if (imports) {
|
|
311
|
-
bindModules(container, imports);
|
|
312
|
-
}
|
|
313
|
-
const exportedValueTokens = new Set();
|
|
314
|
-
if (exports) {
|
|
315
|
-
const keys = Object.keys(exports);
|
|
316
|
-
keys.forEach((valueKey) => {
|
|
317
|
-
const valueToken = exports[valueKey];
|
|
318
|
-
if (valueToken) {
|
|
319
|
-
exportedValueTokens.add(valueToken);
|
|
320
|
-
container.bindFactory(valueToken, injectable((module) => module[valueKey], token), { scope });
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
container.bindFactory(token, factory, {
|
|
325
|
-
scope,
|
|
326
|
-
onRemoved: (module) => {
|
|
327
|
-
if (module.destroy) {
|
|
328
|
-
module.destroy();
|
|
329
|
-
}
|
|
330
|
-
exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
|
|
331
|
-
exportedValueTokens.clear();
|
|
332
|
-
},
|
|
333
|
-
});
|
|
334
|
-
if (afterBinding) {
|
|
335
|
-
afterBinding(container);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Binds dependency modules to the container
|
|
340
|
-
*
|
|
341
|
-
* @param container - Dependency container for binding
|
|
342
|
-
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
343
|
-
*/
|
|
344
|
-
function bindModules(container, modules) {
|
|
345
|
-
modules.forEach((entry) => {
|
|
346
|
-
if ('module' in entry) {
|
|
347
|
-
bindModule(container, entry.module, entry.options);
|
|
348
|
-
}
|
|
349
|
-
else {
|
|
350
|
-
bindModule(container, entry);
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Declares a module binding
|
|
356
|
-
*
|
|
357
|
-
* @param declaration - a module declaration
|
|
358
|
-
* @param declaration.token - optional field
|
|
359
|
-
*
|
|
360
|
-
* @example
|
|
361
|
-
* ```ts
|
|
362
|
-
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
363
|
-
* factory: (container) => {
|
|
364
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
365
|
-
* return {
|
|
366
|
-
* logger: { log: (message) => transport.write(message) },
|
|
367
|
-
* destroy: () => transport.close(),
|
|
368
|
-
* }
|
|
369
|
-
* },
|
|
370
|
-
* exports: {
|
|
371
|
-
* logger: LOGGER_TOKEN,
|
|
372
|
-
* },
|
|
373
|
-
* });
|
|
374
|
-
* ```
|
|
375
|
-
*/
|
|
376
|
-
function declareModule(declaration) {
|
|
377
|
-
var _a;
|
|
378
|
-
return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : token() };
|
|
379
|
-
}
|
|
380
|
-
/**
|
|
381
|
-
* Declares bindings of several modules
|
|
382
|
-
*
|
|
383
|
-
* @param modules - module declaration entries
|
|
384
|
-
*/
|
|
385
|
-
function declareModuleBindings(modules) {
|
|
386
|
-
return declareModule({
|
|
387
|
-
factory: () => ({}),
|
|
388
|
-
imports: modules,
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
export { ResolverError, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, getProps, getValues, injectable, injectableProps, optional, resolveProps, resolveValues, token };
|
|
393
|
-
//# sourceMappingURL=index.js.map
|
|
202
|
+
//# sourceMappingURL=ditox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ditox.js","sourceRoot":"","sources":["../../src/ditox.ts"],"names":[],"mappings":"AA0BA;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAI,WAAoB;IAC3C,OAAO,EAAC,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,EAAC,CAAC;AACvC,CAAC;AAaD,MAAM,UAAU,QAAQ,CACtB,KAAe,EACf,aAAiB;IAEjB,OAAO;QACL,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,UAAU,EAAE,IAAI;QAChB,aAAa;KACd,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,cAAc,CAAC,IAAI,EA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KAAK,CAAC;SACd;QAED,MAAM,cAAc,GAAG,eAAe,aAAf,eAAe,uBAAf,eAAe,CAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;QACtD,IAAI,cAAc,EAAE;YAClB,OAAO,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;SACtC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,SAAS,gBAAgB,CACvB,WAAmB,EACnB,OAA0B;QAE1B,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;QACxC,IAAI,SAAS,EAAE;YACb,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;YACtC,IAAI,KAAK,KAAK,SAAS,IAAI,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;gBAClD,SAAS,CAAC,KAAK,CAAC,CAAC;aAClB;SACF;IACH,CAAC;IAED,SAAS,kBAAkB;QACzB,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QACxC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QACtC,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QAE5C,IAAI,eAAe,EAAE;YACnB,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;SACtD;IACH,CAAC;IAED,kBAAkB,EAAE,CAAC;IACrB,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { token, optional, ResolverError, createContainer } from './ditox';
|
|
2
|
+
export type { RequiredToken, OptionalToken, Token, FactoryScope, FactoryOptions, Container, } from './ditox';
|
|
3
|
+
export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
|
|
4
|
+
export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
|
|
5
|
+
export type { Module, ModuleDeclaration, BindModuleOptions, ModuleBindingEntry, } from './modules';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { token, optional, ResolverError, createContainer } from './ditox';
|
|
2
|
+
export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
|
|
3
|
+
export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAC,MAAM,SAAS,CAAC;AAUxE,OAAO,EACL,OAAO,EACP,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,UAAU,EACV,eAAe,GAChB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,UAAU,EACV,WAAW,EACX,aAAa,EACb,qBAAqB,GACtB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Container, Token } from './ditox';
|
|
2
|
+
declare type AnyObject = Record<string, any>;
|
|
3
|
+
declare type EmptyObject = Record<string, never>;
|
|
4
|
+
declare type ModuleController = {
|
|
5
|
+
/** Dispose the module and clean its resources */
|
|
6
|
+
destroy?: () => void;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Dependency module
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* type LoggerModule = Module<{
|
|
14
|
+
* logger: Logger;
|
|
15
|
+
* }>;
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
|
|
19
|
+
declare type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
|
|
20
|
+
/**
|
|
21
|
+
* Description how to bind the module in declarative way.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
26
|
+
* token: LOGGER_MODULE_TOKEN,
|
|
27
|
+
* factory: (container) => {
|
|
28
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
29
|
+
* return {
|
|
30
|
+
* logger: { log: (message) => transport.write(message) },
|
|
31
|
+
* destroy: () => transport.close(),
|
|
32
|
+
* }
|
|
33
|
+
* },
|
|
34
|
+
* exports: {
|
|
35
|
+
* logger: LOGGER_TOKEN,
|
|
36
|
+
* },
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
41
|
+
/** Token for the module */
|
|
42
|
+
token: Token<T>;
|
|
43
|
+
/** Modules for binding */
|
|
44
|
+
imports?: ReadonlyArray<ModuleBindingEntry>;
|
|
45
|
+
/** Factory of the module */
|
|
46
|
+
factory: (container: Container) => T;
|
|
47
|
+
/** Dictionary of module properties which are bound to tokens. */
|
|
48
|
+
exports?: {
|
|
49
|
+
[K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
|
|
50
|
+
};
|
|
51
|
+
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
52
|
+
beforeBinding?: (container: Container) => void;
|
|
53
|
+
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
54
|
+
afterBinding?: (container: Container) => void;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Options for module binding.
|
|
58
|
+
*
|
|
59
|
+
* `scope` types:
|
|
60
|
+
* - `singleton` - **This is the default**. The module is created and cached by the container which registered the factory.
|
|
61
|
+
* - `scoped` - The module is created and cached by the container which starts resolving.
|
|
62
|
+
*/
|
|
63
|
+
export declare type BindModuleOptions = {
|
|
64
|
+
scope?: 'scoped' | 'singleton';
|
|
65
|
+
};
|
|
66
|
+
export declare type ModuleBindingEntry = ModuleDeclaration<AnyObject> | {
|
|
67
|
+
module: ModuleDeclaration<AnyObject>;
|
|
68
|
+
options: BindModuleOptions;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Binds the dependency module to the container
|
|
72
|
+
* @param container - Dependency container.
|
|
73
|
+
* @param moduleDeclaration - Declaration of the dependency module.
|
|
74
|
+
* @param options - Options for module binding.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* bindModule(container, LOGGER_MODULE);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
82
|
+
/**
|
|
83
|
+
* Binds dependency modules to the container
|
|
84
|
+
*
|
|
85
|
+
* @param container - Dependency container for binding
|
|
86
|
+
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
87
|
+
*/
|
|
88
|
+
export declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
89
|
+
/**
|
|
90
|
+
* Declares a module binding
|
|
91
|
+
*
|
|
92
|
+
* @param declaration - a module declaration
|
|
93
|
+
* @param declaration.token - optional field
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
98
|
+
* factory: (container) => {
|
|
99
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
100
|
+
* return {
|
|
101
|
+
* logger: { log: (message) => transport.write(message) },
|
|
102
|
+
* destroy: () => transport.close(),
|
|
103
|
+
* }
|
|
104
|
+
* },
|
|
105
|
+
* exports: {
|
|
106
|
+
* logger: LOGGER_TOKEN,
|
|
107
|
+
* },
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare function declareModule<T>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
112
|
+
/**
|
|
113
|
+
* Declares bindings of several modules
|
|
114
|
+
*
|
|
115
|
+
* @param modules - module declaration entries
|
|
116
|
+
*/
|
|
117
|
+
export declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
118
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { token } from './ditox';
|
|
2
|
+
import { injectable } from './utils';
|
|
3
|
+
/**
|
|
4
|
+
* Binds the dependency module to the container
|
|
5
|
+
* @param container - Dependency container.
|
|
6
|
+
* @param moduleDeclaration - Declaration of the dependency module.
|
|
7
|
+
* @param options - Options for module binding.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* bindModule(container, LOGGER_MODULE);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function bindModule(container, moduleDeclaration, options) {
|
|
15
|
+
const { token, imports, factory, beforeBinding, afterBinding } = moduleDeclaration;
|
|
16
|
+
const exports = moduleDeclaration.exports;
|
|
17
|
+
const scope = options === null || options === void 0 ? void 0 : options.scope;
|
|
18
|
+
if (beforeBinding) {
|
|
19
|
+
beforeBinding(container);
|
|
20
|
+
}
|
|
21
|
+
if (imports) {
|
|
22
|
+
bindModules(container, imports);
|
|
23
|
+
}
|
|
24
|
+
const exportedValueTokens = new Set();
|
|
25
|
+
if (exports) {
|
|
26
|
+
const keys = Object.keys(exports);
|
|
27
|
+
keys.forEach((valueKey) => {
|
|
28
|
+
const valueToken = exports[valueKey];
|
|
29
|
+
if (valueToken) {
|
|
30
|
+
exportedValueTokens.add(valueToken);
|
|
31
|
+
container.bindFactory(valueToken, injectable((module) => module[valueKey], token), { scope });
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
container.bindFactory(token, factory, {
|
|
36
|
+
scope,
|
|
37
|
+
onRemoved: (module) => {
|
|
38
|
+
if (module.destroy) {
|
|
39
|
+
module.destroy();
|
|
40
|
+
}
|
|
41
|
+
exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
|
|
42
|
+
exportedValueTokens.clear();
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
if (afterBinding) {
|
|
46
|
+
afterBinding(container);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Binds dependency modules to the container
|
|
51
|
+
*
|
|
52
|
+
* @param container - Dependency container for binding
|
|
53
|
+
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
54
|
+
*/
|
|
55
|
+
export function bindModules(container, modules) {
|
|
56
|
+
modules.forEach((entry) => {
|
|
57
|
+
if ('module' in entry) {
|
|
58
|
+
bindModule(container, entry.module, entry.options);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
bindModule(container, entry);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Declares a module binding
|
|
67
|
+
*
|
|
68
|
+
* @param declaration - a module declaration
|
|
69
|
+
* @param declaration.token - optional field
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
74
|
+
* factory: (container) => {
|
|
75
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
76
|
+
* return {
|
|
77
|
+
* logger: { log: (message) => transport.write(message) },
|
|
78
|
+
* destroy: () => transport.close(),
|
|
79
|
+
* }
|
|
80
|
+
* },
|
|
81
|
+
* exports: {
|
|
82
|
+
* logger: LOGGER_TOKEN,
|
|
83
|
+
* },
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export function declareModule(declaration) {
|
|
88
|
+
var _a;
|
|
89
|
+
return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : token() };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Declares bindings of several modules
|
|
93
|
+
*
|
|
94
|
+
* @param modules - module declaration entries
|
|
95
|
+
*/
|
|
96
|
+
export function declareModuleBindings(modules) {
|
|
97
|
+
return declareModule({
|
|
98
|
+
factory: () => ({}),
|
|
99
|
+
imports: modules,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=modules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modules.js","sourceRoot":"","sources":["../../src/modules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,EAAQ,MAAM,SAAS,CAAC;AAChD,OAAO,EAAC,UAAU,EAAC,MAAM,SAAS,CAAC;AAqFnC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CACxB,SAAoB,EACpB,iBAAuC,EACvC,OAA2B;IAE3B,MAAM,EAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAC,GAC1D,iBAAiB,CAAC;IACpB,MAAM,OAAO,GAAG,iBAAiB,CAAC,OAAO,CAAC;IAE1C,MAAM,KAAK,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,CAAC;IAE7B,IAAI,aAAa,EAAE;QACjB,aAAa,CAAC,SAAS,CAAC,CAAC;KAC1B;IAED,IAAI,OAAO,EAAE;QACX,WAAW,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;KACjC;IAED,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAkB,CAAC;IAEtD,IAAI,OAAO,EAAE;QACX,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAElC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YACxB,MAAM,UAAU,GAAG,OAAO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