atomic-di 0.7.1-beta.1 → 0.7.1-beta.2
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.mts +48 -57
- package/dist/index.d.ts +48 -57
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -23,36 +23,43 @@ type MockMap = {
|
|
23
23
|
* Returns the provider's mock or the provider itself
|
24
24
|
* depending on the presence of the mock.
|
25
25
|
*
|
26
|
-
* @param provider - An original provider whose mock is needed to get.
|
27
|
-
*
|
28
26
|
* @returns The passed provider or its mock.
|
29
27
|
*/
|
30
|
-
map<T>(
|
28
|
+
map<T>(
|
29
|
+
/**
|
30
|
+
* An original provider whose mock is needed to get.
|
31
|
+
*/
|
32
|
+
provider: Provider<T>): Provider<T>;
|
31
33
|
/**
|
32
34
|
* Registers a mock provider and retursns a new mock map with that mock.
|
33
35
|
*
|
34
|
-
* @param provider - An original provider.
|
35
|
-
* @param replacement - A provider that will be a mock of the first provider.
|
36
|
-
*
|
37
36
|
* @returns A new mock map with added mock.
|
38
37
|
*/
|
39
|
-
add<T>(
|
38
|
+
add<T>(
|
39
|
+
/**
|
40
|
+
* An original provider.
|
41
|
+
*/
|
42
|
+
provider: Provider<T>,
|
43
|
+
/**
|
44
|
+
* A provider that will be a mock of the first provider.
|
45
|
+
*/
|
46
|
+
replacement: Provider<T>): MockMap;
|
40
47
|
/**
|
41
48
|
* Applies mocks from another map to the current one and returns a new map.
|
42
49
|
* If there are identical keys (original providers),
|
43
50
|
* they will be overwritten by mocks from the other map.
|
44
51
|
*
|
45
|
-
* @param otherMockMap - A mock map that will be applied.
|
46
|
-
*
|
47
52
|
* @returns A new mock map with applied mocks.
|
48
53
|
*/
|
49
|
-
apply(
|
54
|
+
apply(
|
55
|
+
/**
|
56
|
+
* A mock map that will be applied.
|
57
|
+
*/
|
58
|
+
otherMockMap: MockMap): MockMap;
|
50
59
|
};
|
51
60
|
/**
|
52
61
|
* Creates a new mock map that stores and provides provider mocks.
|
53
62
|
*
|
54
|
-
* @param entries - Entries of a mock map.
|
55
|
-
*
|
56
63
|
* @returns A new mock map.
|
57
64
|
*/
|
58
65
|
declare const createMockMap: (entries?: MockMapEntries) => {
|
@@ -85,14 +92,19 @@ type Provider<T> = ProviderCallable<T> & {
|
|
85
92
|
* Mocks(replaces) a provider within the visible graph,
|
86
93
|
* returning a new provider with that mock.
|
87
94
|
*
|
88
|
-
* @
|
95
|
+
* @returns A new provider with the mocked dependency provider.
|
96
|
+
*/
|
97
|
+
mock<U>(
|
98
|
+
/**
|
99
|
+
* A provider used by the current provider
|
89
100
|
* that needs to be replaced.
|
90
|
-
|
101
|
+
*/
|
102
|
+
dependencyProvider: Provider<U>,
|
103
|
+
/**
|
104
|
+
* A provider with a same interface
|
91
105
|
* that will be a replacement for the first one.
|
92
|
-
*
|
93
|
-
* @returns A new provider with the mocked dependency provider.
|
94
106
|
*/
|
95
|
-
|
107
|
+
replacement: Provider<U>): Provider<T>;
|
96
108
|
};
|
97
109
|
type Lifetime = "transient" | "singleton" | "scoped";
|
98
110
|
/**
|
@@ -110,16 +122,6 @@ type Resolver<T> = (use: UseFn) => T;
|
|
110
122
|
/**
|
111
123
|
* Creates a new provider, instance factory with lifetime and dynamic context.
|
112
124
|
*
|
113
|
-
* @param lifetime - Instance lifetime.
|
114
|
-
*
|
115
|
-
* @param resolver - A function that creates an instance
|
116
|
-
* by resolving its dependencies
|
117
|
-
* If there are dependencies, you must use `use` function passed
|
118
|
-
* in the first argument.
|
119
|
-
*
|
120
|
-
* @param mockMap - An optional mock map. Not recommended to specify explicitly,
|
121
|
-
* use `mock` method to mock providers.
|
122
|
-
*
|
123
125
|
* @returns A new provider.
|
124
126
|
*/
|
125
127
|
declare const provide: <T>(lifetime: Lifetime, resolver: Resolver<T>, mockMap?: MockMap) => Provider<T>;
|
@@ -127,11 +129,6 @@ declare const provide: <T>(lifetime: Lifetime, resolver: Resolver<T>, mockMap?:
|
|
127
129
|
* Creates a new transient provider.
|
128
130
|
* This provider will return a new instance on each resolution.
|
129
131
|
*
|
130
|
-
* @param resolver - A function that creates an instance
|
131
|
-
* by resolving its dependencies
|
132
|
-
* If there are dependencies, you must use `use` function passed
|
133
|
-
* in the first argument.
|
134
|
-
*
|
135
132
|
* @returns A new transient provider.
|
136
133
|
*/
|
137
134
|
declare const transient: <T>(resolver: Resolver<T>) => Provider<T>;
|
@@ -139,11 +136,6 @@ declare const transient: <T>(resolver: Resolver<T>) => Provider<T>;
|
|
139
136
|
* Creates a new singleton provider.
|
140
137
|
* This provider will create an instance once and return it on every request.
|
141
138
|
*
|
142
|
-
* @param resolver - A function that creates an instance
|
143
|
-
* by resolving its dependencies
|
144
|
-
* If there are dependencies, you must use `use` function passed
|
145
|
-
* in the first argument.
|
146
|
-
*
|
147
139
|
* @returns A new singleton provider.
|
148
140
|
*/
|
149
141
|
declare const singleton: <T>(resolver: Resolver<T>) => Provider<T>;
|
@@ -153,11 +145,6 @@ declare const singleton: <T>(resolver: Resolver<T>) => Provider<T>;
|
|
153
145
|
* and will retrieve it from scope on next resolutions.
|
154
146
|
* If scope was not specified, will create a new instance on each resolution.
|
155
147
|
*
|
156
|
-
* @param resolver - A function that creates an instance
|
157
|
-
* by resolving its dependencies
|
158
|
-
* If there are dependencies, you must use `use` function passed
|
159
|
-
* in the first argument.
|
160
|
-
*
|
161
148
|
* @returns A new scoped provider.
|
162
149
|
*/
|
163
150
|
declare const scoped: <T>(resolver: Resolver<T>) => Provider<T>;
|
@@ -173,18 +160,19 @@ type InferProviderMapOutputs<Providers extends ProviderMap> = {
|
|
173
160
|
* Resolves all providers in a `ProviderMap` and
|
174
161
|
* returns an object containing their instances.
|
175
162
|
*
|
176
|
-
* @param scope - An optional scope to use for scoped providers.
|
177
|
-
* @param mockMap - An optional mock map to use for mocking providers.
|
178
|
-
*
|
179
163
|
* @returns An object containing the resolved instances of the providers.
|
180
164
|
*/
|
181
|
-
type ProviderSelectionCallable<Providers extends ProviderMap> = (
|
165
|
+
type ProviderSelectionCallable<Providers extends ProviderMap> = (
|
166
|
+
/**
|
167
|
+
* An optional scope to use for scoped providers.
|
168
|
+
*/
|
169
|
+
scope?: Scope,
|
170
|
+
/**
|
171
|
+
* An optional mock map to use for mocking providers.
|
172
|
+
*/
|
173
|
+
mockMap?: MockMap) => InferProviderMapOutputs<Providers>;
|
182
174
|
/**
|
183
175
|
* A selection of providers.
|
184
|
-
*
|
185
|
-
* @property map - The `ProviderMap` that the selection is based on.
|
186
|
-
* @property mock - A function that mocks a provider within the selection,
|
187
|
-
* returning a new `ProviderSelection` with the mock applied.
|
188
176
|
*/
|
189
177
|
type ProviderSelection<Providers extends ProviderMap> = ProviderSelectionCallable<Providers> & {
|
190
178
|
/**
|
@@ -195,20 +183,23 @@ type ProviderSelection<Providers extends ProviderMap> = ProviderSelectionCallabl
|
|
195
183
|
* Mocks a provider within the selection, returning a new
|
196
184
|
* `ProviderSelection` with the mock applied.
|
197
185
|
*
|
198
|
-
* @
|
186
|
+
* @returns A new `ProviderSelection` with the mocked provider.
|
187
|
+
*/
|
188
|
+
mock<T>(
|
189
|
+
/**
|
190
|
+
* A provider used by the current provider
|
199
191
|
* that needs to be replaced.
|
200
|
-
|
192
|
+
*/
|
193
|
+
dependencyProvider: Provider<T>,
|
194
|
+
/**
|
195
|
+
* A provider with a same interface that will be
|
201
196
|
* a replacement for the first one.
|
202
|
-
*
|
203
|
-
* @returns A new `ProviderSelection` with the mocked provider.
|
204
197
|
*/
|
205
|
-
|
198
|
+
replacement: Provider<T>): ProviderSelection<Providers>;
|
206
199
|
};
|
207
200
|
/**
|
208
201
|
* Creates a new provider selection from a provider map.
|
209
202
|
*
|
210
|
-
* @param map - A map of string keys to providers.
|
211
|
-
*
|
212
203
|
* @returns A new provider selection.
|
213
204
|
*/
|
214
205
|
declare const select: <Providers extends ProviderMap>(map: Providers) => ProviderSelection<Providers>;
|
package/dist/index.d.ts
CHANGED
@@ -23,36 +23,43 @@ type MockMap = {
|
|
23
23
|
* Returns the provider's mock or the provider itself
|
24
24
|
* depending on the presence of the mock.
|
25
25
|
*
|
26
|
-
* @param provider - An original provider whose mock is needed to get.
|
27
|
-
*
|
28
26
|
* @returns The passed provider or its mock.
|
29
27
|
*/
|
30
|
-
map<T>(
|
28
|
+
map<T>(
|
29
|
+
/**
|
30
|
+
* An original provider whose mock is needed to get.
|
31
|
+
*/
|
32
|
+
provider: Provider<T>): Provider<T>;
|
31
33
|
/**
|
32
34
|
* Registers a mock provider and retursns a new mock map with that mock.
|
33
35
|
*
|
34
|
-
* @param provider - An original provider.
|
35
|
-
* @param replacement - A provider that will be a mock of the first provider.
|
36
|
-
*
|
37
36
|
* @returns A new mock map with added mock.
|
38
37
|
*/
|
39
|
-
add<T>(
|
38
|
+
add<T>(
|
39
|
+
/**
|
40
|
+
* An original provider.
|
41
|
+
*/
|
42
|
+
provider: Provider<T>,
|
43
|
+
/**
|
44
|
+
* A provider that will be a mock of the first provider.
|
45
|
+
*/
|
46
|
+
replacement: Provider<T>): MockMap;
|
40
47
|
/**
|
41
48
|
* Applies mocks from another map to the current one and returns a new map.
|
42
49
|
* If there are identical keys (original providers),
|
43
50
|
* they will be overwritten by mocks from the other map.
|
44
51
|
*
|
45
|
-
* @param otherMockMap - A mock map that will be applied.
|
46
|
-
*
|
47
52
|
* @returns A new mock map with applied mocks.
|
48
53
|
*/
|
49
|
-
apply(
|
54
|
+
apply(
|
55
|
+
/**
|
56
|
+
* A mock map that will be applied.
|
57
|
+
*/
|
58
|
+
otherMockMap: MockMap): MockMap;
|
50
59
|
};
|
51
60
|
/**
|
52
61
|
* Creates a new mock map that stores and provides provider mocks.
|
53
62
|
*
|
54
|
-
* @param entries - Entries of a mock map.
|
55
|
-
*
|
56
63
|
* @returns A new mock map.
|
57
64
|
*/
|
58
65
|
declare const createMockMap: (entries?: MockMapEntries) => {
|
@@ -85,14 +92,19 @@ type Provider<T> = ProviderCallable<T> & {
|
|
85
92
|
* Mocks(replaces) a provider within the visible graph,
|
86
93
|
* returning a new provider with that mock.
|
87
94
|
*
|
88
|
-
* @
|
95
|
+
* @returns A new provider with the mocked dependency provider.
|
96
|
+
*/
|
97
|
+
mock<U>(
|
98
|
+
/**
|
99
|
+
* A provider used by the current provider
|
89
100
|
* that needs to be replaced.
|
90
|
-
|
101
|
+
*/
|
102
|
+
dependencyProvider: Provider<U>,
|
103
|
+
/**
|
104
|
+
* A provider with a same interface
|
91
105
|
* that will be a replacement for the first one.
|
92
|
-
*
|
93
|
-
* @returns A new provider with the mocked dependency provider.
|
94
106
|
*/
|
95
|
-
|
107
|
+
replacement: Provider<U>): Provider<T>;
|
96
108
|
};
|
97
109
|
type Lifetime = "transient" | "singleton" | "scoped";
|
98
110
|
/**
|
@@ -110,16 +122,6 @@ type Resolver<T> = (use: UseFn) => T;
|
|
110
122
|
/**
|
111
123
|
* Creates a new provider, instance factory with lifetime and dynamic context.
|
112
124
|
*
|
113
|
-
* @param lifetime - Instance lifetime.
|
114
|
-
*
|
115
|
-
* @param resolver - A function that creates an instance
|
116
|
-
* by resolving its dependencies
|
117
|
-
* If there are dependencies, you must use `use` function passed
|
118
|
-
* in the first argument.
|
119
|
-
*
|
120
|
-
* @param mockMap - An optional mock map. Not recommended to specify explicitly,
|
121
|
-
* use `mock` method to mock providers.
|
122
|
-
*
|
123
125
|
* @returns A new provider.
|
124
126
|
*/
|
125
127
|
declare const provide: <T>(lifetime: Lifetime, resolver: Resolver<T>, mockMap?: MockMap) => Provider<T>;
|
@@ -127,11 +129,6 @@ declare const provide: <T>(lifetime: Lifetime, resolver: Resolver<T>, mockMap?:
|
|
127
129
|
* Creates a new transient provider.
|
128
130
|
* This provider will return a new instance on each resolution.
|
129
131
|
*
|
130
|
-
* @param resolver - A function that creates an instance
|
131
|
-
* by resolving its dependencies
|
132
|
-
* If there are dependencies, you must use `use` function passed
|
133
|
-
* in the first argument.
|
134
|
-
*
|
135
132
|
* @returns A new transient provider.
|
136
133
|
*/
|
137
134
|
declare const transient: <T>(resolver: Resolver<T>) => Provider<T>;
|
@@ -139,11 +136,6 @@ declare const transient: <T>(resolver: Resolver<T>) => Provider<T>;
|
|
139
136
|
* Creates a new singleton provider.
|
140
137
|
* This provider will create an instance once and return it on every request.
|
141
138
|
*
|
142
|
-
* @param resolver - A function that creates an instance
|
143
|
-
* by resolving its dependencies
|
144
|
-
* If there are dependencies, you must use `use` function passed
|
145
|
-
* in the first argument.
|
146
|
-
*
|
147
139
|
* @returns A new singleton provider.
|
148
140
|
*/
|
149
141
|
declare const singleton: <T>(resolver: Resolver<T>) => Provider<T>;
|
@@ -153,11 +145,6 @@ declare const singleton: <T>(resolver: Resolver<T>) => Provider<T>;
|
|
153
145
|
* and will retrieve it from scope on next resolutions.
|
154
146
|
* If scope was not specified, will create a new instance on each resolution.
|
155
147
|
*
|
156
|
-
* @param resolver - A function that creates an instance
|
157
|
-
* by resolving its dependencies
|
158
|
-
* If there are dependencies, you must use `use` function passed
|
159
|
-
* in the first argument.
|
160
|
-
*
|
161
148
|
* @returns A new scoped provider.
|
162
149
|
*/
|
163
150
|
declare const scoped: <T>(resolver: Resolver<T>) => Provider<T>;
|
@@ -173,18 +160,19 @@ type InferProviderMapOutputs<Providers extends ProviderMap> = {
|
|
173
160
|
* Resolves all providers in a `ProviderMap` and
|
174
161
|
* returns an object containing their instances.
|
175
162
|
*
|
176
|
-
* @param scope - An optional scope to use for scoped providers.
|
177
|
-
* @param mockMap - An optional mock map to use for mocking providers.
|
178
|
-
*
|
179
163
|
* @returns An object containing the resolved instances of the providers.
|
180
164
|
*/
|
181
|
-
type ProviderSelectionCallable<Providers extends ProviderMap> = (
|
165
|
+
type ProviderSelectionCallable<Providers extends ProviderMap> = (
|
166
|
+
/**
|
167
|
+
* An optional scope to use for scoped providers.
|
168
|
+
*/
|
169
|
+
scope?: Scope,
|
170
|
+
/**
|
171
|
+
* An optional mock map to use for mocking providers.
|
172
|
+
*/
|
173
|
+
mockMap?: MockMap) => InferProviderMapOutputs<Providers>;
|
182
174
|
/**
|
183
175
|
* A selection of providers.
|
184
|
-
*
|
185
|
-
* @property map - The `ProviderMap` that the selection is based on.
|
186
|
-
* @property mock - A function that mocks a provider within the selection,
|
187
|
-
* returning a new `ProviderSelection` with the mock applied.
|
188
176
|
*/
|
189
177
|
type ProviderSelection<Providers extends ProviderMap> = ProviderSelectionCallable<Providers> & {
|
190
178
|
/**
|
@@ -195,20 +183,23 @@ type ProviderSelection<Providers extends ProviderMap> = ProviderSelectionCallabl
|
|
195
183
|
* Mocks a provider within the selection, returning a new
|
196
184
|
* `ProviderSelection` with the mock applied.
|
197
185
|
*
|
198
|
-
* @
|
186
|
+
* @returns A new `ProviderSelection` with the mocked provider.
|
187
|
+
*/
|
188
|
+
mock<T>(
|
189
|
+
/**
|
190
|
+
* A provider used by the current provider
|
199
191
|
* that needs to be replaced.
|
200
|
-
|
192
|
+
*/
|
193
|
+
dependencyProvider: Provider<T>,
|
194
|
+
/**
|
195
|
+
* A provider with a same interface that will be
|
201
196
|
* a replacement for the first one.
|
202
|
-
*
|
203
|
-
* @returns A new `ProviderSelection` with the mocked provider.
|
204
197
|
*/
|
205
|
-
|
198
|
+
replacement: Provider<T>): ProviderSelection<Providers>;
|
206
199
|
};
|
207
200
|
/**
|
208
201
|
* Creates a new provider selection from a provider map.
|
209
202
|
*
|
210
|
-
* @param map - A map of string keys to providers.
|
211
|
-
*
|
212
203
|
* @returns A new provider selection.
|
213
204
|
*/
|
214
205
|
declare const select: <Providers extends ProviderMap>(map: Providers) => ProviderSelection<Providers>;
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/helpers.ts","../src/mock-map.ts","../src/provider.ts","../src/scope.ts","../src/selection.ts"],"sourcesContent":["export * from \"./provider\";\nexport * from \"./scope\";\nexport * from \"./selection\";\nexport * from \"./mock-map\";\n","/**\n * Creates a function that will invoke the provided function `fn` only once,\n * caching the result for subsequent calls with the same arguments.\n *\n * @param fn - The function to invoke once and cache the result.\n * @returns A new function that returns the cached result after the first call.\n */\nexport const once = <A extends any[], R>(fn: (...args: A) => R) => {\n let cachedResult: R | undefined;\n\n return Object.assign(\n (...args: A) => cachedResult ?? (cachedResult = fn(...args)),\n fn,\n );\n};\n","import { Provider } from \"./provider\";\n\ntype MockMapEntries = [Provider<any>, Provider<any>][];\n\n/**\n * Stores and provides provider mocks.\n */\nexport type MockMap = {\n entries: MockMapEntries;\n\n /**\n * Returns the provider's mock or the provider itself\n * depending on the presence of the mock.\n *\n * @param provider - An original provider whose mock is needed to get.\n *\n * @returns The passed provider or its mock.\n */\n map<T>(provider: Provider<T>): Provider<T>;\n\n /**\n * Registers a mock provider and retursns a new mock map with that mock.\n *\n * @param provider - An original provider.\n * @param replacement - A provider that will be a mock of the first provider.\n *\n * @returns A new mock map with added mock.\n */\n add<T>(provider: Provider<T>, replacement: Provider<T>): MockMap;\n\n /**\n * Applies mocks from another map to the current one and returns a new map.\n * If there are identical keys (original providers),\n * they will be overwritten by mocks from the other map.\n *\n * @param otherMockMap - A mock map that will be applied.\n *\n * @returns A new mock map with applied mocks.\n */\n apply(otherMockMap: MockMap): MockMap;\n};\n\n/**\n * Creates a new mock map that stores and provides provider mocks.\n *\n * @param entries - Entries of a mock map.\n *\n * @returns A new mock map.\n */\nexport const createMockMap = (entries: MockMapEntries = []) => {\n const map: MockMap[\"map\"] = (provider) =>\n entries.find((e) => e[0] === provider)?.[1] || provider;\n\n const add: MockMap[\"add\"] = (provider, replacement) => {\n const newEntries: MockMapEntries = entries.map((e) =>\n e[0] === provider ? [e[0], replacement] : e,\n );\n\n if (newEntries.length === entries.length)\n newEntries.push([provider, replacement]);\n\n return createMockMap(newEntries);\n };\n\n const apply: MockMap[\"apply\"] = (otherMockMap) =>\n createMockMap([\n ...entries.filter(\n (e) =>\n otherMockMap.entries.find((oe) => oe[0] === e[0]) ===\n undefined,\n ),\n ...otherMockMap.entries,\n ]);\n\n return { entries, map, add, apply };\n};\n","import { once } from \"./helpers\";\nimport { Scope } from \"./scope\";\nimport { createMockMap, MockMap } from \"./mock-map\";\n\n/**\n * Calls a resolver(factory) with an optional scope and a mock map\n * and returns an instance.\n *\n * A passed scope will be propagated up a graph and will be passed\n * to all scoped providers to resolve their instance within this scope.\n * If a provider is scoped, it will either create a new instance\n * and store it in the scope, or retrieve an already created instance\n * from the scope.\n *\n * It is also possible to explicitly pass a mock map, but It's not recommended.\n * If you want to mock(replace) providers for a resolution,\n * it's best to use `mock` method.\n */\ntype ProviderCallable<T> = (scope?: Scope, mockMap?: MockMap) => T;\n\n/**\n * Instance factory with lifetime and dynamic context.\n */\nexport type Provider<T> = ProviderCallable<T> & {\n /**\n * Mocks(replaces) a provider within the visible graph,\n * returning a new provider with that mock.\n *\n * @param dependencyProvider A provider used by the current provider\n * that needs to be replaced.\n * @param replacement A provider with a same interface\n * that will be a replacement for the first one.\n *\n * @returns A new provider with the mocked dependency provider.\n */\n mock<U>(\n dependencyProvider: Provider<U>,\n replacement: Provider<U>,\n ): Provider<T>;\n};\n\ntype Lifetime = \"transient\" | \"singleton\" | \"scoped\";\n\n/**\n * A function that resolves an instance of a passed provider.\n * It's needed to resolve correct instance in a scope\n * and to use mock of the passed provider, if any.\n */\ntype UseFn = <T>(provider: Provider<T>) => T;\n\n/**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\ntype Resolver<T> = (use: UseFn) => T;\n\n/**\n * Creates a new provider, instance factory with lifetime and dynamic context.\n *\n * @param lifetime - Instance lifetime.\n *\n * @param resolver - A function that creates an instance\n * by resolving its dependencies\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n *\n * @param mockMap - An optional mock map. Not recommended to specify explicitly,\n * use `mock` method to mock providers.\n *\n * @returns A new provider.\n */\nexport const provide = <T>(\n /**\n * Instance lifetime. Can be `\"transient\"`, `\"singleton\"` or `\"scoped\"`.\n *\n * If `\"transient\"`, will return a new instance on each resolution.\n *\n * If `\"singleton\"`, will create an instance once and return it on every request.\n *\n * If `\"scoped\"`, will save the instance in scope on first resolution\n * and will retrieve it from scope on next resolutions.\n * If scope was not specified, will create a new instance on each resolution.\n */\n lifetime: Lifetime,\n\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n\n /**\n * An optional mock map. Not recommended to specify explicitly,\n * use `mock` method to mock providers.\n */\n mockMap: MockMap = createMockMap(),\n): Provider<T> => {\n type ProviderType = Provider<T>;\n\n const getSelf = once(() => Object.assign(resolve, properties));\n\n const originalResolver = resolver;\n resolver = lifetime === \"singleton\" ? once(resolver) : resolver;\n\n const resolve: ProviderCallable<T> = (scope, requestMockMap) => {\n const currentMockMap = requestMockMap\n ? mockMap.apply(requestMockMap)\n : mockMap;\n\n const use: UseFn = (provider) =>\n currentMockMap.map(provider)(scope, currentMockMap);\n\n if (lifetime !== \"scoped\" || !scope) return resolver(use);\n\n const resolution = scope.get(getSelf()) || resolver(use);\n scope.set(getSelf(), resolution);\n\n return resolution;\n };\n\n const mock: ProviderType[\"mock\"] = (dependencyProvider, replacement) =>\n provide(\n lifetime,\n originalResolver,\n mockMap.add(dependencyProvider, replacement),\n );\n\n const properties = {\n mock,\n };\n\n return getSelf();\n};\n\n/**\n * Creates a new transient provider.\n * This provider will return a new instance on each resolution.\n *\n * @param resolver - A function that creates an instance\n * by resolving its dependencies\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n *\n * @returns A new transient provider.\n */\nexport const transient = <T>(resolver: Resolver<T>) =>\n provide(\"transient\", resolver);\n\n/**\n * Creates a new singleton provider.\n * This provider will create an instance once and return it on every request.\n *\n * @param resolver - A function that creates an instance\n * by resolving its dependencies\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n *\n * @returns A new singleton provider.\n */\nexport const singleton = <T>(resolver: Resolver<T>) =>\n provide(\"singleton\", resolver);\n\n/**\n * Creates a new transient provider.\n * This provider will save the instance in scope on first resolution\n * and will retrieve it from scope on next resolutions.\n * If scope was not specified, will create a new instance on each resolution.\n *\n * @param resolver - A function that creates an instance\n * by resolving its dependencies\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n *\n * @returns A new scoped provider.\n */\nexport const scoped = <T>(resolver: Resolver<T>) => provide(\"scoped\", resolver);\n","import { Provider } from \"./provider\";\n\n/**\n * A map of providers to their instances.\n * Passed to the provider call to resolve instances\n * of scoped providers within it.\n */\nexport type Scope = WeakMap<Provider<any>, any>;\n\n/**\n * Creates a new scope, map of providers to their instances.\n * Scope is passed to the provider call to resolve instances\n * of scoped providers within it.\n *\n * @returns A new scope.\n */\nexport const createScope = (): Scope => new WeakMap();\n","import { Provider } from \"./provider\";\nimport { Scope } from \"./scope\";\nimport { MockMap } from \"./mock-map\";\n\n/**\n * A map of string keys to providers.\n */\ntype ProviderMap = Record<string, Provider<any>>;\n\ntype InferProviderMapOutputs<Providers extends ProviderMap> = {\n [K in keyof Providers]: Providers[K] extends Provider<infer T> ? T : never;\n};\n\n/**\n * Resolves all providers in a `ProviderMap` and\n * returns an object containing their instances.\n *\n * @param scope - An optional scope to use for scoped providers.\n * @param mockMap - An optional mock map to use for mocking providers.\n *\n * @returns An object containing the resolved instances of the providers.\n */\ntype ProviderSelectionCallable<Providers extends ProviderMap> = (\n scope?: Scope,\n mockMap?: MockMap,\n) => InferProviderMapOutputs<Providers>;\n\n/**\n * A selection of providers.\n *\n * @property map - The `ProviderMap` that the selection is based on.\n * @property mock - A function that mocks a provider within the selection,\n * returning a new `ProviderSelection` with the mock applied.\n */\nexport type ProviderSelection<Providers extends ProviderMap> =\n ProviderSelectionCallable<Providers> & {\n /**\n * The `ProviderMap` that the selection is based on.\n */\n map: Providers;\n /**\n * Mocks a provider within the selection, returning a new\n * `ProviderSelection` with the mock applied.\n *\n * @param dependencyProvider - A provider used by the current provider\n * that needs to be replaced.\n * @param replacement - A provider with a same interface that will be\n * a replacement for the first one.\n *\n * @returns A new `ProviderSelection` with the mocked provider.\n */\n mock<T>(\n dependencyProvider: Provider<T>,\n replacement: Provider<T>,\n ): ProviderSelection<Providers>;\n };\n\n/**\n * Creates a new provider selection from a provider map.\n *\n * @param map - A map of string keys to providers.\n *\n * @returns A new provider selection.\n */\nexport const select = <Providers extends ProviderMap>(\n map: Providers,\n): ProviderSelection<Providers> => {\n type SelectionType = ProviderSelection<Providers>;\n\n const resolveAll: ProviderSelectionCallable<Providers> = (\n scope,\n mockMap,\n ) => {\n const resultEntries = Object.entries(map).map(([key, provider]) =>\n mockMap\n ? [key, mockMap.map(provider)(scope, mockMap)]\n : [key, provider(scope, mockMap)],\n );\n\n return Object.fromEntries(\n resultEntries,\n ) as InferProviderMapOutputs<Providers>;\n };\n\n const mock: SelectionType[\"mock\"] = (dependencyProvider, replacement) => {\n const newEntries = Object.entries(map).map(([key, provider]) =>\n provider === dependencyProvider\n ? [key, replacement]\n : [key, provider.mock(dependencyProvider, replacement)],\n );\n\n return select(Object.fromEntries(newEntries) as Providers);\n };\n\n return Object.assign(resolveAll, { map, mock });\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOO,IAAM,OAAO,CAAqB,OAA0B;AAC/D,MAAI;AAEJ,SAAO,OAAO;AAAA,IACV,IAAI,SAAY,iBAAiB,eAAe,GAAG,GAAG,IAAI;AAAA,IAC1D;AAAA,EACJ;AACJ;;;ACmCO,IAAM,gBAAgB,CAAC,UAA0B,CAAC,MAAM;AAC3D,QAAM,MAAsB,CAAC,aAAU;AAlD3C;AAmDQ,0BAAQ,KAAK,CAAC,MAAM,EAAE,CAAC,MAAM,QAAQ,MAArC,mBAAyC,OAAM;AAAA;AAEnD,QAAM,MAAsB,CAAC,UAAU,gBAAgB;AACnD,UAAM,aAA6B,QAAQ;AAAA,MAAI,CAAC,MAC5C,EAAE,CAAC,MAAM,WAAW,CAAC,EAAE,CAAC,GAAG,WAAW,IAAI;AAAA,IAC9C;AAEA,QAAI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E,IAAM,cAAc,MAAa,oBAAI,QAAQ;;;ACgD7C,IAAM,SAAS,CAClB,QAC+B;AAG/B,QAAM,aAAmD,CACrD,OACA,YACC;AACD,UAAM,gBAAgB,OAAO,QAAQ,GAAG,EAAE;AAAA,MAAI,CAAC,CAAC,KAAK,QAAQ,MACzD,UACM,CAAC,KAAK,QAAQ,IAAI,QAAQ,EAAE,OAAO,OAAO,CAAC,IAC3C,CAAC,KAAK,SAAS,OAAO,OAAO,CAAC;AAAA,IACxC;AAEA,WAAO,OAAO;AAAA,MACV;AAAA,IACJ;AAAA,EACJ;AAEA,QAAM,OAA8B,CAAC,oBAAoB,gBAAgB;AACrE,UAAM,aAAa,OAAO,QAAQ,GAAG,EAAE;AAAA,MAAI,CAAC,CAAC,KAAK,QAAQ,MACtD,aAAa,qBACP,CAAC,KAAK,WAAW,IACjB,CAAC,KAAK,SAAS,KAAK,oBAAoB,WAAW,CAAC;AAAA,IAC9D;AAEA,WAAO,OAAO,OAAO,YAAY,UAAU,CAAc;AAAA,EAC7D;AAEA,SAAO,OAAO,OAAO,YAAY,EAAE,KAAK,KAAK,CAAC;AAClD;","names":[]}
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/helpers.ts","../src/mock-map.ts","../src/provider.ts","../src/scope.ts","../src/selection.ts"],"sourcesContent":["export * from \"./provider\";\nexport * from \"./scope\";\nexport * from \"./selection\";\nexport * from \"./mock-map\";\n","/**\n * Creates a function that will invoke the provided function `fn` only once,\n * caching the result for subsequent calls with the same arguments.\n *\n * @returns A new function that returns the cached result after the first call.\n */\nexport const once = <A extends any[], R>(\n /**\n * The function to invoke once and cache the result.\n */\n fn: (...args: A) => R,\n) => {\n let cachedResult: R | undefined;\n\n return Object.assign(\n (...args: A) => cachedResult ?? (cachedResult = fn(...args)),\n fn,\n );\n};\n","import { Provider } from \"./provider\";\n\ntype MockMapEntries = [Provider<any>, Provider<any>][];\n\n/**\n * Stores and provides provider mocks.\n */\nexport type MockMap = {\n entries: MockMapEntries;\n\n /**\n * Returns the provider's mock or the provider itself\n * depending on the presence of the mock.\n *\n * @returns The passed provider or its mock.\n */\n map<T>(\n /**\n * An original provider whose mock is needed to get.\n */\n provider: Provider<T>,\n ): Provider<T>;\n\n /**\n * Registers a mock provider and retursns a new mock map with that mock.\n *\n * @returns A new mock map with added mock.\n */\n add<T>(\n /**\n * An original provider.\n */\n provider: Provider<T>,\n /**\n * A provider that will be a mock of the first provider.\n */\n replacement: Provider<T>,\n ): MockMap;\n\n /**\n * Applies mocks from another map to the current one and returns a new map.\n * If there are identical keys (original providers),\n * they will be overwritten by mocks from the other map.\n *\n * @returns A new mock map with applied mocks.\n */\n apply(\n /**\n * A mock map that will be applied.\n */\n otherMockMap: MockMap,\n ): MockMap;\n};\n\n/**\n * Creates a new mock map that stores and provides provider mocks.\n *\n * @returns A new mock map.\n */\nexport const createMockMap = (\n /**\n * Entries of a mock map.\n */\n entries: MockMapEntries = [],\n) => {\n const map: MockMap[\"map\"] = (provider) =>\n entries.find((e) => e[0] === provider)?.[1] || provider;\n\n const add: MockMap[\"add\"] = (provider, replacement) => {\n const newEntries: MockMapEntries = entries.map((e) =>\n e[0] === provider ? [e[0], replacement] : e,\n );\n\n if (newEntries.length === entries.length)\n newEntries.push([provider, replacement]);\n\n return createMockMap(newEntries);\n };\n\n const apply: MockMap[\"apply\"] = (otherMockMap) =>\n createMockMap([\n ...entries.filter(\n (e) =>\n otherMockMap.entries.find((oe) => oe[0] === e[0]) ===\n undefined,\n ),\n ...otherMockMap.entries,\n ]);\n\n return { entries, map, add, apply };\n};\n","import { once } from \"./helpers\";\nimport { Scope } from \"./scope\";\nimport { createMockMap, MockMap } from \"./mock-map\";\n\n/**\n * Calls a resolver(factory) with an optional scope and a mock map\n * and returns an instance.\n *\n * A passed scope will be propagated up a graph and will be passed\n * to all scoped providers to resolve their instance within this scope.\n * If a provider is scoped, it will either create a new instance\n * and store it in the scope, or retrieve an already created instance\n * from the scope.\n *\n * It is also possible to explicitly pass a mock map, but It's not recommended.\n * If you want to mock(replace) providers for a resolution,\n * it's best to use `mock` method.\n */\ntype ProviderCallable<T> = (scope?: Scope, mockMap?: MockMap) => T;\n\n/**\n * Instance factory with lifetime and dynamic context.\n */\nexport type Provider<T> = ProviderCallable<T> & {\n /**\n * Mocks(replaces) a provider within the visible graph,\n * returning a new provider with that mock.\n *\n * @returns A new provider with the mocked dependency provider.\n */\n mock<U>(\n /**\n * A provider used by the current provider\n * that needs to be replaced.\n */\n dependencyProvider: Provider<U>,\n /**\n * A provider with a same interface\n * that will be a replacement for the first one.\n */\n replacement: Provider<U>,\n ): Provider<T>;\n};\n\ntype Lifetime = \"transient\" | \"singleton\" | \"scoped\";\n\n/**\n * A function that resolves an instance of a passed provider.\n * It's needed to resolve correct instance in a scope\n * and to use mock of the passed provider, if any.\n */\ntype UseFn = <T>(provider: Provider<T>) => T;\n\n/**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\ntype Resolver<T> = (use: UseFn) => T;\n\n/**\n * Creates a new provider, instance factory with lifetime and dynamic context.\n *\n * @returns A new provider.\n */\nexport const provide = <T>(\n /**\n * Instance lifetime. Can be `\"transient\"`, `\"singleton\"` or `\"scoped\"`.\n *\n * If `\"transient\"`, will return a new instance on each resolution.\n *\n * If `\"singleton\"`, will create an instance once and return it on every request.\n *\n * If `\"scoped\"`, will save the instance in scope on first resolution\n * and will retrieve it from scope on next resolutions.\n * If scope was not specified, will create a new instance on each resolution.\n */\n lifetime: Lifetime,\n\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n\n /**\n * An optional mock map. Not recommended to specify explicitly,\n * use `mock` method to mock providers.\n */\n mockMap: MockMap = createMockMap(),\n): Provider<T> => {\n type ProviderType = Provider<T>;\n\n const getSelf = once(() => Object.assign(resolve, properties));\n\n const originalResolver = resolver;\n resolver = lifetime === \"singleton\" ? once(resolver) : resolver;\n\n const resolve: ProviderCallable<T> = (scope, requestMockMap) => {\n const currentMockMap = requestMockMap\n ? mockMap.apply(requestMockMap)\n : mockMap;\n\n const use: UseFn = (provider) =>\n currentMockMap.map(provider)(scope, currentMockMap);\n\n if (lifetime !== \"scoped\" || !scope) return resolver(use);\n\n const resolution = scope.get(getSelf()) || resolver(use);\n scope.set(getSelf(), resolution);\n\n return resolution;\n };\n\n const mock: ProviderType[\"mock\"] = (dependencyProvider, replacement) =>\n provide(\n lifetime,\n originalResolver,\n mockMap.add(dependencyProvider, replacement),\n );\n\n const properties = {\n mock,\n };\n\n return getSelf();\n};\n\n/**\n * Creates a new transient provider.\n * This provider will return a new instance on each resolution.\n *\n * @returns A new transient provider.\n */\nexport const transient = <T>(\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n) => provide(\"transient\", resolver);\n\n/**\n * Creates a new singleton provider.\n * This provider will create an instance once and return it on every request.\n *\n * @returns A new singleton provider.\n */\nexport const singleton = <T>(\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n) => provide(\"singleton\", resolver);\n\n/**\n * Creates a new transient provider.\n * This provider will save the instance in scope on first resolution\n * and will retrieve it from scope on next resolutions.\n * If scope was not specified, will create a new instance on each resolution.\n *\n * @returns A new scoped provider.\n */\nexport const scoped = <T>(\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n) => provide(\"scoped\", resolver);\n","import { Provider } from \"./provider\";\n\n/**\n * A map of providers to their instances.\n * Passed to the provider call to resolve instances\n * of scoped providers within it.\n */\nexport type Scope = WeakMap<Provider<any>, any>;\n\n/**\n * Creates a new scope, map of providers to their instances.\n * Scope is passed to the provider call to resolve instances\n * of scoped providers within it.\n *\n * @returns A new scope.\n */\nexport const createScope = (): Scope => new WeakMap();\n","import { Provider } from \"./provider\";\nimport { Scope } from \"./scope\";\nimport { MockMap } from \"./mock-map\";\n\n/**\n * A map of string keys to providers.\n */\ntype ProviderMap = Record<string, Provider<any>>;\n\ntype InferProviderMapOutputs<Providers extends ProviderMap> = {\n [K in keyof Providers]: Providers[K] extends Provider<infer T> ? T : never;\n};\n\n/**\n * Resolves all providers in a `ProviderMap` and\n * returns an object containing their instances.\n *\n * @returns An object containing the resolved instances of the providers.\n */\ntype ProviderSelectionCallable<Providers extends ProviderMap> = (\n /**\n * An optional scope to use for scoped providers.\n */\n scope?: Scope,\n /**\n * An optional mock map to use for mocking providers.\n */\n mockMap?: MockMap,\n) => InferProviderMapOutputs<Providers>;\n\n/**\n * A selection of providers.\n */\nexport type ProviderSelection<Providers extends ProviderMap> =\n ProviderSelectionCallable<Providers> & {\n /**\n * The `ProviderMap` that the selection is based on.\n */\n map: Providers;\n /**\n * Mocks a provider within the selection, returning a new\n * `ProviderSelection` with the mock applied.\n *\n * @returns A new `ProviderSelection` with the mocked provider.\n */\n mock<T>(\n /**\n * A provider used by the current provider\n * that needs to be replaced.\n */\n dependencyProvider: Provider<T>,\n /**\n * A provider with a same interface that will be\n * a replacement for the first one.\n */\n replacement: Provider<T>,\n ): ProviderSelection<Providers>;\n };\n\n/**\n * Creates a new provider selection from a provider map.\n *\n * @returns A new provider selection.\n */\nexport const select = <Providers extends ProviderMap>(\n /**\n * A map of string keys to providers.\n */\n map: Providers,\n): ProviderSelection<Providers> => {\n type SelectionType = ProviderSelection<Providers>;\n\n const resolveAll: ProviderSelectionCallable<Providers> = (\n scope,\n mockMap,\n ) => {\n const resultEntries = Object.entries(map).map(([key, provider]) =>\n mockMap\n ? [key, mockMap.map(provider)(scope, mockMap)]\n : [key, provider(scope, mockMap)],\n );\n\n return Object.fromEntries(\n resultEntries,\n ) as InferProviderMapOutputs<Providers>;\n };\n\n const mock: SelectionType[\"mock\"] = (dependencyProvider, replacement) => {\n const newEntries = Object.entries(map).map(([key, provider]) =>\n provider === dependencyProvider\n ? [key, replacement]\n : [key, provider.mock(dependencyProvider, replacement)],\n );\n\n return select(Object.fromEntries(newEntries) as Providers);\n };\n\n return Object.assign(resolveAll, { map, mock });\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,IAAM,OAAO,CAIhB,OACC;AACD,MAAI;AAEJ,SAAO,OAAO;AAAA,IACV,IAAI,SAAY,iBAAiB,eAAe,GAAG,GAAG,IAAI;AAAA,IAC1D;AAAA,EACJ;AACJ;;;ACyCO,IAAM,gBAAgB,CAIzB,UAA0B,CAAC,MAC1B;AACD,QAAM,MAAsB,CAAC,aAAU;AAjE3C;AAkEQ,0BAAQ,KAAK,CAAC,MAAM,EAAE,CAAC,MAAM,QAAQ,MAArC,mBAAyC,OAAM;AAAA;AAEnD,QAAM,MAAsB,CAAC,UAAU,gBAAgB;AACnD,UAAM,aAA6B,QAAQ;AAAA,MAAI,CAAC,MAC5C,EAAE,CAAC,MAAM,WAAW,CAAC,EAAE,CAAC,GAAG,WAAW,IAAI;AAAA,IAC9C;AAEA,QAAI,WAAW,WAAW,QAAQ;AAC9B,iBAAW,KAAK,CAAC,UAAU,WAAW,CAAC;AAE3C,WAAO,cAAc,UAAU;AAAA,EACnC;AAEA,QAAM,QAA0B,CAAC,iBAC7B,cAAc;AAAA,IACV,GAAG,QAAQ;AAAA,MACP,CAAC,MACG,aAAa,QAAQ,KAAK,CAAC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,MAChD;AAAA,IACR;AAAA,IACA,GAAG,aAAa;AAAA,EACpB,CAAC;AAEL,SAAO,EAAE,SAAS,KAAK,KAAK,MAAM;AACtC;;;ACzBO,IAAM,UAAU,CAYnB,UAOA,UAMA,UAAmB,cAAc,MACnB;AAGd,QAAM,UAAU,KAAK,MAAM,OAAO,OAAO,SAAS,UAAU,CAAC;AAE7D,QAAM,mBA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Aa,qBACP,CAAC,KAAK,WAAW,IACjB,CAAC,KAAK,SAAS,KAAK,oBAAoB,WAAW,CAAC;AAAA,IAC9D;AAEA,WAAO,OAAO,OAAO,YAAY,UAAU,CAAc;AAAA,EAC7D;AAEA,SAAO,OAAO,OAAO,YAAY,EAAE,KAAK,KAAK,CAAC;AAClD;","names":[]}
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/helpers.ts","../src/mock-map.ts","../src/provider.ts","../src/scope.ts","../src/selection.ts"],"sourcesContent":["/**\n * Creates a function that will invoke the provided function `fn` only once,\n * caching the result for subsequent calls with the same arguments.\n *\n * @param fn - The function to invoke once and cache the result.\n * @returns A new function that returns the cached result after the first call.\n */\nexport const once = <A extends any[], R>(fn: (...args: A) => R) => {\n let cachedResult: R | undefined;\n\n return Object.assign(\n (...args: A) => cachedResult ?? (cachedResult = fn(...args)),\n fn,\n );\n};\n","import { Provider } from \"./provider\";\n\ntype MockMapEntries = [Provider<any>, Provider<any>][];\n\n/**\n * Stores and provides provider mocks.\n */\nexport type MockMap = {\n entries: MockMapEntries;\n\n /**\n * Returns the provider's mock or the provider itself\n * depending on the presence of the mock.\n *\n * @param provider - An original provider whose mock is needed to get.\n *\n * @returns The passed provider or its mock.\n */\n map<T>(provider: Provider<T>): Provider<T>;\n\n /**\n * Registers a mock provider and retursns a new mock map with that mock.\n *\n * @param provider - An original provider.\n * @param replacement - A provider that will be a mock of the first provider.\n *\n * @returns A new mock map with added mock.\n */\n add<T>(provider: Provider<T>, replacement: Provider<T>): MockMap;\n\n /**\n * Applies mocks from another map to the current one and returns a new map.\n * If there are identical keys (original providers),\n * they will be overwritten by mocks from the other map.\n *\n * @param otherMockMap - A mock map that will be applied.\n *\n * @returns A new mock map with applied mocks.\n */\n apply(otherMockMap: MockMap): MockMap;\n};\n\n/**\n * Creates a new mock map that stores and provides provider mocks.\n *\n * @param entries - Entries of a mock map.\n *\n * @returns A new mock map.\n */\nexport const createMockMap = (entries: MockMapEntries = []) => {\n const map: MockMap[\"map\"] = (provider) =>\n entries.find((e) => e[0] === provider)?.[1] || provider;\n\n const add: MockMap[\"add\"] = (provider, replacement) => {\n const newEntries: MockMapEntries = entries.map((e) =>\n e[0] === provider ? [e[0], replacement] : e,\n );\n\n if (newEntries.length === entries.length)\n newEntries.push([provider, replacement]);\n\n return createMockMap(newEntries);\n };\n\n const apply: MockMap[\"apply\"] = (otherMockMap) =>\n createMockMap([\n ...entries.filter(\n (e) =>\n otherMockMap.entries.find((oe) => oe[0] === e[0]) ===\n undefined,\n ),\n ...otherMockMap.entries,\n ]);\n\n return { entries, map, add, apply };\n};\n","import { once } from \"./helpers\";\nimport { Scope } from \"./scope\";\nimport { createMockMap, MockMap } from \"./mock-map\";\n\n/**\n * Calls a resolver(factory) with an optional scope and a mock map\n * and returns an instance.\n *\n * A passed scope will be propagated up a graph and will be passed\n * to all scoped providers to resolve their instance within this scope.\n * If a provider is scoped, it will either create a new instance\n * and store it in the scope, or retrieve an already created instance\n * from the scope.\n *\n * It is also possible to explicitly pass a mock map, but It's not recommended.\n * If you want to mock(replace) providers for a resolution,\n * it's best to use `mock` method.\n */\ntype ProviderCallable<T> = (scope?: Scope, mockMap?: MockMap) => T;\n\n/**\n * Instance factory with lifetime and dynamic context.\n */\nexport type Provider<T> = ProviderCallable<T> & {\n /**\n * Mocks(replaces) a provider within the visible graph,\n * returning a new provider with that mock.\n *\n * @param dependencyProvider A provider used by the current provider\n * that needs to be replaced.\n * @param replacement A provider with a same interface\n * that will be a replacement for the first one.\n *\n * @returns A new provider with the mocked dependency provider.\n */\n mock<U>(\n dependencyProvider: Provider<U>,\n replacement: Provider<U>,\n ): Provider<T>;\n};\n\ntype Lifetime = \"transient\" | \"singleton\" | \"scoped\";\n\n/**\n * A function that resolves an instance of a passed provider.\n * It's needed to resolve correct instance in a scope\n * and to use mock of the passed provider, if any.\n */\ntype UseFn = <T>(provider: Provider<T>) => T;\n\n/**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\ntype Resolver<T> = (use: UseFn) => T;\n\n/**\n * Creates a new provider, instance factory with lifetime and dynamic context.\n *\n * @param lifetime - Instance lifetime.\n *\n * @param resolver - A function that creates an instance\n * by resolving its dependencies\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n *\n * @param mockMap - An optional mock map. Not recommended to specify explicitly,\n * use `mock` method to mock providers.\n *\n * @returns A new provider.\n */\nexport const provide = <T>(\n /**\n * Instance lifetime. Can be `\"transient\"`, `\"singleton\"` or `\"scoped\"`.\n *\n * If `\"transient\"`, will return a new instance on each resolution.\n *\n * If `\"singleton\"`, will create an instance once and return it on every request.\n *\n * If `\"scoped\"`, will save the instance in scope on first resolution\n * and will retrieve it from scope on next resolutions.\n * If scope was not specified, will create a new instance on each resolution.\n */\n lifetime: Lifetime,\n\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n\n /**\n * An optional mock map. Not recommended to specify explicitly,\n * use `mock` method to mock providers.\n */\n mockMap: MockMap = createMockMap(),\n): Provider<T> => {\n type ProviderType = Provider<T>;\n\n const getSelf = once(() => Object.assign(resolve, properties));\n\n const originalResolver = resolver;\n resolver = lifetime === \"singleton\" ? once(resolver) : resolver;\n\n const resolve: ProviderCallable<T> = (scope, requestMockMap) => {\n const currentMockMap = requestMockMap\n ? mockMap.apply(requestMockMap)\n : mockMap;\n\n const use: UseFn = (provider) =>\n currentMockMap.map(provider)(scope, currentMockMap);\n\n if (lifetime !== \"scoped\" || !scope) return resolver(use);\n\n const resolution = scope.get(getSelf()) || resolver(use);\n scope.set(getSelf(), resolution);\n\n return resolution;\n };\n\n const mock: ProviderType[\"mock\"] = (dependencyProvider, replacement) =>\n provide(\n lifetime,\n originalResolver,\n mockMap.add(dependencyProvider, replacement),\n );\n\n const properties = {\n mock,\n };\n\n return getSelf();\n};\n\n/**\n * Creates a new transient provider.\n * This provider will return a new instance on each resolution.\n *\n * @param resolver - A function that creates an instance\n * by resolving its dependencies\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n *\n * @returns A new transient provider.\n */\nexport const transient = <T>(resolver: Resolver<T>) =>\n provide(\"transient\", resolver);\n\n/**\n * Creates a new singleton provider.\n * This provider will create an instance once and return it on every request.\n *\n * @param resolver - A function that creates an instance\n * by resolving its dependencies\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n *\n * @returns A new singleton provider.\n */\nexport const singleton = <T>(resolver: Resolver<T>) =>\n provide(\"singleton\", resolver);\n\n/**\n * Creates a new transient provider.\n * This provider will save the instance in scope on first resolution\n * and will retrieve it from scope on next resolutions.\n * If scope was not specified, will create a new instance on each resolution.\n *\n * @param resolver - A function that creates an instance\n * by resolving its dependencies\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n *\n * @returns A new scoped provider.\n */\nexport const scoped = <T>(resolver: Resolver<T>) => provide(\"scoped\", resolver);\n","import { Provider } from \"./provider\";\n\n/**\n * A map of providers to their instances.\n * Passed to the provider call to resolve instances\n * of scoped providers within it.\n */\nexport type Scope = WeakMap<Provider<any>, any>;\n\n/**\n * Creates a new scope, map of providers to their instances.\n * Scope is passed to the provider call to resolve instances\n * of scoped providers within it.\n *\n * @returns A new scope.\n */\nexport const createScope = (): Scope => new WeakMap();\n","import { Provider } from \"./provider\";\nimport { Scope } from \"./scope\";\nimport { MockMap } from \"./mock-map\";\n\n/**\n * A map of string keys to providers.\n */\ntype ProviderMap = Record<string, Provider<any>>;\n\ntype InferProviderMapOutputs<Providers extends ProviderMap> = {\n [K in keyof Providers]: Providers[K] extends Provider<infer T> ? T : never;\n};\n\n/**\n * Resolves all providers in a `ProviderMap` and\n * returns an object containing their instances.\n *\n * @param scope - An optional scope to use for scoped providers.\n * @param mockMap - An optional mock map to use for mocking providers.\n *\n * @returns An object containing the resolved instances of the providers.\n */\ntype ProviderSelectionCallable<Providers extends ProviderMap> = (\n scope?: Scope,\n mockMap?: MockMap,\n) => InferProviderMapOutputs<Providers>;\n\n/**\n * A selection of providers.\n *\n * @property map - The `ProviderMap` that the selection is based on.\n * @property mock - A function that mocks a provider within the selection,\n * returning a new `ProviderSelection` with the mock applied.\n */\nexport type ProviderSelection<Providers extends ProviderMap> =\n ProviderSelectionCallable<Providers> & {\n /**\n * The `ProviderMap` that the selection is based on.\n */\n map: Providers;\n /**\n * Mocks a provider within the selection, returning a new\n * `ProviderSelection` with the mock applied.\n *\n * @param dependencyProvider - A provider used by the current provider\n * that needs to be replaced.\n * @param replacement - A provider with a same interface that will be\n * a replacement for the first one.\n *\n * @returns A new `ProviderSelection` with the mocked provider.\n */\n mock<T>(\n dependencyProvider: Provider<T>,\n replacement: Provider<T>,\n ): ProviderSelection<Providers>;\n };\n\n/**\n * Creates a new provider selection from a provider map.\n *\n * @param map - A map of string keys to providers.\n *\n * @returns A new provider selection.\n */\nexport const select = <Providers extends ProviderMap>(\n map: Providers,\n): ProviderSelection<Providers> => {\n type SelectionType = ProviderSelection<Providers>;\n\n const resolveAll: ProviderSelectionCallable<Providers> = (\n scope,\n mockMap,\n ) => {\n const resultEntries = Object.entries(map).map(([key, provider]) =>\n mockMap\n ? [key, mockMap.map(provider)(scope, mockMap)]\n : [key, provider(scope, mockMap)],\n );\n\n return Object.fromEntries(\n resultEntries,\n ) as InferProviderMapOutputs<Providers>;\n };\n\n const mock: SelectionType[\"mock\"] = (dependencyProvider, replacement) => {\n const newEntries = Object.entries(map).map(([key, provider]) =>\n provider === dependencyProvider\n ? [key, replacement]\n : [key, provider.mock(dependencyProvider, replacement)],\n );\n\n return select(Object.fromEntries(newEntries) as Providers);\n };\n\n return Object.assign(resolveAll, { map, mock });\n};\n"],"mappings":";AAOO,IAAM,OAAO,CAAqB,OAA0B;AAC/D,MAAI;AAEJ,SAAO,OAAO;AAAA,IACV,IAAI,SAAY,iBAAiB,eAAe,GAAG,GAAG,IAAI;AAAA,IAC1D;AAAA,EACJ;AACJ;;;ACm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X;AAEA,QAAM,OAA6B,CAAC,oBAAoB,gBACpD;AAAA,IACI;AAAA,IACA;AAAA,IACA,QAAQ,IAAI,oBAAoB,WAAW;AAAA,EAC/C;AAEJ,QAAM,aAAa;AAAA,IACf;AAAA,EACJ;AAEA,SAAO,QAAQ;AACnB;AAaO,IAAM,YAAY,CAAI,aACzB,QAAQ,aAAa,QAAQ;AAa1B,IAAM,YAAY,CAAI,aACzB,QAAQ,aAAa,QAAQ;AAe1B,IAAM,SAAS,CAAI,aAA0B,QAAQ,UAAU,QAAQ;;;ACjKvE,IAAM,cAAc,MAAa,oBAAI,QAAQ;;;ACgD7C,IAAM,SAAS,CAClB,QAC+B;AAG/B,QAAM,aAAmD,CACrD,OACA,YACC;AACD,UAAM,gBAAgB,OAAO,QAAQ,GAAG,EAAE;AAAA,MAAI,CAAC,CAAC,KAAK,QAAQ,MACzD,UACM,CAAC,KAAK,QAAQ,IAAI,QAAQ,EAAE,OAAO,OAAO,CAAC,IAC3C,CAAC,KAAK,SAAS,OAAO,OAAO,CAAC;AAAA,IACxC;AAEA,WAAO,OAAO;AAAA,MACV;AAAA,IACJ;AAAA,EACJ;AAEA,QAAM,OAA8B,CAAC,oBAAoB,gBAAgB;AACrE,UAAM,aAAa,OAAO,QAAQ,GAAG,EAAE;AAAA,MAAI,CAAC,CAAC,KAAK,QAAQ,MACtD,aAAa,qBACP,CAAC,KAAK,WAAW,IACjB,CAAC,KAAK,SAAS,KAAK,oBAAoB,WAAW,CAAC;AAAA,IAC9D;AAEA,WAAO,OAAO,OAAO,YAAY,UAAU,CAAc;AAAA,EAC7D;AAEA,SAAO,OAAO,OAAO,YAAY,EAAE,KAAK,KAAK,CAAC;AAClD;","names":[]}
|
1
|
+
{"version":3,"sources":["../src/helpers.ts","../src/mock-map.ts","../src/provider.ts","../src/scope.ts","../src/selection.ts"],"sourcesContent":["/**\n * Creates a function that will invoke the provided function `fn` only once,\n * caching the result for subsequent calls with the same arguments.\n *\n * @returns A new function that returns the cached result after the first call.\n */\nexport const once = <A extends any[], R>(\n /**\n * The function to invoke once and cache the result.\n */\n fn: (...args: A) => R,\n) => {\n let cachedResult: R | undefined;\n\n return Object.assign(\n (...args: A) => cachedResult ?? (cachedResult = fn(...args)),\n fn,\n );\n};\n","import { Provider } from \"./provider\";\n\ntype MockMapEntries = [Provider<any>, Provider<any>][];\n\n/**\n * Stores and provides provider mocks.\n */\nexport type MockMap = {\n entries: MockMapEntries;\n\n /**\n * Returns the provider's mock or the provider itself\n * depending on the presence of the mock.\n *\n * @returns The passed provider or its mock.\n */\n map<T>(\n /**\n * An original provider whose mock is needed to get.\n */\n provider: Provider<T>,\n ): Provider<T>;\n\n /**\n * Registers a mock provider and retursns a new mock map with that mock.\n *\n * @returns A new mock map with added mock.\n */\n add<T>(\n /**\n * An original provider.\n */\n provider: Provider<T>,\n /**\n * A provider that will be a mock of the first provider.\n */\n replacement: Provider<T>,\n ): MockMap;\n\n /**\n * Applies mocks from another map to the current one and returns a new map.\n * If there are identical keys (original providers),\n * they will be overwritten by mocks from the other map.\n *\n * @returns A new mock map with applied mocks.\n */\n apply(\n /**\n * A mock map that will be applied.\n */\n otherMockMap: MockMap,\n ): MockMap;\n};\n\n/**\n * Creates a new mock map that stores and provides provider mocks.\n *\n * @returns A new mock map.\n */\nexport const createMockMap = (\n /**\n * Entries of a mock map.\n */\n entries: MockMapEntries = [],\n) => {\n const map: MockMap[\"map\"] = (provider) =>\n entries.find((e) => e[0] === provider)?.[1] || provider;\n\n const add: MockMap[\"add\"] = (provider, replacement) => {\n const newEntries: MockMapEntries = entries.map((e) =>\n e[0] === provider ? [e[0], replacement] : e,\n );\n\n if (newEntries.length === entries.length)\n newEntries.push([provider, replacement]);\n\n return createMockMap(newEntries);\n };\n\n const apply: MockMap[\"apply\"] = (otherMockMap) =>\n createMockMap([\n ...entries.filter(\n (e) =>\n otherMockMap.entries.find((oe) => oe[0] === e[0]) ===\n undefined,\n ),\n ...otherMockMap.entries,\n ]);\n\n return { entries, map, add, apply };\n};\n","import { once } from \"./helpers\";\nimport { Scope } from \"./scope\";\nimport { createMockMap, MockMap } from \"./mock-map\";\n\n/**\n * Calls a resolver(factory) with an optional scope and a mock map\n * and returns an instance.\n *\n * A passed scope will be propagated up a graph and will be passed\n * to all scoped providers to resolve their instance within this scope.\n * If a provider is scoped, it will either create a new instance\n * and store it in the scope, or retrieve an already created instance\n * from the scope.\n *\n * It is also possible to explicitly pass a mock map, but It's not recommended.\n * If you want to mock(replace) providers for a resolution,\n * it's best to use `mock` method.\n */\ntype ProviderCallable<T> = (scope?: Scope, mockMap?: MockMap) => T;\n\n/**\n * Instance factory with lifetime and dynamic context.\n */\nexport type Provider<T> = ProviderCallable<T> & {\n /**\n * Mocks(replaces) a provider within the visible graph,\n * returning a new provider with that mock.\n *\n * @returns A new provider with the mocked dependency provider.\n */\n mock<U>(\n /**\n * A provider used by the current provider\n * that needs to be replaced.\n */\n dependencyProvider: Provider<U>,\n /**\n * A provider with a same interface\n * that will be a replacement for the first one.\n */\n replacement: Provider<U>,\n ): Provider<T>;\n};\n\ntype Lifetime = \"transient\" | \"singleton\" | \"scoped\";\n\n/**\n * A function that resolves an instance of a passed provider.\n * It's needed to resolve correct instance in a scope\n * and to use mock of the passed provider, if any.\n */\ntype UseFn = <T>(provider: Provider<T>) => T;\n\n/**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\ntype Resolver<T> = (use: UseFn) => T;\n\n/**\n * Creates a new provider, instance factory with lifetime and dynamic context.\n *\n * @returns A new provider.\n */\nexport const provide = <T>(\n /**\n * Instance lifetime. Can be `\"transient\"`, `\"singleton\"` or `\"scoped\"`.\n *\n * If `\"transient\"`, will return a new instance on each resolution.\n *\n * If `\"singleton\"`, will create an instance once and return it on every request.\n *\n * If `\"scoped\"`, will save the instance in scope on first resolution\n * and will retrieve it from scope on next resolutions.\n * If scope was not specified, will create a new instance on each resolution.\n */\n lifetime: Lifetime,\n\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n\n /**\n * An optional mock map. Not recommended to specify explicitly,\n * use `mock` method to mock providers.\n */\n mockMap: MockMap = createMockMap(),\n): Provider<T> => {\n type ProviderType = Provider<T>;\n\n const getSelf = once(() => Object.assign(resolve, properties));\n\n const originalResolver = resolver;\n resolver = lifetime === \"singleton\" ? once(resolver) : resolver;\n\n const resolve: ProviderCallable<T> = (scope, requestMockMap) => {\n const currentMockMap = requestMockMap\n ? mockMap.apply(requestMockMap)\n : mockMap;\n\n const use: UseFn = (provider) =>\n currentMockMap.map(provider)(scope, currentMockMap);\n\n if (lifetime !== \"scoped\" || !scope) return resolver(use);\n\n const resolution = scope.get(getSelf()) || resolver(use);\n scope.set(getSelf(), resolution);\n\n return resolution;\n };\n\n const mock: ProviderType[\"mock\"] = (dependencyProvider, replacement) =>\n provide(\n lifetime,\n originalResolver,\n mockMap.add(dependencyProvider, replacement),\n );\n\n const properties = {\n mock,\n };\n\n return getSelf();\n};\n\n/**\n * Creates a new transient provider.\n * This provider will return a new instance on each resolution.\n *\n * @returns A new transient provider.\n */\nexport const transient = <T>(\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n) => provide(\"transient\", resolver);\n\n/**\n * Creates a new singleton provider.\n * This provider will create an instance once and return it on every request.\n *\n * @returns A new singleton provider.\n */\nexport const singleton = <T>(\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n) => provide(\"singleton\", resolver);\n\n/**\n * Creates a new transient provider.\n * This provider will save the instance in scope on first resolution\n * and will retrieve it from scope on next resolutions.\n * If scope was not specified, will create a new instance on each resolution.\n *\n * @returns A new scoped provider.\n */\nexport const scoped = <T>(\n /**\n * A function that creates an instance by resolving its dependencies.\n * If there are dependencies, you must use `use` function passed\n * in the first argument.\n */\n resolver: Resolver<T>,\n) => provide(\"scoped\", resolver);\n","import { Provider } from \"./provider\";\n\n/**\n * A map of providers to their instances.\n * Passed to the provider call to resolve instances\n * of scoped providers within it.\n */\nexport type Scope = WeakMap<Provider<any>, any>;\n\n/**\n * Creates a new scope, map of providers to their instances.\n * Scope is passed to the provider call to resolve instances\n * of scoped providers within it.\n *\n * @returns A new scope.\n */\nexport const createScope = (): Scope => new WeakMap();\n","import { Provider } from \"./provider\";\nimport { Scope } from \"./scope\";\nimport { MockMap } from \"./mock-map\";\n\n/**\n * A map of string keys to providers.\n */\ntype ProviderMap = Record<string, Provider<any>>;\n\ntype InferProviderMapOutputs<Providers extends ProviderMap> = {\n [K in keyof Providers]: Providers[K] extends Provider<infer T> ? T : never;\n};\n\n/**\n * Resolves all providers in a `ProviderMap` and\n * returns an object containing their instances.\n *\n * @returns An object containing the resolved instances of the providers.\n */\ntype ProviderSelectionCallable<Providers extends ProviderMap> = (\n /**\n * An optional scope to use for scoped providers.\n */\n scope?: Scope,\n /**\n * An optional mock map to use for mocking providers.\n */\n mockMap?: MockMap,\n) => InferProviderMapOutputs<Providers>;\n\n/**\n * A selection of providers.\n */\nexport type ProviderSelection<Providers extends ProviderMap> =\n ProviderSelectionCallable<Providers> & {\n /**\n * The `ProviderMap` that the selection is based on.\n */\n map: Providers;\n /**\n * Mocks a provider within the selection, returning a new\n * `ProviderSelection` with the mock applied.\n *\n * @returns A new `ProviderSelection` with the mocked provider.\n */\n mock<T>(\n /**\n * A provider used by the current provider\n * that needs to be replaced.\n */\n dependencyProvider: Provider<T>,\n /**\n * A provider with a same interface that will be\n * a replacement for the first one.\n */\n replacement: Provider<T>,\n ): ProviderSelection<Providers>;\n };\n\n/**\n * Creates a new provider selection from a provider map.\n *\n * @returns A new provider selection.\n */\nexport const select = <Providers extends ProviderMap>(\n /**\n * A map of string keys to providers.\n */\n map: Providers,\n): ProviderSelection<Providers> => {\n type SelectionType = ProviderSelection<Providers>;\n\n const resolveAll: ProviderSelectionCallable<Providers> = (\n scope,\n mockMap,\n ) => {\n const resultEntries = Object.entries(map).map(([key, provider]) =>\n mockMap\n ? [key, mockMap.map(provider)(scope, mockMap)]\n : [key, provider(scope, mockMap)],\n );\n\n return Object.fromEntries(\n resultEntries,\n ) as InferProviderMapOutputs<Providers>;\n };\n\n const mock: SelectionType[\"mock\"] = (dependencyProvider, replacement) => {\n const newEntries = Object.entries(map).map(([key, provider]) =>\n provider === dependencyProvider\n ? [key, replacement]\n : [key, provider.mock(dependencyProvider, replacement)],\n );\n\n return select(Object.fromEntries(newEntries) as Providers);\n };\n\n return Object.assign(resolveAll, { map, mock });\n};\n"],"mappings":";AAMO,IAAM,OAAO,CAIhB,OACC;AACD,MAAI;AAEJ,SAAO,OAAO;AAAA,IACV,IAAI,SAAY,iBAAiB,eAAe,GAAG,GAAG,IAAI;AAAA,IAC1D;AAAA,EACJ;AACJ;;;ACyCO,IAAM,gBAAgB,CAIzB,UAA0B,CAAC,MAC1B;AACD,QAAM,MAAsB,CAAC,aAAU;AAjE3C;AAkEQ,0BAAQ,KAAK,CAAC,MAAM,EAAE,CAAC,MAAM,QAAQ,MAArC,mBAAyC,OAAM;AAAA;AAEnD,QAAM,MAAsB,CAAC,UAAU,gBAAgB;AACnD,UAAM,aAA6B,QAAQ;AAAA,MAAI,CAAC,MAC5C,EAAE,CAAC,MAAM,WAAW,CAAC,EAAE,CAAC,GAAG,WAAW,IAAI;AAAA,IAC9C;AAEA,QAAI,WAAW,WAAW,QAAQ;AAC9B,iBAAW,KAAK,CAAC,UAAU,WAAW,CAAC;AAE3C,WAAO,cAAc,UAAU;AAAA,EACnC;AAEA,QAAM,QAA0B,CAAC,iBAC7B,cAAc;AAAA,IACV,GAAG,QAAQ;AAAA,MACP,CAAC,MACG,aAAa,QAAQ,KAAK,CAAC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,MAChD;AAAA,IACR;AAAA,IACA,GAAG,aAAa;AAAA,EACpB,CAAC;AA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