@adimm/x-injection-reactjs 0.3.0 → 1.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 +197 -123
- package/dist/index.cjs +84 -150
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +97 -133
- package/dist/index.d.ts +97 -133
- package/dist/index.js +63 -129
- package/dist/index.js.map +1 -1
- package/package.json +13 -13
package/dist/index.cjs
CHANGED
|
@@ -1,196 +1,130 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var e,
|
|
4
|
-
value:
|
|
3
|
+
var e, r = Object.create, t = Object.defineProperty, o = Object.getOwnPropertyDescriptor, n = Object.getOwnPropertyNames, u = Object.getPrototypeOf, i = Object.prototype.hasOwnProperty, c = (e, r) => t(e, "name", {
|
|
4
|
+
value: r,
|
|
5
5
|
configurable: !0
|
|
6
|
-
}), a = (e,
|
|
7
|
-
if (
|
|
8
|
-
get: () =>
|
|
9
|
-
enumerable: !(
|
|
6
|
+
}), a = (e, r, u, c) => {
|
|
7
|
+
if (r && "object" == typeof r || "function" == typeof r) for (let a of n(r)) i.call(e, a) || a === u || t(e, a, {
|
|
8
|
+
get: () => r[a],
|
|
9
|
+
enumerable: !(c = o(r, a)) || c.enumerable
|
|
10
10
|
});
|
|
11
11
|
return e;
|
|
12
|
-
}, d =
|
|
13
|
-
value: e,
|
|
14
|
-
enumerable: !0
|
|
15
|
-
}), e)), c = {};
|
|
12
|
+
}, d = {};
|
|
16
13
|
|
|
17
|
-
((e,
|
|
18
|
-
for (var
|
|
19
|
-
get:
|
|
14
|
+
((e, r) => {
|
|
15
|
+
for (var o in r) t(e, o, {
|
|
16
|
+
get: r[o],
|
|
20
17
|
enumerable: !0
|
|
21
18
|
});
|
|
22
|
-
})(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
useInjectMany: () => M
|
|
31
|
-
}), module.exports = (e = c, a(o({}, "__esModule", {
|
|
19
|
+
})(d, {
|
|
20
|
+
REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT: () => l,
|
|
21
|
+
hookFactory: () => _,
|
|
22
|
+
provideModuleToComponent: () => T,
|
|
23
|
+
useComponentModule: () => m,
|
|
24
|
+
useInject: () => f,
|
|
25
|
+
useInjectMany: () => v
|
|
26
|
+
}), module.exports = (e = d, a(t({}, "__esModule", {
|
|
32
27
|
value: !0
|
|
33
28
|
}), e));
|
|
34
29
|
|
|
35
|
-
var
|
|
30
|
+
var s = require("@adimm/x-injection"), l = (0, require("react").createContext)(s.AppModule), p = require("react");
|
|
36
31
|
|
|
37
|
-
function
|
|
38
|
-
return (0,
|
|
32
|
+
function m() {
|
|
33
|
+
return (0, p.useContext)(l);
|
|
39
34
|
}
|
|
40
35
|
|
|
41
|
-
function
|
|
42
|
-
return
|
|
36
|
+
function f(e, r) {
|
|
37
|
+
return m().get(e, r?.isOptional, r?.asList);
|
|
43
38
|
}
|
|
44
39
|
|
|
45
|
-
function
|
|
46
|
-
return
|
|
40
|
+
function v(...e) {
|
|
41
|
+
return m().getMany(...e);
|
|
47
42
|
}
|
|
48
43
|
|
|
49
|
-
|
|
44
|
+
c(m, "useComponentModule"), c(f, "useInject"), c(v, "useInjectMany");
|
|
50
45
|
|
|
51
|
-
var
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
identifier: t,
|
|
64
|
-
imports: o ? e.imports?.map((e => {
|
|
65
|
-
const n = "function" == typeof e ? e() : e;
|
|
66
|
-
return o ? () => {
|
|
67
|
-
const e = n._createContextualizedComponentInstance(t);
|
|
68
|
-
return r.set(n.originalIdentifier.toString(), e), e;
|
|
69
|
-
} : n;
|
|
70
|
-
})) : e.imports,
|
|
71
|
-
dynamicExports: s(((e, t) => o ? t.map((e => {
|
|
72
|
-
if (!(e instanceof v.ProviderModule)) return e;
|
|
73
|
-
return r.get(e.originalIdentifier.toString());
|
|
74
|
-
})) : t), "dynamicExports")
|
|
75
|
-
})), this.originalIdentifier = e.identifier, this.hasContextualizedImports = o,
|
|
76
|
-
this.initializedFromComponent = !1;
|
|
77
|
-
}
|
|
78
|
-
toNaked() {
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
|
-
clone(t) {
|
|
82
|
-
const o = t, r = new e(v.ProviderModuleHelpers.buildInternalConstructorParams({
|
|
83
|
-
isAppModule: this.isAppModule,
|
|
84
|
-
markAsGlobal: this.isMarkedAsGlobal,
|
|
85
|
-
identifier: Symbol(this.identifier.description.replace("Component", "")),
|
|
86
|
-
defaultScope: this.defaultScope.native,
|
|
87
|
-
dynamicExports: this.dynamicExports,
|
|
88
|
-
onReady: this.onReady,
|
|
89
|
-
onDispose: this.onDispose,
|
|
90
|
-
contextualizeImports: this.hasContextualizedImports,
|
|
91
|
-
importedProvidersMap: this.importedProvidersMap,
|
|
92
|
-
imports: [ ...this.imports ],
|
|
93
|
-
providers: [ ...this.providers ],
|
|
94
|
-
exports: [ ...this.exports ],
|
|
95
|
-
...o
|
|
96
|
-
}));
|
|
97
|
-
return r.initializedFromComponent = this.initializedFromComponent, r;
|
|
98
|
-
}
|
|
99
|
-
async dispose() {
|
|
100
|
-
await super.dispose();
|
|
101
|
-
}
|
|
102
|
-
_createContextualizedComponentInstance(e) {
|
|
103
|
-
if (this.initializedFromComponent) return this;
|
|
104
|
-
const t = this.clone().toNaked();
|
|
105
|
-
return t.identifier = Symbol(`${e ? `[Parent:${e.description ?? "Unknown"}]` : ""}Contextualized${t.identifier.description}`),
|
|
106
|
-
t.initializedFromComponent = !0, t;
|
|
107
|
-
}
|
|
108
|
-
}, y = d(require("react"), 1), P = require("react"), x = require("react");
|
|
109
|
-
|
|
110
|
-
function I(e) {
|
|
111
|
-
const t = (0, x.useRef)(void 0), o = (0, x.useRef)(!1), r = (0, x.useRef)(!1), [, n] = (0,
|
|
112
|
-
x.useState)(0);
|
|
113
|
-
o.current && (r.current = !0), (0, x.useEffect)((() => (o.current || (t.current = e(),
|
|
114
|
-
o.current = !0), n((e => e + 1)), () => {
|
|
115
|
-
r.current && t.current?.();
|
|
116
|
-
})), []);
|
|
46
|
+
var M = ((e, o, n) => (n = null != e ? r(u(e)) : {}, a(!o && e && e.__esModule ? n : t(n, "default", {
|
|
47
|
+
value: e,
|
|
48
|
+
enumerable: !0
|
|
49
|
+
}), e)))(require("react"), 1), j = require("@adimm/x-injection"), P = require("react"), y = require("react");
|
|
50
|
+
|
|
51
|
+
function b(e) {
|
|
52
|
+
const r = (0, y.useRef)(void 0), t = (0, y.useRef)(!1), o = (0, y.useRef)(!1), [, n] = (0,
|
|
53
|
+
y.useState)(0);
|
|
54
|
+
t.current && (o.current = !0), (0, y.useEffect)(() => (t.current || (r.current = e(),
|
|
55
|
+
t.current = !0), n(e => e + 1), () => {
|
|
56
|
+
o.current && r.current?.();
|
|
57
|
+
}), []);
|
|
117
58
|
}
|
|
118
59
|
|
|
119
|
-
function
|
|
120
|
-
const
|
|
121
|
-
const o = (t ?? e)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
60
|
+
function O(e, r) {
|
|
61
|
+
const t = (0, P.useMemo)(() => {
|
|
62
|
+
const {module: t, inject: o} = r ?? {}, n = j.ProviderModuleHelpers.tryBlueprintToModule(t ?? e);
|
|
63
|
+
if (n.options.isGlobal) throw new j.InjectionProviderModuleError(n, "A 'global' module can't be supplied to a component!");
|
|
64
|
+
return o?.forEach(e => {
|
|
65
|
+
if (!n.hasProvider(e)) throw new j.InjectionProviderModuleError(n, `The [${j.ProviderTokenHelpers.providerTokenToString(e)}] provider can't be replaced because it is not part of the component module!`);
|
|
66
|
+
n.update.removeProvider(j.ProviderTokenHelpers.toProviderIdentifier(e)), n.update.addProvider(e);
|
|
67
|
+
}), n;
|
|
68
|
+
}, [ e, r?.inject ]);
|
|
69
|
+
return b(() => () => {
|
|
70
|
+
t.dispose();
|
|
71
|
+
}), t;
|
|
127
72
|
}
|
|
128
73
|
|
|
129
|
-
|
|
74
|
+
c(b, "useEffectOnce"), c(O, "useMakeOrGetComponentModule");
|
|
130
75
|
|
|
131
|
-
var
|
|
76
|
+
var h, E = require("@adimm/x-injection");
|
|
132
77
|
|
|
133
78
|
!function(e) {
|
|
134
|
-
function
|
|
135
|
-
const
|
|
136
|
-
...
|
|
79
|
+
function r(e, r, t) {
|
|
80
|
+
const o = {
|
|
81
|
+
...r
|
|
137
82
|
};
|
|
138
|
-
return ("object" == typeof e && "type" in e && (0,
|
|
139
|
-
|
|
83
|
+
return ("object" == typeof e && "type" in e && (0, E.isFunction)(e.type) || (0,
|
|
84
|
+
E.isFunction)(e)) && (o.module = t), o;
|
|
140
85
|
}
|
|
141
|
-
|
|
142
|
-
}(
|
|
143
|
-
|
|
144
|
-
var
|
|
145
|
-
|
|
146
|
-
function
|
|
147
|
-
return
|
|
148
|
-
const
|
|
149
|
-
return
|
|
150
|
-
value:
|
|
151
|
-
},
|
|
152
|
-
module:
|
|
153
|
-
componentProps:
|
|
154
|
-
component:
|
|
86
|
+
c(r, "forwardPropsWithModule"), e.forwardPropsWithModule = r;
|
|
87
|
+
}(h || (h = {}));
|
|
88
|
+
|
|
89
|
+
var g = M.default.memo(w);
|
|
90
|
+
|
|
91
|
+
function T(e, r) {
|
|
92
|
+
return t => {
|
|
93
|
+
const o = O(e, t);
|
|
94
|
+
return M.default.createElement(l.Provider, {
|
|
95
|
+
value: o
|
|
96
|
+
}, M.default.createElement(g, {
|
|
97
|
+
module: o,
|
|
98
|
+
componentProps: t,
|
|
99
|
+
component: r
|
|
155
100
|
}));
|
|
156
101
|
};
|
|
157
102
|
}
|
|
158
103
|
|
|
159
|
-
function
|
|
160
|
-
return
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
s(z, "provideModuleToComponent"), s(O, "_ComponentRenderer");
|
|
164
|
-
|
|
165
|
-
var w = d(require("react"), 1);
|
|
166
|
-
|
|
167
|
-
function _({module: e, children: t}) {
|
|
168
|
-
const o = t.props ?? {}, r = b(e, o.module), n = (0, w.useMemo)((() => w.default.cloneElement(t, g.forwardPropsWithModule(t, o, r))), [ o, r ]);
|
|
169
|
-
return w.default.createElement(p.Provider, {
|
|
170
|
-
value: r
|
|
171
|
-
}, n);
|
|
104
|
+
function w({module: e, component: r, componentProps: t}) {
|
|
105
|
+
return M.default.createElement(M.default.Fragment, null, r(h.forwardPropsWithModule(r, t, e)));
|
|
172
106
|
}
|
|
173
107
|
|
|
174
|
-
|
|
108
|
+
c(T, "provideModuleToComponent"), c(w, "_ComponentRenderer");
|
|
175
109
|
|
|
176
|
-
var
|
|
110
|
+
var I = require("react"), C = require("@adimm/x-injection"), q = class e extends C.InjectionProviderModuleError {
|
|
177
111
|
static {
|
|
178
|
-
|
|
112
|
+
c(this, "InjectionHookFactoryError");
|
|
179
113
|
}
|
|
180
114
|
name=e.name;
|
|
181
115
|
};
|
|
182
116
|
|
|
183
|
-
function
|
|
184
|
-
return
|
|
185
|
-
const
|
|
186
|
-
if (0 ===
|
|
187
|
-
return
|
|
188
|
-
}
|
|
117
|
+
function _({use: e, inject: r}) {
|
|
118
|
+
return t => {
|
|
119
|
+
const o = m(), n = (0, I.useMemo)(() => {
|
|
120
|
+
if (0 === r.length) throw new q(o, "The 'deps' property array is missing!");
|
|
121
|
+
return o.getMany(...r);
|
|
122
|
+
}, [ r ]);
|
|
189
123
|
return e({
|
|
190
|
-
...
|
|
124
|
+
...t,
|
|
191
125
|
deps: [ ...n ]
|
|
192
126
|
});
|
|
193
127
|
};
|
|
194
128
|
}
|
|
195
129
|
|
|
196
|
-
|
|
130
|
+
c(_, "hookFactory");//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/core/react-context.ts","../src/core/hooks/use-component-module.ts","../src/core/hooks/use-inject.ts","../src/core/hooks/use-inject-many.ts","../src/core/component-provider-module.ts","../src/core/provide-module/provide-module.arrow-function.tsx","../src/helpers/hooks/use-contextualized-module.ts","../src/helpers/hooks/use-effect-once.ts","../src/helpers/component-provider-module.ts","../src/core/provide-module/provide-module.provider.tsx","../src/core/hook-factory.ts","../src/errors/hook-factory.ts"],"sourcesContent":["export * from './core';\nexport type * from './types';\n","import { AppModule } from '@adimm/x-injection';\nimport { createContext } from 'react';\n\nimport type { IComponentProviderModule } from '../types';\n\nexport const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT = createContext<IComponentProviderModule>(AppModule as any);\n","import { useContext } from 'react';\n\nimport type { IComponentProviderModule } from '../../types';\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from '../react-context';\n\n/** Can be used to retrieve the {@link IComponentProviderModule} from the current context. */\nexport function useComponentModule(): IComponentProviderModule {\n return useContext(REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT);\n}\n","import type { ProviderToken } from '@adimm/x-injection';\n\nimport { useComponentModule } from './use-component-module';\n\n/**\n * Low-level hook which can be used to resolve a single dependency from the current\n * context module.\n *\n * **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_\n * _`hookFactory` method to compose a custom hook._\n *\n * @param provider The {@link ProviderToken}.\n * @param options See {@link UseInjectOptions}.\n * @returns The resolved {@link T | dependency}.\n */\nexport function useInject<T>(provider: ProviderToken<T>, options?: UseInjectOptions): T {\n const componentModule = useComponentModule();\n\n return componentModule.get(provider, options?.isOptional);\n}\n\nexport type UseInjectOptions = {\n /** When set to `false` _(default)_ an exception will be thrown when the `providerOrIdentifier` isn't bound. */\n isOptional?: boolean;\n};\n","import type { ProviderModuleGetManyParam, ProviderModuleGetManySignature, ProviderToken } from '@adimm/x-injection';\n\nimport { useComponentModule } from './use-component-module';\n\n/**\n * Low-level hook which can be used to resolve multiple dependencies at once from the current\n * context module.\n *\n * **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_\n * _`hookFactory` method to compose a custom hook._\n *\n * @param deps Either one or more {@link ProviderToken}.\n * @returns Tuple containing the {@link D | dependencies}.\n */\nexport function useInjectMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(\n ...deps: D | unknown[]\n): ProviderModuleGetManySignature<D> {\n const componentModule = useComponentModule();\n\n return componentModule.getMany(...deps);\n}\n","import {\n InjectionScope,\n ProviderModule,\n ProviderModuleHelpers,\n type IProviderModule,\n type IProviderModuleNaked,\n type ProviderModuleOptionsInternal,\n} from '@adimm/x-injection';\n\nimport type { ComponentProviderModuleOptions, IComponentProviderModule, IComponentProviderModuleNaked } from '../types';\n\n/** A superset of the {@link ProviderModule} used to integrate within a `React` component. */\nexport class ComponentProviderModule extends ProviderModule implements IComponentProviderModule {\n protected readonly originalIdentifier: symbol;\n protected readonly hasContextualizedImports: IComponentProviderModuleNaked['hasContextualizedImports'];\n protected readonly initializedFromComponent: IComponentProviderModuleNaked['initializedFromComponent'];\n\n constructor(options: ComponentProviderModuleOptions) {\n const identifier = Symbol(`Component${options.identifier.description}`);\n const contextualizeImports = options.markAsGlobal ? false : options.contextualizeImports ?? true;\n const contextualizedImportsCache: Map<string, IProviderModule> | undefined = contextualizeImports\n ? new Map()\n : undefined;\n\n super(\n ProviderModuleHelpers.buildInternalConstructorParams({\n ...options,\n defaultScope: options.defaultScope ?? InjectionScope.Singleton,\n identifier: identifier,\n imports: !contextualizeImports\n ? options.imports\n : options.imports?.map((imp) => {\n const module = (typeof imp === 'function' ? imp() : imp) as IComponentProviderModuleNaked;\n /* istanbul ignore next */\n if (!contextualizeImports) return module;\n\n return () => {\n const ctxModule = module._createContextualizedComponentInstance(identifier);\n\n contextualizedImportsCache!.set(module.originalIdentifier.toString(), ctxModule);\n\n return ctxModule;\n };\n }),\n dynamicExports: (_, exports) => {\n if (!contextualizeImports) return exports;\n\n return exports.map((exp) => {\n if (!(exp instanceof ProviderModule)) return exp;\n\n const cachedCtxModule = contextualizedImportsCache!.get(\n (exp as unknown as IComponentProviderModuleNaked).originalIdentifier.toString()\n )!;\n\n return cachedCtxModule;\n });\n },\n })\n );\n\n this.originalIdentifier = options.identifier;\n this.hasContextualizedImports = contextualizeImports;\n this.initializedFromComponent = false;\n }\n\n override toNaked(): IComponentProviderModuleNaked & IProviderModuleNaked {\n return this as any;\n }\n\n /* istanbul ignore next */\n override clone(options?: Partial<ComponentProviderModuleOptions>): IComponentProviderModule {\n const _options = options as ProviderModuleOptionsInternal;\n\n const clonedModule = new ComponentProviderModule(\n ProviderModuleHelpers.buildInternalConstructorParams({\n isAppModule: this.isAppModule,\n markAsGlobal: this.isMarkedAsGlobal,\n identifier: Symbol(this.identifier.description!.replace('Component', '')),\n defaultScope: this.defaultScope.native,\n dynamicExports: this.dynamicExports,\n onReady: this.onReady,\n onDispose: this.onDispose,\n contextualizeImports: this.hasContextualizedImports,\n importedProvidersMap: this.importedProvidersMap,\n imports: [...this.imports],\n providers: [...this.providers],\n exports: [...this.exports],\n ..._options,\n } as ComponentProviderModuleOptions)\n );\n\n //@ts-expect-error Read-only method.\n clonedModule.initializedFromComponent = this.initializedFromComponent;\n\n return clonedModule;\n }\n\n /* istanbul ignore next */\n override async dispose(): Promise<void> {\n await super.dispose();\n }\n\n //#region IComponentProviderModuleNaked methods\n\n /**\n * **Publicly visible when the instance is casted to {@link IComponentProviderModuleNaked}.**\n *\n * See {@link IComponentProviderModuleNaked._createContextualizedComponentInstance}.\n */\n protected _createContextualizedComponentInstance(parentIdentifier?: symbol): IComponentProviderModule {\n if (this.initializedFromComponent) return this;\n\n const ctxModule = this.clone().toNaked();\n\n /* istanbul ignore next */\n //@ts-expect-error Read-only property\n ctxModule.identifier = Symbol(\n `${parentIdentifier ? `[Parent:${parentIdentifier.description ?? 'Unknown'}]` : ''}Contextualized${ctxModule.identifier.description}`\n );\n ctxModule.initializedFromComponent = true;\n\n return ctxModule;\n }\n\n //#endregion\n}\n","import React from 'react';\n\nimport { ComponentProviderModuleHelpers, useContextualizedModule } from '../../helpers';\nimport type { IComponentProviderModule, PropsWithModule } from '../../types';\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from '../react-context';\n\nconst ComponentRenderer = React.memo(_ComponentRenderer);\n\n/**\n * Can be used to easily provide a {@link module} to any component.\n *\n * @example\n * ```tsx\n * interface MyComponentProps {\n * firstName: string;\n * lastName: string;\n * }\n *\n * export const MyComponent = provideModuleToComponent<MyComponentProps>(\n * MyComponentModule,\n * ({ firstName, lastName }) => {\n * const service = useInject(MyComponentService);\n *\n * return <h1>Hello {service.computeUserName(firstName, lastName)}!</h1>\n * }\n * );\n *\n * function App() {\n * return <MyComponent firstName={'John'} lastName={'Doe'} />;\n * }\n * ```\n *\n * @param module The {@link IComponentProviderModule | Module} which should be consumed by the {@link component}.\n * @returns The provided {@link toComponent | Component}.\n */\nexport function provideModuleToComponent<\n P extends Record<string, any>,\n C extends ReactElementWithProviderModule<P> = ReactElementWithProviderModule<P>,\n>(module: IComponentProviderModule, component: ReactElementWithProviderModule<P>): C {\n return ((componentProps: PropsWithModule<P>) => {\n const moduleCtx = useContextualizedModule(module, componentProps.module);\n\n return (\n <REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider value={moduleCtx}>\n <ComponentRenderer module={moduleCtx} componentProps={componentProps} component={component as any} />\n </REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider>\n );\n }) as any;\n}\n\nfunction _ComponentRenderer<P extends Record<string, any>>({\n module,\n component,\n componentProps,\n}: {\n module: IComponentProviderModule;\n component: ReactElementWithProviderModule<P>;\n componentProps: P;\n}) {\n return <>{component(ComponentProviderModuleHelpers.forwardPropsWithModule(component, componentProps, module))}</>;\n}\n\nexport type ReactElementWithProviderModule<P extends Record<string, any>> = (p: PropsWithModule<P>) => React.ReactNode;\n","import { useMemo } from 'react';\n\nimport type { IComponentProviderModule } from '../../types';\nimport { useEffectOnce } from './use-effect-once';\n\nexport function useContextualizedModule(\n originalModule: IComponentProviderModule,\n forwardedModule?: IComponentProviderModule\n): IComponentProviderModule {\n const ctxModule = useMemo(() => {\n const module = (forwardedModule ?? originalModule).toNaked();\n\n if (module.isMarkedAsGlobal) return module;\n\n return module._createContextualizedComponentInstance();\n }, [originalModule, forwardedModule]);\n\n useEffectOnce(() => {\n return () => {\n ctxModule.dispose();\n };\n });\n\n return ctxModule;\n}\n","import { useEffect, useRef, useState } from 'react';\n\n// Credits: https://stackoverflow.com/a/74000921\n\n/** Custom {@link useEffect} hook which will be run once. _(In `StrictMode` as well)_ */\nexport function useEffectOnce(effect: () => React.EffectCallback) {\n const destroyFunc = useRef<React.EffectCallback>(undefined);\n const effectCalled = useRef(false);\n const renderAfterCalled = useRef(false);\n const [, forceRerender] = useState(0);\n\n if (effectCalled.current) renderAfterCalled.current = true;\n\n useEffect(() => {\n // only execute the effect first time around\n if (!effectCalled.current) {\n destroyFunc.current = effect();\n effectCalled.current = true;\n }\n\n // this forces one render after the effect is run\n forceRerender((x) => x + 1);\n\n return () => {\n // if the comp didn't render since the useEffect was called,\n // we know it's the dummy React cycle\n if (!renderAfterCalled.current) return;\n\n destroyFunc.current?.();\n };\n }, []);\n}\n","import { isFunction } from '@adimm/x-injection';\n\nimport type { ReactElementWithProviderModule } from '../core';\nimport type { IComponentProviderModule, PropsWithModule } from '../types';\n\nexport namespace ComponentProviderModuleHelpers {\n export function forwardPropsWithModule<P extends Record<string, any>>(\n component: ReactElementWithProviderModule<P> | React.ReactElement,\n props: Record<string, any>,\n module: IComponentProviderModule\n ): PropsWithModule<P> {\n const isReactElement = typeof component === 'object' && 'type' in component;\n\n const result = {\n ...props,\n } as any;\n\n if ((isReactElement && isFunction(component.type)) || isFunction(component)) {\n result['module'] = module;\n }\n\n return result;\n }\n}\n","import React, { useMemo } from 'react';\n\nimport { ComponentProviderModuleHelpers, useContextualizedModule } from '../../helpers';\nimport type { IComponentProviderModule } from '../../types';\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from '../react-context';\n\n/**\n * Can be used to easily provide a {@link module} to any component.\n *\n * @example\n * ```tsx\n * interface MyComponentProps {\n * firstName: string;\n * lastName: string;\n * }\n *\n * function MyComponent({ firstName, lastName }: MyComponentProps) {\n * const service = useInject(MyComponentService);\n *\n * return <h1>Hello {service.computeUserName(firstName, lastName)}!</h1>\n * }\n *\n * function App() {\n * return (\n * <ProvideModule module={MyComponentModule}>\n * <MyComponent firstName={'John'} lastName={'Doe'} />\n * </ProvideModule>\n * );\n * }\n * ```\n *\n * @param param0 See {@link ProvideModuleFunctionParams}.\n * @returns The provided {@link toComponent | Component}.\n */\nexport function ProvideModule({ module, children }: ProvideModuleFunctionParams) {\n /* istanbul ignore next */\n const componentProps = (children.props ?? {}) as any;\n const moduleCtx = useContextualizedModule(module, componentProps.module);\n const component = useMemo(\n () =>\n React.cloneElement(\n children,\n ComponentProviderModuleHelpers.forwardPropsWithModule(children, componentProps, moduleCtx)\n ),\n [componentProps, moduleCtx]\n );\n\n return (\n <REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider value={moduleCtx}>\n {component}\n </REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider>\n );\n}\n\nexport interface ProvideModuleFunctionParams {\n /** The {@link IComponentProviderModule | Module} which should be consumed by the {@link children | component}. */\n module: IComponentProviderModule;\n\n children: React.ReactElement;\n}\n","import type { ProviderToken } from '@adimm/x-injection';\nimport { useMemo } from 'react';\n\nimport { InjectionHookFactoryError } from '../errors';\nimport { useComponentModule } from './hooks';\n\nexport function hookFactory<P extends HookParams, D extends any[], T>({\n use: hook,\n inject,\n}: HookFactoryParams<P, D, T>): (p: P) => T {\n return (p: P) => {\n const componentModule = useComponentModule();\n\n const deps = useMemo(() => {\n if (inject.length === 0) {\n throw new InjectionHookFactoryError(componentModule, `The 'deps' property array is missing!`);\n }\n\n return componentModule.getMany(...inject);\n }, [inject]);\n\n return hook({ ...p, deps: [...deps] } as any);\n };\n}\n\nexport interface HookFactoryParams<P extends HookParams, D extends any[], T> {\n use: HookWithProviderModuleDependencies<P, D, T>;\n inject: ProviderToken[];\n}\n\nexport type HookWithProviderModuleDependencies<P extends HookParams, D extends any[], T> = (p: HookWithDeps<P, D>) => T;\n\nexport type HookWithDeps<P extends HookParams, D extends any[]> = P & {\n /** Array containing the resolved dependencies from the component context. */\n deps: D;\n};\n\ntype HookParams = Record<string, any> | void;\n","import { InjectionProviderModuleError } from '@adimm/x-injection';\n\nexport class InjectionHookFactoryError extends InjectionProviderModuleError {\n override name = InjectionHookFactoryError.name;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;ACAA,yBAA0B;AAC1B,mBAA8B;AAIvB,IAAMA,gDAA4CC,4BAAwCC,4BAAAA;;;ACLjG,IAAAC,gBAA2B;AAMpB,SAASC,qBAAAA;AACd,aAAOC,0BAAWC,yCAAAA;AACpB;AAFgBF;;;ACST,SAASG,UAAaC,UAA4BC,SAA0B;AACjF,QAAMC,kBAAkBC,mBAAAA;AAExB,SAAOD,gBAAgBE,IAAIJ,UAAUC,SAASI,UAAAA;AAChD;AAJgBN;;;ACDT,SAASO,iBACXC,MAAmB;AAEtB,QAAMC,kBAAkBC,mBAAAA;AAExB,SAAOD,gBAAgBE,QAAO,GAAIH,IAAAA;AACpC;AANgBD;;;ACdhB,IAAAK,sBAOO;AAKA,IAAMC,0BAAN,MAAMA,iCAAgCC,mCAAAA;EAZ7C,OAY6CA;;;EACxBC;EACAC;EACAC;EAEnBC,YAAYC,SAAyC;AACnD,UAAMC,aAAaC,OAAO,YAAYF,QAAQC,WAAWE,WAAW,EAAE;AACtE,UAAMC,uBAAuBJ,QAAQK,eAAe,QAAQL,QAAQI,wBAAwB;AAC5F,UAAME,6BAAuEF,uBACzE,oBAAIG,IAAAA,IACJC;AAEJ,UACEC,0CAAsBC,+BAA+B;MACnD,GAAGV;MACHW,cAAcX,QAAQW,gBAAgBC,mCAAeC;MACrDZ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ACF;IAAgBE;GAAU;AAG7B,SACE,8BAAAI,QAAA,cAACI,0CAA0CC,UAAQ;IAACC,OAAOV;KACxDE,SAAAA;AAGP;AAlBgBP;;;ACjChB,IAAAgB,gBAAwB;;;ACDxB,IAAAC,sBAA6C;AAEtC,IAAMC,4BAAN,MAAMA,mCAAkCC,iDAAAA;EAF/C,OAE+CA;;;EACpCC,OAAOF,2BAA0BE;AAC5C;;;ADEO,SAASC,YAAsD,EACpEC,KAAKC,MACLC,OAAM,GACqB;AAC3B,SAAO,CAACC,MAAAA;AACN,UAAMC,kBAAkBC,mBAAAA;AAExB,UAAMC,WAAOC,uBAAQ,MAAA;AACnB,UAAIL,OAAOM,WAAW,GAAG;AACvB,cAAM,IAAIC,0BAA0BL,iBAAiB,uCAAuC;MAC9F;AAEA,aAAOA,gBAAgBM,QAAO,GAAIR,MAAAA;IACpC,GAAG;MAACA;KAAO;AAEX,WAAOD,KAAK;MAAE,GAAGE;MAAGG,MAAM;WAAIA;;IAAM,CAAA;EACtC;AACF;AAjBgBP;","names":["REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","createContext","AppModule","import_react","useComponentModule","useContext","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","useInject","provider","options","componentModule","useComponentModule","get","isOptional","useInjectMany","deps","componentModule","useComponentModule","getMany","import_x_injection","ComponentProviderModule","ProviderModule","originalIdentifier","hasContextualizedImports","initializedFromComponent","constructor","options","identifier","Symbol","description","contextualizeImports","markAsGlobal","contextualizedImportsCache","Map","undefined","ProviderModuleHelpers","buildInternalConstructorParams","defaultScope","InjectionScope","Singleton","imports","map","imp","module","ctxModule","_createContextualizedComponentInstance","set","toString","dynamicExports","_","exports","exp","cachedCtxModule","get","toNaked","clone","_options","clonedModule","isAppModule","isMarkedAsGlobal","replace","native","onReady","onDispose","importedProvidersMap","providers","dispose","parentIdentifier","import_react","import_react","import_react","useEffectOnce","effect","destroyFunc","useRef","undefined","effectCalled","renderAfterCalled","forceRerender","useState","current","useEffect","x","useContextualizedModule","originalModule","forwardedModule","ctxModule","useMemo","module","toNaked","isMarkedAsGlobal","_createContextualizedComponentInstance","useEffectOnce","dispose","import_x_injection","ComponentProviderModuleHelpers","forwardPropsWithModule","component","props","module","isReactElement","result","isFunction","type","ComponentRenderer","React","memo","_ComponentRenderer","provideModuleToComponent","module","component","componentProps","moduleCtx","useContextualizedModule","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","Provider","value","ComponentProviderModuleHelpers","forwardPropsWithModule","import_react","ProvideModule","module","children","componentProps","props","moduleCtx","useContextualizedModule","component","useMemo","React","cloneElement","ComponentProviderModuleHelpers","forwardPropsWithModule","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","Provider","value","import_react","import_x_injection","InjectionHookFactoryError","InjectionProviderModuleError","name","hookFactory","use","hook","inject","p","componentModule","useComponentModule","deps","useMemo","length","InjectionHookFactoryError","getMany"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/core/react-context.ts","../src/core/hooks/use-component-module.ts","../src/core/hooks/use-inject.ts","../src/core/hooks/use-inject-many.ts","../src/core/provide-module-to-component.hoc.tsx","../src/helpers/hooks/use-make-or-get-component-module.ts","../src/helpers/hooks/use-effect-once.ts","../src/helpers/component-provider-module.ts","../src/core/hook-factory.ts","../src/errors/hook-factory.ts"],"sourcesContent":["export * from './core';\nexport type * from './types';\n","import { AppModule, IProviderModule } from '@adimm/x-injection';\nimport { createContext } from 'react';\n\nexport const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT = createContext<IProviderModule>(AppModule);\n","import type { IProviderModule } from '@adimm/x-injection';\nimport { useContext } from 'react';\n\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from '../react-context';\n\n/** Can be used to retrieve the {@link IProviderModule | Module} from the current context. */\nexport function useComponentModule(): IProviderModule {\n return useContext(REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT);\n}\n","import type { ProviderToken } from '@adimm/x-injection';\n\nimport { useComponentModule } from './use-component-module';\n\n/**\n * Low-level hook which can be used to resolve a single dependency.\n *\n * **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_\n * _`hookFactory` method to compose a custom hook._\n *\n * @param provider The {@link ProviderToken}.\n * @param options See {@link UseInjectOptions}.\n * @returns The resolved {@link T | dependency}.\n */\nexport function useInject<\n T,\n IsOptional extends boolean | undefined = undefined,\n AsList extends boolean | undefined = undefined,\n>(provider: ProviderToken<T>, options?: UseInjectOptions<IsOptional, AsList>) {\n const componentModule = useComponentModule();\n\n return componentModule.get<T, IsOptional, AsList>(provider, options?.isOptional, options?.asList);\n}\n\nexport type UseInjectOptions<\n IsOptional extends boolean | undefined = undefined,\n AsList extends boolean | undefined = undefined,\n> = {\n /**\n * When set to `false` an exception will be thrown when the supplied `ProviderToken` isn't bound.\n *\n * Defaults to `false`.\n */\n isOptional?: IsOptional;\n\n /**\n * Set to `true` if you need to retrieve _all_ the bound identifiers of the supplied `ProviderToken`.\n *\n * Defaults to `false`.\n */\n asList?: AsList;\n};\n","import type { ProviderIdentifier, ProviderModuleGetManyParam, ProviderToken } from '@adimm/x-injection';\n\nimport { useComponentModule } from './use-component-module';\n\n/**\n * Low-level hook which can be used to resolve multiple dependencies at once.\n *\n * **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_\n * _`hookFactory` method to compose a custom hook._\n *\n * @param deps Either one or more {@link ProviderToken}.\n * @returns Tuple containing the {@link D | dependencies}.\n */\nexport function useInjectMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken | ProviderIdentifier)[]>(\n ...deps: D\n) {\n const componentModule = useComponentModule();\n\n return componentModule.getMany<D>(...deps);\n}\n","import type { IProviderModule, ModuleOrBlueprint } from '@adimm/x-injection';\nimport React from 'react';\n\nimport { ComponentProviderModuleHelpers, useMakeOrGetComponentModule } from '../helpers';\nimport type { PropsWithModule } from '../types';\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from './react-context';\n\nconst ComponentRenderer = React.memo(_ComponentRenderer);\n\n/**\n * Can be used to easily provide a {@link module} to a component.\n *\n * **Note:** _An error will be thrown if a `global` module is provided._\n *\n * @example\n * ```tsx\n * interface MyComponentProps {\n * firstName: string;\n * lastName: string;\n * }\n *\n * export const MyComponent = provideModuleToComponent<MyComponentProps>(\n * MyComponentModule,\n * ({ firstName, lastName }) => {\n * const service = useInject(MyComponentService);\n *\n * return <h1>Hello {service.computeUserName(firstName, lastName)}!</h1>\n * }\n * );\n *\n * function App() {\n * return <MyComponent firstName={'John'} lastName={'Doe'} />;\n * }\n * ```\n *\n * @param module The {@link ModuleOrBlueprint} which should be consumed by the {@link component}.\n * @returns The provided {@link toComponent | Component}.\n */\nexport function provideModuleToComponent<\n P extends Record<string, any>,\n C extends ReactElementWithProviderModule<P> = ReactElementWithProviderModule<P>,\n>(module: ModuleOrBlueprint, component: ReactElementWithProviderModule<P>): C {\n return ((componentProps: PropsWithModule<P>) => {\n const moduleCtx = useMakeOrGetComponentModule(module, componentProps);\n\n return (\n <REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider value={moduleCtx}>\n <ComponentRenderer module={moduleCtx} componentProps={componentProps} component={component as any} />\n </REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider>\n );\n }) as any;\n}\n\nfunction _ComponentRenderer<P extends Record<string, any>>({\n module,\n component,\n componentProps,\n}: {\n module: IProviderModule;\n component: ReactElementWithProviderModule<P>;\n componentProps: P;\n}) {\n return <>{component(ComponentProviderModuleHelpers.forwardPropsWithModule(component, componentProps, module))}</>;\n}\n\nexport type ReactElementWithProviderModule<P extends Record<string, any>> = (p: PropsWithModule<P>) => React.ReactNode;\n","import {\n InjectionProviderModuleError,\n ProviderModuleHelpers,\n ProviderTokenHelpers,\n type IProviderModule,\n type ModuleOrBlueprint,\n type ProviderModule,\n} from '@adimm/x-injection';\nimport { useMemo } from 'react';\n\nimport type { PropsWithModule } from '../../types';\nimport { useEffectOnce } from './use-effect-once';\n\nexport function useMakeOrGetComponentModule(\n originalModule: ModuleOrBlueprint,\n componentProps?: PropsWithModule<object>\n): IProviderModule {\n const componentModule = useMemo(() => {\n /* istanbul ignore next */\n const { module: forwardedModule, inject } = componentProps ?? {};\n const module = ProviderModuleHelpers.tryBlueprintToModule(forwardedModule ?? originalModule) as ProviderModule;\n\n if (module.options.isGlobal) {\n throw new InjectionProviderModuleError(module, `A 'global' module can't be supplied to a component!`);\n }\n\n inject?.forEach((provider) => {\n if (!module.hasProvider(provider)) {\n throw new InjectionProviderModuleError(\n module,\n `The [${ProviderTokenHelpers.providerTokenToString(provider)}] provider can't be replaced because it is not part of the component module!`\n );\n }\n\n module.update.removeProvider(ProviderTokenHelpers.toProviderIdentifier(provider));\n module.update.addProvider(provider);\n });\n\n return module;\n }, [originalModule, componentProps?.inject]);\n\n useEffectOnce(() => {\n return () => {\n componentModule.dispose();\n };\n });\n\n return componentModule;\n}\n","import { useEffect, useRef, useState } from 'react';\n\n// Credits: https://stackoverflow.com/a/74000921\n\n/** Custom {@link useEffect} hook which will be run once. _(In `StrictMode` as well)_ */\nexport function useEffectOnce(effect: () => React.EffectCallback) {\n const destroyFunc = useRef<React.EffectCallback>(undefined);\n const effectCalled = useRef(false);\n const renderAfterCalled = useRef(false);\n const [, forceRerender] = useState(0);\n\n if (effectCalled.current) renderAfterCalled.current = true;\n\n useEffect(() => {\n // only execute the effect first time around\n if (!effectCalled.current) {\n destroyFunc.current = effect();\n effectCalled.current = true;\n }\n\n // this forces one render after the effect is run\n forceRerender((x) => x + 1);\n\n return () => {\n // if the comp didn't render since the useEffect was called,\n // we know it's the dummy React cycle\n if (!renderAfterCalled.current) return;\n\n destroyFunc.current?.();\n };\n }, []);\n}\n","import { isFunction, type ModuleOrBlueprint } from '@adimm/x-injection';\n\nimport type { ReactElementWithProviderModule } from '../core';\nimport type { PropsWithModule } from '../types';\n\nexport namespace ComponentProviderModuleHelpers {\n export function forwardPropsWithModule<P extends Record<string, any>>(\n component: ReactElementWithProviderModule<P> | React.ReactElement,\n props: Record<string, any>,\n module: ModuleOrBlueprint\n ): PropsWithModule<P> {\n const isReactElement = typeof component === 'object' && 'type' in component;\n\n const result = {\n ...props,\n } as any;\n\n if ((isReactElement && isFunction(component.type)) || isFunction(component)) {\n result['module'] = module;\n }\n\n return result;\n }\n}\n","import type { ProviderToken } from '@adimm/x-injection';\nimport { useMemo } from 'react';\n\nimport { InjectionHookFactoryError } from '../errors';\nimport { useComponentModule } from './hooks';\n\nexport function hookFactory<P extends HookParams, D extends any[], T>({\n use: hook,\n inject,\n}: HookFactoryParams<P, D, T>): (p: P) => T {\n return (p: P) => {\n const componentModule = useComponentModule();\n\n const deps = useMemo(() => {\n if (inject.length === 0) {\n throw new InjectionHookFactoryError(componentModule, `The 'deps' property array is missing!`);\n }\n\n return componentModule.getMany(...inject);\n }, [inject]);\n\n return hook({ ...p, deps: [...deps] } as any);\n };\n}\n\nexport interface HookFactoryParams<P extends HookParams, D extends any[], T> {\n use: (p: HookWithDeps<P, D>) => T;\n inject: ProviderToken[];\n}\n\nexport type HookWithDeps<P extends HookParams, D extends any[]> = P & {\n /** Array containing the resolved dependencies. */\n deps: D;\n};\n\ntype HookParams = Record<string, any> | void;\n","import { InjectionProviderModuleError } from '@adimm/x-injection';\n\nexport class InjectionHookFactoryError extends InjectionProviderModuleError {\n override name = InjectionHookFactoryError.name;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;ACAA,yBAA2C;AAC3C,mBAA8B;AAEvB,IAAMA,gDAA4CC,4BAA+BC,4BAAAA;;;ACFxF,IAAAC,gBAA2B;AAKpB,SAASC,qBAAAA;AACd,aAAOC,0BAAWC,yCAAAA;AACpB;AAFgBF;;;ACQT,SAASG,UAIdC,UAA4BC,SAA8C;AAC1E,QAAMC,kBAAkBC,mBAAAA;AAExB,SAAOD,gBAAgBE,IAA2BJ,UAAUC,SAASI,YAAYJ,SAASK,MAAAA;AAC5F;AARgBP;;;ACDT,SAASQ,iBACXC,MA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ADEO,SAASC,YAAsD,EACpEC,KAAKC,MACLC,OAAM,GACqB;AAC3B,SAAO,CAACC,MAAAA;AACN,UAAMC,kBAAkBC,mBAAAA;AAExB,UAAMC,WAAOC,uBAAQ,MAAA;AACnB,UAAIL,OAAOM,WAAW,GAAG;AACvB,cAAM,IAAIC,0BAA0BL,iBAAiB,uCAAuC;MAC9F;AAEA,aAAOA,gBAAgBM,QAAO,GAAIR,MAAAA;IACpC,GAAG;MAACA;KAAO;AAEX,WAAOD,KAAK;MAAE,GAAGE;MAAGG,MAAM;WAAIA;;IAAM,CAAA;EACtC;AACF;AAjBgBP;","names":["REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","createContext","AppModule","import_react","useComponentModule","useContext","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","useInject","provider","options","componentModule","useComponentModule","get","isOptional","asList","useInjectMany","deps","componentModule","useComponentModule","getMany","import_react","import_x_injection","import_react","import_react","useEffectOnce","effect","destroyFunc","useRef","undefined","effectCalled","renderAfterCalled","forceRerender","useState","current","useEffect","x","useMakeOrGetComponentModule","originalModule","componentProps","componentModule","useMemo","module","forwardedModule","inject","ProviderModuleHelpers","tryBlueprintToModule","options","isGlobal","InjectionProviderModuleError","forEach","provider","hasProvider","ProviderTokenHelpers","providerTokenToString","update","removeProvider","toProviderIdentifier","addProvider","useEffectOnce","dispose","import_x_injection","ComponentProviderModuleHelpers","forwardPropsWithModule","component","props","module","isReactElement","result","isFunction","type","ComponentRenderer","React","memo","_ComponentRenderer","provideModuleToComponent","module","component","componentProps","moduleCtx","useMakeOrGetComponentModule","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","Provider","value","ComponentProviderModuleHelpers","forwardPropsWithModule","import_react","import_x_injection","InjectionHookFactoryError","InjectionProviderModuleError","name","hookFactory","use","hook","inject","p","componentModule","useComponentModule","deps","useMemo","length","InjectionHookFactoryError","getMany"]}
|