ditox 1.4.2 → 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 +1 -1
- package/dist/{index.d.ts → browser/index.d.ts} +0 -0
- 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 -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 +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 +0 -0
- package/dist/umd/index.js +2 -0
- package/dist/umd/index.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +111 -96
- 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 +8 -9
- package/src/modules.test.ts +2 -0
- package/CHANGELOG.md +0 -132
- 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/dist/index.module.d.ts
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @ignore
|
|
3
|
-
* Binding token for mandatory value
|
|
4
|
-
*/
|
|
5
|
-
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
|
-
declare type OptionalToken<T> = {
|
|
15
|
-
symbol: symbol;
|
|
16
|
-
type?: T;
|
|
17
|
-
isOptional: true;
|
|
18
|
-
optionalValue: T;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Binding token.
|
|
22
|
-
*/
|
|
23
|
-
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
|
-
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
|
-
declare function optional<T>(token: Token<T>, optionalValue: T): OptionalToken<T>;
|
|
36
|
-
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
|
-
declare class ResolverError extends Error {
|
|
41
|
-
constructor(message: string);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* @see https://github.com/mnasyrov/ditox#factory-lifetimes
|
|
45
|
-
*/
|
|
46
|
-
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
|
-
declare type FactoryOptions<T> = {
|
|
58
|
-
scope?: 'scoped' | 'singleton';
|
|
59
|
-
onRemoved?: (value: T) => void;
|
|
60
|
-
} | {
|
|
61
|
-
scope: 'transient';
|
|
62
|
-
};
|
|
63
|
-
/**
|
|
64
|
-
* Dependency container.
|
|
65
|
-
*/
|
|
66
|
-
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
|
-
/**
|
|
97
|
-
* Creates a new dependency container.
|
|
98
|
-
*
|
|
99
|
-
* 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.
|
|
100
|
-
*
|
|
101
|
-
* @param parentContainer - Optional parent container.
|
|
102
|
-
*/
|
|
103
|
-
declare function createContainer(parentContainer?: Container): Container;
|
|
104
|
-
|
|
105
|
-
declare type ValuesProps = {
|
|
106
|
-
[key: string]: unknown;
|
|
107
|
-
};
|
|
108
|
-
declare type TokenProps<Props extends ValuesProps> = {
|
|
109
|
-
[K in keyof Props]: Token<Props[K]>;
|
|
110
|
-
};
|
|
111
|
-
/**
|
|
112
|
-
* Rebinds the array by the token with added new value.
|
|
113
|
-
* @param container - Dependency container.
|
|
114
|
-
* @param token - Token for an array of values.
|
|
115
|
-
* @param value - New value which is added to the end of the array.
|
|
116
|
-
*/
|
|
117
|
-
declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyArray<T>>, value: T): void;
|
|
118
|
-
/**
|
|
119
|
-
* Returns an array of resolved values by the specified token.
|
|
120
|
-
* If a token is not found, then `undefined` value is used.
|
|
121
|
-
*/
|
|
122
|
-
declare function getValues<Tokens extends Token<unknown>[], Values extends {
|
|
123
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
|
|
124
|
-
}>(container: Container, ...tokens: Tokens): Values;
|
|
125
|
-
/**
|
|
126
|
-
* Returns an array of resolved values by the specified token.
|
|
127
|
-
* If a token is not found, then `ResolverError` is thrown.
|
|
128
|
-
*/
|
|
129
|
-
declare function resolveValues<Tokens extends Token<unknown>[], Values extends {
|
|
130
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
|
|
131
|
-
}>(container: Container, ...tokens: Tokens): Values;
|
|
132
|
-
/**
|
|
133
|
-
* Decorates a factory by passing resolved tokens as factory arguments.
|
|
134
|
-
* @param factory - A factory.
|
|
135
|
-
* @param tokens - Tokens which correspond to factory arguments.
|
|
136
|
-
* @return Decorated factory which takes a dependency container as a single argument.
|
|
137
|
-
*/
|
|
138
|
-
declare function injectable<Tokens extends Token<unknown>[], Values extends {
|
|
139
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
|
|
140
|
-
}, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
|
|
141
|
-
/**
|
|
142
|
-
* Returns an object with resolved properties which are specified by token properties.
|
|
143
|
-
* If a token is not found, then `undefined` value is used.
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
* ```ts
|
|
147
|
-
* const props = getProps(container, {a: tokenA, b: tokenB});
|
|
148
|
-
* console.log(props); // {a: 1, b: 2}
|
|
149
|
-
* ```
|
|
150
|
-
*/
|
|
151
|
-
declare function getProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Partial<Props>;
|
|
152
|
-
/**
|
|
153
|
-
* Returns an object with resolved properties which are specified by token properties.
|
|
154
|
-
* If a token is not found, then `ResolverError` is thrown.
|
|
155
|
-
*
|
|
156
|
-
* @example
|
|
157
|
-
* ```ts
|
|
158
|
-
* const props = resolveProps(container, {a: tokenA, b: tokenB});
|
|
159
|
-
* console.log(props); // {a: 1, b: 2}
|
|
160
|
-
* ```
|
|
161
|
-
*/
|
|
162
|
-
declare function resolveProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Props;
|
|
163
|
-
/**
|
|
164
|
-
* Decorates a factory by passing a resolved object with tokens as the first argument.
|
|
165
|
-
* @param factory - A factory.
|
|
166
|
-
* @param tokens - Object with tokens.
|
|
167
|
-
* @return Decorated factory which takes a dependency container as a single argument.
|
|
168
|
-
*
|
|
169
|
-
* @example
|
|
170
|
-
* ```ts
|
|
171
|
-
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
172
|
-
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
173
|
-
* const result = decoratedFactory(container);
|
|
174
|
-
* ```
|
|
175
|
-
*/
|
|
176
|
-
declare function injectableProps<Props extends ValuesProps, Result>(factory: (props: Props) => Result, tokens: TokenProps<Props>): (container: Container) => Result;
|
|
177
|
-
|
|
178
|
-
declare type AnyObject = Record<string, any>;
|
|
179
|
-
declare type EmptyObject = Record<string, never>;
|
|
180
|
-
declare type ModuleController = {
|
|
181
|
-
/** Dispose the module and clean its resources */
|
|
182
|
-
destroy?: () => void;
|
|
183
|
-
};
|
|
184
|
-
/**
|
|
185
|
-
* Dependency module
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
* ```ts
|
|
189
|
-
* type LoggerModule = Module<{
|
|
190
|
-
* logger: Logger;
|
|
191
|
-
* }>;
|
|
192
|
-
* ```
|
|
193
|
-
*/
|
|
194
|
-
declare type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
|
|
195
|
-
declare type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
|
|
196
|
-
/**
|
|
197
|
-
* Description how to bind the module in declarative way.
|
|
198
|
-
*
|
|
199
|
-
* @example
|
|
200
|
-
* ```ts
|
|
201
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
202
|
-
* token: LOGGER_MODULE_TOKEN,
|
|
203
|
-
* factory: (container) => {
|
|
204
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
205
|
-
* return {
|
|
206
|
-
* logger: { log: (message) => transport.write(message) },
|
|
207
|
-
* destroy: () => transport.close(),
|
|
208
|
-
* }
|
|
209
|
-
* },
|
|
210
|
-
* exports: {
|
|
211
|
-
* logger: LOGGER_TOKEN,
|
|
212
|
-
* },
|
|
213
|
-
* };
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
declare type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
217
|
-
/** Token for the module */
|
|
218
|
-
token: Token<T>;
|
|
219
|
-
/** Modules for binding */
|
|
220
|
-
imports?: ReadonlyArray<ModuleBindingEntry>;
|
|
221
|
-
/** Factory of the module */
|
|
222
|
-
factory: (container: Container) => T;
|
|
223
|
-
/** Dictionary of module properties which are bound to tokens. */
|
|
224
|
-
exports?: {
|
|
225
|
-
[K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
|
|
226
|
-
};
|
|
227
|
-
/**
|
|
228
|
-
* Dictionary of module properties which are bound to tokens.
|
|
229
|
-
*
|
|
230
|
-
* @deprecated Use `exports` property.
|
|
231
|
-
*/
|
|
232
|
-
exportedProps?: ModuleDeclaration<T>['exports'];
|
|
233
|
-
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
234
|
-
beforeBinding?: (container: Container) => void;
|
|
235
|
-
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
236
|
-
afterBinding?: (container: Container) => void;
|
|
237
|
-
};
|
|
238
|
-
/**
|
|
239
|
-
* Options for module binding.
|
|
240
|
-
*
|
|
241
|
-
* `scope` types:
|
|
242
|
-
* - `singleton` - **This is the default**. The module is created and cached by the container which registered the factory.
|
|
243
|
-
* - `scoped` - The module is created and cached by the container which starts resolving.
|
|
244
|
-
*/
|
|
245
|
-
declare type BindModuleOptions = {
|
|
246
|
-
scope?: 'scoped' | 'singleton';
|
|
247
|
-
};
|
|
248
|
-
declare type ModuleBindingEntry = ModuleDeclaration<AnyObject> | {
|
|
249
|
-
module: ModuleDeclaration<AnyObject>;
|
|
250
|
-
options: BindModuleOptions;
|
|
251
|
-
};
|
|
252
|
-
/**
|
|
253
|
-
* Binds the dependency module to the container
|
|
254
|
-
* @param container - Dependency container.
|
|
255
|
-
* @param moduleDeclaration - Declaration of the dependency module.
|
|
256
|
-
* @param options - Options for module binding.
|
|
257
|
-
*
|
|
258
|
-
* @example
|
|
259
|
-
* ```ts
|
|
260
|
-
* bindModule(container, LOGGER_MODULE);
|
|
261
|
-
* ```
|
|
262
|
-
*/
|
|
263
|
-
declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
264
|
-
/**
|
|
265
|
-
* Binds dependency modules to the container
|
|
266
|
-
*
|
|
267
|
-
* @param container - Dependency container for binding
|
|
268
|
-
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
269
|
-
*/
|
|
270
|
-
declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
271
|
-
/**
|
|
272
|
-
* Declares a module binding
|
|
273
|
-
*
|
|
274
|
-
* @param declaration - a module declaration
|
|
275
|
-
* @param declaration.token - optional field
|
|
276
|
-
*
|
|
277
|
-
* @example
|
|
278
|
-
* ```ts
|
|
279
|
-
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
280
|
-
* factory: (container) => {
|
|
281
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
282
|
-
* return {
|
|
283
|
-
* logger: { log: (message) => transport.write(message) },
|
|
284
|
-
* destroy: () => transport.close(),
|
|
285
|
-
* }
|
|
286
|
-
* },
|
|
287
|
-
* exports: {
|
|
288
|
-
* logger: LOGGER_TOKEN,
|
|
289
|
-
* },
|
|
290
|
-
* });
|
|
291
|
-
* ```
|
|
292
|
-
*/
|
|
293
|
-
declare function declareModule<T>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
294
|
-
/**
|
|
295
|
-
* Declares bindings of several modules
|
|
296
|
-
*
|
|
297
|
-
* @param modules - module declaration entries
|
|
298
|
-
*/
|
|
299
|
-
declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
300
|
-
|
|
301
|
-
export { BindModuleOptions, Container, FactoryOptions, FactoryScope, Module, ModuleBindingEntry, ModuleDeclaration, OptionalToken, RequiredToken, ResolverError, Token, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, getProps, getValues, injectable, injectableProps, optional, resolveProps, resolveValues, token };
|
package/dist/index.module.js
DELETED
|
@@ -1,393 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a new binding token.
|
|
3
|
-
* @param description - Token description for better error messages.
|
|
4
|
-
*/
|
|
5
|
-
function token(description) {
|
|
6
|
-
return { symbol: Symbol(description) };
|
|
7
|
-
}
|
|
8
|
-
function optional(token, optionalValue) {
|
|
9
|
-
return {
|
|
10
|
-
symbol: token.symbol,
|
|
11
|
-
isOptional: true,
|
|
12
|
-
optionalValue,
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* ResolverError is thrown by the resolver when a token is not found in a container.
|
|
17
|
-
*/
|
|
18
|
-
class ResolverError extends Error {
|
|
19
|
-
constructor(message) {
|
|
20
|
-
super(message);
|
|
21
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
22
|
-
this.name = 'ResolverError';
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
/** @internal */
|
|
26
|
-
const CONTAINER = token('ditox.Container');
|
|
27
|
-
/** @internal */
|
|
28
|
-
const PARENT_CONTAINER = token('ditox.ParentContainer');
|
|
29
|
-
/** @internal */
|
|
30
|
-
const RESOLVER = token('ditox.Resolver');
|
|
31
|
-
/** @internal */
|
|
32
|
-
const NOT_FOUND = Symbol();
|
|
33
|
-
/** @internal */
|
|
34
|
-
const FAKE_FACTORY = () => {
|
|
35
|
-
throw new Error('FAKE_FACTORY');
|
|
36
|
-
};
|
|
37
|
-
/** @internal */
|
|
38
|
-
const DEFAULT_SCOPE = 'singleton';
|
|
39
|
-
/** @internal */
|
|
40
|
-
const FACTORIES_MAP = token('ditox.FactoriesMap');
|
|
41
|
-
/** @internal */
|
|
42
|
-
function getScope(options) {
|
|
43
|
-
var _a;
|
|
44
|
-
return (_a = options === null || options === void 0 ? void 0 : options.scope) !== null && _a !== void 0 ? _a : DEFAULT_SCOPE;
|
|
45
|
-
}
|
|
46
|
-
/** @internal */
|
|
47
|
-
function getOnRemoved(options) {
|
|
48
|
-
return options.scope === undefined ||
|
|
49
|
-
options.scope === 'scoped' ||
|
|
50
|
-
options.scope === 'singleton'
|
|
51
|
-
? options.onRemoved
|
|
52
|
-
: undefined;
|
|
53
|
-
}
|
|
54
|
-
/** @internal */
|
|
55
|
-
function isInternalToken(token) {
|
|
56
|
-
return (token.symbol === CONTAINER.symbol ||
|
|
57
|
-
token.symbol === PARENT_CONTAINER.symbol ||
|
|
58
|
-
token.symbol === RESOLVER.symbol);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Creates a new dependency container.
|
|
62
|
-
*
|
|
63
|
-
* 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.
|
|
64
|
-
*
|
|
65
|
-
* @param parentContainer - Optional parent container.
|
|
66
|
-
*/
|
|
67
|
-
function createContainer(parentContainer) {
|
|
68
|
-
const values = new Map();
|
|
69
|
-
const factories = new Map();
|
|
70
|
-
const container = {
|
|
71
|
-
bindValue(token, value) {
|
|
72
|
-
if (isInternalToken(token)) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
values.set(token.symbol, value);
|
|
76
|
-
},
|
|
77
|
-
bindFactory(token, factory, options) {
|
|
78
|
-
if (isInternalToken(token)) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
factories.set(token.symbol, { factory, options });
|
|
82
|
-
},
|
|
83
|
-
remove(token) {
|
|
84
|
-
var _a;
|
|
85
|
-
if (isInternalToken(token)) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
const options = (_a = factories.get(token.symbol)) === null || _a === void 0 ? void 0 : _a.options;
|
|
89
|
-
if (options) {
|
|
90
|
-
executeOnRemoved(token.symbol, options);
|
|
91
|
-
}
|
|
92
|
-
values.delete(token.symbol);
|
|
93
|
-
factories.delete(token.symbol);
|
|
94
|
-
},
|
|
95
|
-
removeAll() {
|
|
96
|
-
factories.forEach((context, tokenSymbol) => {
|
|
97
|
-
if (context.options) {
|
|
98
|
-
executeOnRemoved(tokenSymbol, context.options);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
values.clear();
|
|
102
|
-
factories.clear();
|
|
103
|
-
bindInternalTokens();
|
|
104
|
-
},
|
|
105
|
-
hasToken(token) {
|
|
106
|
-
var _a;
|
|
107
|
-
return (values.has(token.symbol) ||
|
|
108
|
-
factories.has(token.symbol) ||
|
|
109
|
-
((_a = parentContainer === null || parentContainer === void 0 ? void 0 : parentContainer.hasToken(token)) !== null && _a !== void 0 ? _a : false));
|
|
110
|
-
},
|
|
111
|
-
get(token) {
|
|
112
|
-
const value = resolver(token, container);
|
|
113
|
-
if (value !== NOT_FOUND) {
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
if (token.isOptional) {
|
|
117
|
-
return token.optionalValue;
|
|
118
|
-
}
|
|
119
|
-
return undefined;
|
|
120
|
-
},
|
|
121
|
-
resolve(token) {
|
|
122
|
-
var _a;
|
|
123
|
-
const value = resolver(token, container);
|
|
124
|
-
if (value !== NOT_FOUND) {
|
|
125
|
-
return value;
|
|
126
|
-
}
|
|
127
|
-
if (token.isOptional) {
|
|
128
|
-
return token.optionalValue;
|
|
129
|
-
}
|
|
130
|
-
throw new ResolverError(`Token "${(_a = token.symbol.description) !== null && _a !== void 0 ? _a : ''}" is not provided`);
|
|
131
|
-
},
|
|
132
|
-
};
|
|
133
|
-
function resolver(token, origin) {
|
|
134
|
-
const value = values.get(token.symbol);
|
|
135
|
-
const hasValue = value !== undefined || values.has(token.symbol);
|
|
136
|
-
if (hasValue && origin === container) {
|
|
137
|
-
return value;
|
|
138
|
-
}
|
|
139
|
-
const factoryContext = factories.get(token.symbol);
|
|
140
|
-
if (factoryContext && factoryContext.factory !== FAKE_FACTORY) {
|
|
141
|
-
const scope = getScope(factoryContext.options);
|
|
142
|
-
switch (scope) {
|
|
143
|
-
case 'singleton': {
|
|
144
|
-
if (hasValue) {
|
|
145
|
-
return value;
|
|
146
|
-
}
|
|
147
|
-
else if (parentContainer === null || parentContainer === void 0 ? void 0 : parentContainer.hasToken(token)) {
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
// Cache the value in the same container where the factory is registered.
|
|
152
|
-
const value = factoryContext.factory(container);
|
|
153
|
-
container.bindValue(token, value);
|
|
154
|
-
return value;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
case 'scoped': {
|
|
158
|
-
// Create a value within the origin container and cache it.
|
|
159
|
-
const value = factoryContext.factory(origin);
|
|
160
|
-
origin.bindValue(token, value);
|
|
161
|
-
if (origin !== container) {
|
|
162
|
-
// Bind a fake factory with actual options to make onRemoved() works.
|
|
163
|
-
origin.bindFactory(token, FAKE_FACTORY, factoryContext.options);
|
|
164
|
-
}
|
|
165
|
-
return value;
|
|
166
|
-
}
|
|
167
|
-
case 'transient': {
|
|
168
|
-
// Create a value within the origin container and don't cache it.
|
|
169
|
-
return factoryContext.factory(origin);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (hasValue) {
|
|
174
|
-
return value;
|
|
175
|
-
}
|
|
176
|
-
const parentResolver = parentContainer === null || parentContainer === void 0 ? void 0 : parentContainer.get(RESOLVER);
|
|
177
|
-
if (parentResolver) {
|
|
178
|
-
return parentResolver(token, origin);
|
|
179
|
-
}
|
|
180
|
-
return NOT_FOUND;
|
|
181
|
-
}
|
|
182
|
-
function executeOnRemoved(tokenSymbol, options) {
|
|
183
|
-
const onRemoved = getOnRemoved(options);
|
|
184
|
-
if (onRemoved) {
|
|
185
|
-
const value = values.get(tokenSymbol);
|
|
186
|
-
if (value !== undefined || values.has(tokenSymbol)) {
|
|
187
|
-
onRemoved(value);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
function bindInternalTokens() {
|
|
192
|
-
values.set(CONTAINER.symbol, container);
|
|
193
|
-
values.set(RESOLVER.symbol, resolver);
|
|
194
|
-
values.set(FACTORIES_MAP.symbol, factories);
|
|
195
|
-
if (parentContainer) {
|
|
196
|
-
values.set(PARENT_CONTAINER.symbol, parentContainer);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
bindInternalTokens();
|
|
200
|
-
return container;
|
|
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.module.js.map
|