ditox 1.3.0 → 2.0.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 +5 -5
- package/dist/{index.d.ts → browser/index.d.ts} +18 -10
- 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 +22 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/modules.d.ts +124 -0
- package/dist/cjs/modules.js +110 -0
- package/dist/cjs/modules.js.map +1 -0
- package/{src/utils.js.flow → dist/cjs/utils.d.ts} +21 -60
- package/dist/cjs/utils.js +98 -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 -196
- 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 +124 -0
- package/dist/esm/modules.js +103 -0
- package/dist/esm/modules.js.map +1 -0
- package/dist/esm/utils.d.ts +74 -0
- package/dist/esm/utils.js +88 -0
- package/dist/esm/utils.js.map +1 -0
- package/{lib → dist/umd}/index.d.ts +18 -10
- package/dist/umd/index.js +2 -0
- package/dist/umd/index.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +113 -96
- package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
- package/package.json +16 -17
- package/src/ditox.test.ts +10 -1
- package/src/index.ts +8 -9
- package/src/modules.test.ts +79 -5
- package/src/modules.ts +33 -17
- package/src/utils.ts +4 -4
- package/CHANGELOG.md +0 -99
- 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 -406
- package/dist/index.js.flow +0 -4
- package/dist/index.js.map +0 -1
- package/dist/index.module.d.ts +0 -293
- package/dist/index.module.js +0 -388
- package/dist/index.module.js.flow +0 -4
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.d.ts +0 -293
- 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 -146
|
@@ -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,190 +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
|
-
const { token, factory, exportedProps, beforeBinding, afterBinding } = moduleDeclaration;
|
|
304
|
-
const scope = options === null || options === void 0 ? void 0 : options.scope;
|
|
305
|
-
if (beforeBinding) {
|
|
306
|
-
beforeBinding(container);
|
|
307
|
-
}
|
|
308
|
-
const exportedValueTokens = new Set();
|
|
309
|
-
if (exportedProps) {
|
|
310
|
-
const keys = Object.keys(exportedProps);
|
|
311
|
-
keys.forEach((valueKey) => {
|
|
312
|
-
const valueToken = exportedProps[valueKey];
|
|
313
|
-
if (valueToken) {
|
|
314
|
-
exportedValueTokens.add(valueToken);
|
|
315
|
-
container.bindFactory(valueToken, injectable((module) => module[valueKey], token), { scope });
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
container.bindFactory(token, factory, {
|
|
320
|
-
scope,
|
|
321
|
-
onRemoved: (module) => {
|
|
322
|
-
if (module.destroy) {
|
|
323
|
-
module.destroy();
|
|
324
|
-
}
|
|
325
|
-
exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
|
|
326
|
-
exportedValueTokens.clear();
|
|
327
|
-
},
|
|
328
|
-
});
|
|
329
|
-
if (afterBinding) {
|
|
330
|
-
afterBinding(container);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Binds dependency modules to the container
|
|
335
|
-
*
|
|
336
|
-
* @param container - Dependency container for binding
|
|
337
|
-
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
338
|
-
*/
|
|
339
|
-
function bindModules(container, modules) {
|
|
340
|
-
modules.forEach((entry) => {
|
|
341
|
-
if ('module' in entry) {
|
|
342
|
-
bindModule(container, entry.module, entry.options);
|
|
343
|
-
}
|
|
344
|
-
else {
|
|
345
|
-
bindModule(container, entry);
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Declares a module binding
|
|
351
|
-
*
|
|
352
|
-
* @param declaration - a module declaration
|
|
353
|
-
* @param declaration.token - optional field
|
|
354
|
-
*
|
|
355
|
-
* @example
|
|
356
|
-
* ```ts
|
|
357
|
-
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
358
|
-
* factory: (container) => {
|
|
359
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
360
|
-
* return {
|
|
361
|
-
* logger: { log: (message) => transport.write(message) },
|
|
362
|
-
* destroy: () => transport.close(),
|
|
363
|
-
* }
|
|
364
|
-
* },
|
|
365
|
-
* exportedProps: {
|
|
366
|
-
* logger: LOGGER_TOKEN,
|
|
367
|
-
* },
|
|
368
|
-
* });
|
|
369
|
-
* ```
|
|
370
|
-
*/
|
|
371
|
-
function declareModule(declaration) {
|
|
372
|
-
var _a;
|
|
373
|
-
return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : token() };
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Declares bindings of several modules
|
|
377
|
-
*
|
|
378
|
-
* @param modules - module declaration entries
|
|
379
|
-
*/
|
|
380
|
-
function declareModuleBindings(modules) {
|
|
381
|
-
return declareModule({
|
|
382
|
-
factory: () => ({}),
|
|
383
|
-
beforeBinding: (container) => bindModules(container, modules),
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
export { ResolverError, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, getProps, getValues, injectable, injectableProps, optional, resolveProps, resolveValues, token };
|
|
388
|
-
//# 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,EAAE,GAAG,CAAC,MAAM,CAAC,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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 { bindMultiValue, getValues, resolveValues, injectable, getProps, resolveProps, injectableProps, } 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 { bindMultiValue, getValues, resolveValues, injectable, getProps, resolveProps, injectableProps, } 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,cAAc,EACd,SAAS,EACT,aAAa,EACb,UAAU,EACV,QAAQ,EACR,YAAY,EACZ,eAAe,GAChB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,UAAU,EACV,WAAW,EACX,aAAa,EACb,qBAAqB,GACtB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
/**
|
|
52
|
+
* Dictionary of module properties which are bound to tokens.
|
|
53
|
+
*
|
|
54
|
+
* @deprecated Use `exports` property.
|
|
55
|
+
*/
|
|
56
|
+
exportedProps?: ModuleDeclaration<T>['exports'];
|
|
57
|
+
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
58
|
+
beforeBinding?: (container: Container) => void;
|
|
59
|
+
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
60
|
+
afterBinding?: (container: Container) => void;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Options for module binding.
|
|
64
|
+
*
|
|
65
|
+
* `scope` types:
|
|
66
|
+
* - `singleton` - **This is the default**. The module is created and cached by the container which registered the factory.
|
|
67
|
+
* - `scoped` - The module is created and cached by the container which starts resolving.
|
|
68
|
+
*/
|
|
69
|
+
export declare type BindModuleOptions = {
|
|
70
|
+
scope?: 'scoped' | 'singleton';
|
|
71
|
+
};
|
|
72
|
+
export declare type ModuleBindingEntry = ModuleDeclaration<AnyObject> | {
|
|
73
|
+
module: ModuleDeclaration<AnyObject>;
|
|
74
|
+
options: BindModuleOptions;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Binds the dependency module to the container
|
|
78
|
+
* @param container - Dependency container.
|
|
79
|
+
* @param moduleDeclaration - Declaration of the dependency module.
|
|
80
|
+
* @param options - Options for module binding.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* bindModule(container, LOGGER_MODULE);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
88
|
+
/**
|
|
89
|
+
* Binds dependency modules to the container
|
|
90
|
+
*
|
|
91
|
+
* @param container - Dependency container for binding
|
|
92
|
+
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
93
|
+
*/
|
|
94
|
+
export declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
95
|
+
/**
|
|
96
|
+
* Declares a module binding
|
|
97
|
+
*
|
|
98
|
+
* @param declaration - a module declaration
|
|
99
|
+
* @param declaration.token - optional field
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
104
|
+
* factory: (container) => {
|
|
105
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
106
|
+
* return {
|
|
107
|
+
* logger: { log: (message) => transport.write(message) },
|
|
108
|
+
* destroy: () => transport.close(),
|
|
109
|
+
* }
|
|
110
|
+
* },
|
|
111
|
+
* exports: {
|
|
112
|
+
* logger: LOGGER_TOKEN,
|
|
113
|
+
* },
|
|
114
|
+
* });
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare function declareModule<T>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
118
|
+
/**
|
|
119
|
+
* Declares bindings of several modules
|
|
120
|
+
*
|
|
121
|
+
* @param modules - module declaration entries
|
|
122
|
+
*/
|
|
123
|
+
export declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
124
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
var _a;
|
|
16
|
+
const { token, imports, factory, beforeBinding, afterBinding } = moduleDeclaration;
|
|
17
|
+
const exports = (_a = moduleDeclaration.exports) !== null && _a !== void 0 ? _a : moduleDeclaration.exportedProps;
|
|
18
|
+
const scope = options === null || options === void 0 ? void 0 : options.scope;
|
|
19
|
+
if (beforeBinding) {
|
|
20
|
+
beforeBinding(container);
|
|
21
|
+
}
|
|
22
|
+
if (imports) {
|
|
23
|
+
bindModules(container, imports);
|
|
24
|
+
}
|
|
25
|
+
const exportedValueTokens = new Set();
|
|
26
|
+
if (exports) {
|
|
27
|
+
const keys = Object.keys(exports);
|
|
28
|
+
keys.forEach((valueKey) => {
|
|
29
|
+
const valueToken = exports[valueKey];
|
|
30
|
+
if (valueToken) {
|
|
31
|
+
exportedValueTokens.add(valueToken);
|
|
32
|
+
container.bindFactory(valueToken, injectable((module) => module[valueKey], token), { scope });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
container.bindFactory(token, factory, {
|
|
37
|
+
scope,
|
|
38
|
+
onRemoved: (module) => {
|
|
39
|
+
if (module.destroy) {
|
|
40
|
+
module.destroy();
|
|
41
|
+
}
|
|
42
|
+
exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
|
|
43
|
+
exportedValueTokens.clear();
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
if (afterBinding) {
|
|
47
|
+
afterBinding(container);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Binds dependency modules to the container
|
|
52
|
+
*
|
|
53
|
+
* @param container - Dependency container for binding
|
|
54
|
+
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
55
|
+
*/
|
|
56
|
+
export function bindModules(container, modules) {
|
|
57
|
+
modules.forEach((entry) => {
|
|
58
|
+
if ('module' in entry) {
|
|
59
|
+
bindModule(container, entry.module, entry.options);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
bindModule(container, entry);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Declares a module binding
|
|
68
|
+
*
|
|
69
|
+
* @param declaration - a module declaration
|
|
70
|
+
* @param declaration.token - optional field
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
75
|
+
* factory: (container) => {
|
|
76
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
77
|
+
* return {
|
|
78
|
+
* logger: { log: (message) => transport.write(message) },
|
|
79
|
+
* destroy: () => transport.close(),
|
|
80
|
+
* }
|
|
81
|
+
* },
|
|
82
|
+
* exports: {
|
|
83
|
+
* logger: LOGGER_TOKEN,
|
|
84
|
+
* },
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export function declareModule(declaration) {
|
|
89
|
+
var _a;
|
|
90
|
+
return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : token() };
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Declares bindings of several modules
|
|
94
|
+
*
|
|
95
|
+
* @param modules - module declaration entries
|
|
96
|
+
*/
|
|
97
|
+
export function declareModuleBindings(modules) {
|
|
98
|
+
return declareModule({
|
|
99
|
+
factory: () => ({}),
|
|
100
|
+
imports: modules,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
//# 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;AA4FnC;;;;;;;;;;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,MAAA,iBAAiB,CAAC,OAAO,mCAAI,iBAAiB,CAAC,aAAa,CAAC;IAE7E,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,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H,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,aAAa,CAC3B,WAC8C;;IAE9C,OAAO,EAAC,GAAG,WAAW,EAAE,KAAK,EAAE,MAAA,WAAW,CAAC,KAAK,mCAAI,KAAK,EAAE,EAAC,CAAC;AAC/D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CACnC,OAA0C;IAE1C,OAAO,aAAa,CAAC;QACnB,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;QACnB,OAAO,EAAE,OAAO;KACjB,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Container, Token } from './ditox';
|
|
2
|
+
declare type ValuesProps = {
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
};
|
|
5
|
+
declare type TokenProps<Props extends ValuesProps> = {
|
|
6
|
+
[K in keyof Props]: Token<Props[K]>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Rebinds the array by the token with added new value.
|
|
10
|
+
* @param container - Dependency container.
|
|
11
|
+
* @param token - Token for an array of values.
|
|
12
|
+
* @param value - New value which is added to the end of the array.
|
|
13
|
+
*/
|
|
14
|
+
export declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyArray<T>>, value: T): void;
|
|
15
|
+
/**
|
|
16
|
+
* Returns an array of resolved values by the specified token.
|
|
17
|
+
* If a token is not found, then `undefined` value is used.
|
|
18
|
+
*/
|
|
19
|
+
export declare function getValues<Tokens extends Token<unknown>[], Values extends {
|
|
20
|
+
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
|
|
21
|
+
}>(container: Container, ...tokens: Tokens): Values;
|
|
22
|
+
/**
|
|
23
|
+
* Returns an array of resolved values by the specified token.
|
|
24
|
+
* If a token is not found, then `ResolverError` is thrown.
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveValues<Tokens extends Token<unknown>[], Values extends {
|
|
27
|
+
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
|
|
28
|
+
}>(container: Container, ...tokens: Tokens): Values;
|
|
29
|
+
/**
|
|
30
|
+
* Decorates a factory by passing resolved tokens as factory arguments.
|
|
31
|
+
* @param factory - A factory.
|
|
32
|
+
* @param tokens - Tokens which correspond to factory arguments.
|
|
33
|
+
* @return Decorated factory which takes a dependency container as a single argument.
|
|
34
|
+
*/
|
|
35
|
+
export declare function injectable<Tokens extends Token<unknown>[], Values extends {
|
|
36
|
+
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
|
|
37
|
+
}, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
|
|
38
|
+
/**
|
|
39
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
40
|
+
* If a token is not found, then `undefined` value is used.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const props = getProps(container, {a: tokenA, b: tokenB});
|
|
45
|
+
* console.log(props); // {a: 1, b: 2}
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function getProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Partial<Props>;
|
|
49
|
+
/**
|
|
50
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
51
|
+
* If a token is not found, then `ResolverError` is thrown.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const props = resolveProps(container, {a: tokenA, b: tokenB});
|
|
56
|
+
* console.log(props); // {a: 1, b: 2}
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function resolveProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Props;
|
|
60
|
+
/**
|
|
61
|
+
* Decorates a factory by passing a resolved object with tokens as the first argument.
|
|
62
|
+
* @param factory - A factory.
|
|
63
|
+
* @param tokens - Object with tokens.
|
|
64
|
+
* @return Decorated factory which takes a dependency container as a single argument.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
69
|
+
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
70
|
+
* const result = decoratedFactory(container);
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function injectableProps<Props extends ValuesProps, Result>(factory: (props: Props) => Result, tokens: TokenProps<Props>): (container: Container) => Result;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rebinds the array by the token with added new value.
|
|
3
|
+
* @param container - Dependency container.
|
|
4
|
+
* @param token - Token for an array of values.
|
|
5
|
+
* @param value - New value which is added to the end of the array.
|
|
6
|
+
*/
|
|
7
|
+
export function bindMultiValue(container, token, value) {
|
|
8
|
+
var _a;
|
|
9
|
+
const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
|
|
10
|
+
const nextValues = [...prevValues, value];
|
|
11
|
+
container.bindValue(token, nextValues);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Returns an array of resolved values by the specified token.
|
|
15
|
+
* If a token is not found, then `undefined` value is used.
|
|
16
|
+
*/
|
|
17
|
+
export function getValues(container, ...tokens) {
|
|
18
|
+
return tokens.map(container.get);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns an array of resolved values by the specified token.
|
|
22
|
+
* If a token is not found, then `ResolverError` is thrown.
|
|
23
|
+
*/
|
|
24
|
+
export function resolveValues(container, ...tokens) {
|
|
25
|
+
return tokens.map(container.resolve);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Decorates a factory by passing resolved tokens as factory arguments.
|
|
29
|
+
* @param factory - A factory.
|
|
30
|
+
* @param tokens - Tokens which correspond to factory arguments.
|
|
31
|
+
* @return Decorated factory which takes a dependency container as a single argument.
|
|
32
|
+
*/
|
|
33
|
+
export function injectable(factory, ...tokens) {
|
|
34
|
+
return (container) => {
|
|
35
|
+
const values = resolveValues(container, ...tokens);
|
|
36
|
+
return factory.apply(this, values);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
41
|
+
* If a token is not found, then `undefined` value is used.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const props = getProps(container, {a: tokenA, b: tokenB});
|
|
46
|
+
* console.log(props); // {a: 1, b: 2}
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function getProps(container, tokens) {
|
|
50
|
+
const obj = { ...tokens };
|
|
51
|
+
Object.keys(obj).forEach((key) => (obj[key] = container.get(obj[key])));
|
|
52
|
+
return obj;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
56
|
+
* If a token is not found, then `ResolverError` is thrown.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const props = resolveProps(container, {a: tokenA, b: tokenB});
|
|
61
|
+
* console.log(props); // {a: 1, b: 2}
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export function resolveProps(container, tokens) {
|
|
65
|
+
const obj = { ...tokens };
|
|
66
|
+
Object.keys(obj).forEach((key) => (obj[key] = container.resolve(obj[key])));
|
|
67
|
+
return obj;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Decorates a factory by passing a resolved object with tokens as the first argument.
|
|
71
|
+
* @param factory - A factory.
|
|
72
|
+
* @param tokens - Object with tokens.
|
|
73
|
+
* @return Decorated factory which takes a dependency container as a single argument.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
78
|
+
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
79
|
+
* const result = decoratedFactory(container);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function injectableProps(factory, tokens) {
|
|
83
|
+
return (container) => {
|
|
84
|
+
const values = resolveProps(container, tokens);
|
|
85
|
+
return factory(values);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAOA;;;;;GAKG;AACH,MAAM,UAAU,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;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAKvB,SAAoB,EAAE,GAAG,MAAc;IACvC,OAAO,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAW,CAAC;AAC7C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAK3B,SAAoB,EAAE,GAAG,MAAc;IACvC,OAAO,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,OAAO,CAAW,CAAC;AACjD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAQxB,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;AAED;;;;;;;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