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.
Files changed (53) hide show
  1. package/README.md +135 -32
  2. package/dist/index.cjs +518 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/{umd/index.d.ts → index.d.cts} +195 -197
  5. package/dist/{browser/index.d.ts → index.d.ts} +195 -197
  6. package/dist/index.js +501 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/index.umd.js +524 -0
  9. package/dist/index.umd.js.map +1 -0
  10. package/package.json +22 -19
  11. package/src/container.test.ts +34 -33
  12. package/src/container.ts +12 -6
  13. package/src/index.ts +26 -23
  14. package/src/modules.test.ts +172 -37
  15. package/src/modules.ts +22 -5
  16. package/src/tokens.test.ts +9 -8
  17. package/src/tokens.ts +2 -2
  18. package/src/utils.test.ts +18 -17
  19. package/src/utils.ts +21 -21
  20. package/dist/browser/index.js +0 -2
  21. package/dist/browser/index.js.map +0 -1
  22. package/dist/cjs/container.d.ts +0 -88
  23. package/dist/cjs/container.js +0 -216
  24. package/dist/cjs/container.js.map +0 -1
  25. package/dist/cjs/index.d.ts +0 -7
  26. package/dist/cjs/index.js +0 -24
  27. package/dist/cjs/index.js.map +0 -1
  28. package/dist/cjs/modules.d.ts +0 -121
  29. package/dist/cjs/modules.js +0 -143
  30. package/dist/cjs/modules.js.map +0 -1
  31. package/dist/cjs/tokens.d.ts +0 -56
  32. package/dist/cjs/tokens.js +0 -20
  33. package/dist/cjs/tokens.js.map +0 -1
  34. package/dist/cjs/utils.d.ts +0 -137
  35. package/dist/cjs/utils.js +0 -156
  36. package/dist/cjs/utils.js.map +0 -1
  37. package/dist/esm/container.d.ts +0 -88
  38. package/dist/esm/container.js +0 -211
  39. package/dist/esm/container.js.map +0 -1
  40. package/dist/esm/index.d.ts +0 -7
  41. package/dist/esm/index.js +0 -5
  42. package/dist/esm/index.js.map +0 -1
  43. package/dist/esm/modules.d.ts +0 -121
  44. package/dist/esm/modules.js +0 -136
  45. package/dist/esm/modules.js.map +0 -1
  46. package/dist/esm/tokens.d.ts +0 -56
  47. package/dist/esm/tokens.js +0 -15
  48. package/dist/esm/tokens.js.map +0 -1
  49. package/dist/esm/utils.d.ts +0 -137
  50. package/dist/esm/utils.js +0 -145
  51. package/dist/esm/utils.js.map +0 -1
  52. package/dist/umd/index.js +0 -2
  53. package/dist/umd/index.js.map +0 -1
@@ -1,136 +0,0 @@
1
- import { token } from './tokens';
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, _b, _c;
16
- const rootEntry = {
17
- module: moduleDeclaration,
18
- options: options !== null && options !== void 0 ? options : {},
19
- };
20
- const bfsVisits = new Set([rootEntry]);
21
- const bfsQueue = [rootEntry];
22
- let bfsIndex = 0;
23
- while (bfsIndex < bfsQueue.length) {
24
- const entry = bfsQueue[bfsIndex];
25
- const m = 'module' in entry ? entry.module : entry;
26
- (_a = m.imports) === null || _a === void 0 ? void 0 : _a.forEach((depEntry) => {
27
- if (!bfsVisits.has(depEntry)) {
28
- bfsVisits.add(depEntry);
29
- bfsQueue.push(depEntry);
30
- }
31
- });
32
- bfsIndex++;
33
- }
34
- for (let i = 0; i < bfsQueue.length; i++) {
35
- const entry = bfsQueue[i];
36
- const m = 'module' in entry ? entry.module : entry;
37
- (_b = m.beforeBinding) === null || _b === void 0 ? void 0 : _b.call(m, container);
38
- }
39
- for (let i = 0; i < bfsQueue.length; i++) {
40
- const entry = bfsQueue[i];
41
- bindModuleEntry(container, entry);
42
- }
43
- for (let i = bfsQueue.length - 1; i >= 0; i--) {
44
- const entry = bfsQueue[i];
45
- const m = 'module' in entry ? entry.module : entry;
46
- (_c = m.afterBinding) === null || _c === void 0 ? void 0 : _c.call(m, container);
47
- }
48
- }
49
- function bindModuleEntry(container, entry) {
50
- let module;
51
- let options;
52
- if ('module' in entry) {
53
- module = entry.module;
54
- options = entry.options;
55
- }
56
- else {
57
- module = entry;
58
- }
59
- const scope = options === null || options === void 0 ? void 0 : options.scope;
60
- const exportedValueTokens = new Set();
61
- const moduleExports = module.exports;
62
- if (moduleExports) {
63
- const keys = Object.keys(moduleExports);
64
- keys.forEach((valueKey) => {
65
- const valueToken = moduleExports[valueKey];
66
- if (valueToken) {
67
- exportedValueTokens.add(valueToken);
68
- container.bindFactory(valueToken, injectable((module) => module[valueKey], module.token), { scope });
69
- }
70
- });
71
- }
72
- container.bindFactory(module.token, module.factory, {
73
- scope,
74
- onRemoved: (moduleInstance) => {
75
- if (moduleInstance.destroy) {
76
- moduleInstance.destroy();
77
- }
78
- exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
79
- exportedValueTokens.clear();
80
- },
81
- });
82
- }
83
- /**
84
- * Binds dependency modules to the container
85
- *
86
- * @param container - Dependency container for binding
87
- * @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
88
- */
89
- export function bindModules(container, modules) {
90
- modules.forEach((entry) => {
91
- if ('module' in entry) {
92
- bindModule(container, entry.module, entry.options);
93
- }
94
- else {
95
- bindModule(container, entry);
96
- }
97
- });
98
- }
99
- /**
100
- * Declares a module binding
101
- *
102
- * @param declaration - a module declaration
103
- * @param declaration.token - optional field
104
- *
105
- * @example
106
- * ```ts
107
- * const LOGGER_MODULE = declareModule<LoggerModule>({
108
- * factory: (container) => {
109
- * const transport = container.resolve(TRANSPORT_TOKEN).open();
110
- * return {
111
- * logger: { log: (message) => transport.write(message) },
112
- * destroy: () => transport.close(),
113
- * }
114
- * },
115
- * exports: {
116
- * logger: LOGGER_TOKEN,
117
- * },
118
- * });
119
- * ```
120
- */
121
- export function declareModule(declaration) {
122
- var _a;
123
- return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : token() };
124
- }
125
- /**
126
- * Declares bindings of several modules
127
- *
128
- * @param modules - module declaration entries
129
- */
130
- export function declareModuleBindings(modules) {
131
- return declareModule({
132
- factory: () => ({}),
133
- imports: modules,
134
- });
135
- }
136
- //# sourceMappingURL=modules.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"modules.js","sourceRoot":"","sources":["../../src/modules.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,KAAK,EAAC,MAAM,UAAU,CAAC;AACtC,OAAO,EAAC,UAAU,EAAC,MAAM,SAAS,CAAC;AAyFnC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CACxB,SAAoB,EACpB,iBAAuC,EACvC,OAA2B;;IAE3B,MAAM,SAAS,GAAuB;QACpC,MAAM,EAAE,iBAAiB;QACzB,OAAO,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE;KACvB,CAAC;IAEF,MAAM,SAAS,GAAG,IAAI,GAAG,CAAqB,CAAC,SAAS,CAAC,CAAC,CAAC;IAC3D,MAAM,QAAQ,GAAyB,CAAC,SAAS,CAAC,CAAC;IAEnD,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,OAAO,QAAQ,GAAG,QAAQ,CAAC,MAAM,EAAE;QACjC,MAAM,KAAK,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAEjC,MAAM,CAAC,GAAG,QAAQ,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;QAEnD,MAAA,CAAC,CAAC,OAAO,0CAAE,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YAC9B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;gBAC5B,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBACxB,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;aACzB;QACH,CAAC,CAAC,C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AC,OAAO,CAAC,CAAC;SACpD;aAAM;YACL,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;SAC9B;IACH,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"}
@@ -1,56 +0,0 @@
1
- /**
2
- * @ignore
3
- * Binding token for mandatory value
4
- */
5
- export 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 type OptionalToken<T> = {
15
- symbol: symbol;
16
- type?: T;
17
- isOptional: true;
18
- optionalValue: T;
19
- };
20
- /**
21
- * Binding token
22
- */
23
- export type Token<T> = RequiredToken<T> | OptionalToken<T>;
24
- /**
25
- * Token options
26
- */
27
- export type TokenOptions = {
28
- /**
29
- * Key for token's symbol. It allows to create shareable tokens.
30
- */
31
- key: string;
32
- /** @ignore */
33
- description?: undefined;
34
- } | {
35
- /** Description for better error messages */
36
- description?: string;
37
- /** @ignore */
38
- key?: undefined;
39
- };
40
- /**
41
- * Creates a new binding token.
42
- * @param description - Token description for better error messages.
43
- */
44
- export declare function token<T>(description?: string): Token<T>;
45
- /**
46
- * Creates a new binding token.
47
- * @param options - Token description for better error messages.
48
- */ export declare function token<T>(options?: TokenOptions): Token<T>;
49
- /**
50
- * Decorate a token with an optional value.
51
- * This value is be used as default value in case a container does not have registered token.
52
- * @param token - Existed token.
53
- * @param optionalValue - Default value for the resolver.
54
- */
55
- export declare function optional<T>(token: Token<T>, optionalValue: T): OptionalToken<T>;
56
- export declare function optional<T>(token: Token<T>): OptionalToken<T | undefined>;
@@ -1,15 +0,0 @@
1
- export function token(options) {
2
- const normalized = typeof options === 'string' ? { description: options } : options;
3
- const symbol = (normalized === null || normalized === void 0 ? void 0 : normalized.key)
4
- ? Symbol.for(normalized.key)
5
- : Symbol(normalized === null || normalized === void 0 ? void 0 : normalized.description);
6
- return { symbol };
7
- }
8
- export function optional(token, optionalValue) {
9
- return {
10
- symbol: token.symbol,
11
- isOptional: true,
12
- optionalValue,
13
- };
14
- }
15
- //# sourceMappingURL=tokens.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../src/tokens.ts"],"names":[],"mappings":"AAwDA,MAAM,UAAU,KAAK,CAAI,OAA+B;IACtD,MAAM,UAAU,GACd,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAC,WAAW,EAAE,OAAO,EAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAEjE,MAAM,MAAM,GAAW,CAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,GAAG;QACpC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC;QAC5B,CAAC,CAAC,MAAM,CAAC,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,WAAW,CAAC,CAAC;IAEpC,OAAO,EAAC,MAAM,EAAC,CAAC;AAClB,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"}
@@ -1,137 +0,0 @@
1
- import type { Container } from './container';
2
- import { Token } from './tokens';
3
- type ValuesProps = {
4
- [key: string]: unknown;
5
- };
6
- type TokenProps<Props extends ValuesProps> = {
7
- [K in keyof Props]: Token<Props[K]>;
8
- };
9
- /**
10
- * Checks if a value is the token
11
- */
12
- export declare function isToken<T>(value: unknown): value is Token<T>;
13
- /**
14
- * Rebinds the array by the token with added new value.
15
- * @param container - Dependency container.
16
- * @param token - Token for an array of values.
17
- * @param value - New value which is added to the end of the array.
18
- */
19
- export declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyArray<T>>, value: T): void;
20
- /**
21
- * Tries to resolve a value by the provided token.
22
- *
23
- * If an argument is an object which has tokens as its properties,
24
- * then returns an object containing resolved values as properties.
25
-
26
- * If a token is not found, then `undefined` value is used.
27
- *
28
- * @example
29
- * ```ts
30
- * const value = tryResolveValue(container, tokenA);
31
- * console.log(value); // 1
32
- *
33
- * const props = tryResolveValue(container, {a: tokenA, b: tokenB});
34
- * console.log(props); // {a: 1, b: 2}
35
- * ```
36
- */
37
- export declare function tryResolveValue<Tokens extends Token<unknown> | {
38
- [key: string]: Token<unknown>;
39
- }, Values extends Tokens extends Token<infer V> ? V | undefined : Tokens extends TokenProps<infer Props> ? Partial<Props> : never>(container: Container, token: Tokens): Values;
40
- /**
41
- * Returns an array of resolved values or objects with resolved values.
42
- *
43
- * If an item of the array is an object which has tokens as its properties,
44
- * then returns an object containing resolved values as properties.
45
-
46
- * If a token is not found, then `undefined` value is used.
47
- *
48
- * @example
49
- * ```ts
50
- * const items1 = tryResolveValues(container, tokenA);
51
- * console.log(items1); // [1]
52
- *
53
- * const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB});
54
- * console.log(items2); // [1, {a: 1, b: 2}]
55
- * ```
56
- */
57
- export declare function tryResolveValues<Tokens extends (Token<unknown> | {
58
- [key: string]: Token<unknown>;
59
- })[], Values extends {
60
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V | undefined : Tokens[K] extends TokenProps<infer Props> ? Partial<Props> : never;
61
- }>(container: Container, ...tokens: Tokens): Values;
62
- /**
63
- * Resolves a value by the provided token.
64
- *
65
- * If an argument is an object which has tokens as its properties,
66
- * then returns an object containing resolved values as properties.
67
-
68
- * If a value is not found by the token, then `ResolverError` is thrown.
69
- *
70
- * @example
71
- * ```ts
72
- * const value = resolveValue(container, tokenA);
73
- * console.log(value); // 1
74
- *
75
- * const props = resolveValue(container, {a: tokenA, b: tokenB});
76
- * console.log(props); // {a: 1, b: 2}
77
- * ```
78
- */
79
- export declare function resolveValue<Tokens extends Token<unknown> | {
80
- [key: string]: Token<unknown>;
81
- }, Values extends Tokens extends Token<infer V> ? V : Tokens extends TokenProps<infer Props> ? Props : never>(container: Container, token: Tokens): Values;
82
- /**
83
- * Returns an array of resolved values or objects with resolved values.
84
- *
85
- * If an item of the array is an object which has tokens as its properties,
86
- * then returns an object containing resolved values as properties.
87
-
88
- * If a token is not found, then `ResolverError` is thrown.
89
- *
90
- * @example
91
- * ```ts
92
- * const items1 = resolveValues(container, tokenA);
93
- * console.log(items1); // [1]
94
- *
95
- * const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB});
96
- * console.log(items2); // [1, {a: 1, b: 2}]
97
- * ```
98
- */
99
- export declare function resolveValues<Tokens extends (Token<unknown> | {
100
- [key: string]: Token<unknown>;
101
- })[], Values extends {
102
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
103
- }>(container: Container, ...tokens: Tokens): Values;
104
- /**
105
- * Decorates a factory by passing resolved values as factory arguments.
106
- *
107
- * If an argument is an object which has tokens as its properties,
108
- * then returns an object containing resolved values as properties.
109
- *
110
- * @param factory - A factory.
111
- * @param tokens - Tokens which correspond to factory arguments.
112
- *
113
- * @return Decorated factory which takes a dependency container as a single argument.
114
- */
115
- export declare function injectable<Tokens extends (Token<unknown> | {
116
- [key: string]: Token<unknown>;
117
- })[], Values extends {
118
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
119
- }, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
120
- /**
121
- * Decorates a class by passing resolved values as arguments to its constructor.
122
- *
123
- * If an argument is an object which has tokens as its properties,
124
- * then returns an object containing resolved values as properties.
125
- *
126
- * @param constructor - Constructor of a class
127
- * @param tokens - Tokens which correspond to constructor arguments
128
- *
129
- * @return A factory function which takes a dependency container as a single argument
130
- * and returns a new created class.
131
- */
132
- export declare function injectableClass<Tokens extends (Token<unknown> | {
133
- [key: string]: Token<unknown>;
134
- })[], Values extends {
135
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
136
- }, Result>(this: unknown, constructor: new (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
137
- export {};
package/dist/esm/utils.js DELETED
@@ -1,145 +0,0 @@
1
- /**
2
- * Checks if a value is the token
3
- */
4
- export function isToken(value) {
5
- return (value !== undefined &&
6
- value !== null &&
7
- typeof value === 'object' &&
8
- 'symbol' in value &&
9
- typeof value.symbol === 'symbol');
10
- }
11
- /**
12
- * Rebinds the array by the token with added new value.
13
- * @param container - Dependency container.
14
- * @param token - Token for an array of values.
15
- * @param value - New value which is added to the end of the array.
16
- */
17
- export function bindMultiValue(container, token, value) {
18
- var _a;
19
- const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
20
- const nextValues = [...prevValues, value];
21
- container.bindValue(token, nextValues);
22
- }
23
- /**
24
- * Tries to resolve a value by the provided token.
25
- *
26
- * If an argument is an object which has tokens as its properties,
27
- * then returns an object containing resolved values as properties.
28
-
29
- * If a token is not found, then `undefined` value is used.
30
- *
31
- * @example
32
- * ```ts
33
- * const value = tryResolveValue(container, tokenA);
34
- * console.log(value); // 1
35
- *
36
- * const props = tryResolveValue(container, {a: tokenA, b: tokenB});
37
- * console.log(props); // {a: 1, b: 2}
38
- * ```
39
- */
40
- export function tryResolveValue(container, token) {
41
- if (isToken(token)) {
42
- return container.get(token);
43
- }
44
- const obj = {};
45
- Object.keys(token).forEach((key) => (obj[key] = container.get(token[key])));
46
- return obj;
47
- }
48
- /**
49
- * Returns an array of resolved values or objects with resolved values.
50
- *
51
- * If an item of the array is an object which has tokens as its properties,
52
- * then returns an object containing resolved values as properties.
53
-
54
- * If a token is not found, then `undefined` value is used.
55
- *
56
- * @example
57
- * ```ts
58
- * const items1 = tryResolveValues(container, tokenA);
59
- * console.log(items1); // [1]
60
- *
61
- * const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB});
62
- * console.log(items2); // [1, {a: 1, b: 2}]
63
- * ```
64
- */
65
- export function tryResolveValues(container, ...tokens) {
66
- return tokens.map((item) => tryResolveValue(container, item));
67
- }
68
- /**
69
- * Resolves a value by the provided token.
70
- *
71
- * If an argument is an object which has tokens as its properties,
72
- * then returns an object containing resolved values as properties.
73
-
74
- * If a value is not found by the token, then `ResolverError` is thrown.
75
- *
76
- * @example
77
- * ```ts
78
- * const value = resolveValue(container, tokenA);
79
- * console.log(value); // 1
80
- *
81
- * const props = resolveValue(container, {a: tokenA, b: tokenB});
82
- * console.log(props); // {a: 1, b: 2}
83
- * ```
84
- */
85
- export function resolveValue(container, token) {
86
- if (isToken(token)) {
87
- return container.resolve(token);
88
- }
89
- const obj = {};
90
- Object.keys(token).forEach((key) => (obj[key] = container.resolve(token[key])));
91
- return obj;
92
- }
93
- /**
94
- * Returns an array of resolved values or objects with resolved values.
95
- *
96
- * If an item of the array is an object which has tokens as its properties,
97
- * then returns an object containing resolved values as properties.
98
-
99
- * If a token is not found, then `ResolverError` is thrown.
100
- *
101
- * @example
102
- * ```ts
103
- * const items1 = resolveValues(container, tokenA);
104
- * console.log(items1); // [1]
105
- *
106
- * const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB});
107
- * console.log(items2); // [1, {a: 1, b: 2}]
108
- * ```
109
- */
110
- export function resolveValues(container, ...tokens) {
111
- return tokens.map((item) => resolveValue(container, item));
112
- }
113
- /**
114
- * Decorates a factory by passing resolved values as factory arguments.
115
- *
116
- * If an argument is an object which has tokens as its properties,
117
- * then returns an object containing resolved values as properties.
118
- *
119
- * @param factory - A factory.
120
- * @param tokens - Tokens which correspond to factory arguments.
121
- *
122
- * @return Decorated factory which takes a dependency container as a single argument.
123
- */
124
- export function injectable(factory, ...tokens) {
125
- return (container) => {
126
- const values = resolveValues(container, ...tokens);
127
- return factory.apply(this, values);
128
- };
129
- }
130
- /**
131
- * Decorates a class by passing resolved values as arguments to its constructor.
132
- *
133
- * If an argument is an object which has tokens as its properties,
134
- * then returns an object containing resolved values as properties.
135
- *
136
- * @param constructor - Constructor of a class
137
- * @param tokens - Tokens which correspond to constructor arguments
138
- *
139
- * @return A factory function which takes a dependency container as a single argument
140
- * and returns a new created class.
141
- */
142
- export function injectableClass(constructor, ...tokens) {
143
- return injectable((...values) => new constructor(...values), ...tokens);
144
- }
145
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAQA;;GAEG;AACH,MAAM,UAAU,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;AAED;;;;;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;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,eAAe,CAO7B,SAAoB,EAAE,KAAa;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,CAAC;AACvE,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,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;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,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"}
package/dist/umd/index.js DELETED
@@ -1,2 +0,0 @@
1
- !function(o,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((o="undefined"!=typeof globalThis?globalThis:o||self).Ditox={})}(this,(function(o){"use strict";function n(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)}}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 e=function(){return e=Object.assign||function(o){for(var n,t=1,e=arguments.length;t<e;t++)for(var r in n=arguments[t])Object.prototype.hasOwnProperty.call(n,r)&&(o[r]=n[r]);return o},e.apply(this,arguments)};function r(o,n,t){if(t||2===arguments.length)for(var e,r=0,i=n.length;r<i;r++)!e&&r in n||(e||(e=Array.prototype.slice.call(n,0,r)),e[r]=n[r]);return o.concat(e||Array.prototype.slice.call(n))}var i=function(o){function n(n){var t=this.constructor,e=o.call(this,n)||this;return Object.setPrototypeOf(e,t.prototype),e.name="ResolverError",e}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 e(){this.constructor=o}t(o,n),o.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}(n,o),n}(Error),l=n("ditox.Container"),u=n("ditox.ParentContainer"),a=n("ditox.Resolver"),s=Symbol(),c="singleton",f=n("ditox.FactoriesMap");function d(o){return o.symbol===l.symbol||o.symbol===u.symbol||o.symbol===a.symbol}function p(o,n){if(!o)return!1;for(var t=0;t<o.length;t++)if(o[t].hasToken(n))return!0;return!1}function y(o){return null!=o&&"object"==typeof o&&"symbol"in o&&"symbol"==typeof o.symbol}function v(o,n){if(y(n))return o.get(n);var t={};return Object.keys(n).forEach((function(e){return t[e]=o.get(n[e])})),t}function b(o,n){if(y(n))return o.resolve(n);var t={};return Object.keys(n).forEach((function(e){return t[e]=o.resolve(n[e])})),t}function m(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 h(o){for(var n=this,t=[],e=1;e<arguments.length;e++)t[e-1]=arguments[e];return function(e){var i=m.apply(void 0,r([e],t,!1));return o.apply(n,i)}}function g(o,n,t){for(var e,r,i,l={module:n,options:null!=t?t:{}},u=new Set([l]),a=[l],s=0;s<a.length;){null===(e=(d="module"in(f=a[s])?f.module:f).imports)||void 0===e||e.forEach((function(o){u.has(o)||(u.add(o),a.push(o))})),s++}for(var c=0;c<a.length;c++){null===(r=(d="module"in(f=a[c])?f.module:f).beforeBinding)||void 0===r||r.call(d,o)}for(c=0;c<a.length;c++){O(o,f=a[c])}for(c=a.length-1;c>=0;c--){var f,d;null===(i=(d="module"in(f=a[c])?f.module:f).afterBinding)||void 0===i||i.call(d,o)}}function O(o,n){var t,e;"module"in n?(t=n.module,e=n.options):t=n;var r=null==e?void 0:e.scope,i=new Set,l=t.exports;l&&Object.keys(l).forEach((function(n){var e=l[n];e&&(i.add(e),o.bindFactory(e,h((function(o){return o[n]}),t.token),{scope:r}))}));o.bindFactory(t.token,t.factory,{scope:r,onRemoved:function(n){n.destroy&&n.destroy(),i.forEach((function(n){return o.remove(n)})),i.clear()}})}function k(o){var t;return e(e({},o),{token:null!==(t=o.token)&&void 0!==t?t:n()})}o.ResolverError=i,o.bindModule=g,o.bindModules=function(o,n){n.forEach((function(n){"module"in n?g(o,n.module,n.options):g(o,n)}))},o.bindMultiValue=function(o,n,t){var e,i=r(r([],null!==(e=o.get(n))&&void 0!==e?e:[],!0),[t],!1);o.bindValue(n,i)},o.createContainer=function(o){var n=o?Array.isArray(o)?r([],o,!0):[o]:void 0,t=new Map,e=new Map,y={bindValue:function(o,n){d(o)||t.set(o.symbol,n)},bindFactory:function(o,n,t){d(o)||e.set(o.symbol,{factory:n,options:t})},remove:function(o){var n;if(!d(o)){var r=null===(n=e.get(o.symbol))||void 0===n?void 0:n.options;r&&b(o.symbol,r),t.delete(o.symbol),e.delete(o.symbol)}},removeAll:function(){e.forEach((function(o,n){o.options&&b(n,o.options)})),t.clear(),e.clear(),m()},hasToken:function(o){return t.has(o.symbol)||e.has(o.symbol)||p(n,o)},get:function(o){var n=v(o,y);return n!==s?n:o.isOptional?o.optionalValue:void 0},resolve:function(o){var n,t=v(o,y);if(t!==s)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 v(o,r){var i=t.get(o.symbol),l=void 0!==i||t.has(o.symbol);if(l&&r===y)return i;var u,f,d=e.get(o.symbol);if(d)switch(u=d.options,null!==(f=null==u?void 0:u.scope)&&void 0!==f?f:c){case"singleton":if(l)return i;if(p(n,o))break;var v=d.factory(y);return y.bindValue(o,v),v;case"scoped":if(l)return i;var b=d.factory(y);return y.bindValue(o,b),b;case"transient":return d.factory(r)}return l?i:n?function(o,n,t){for(var e=0,r=o;e<r.length;e++){var i=r[e].get(a);if(i){var l=i(n,t);if(l!==s)return l}}return s}(n,o,r):s}function b(o,n){var e=function(o){return void 0===o.scope||"scoped"===o.scope||"singleton"===o.scope?o.onRemoved:void 0}(n);if(e){var r=t.get(o);(void 0!==r||t.has(o))&&e(r)}}function m(){t.set(l.symbol,y),t.set(a.symbol,v),t.set(f.symbol,e),n&&t.set(u.symbol,n)}return m(),y},o.declareModule=k,o.declareModuleBindings=function(o){return k({factory:function(){return{}},imports:o})},o.injectable=h,o.injectableClass=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return h.apply(void 0,r([function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return new(o.bind.apply(o,r([void 0],n,!1)))}],n,!1))},o.isToken=y,o.optional=function(o,n){return{symbol:o.symbol,isOptional:!0,optionalValue:n}},o.resolveValue=b,o.resolveValues=m,o.token=n,o.tryResolveValue=v,o.tryResolveValues=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return v(o,n)}))}}));
2
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}