@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.d.cts
CHANGED
|
@@ -1,74 +1,12 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import react__default from 'react';
|
|
3
|
+
import * as _adimm_x_injection from '@adimm/x-injection';
|
|
4
|
+
import { IProviderModule, ProviderToken, ProviderModuleGetManyParam, ProviderIdentifier, ModuleOrBlueprint, DependencyProvider } from '@adimm/x-injection';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* When set to `true`, it'll automatically `contextualize` all the `imports` array.
|
|
9
|
-
*
|
|
10
|
-
* **Note:** _This is required when you have a parent component which renders one or more children
|
|
11
|
-
* and does also need to control their providers._
|
|
12
|
-
* _If not set to `true`, then the children providers would be resolved from their original `module`, therefore_
|
|
13
|
-
* _if your parent component has multiple instances of itself, all its children will actually share_
|
|
14
|
-
* _the same providers, especially when their module injection scope is set to `Singleton` (by default)._
|
|
15
|
-
*
|
|
16
|
-
* **Note2:** _If a module has {@link ProviderModuleOptions.markAsGlobal | markAsGlobal} set to `true`,_
|
|
17
|
-
* _this will automatically default to `false`._
|
|
18
|
-
*
|
|
19
|
-
* Defaults to `true`.
|
|
20
|
-
*/
|
|
21
|
-
contextualizeImports?: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface IComponentProviderModuleNaked extends IComponentProviderModule {
|
|
25
|
-
/**
|
|
26
|
-
* The original `id` which was assigned to this module.
|
|
27
|
-
*
|
|
28
|
-
* **Note:** _The module initial {@link ComponentProviderModuleOptions.identifier | id} will be suffixed with `Contextualized`._
|
|
29
|
-
*/
|
|
30
|
-
originalIdentifier: symbol;
|
|
31
|
-
/** See {@link ComponentProviderModuleOptions.contextualizeImports}. */
|
|
32
|
-
hasContextualizedImports: ComponentProviderModuleOptions['contextualizeImports'];
|
|
33
|
-
/** Indicates if this module has been initialized by a React Component or not. */
|
|
34
|
-
initializedFromComponent: boolean;
|
|
35
|
-
/**
|
|
36
|
-
* It is used internally by the `ProviderModule` to create a new cloned
|
|
37
|
-
* module which will be consumed only by that specific component instance.
|
|
38
|
-
*/
|
|
39
|
-
_createContextualizedComponentInstance(parentIdentifier?: symbol): IComponentProviderModule;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface IComponentProviderModule extends IProviderModule {
|
|
43
|
-
/**
|
|
44
|
-
* Casts the current module type to the {@link IComponentProviderModuleNaked} type.
|
|
45
|
-
*
|
|
46
|
-
* **Internally used and for testing purposes!**
|
|
47
|
-
*/
|
|
48
|
-
toNaked(): IComponentProviderModuleNaked & IProviderModuleNaked;
|
|
49
|
-
/**
|
|
50
|
-
* Should be invoked only when the component is not needed anymore.
|
|
51
|
-
*
|
|
52
|
-
* _eg: When changing page, removable components and so on._
|
|
53
|
-
*/
|
|
54
|
-
dispose(): Promise<void>;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
type PropsWithModule<P extends Record<string, any>> = P & {
|
|
58
|
-
/**
|
|
59
|
-
* The {@link IComponentProviderModule | Module} which this component should consume.
|
|
60
|
-
*
|
|
61
|
-
* **Note:** _You can easily override this in your unit tests to provide a module having mocked providers_
|
|
62
|
-
* _without the need to create a new mocked component by using the composable `ProvideModule` method._
|
|
63
|
-
*/
|
|
64
|
-
module?: IComponentProviderModule;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
declare const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT: React.Context<IComponentProviderModule>;
|
|
6
|
+
declare const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT: react.Context<IProviderModule>;
|
|
68
7
|
|
|
69
8
|
/**
|
|
70
|
-
* Low-level hook which can be used to resolve a single dependency
|
|
71
|
-
* context module.
|
|
9
|
+
* Low-level hook which can be used to resolve a single dependency.
|
|
72
10
|
*
|
|
73
11
|
* **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_
|
|
74
12
|
* _`hookFactory` method to compose a custom hook._
|
|
@@ -77,15 +15,24 @@ declare const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT: React.Context<IComponen
|
|
|
77
15
|
* @param options See {@link UseInjectOptions}.
|
|
78
16
|
* @returns The resolved {@link T | dependency}.
|
|
79
17
|
*/
|
|
80
|
-
declare function useInject<T>(provider: ProviderToken<T>, options?: UseInjectOptions): T
|
|
81
|
-
type UseInjectOptions = {
|
|
82
|
-
/**
|
|
83
|
-
|
|
18
|
+
declare function useInject<T, IsOptional extends boolean | undefined = undefined, AsList extends boolean | undefined = undefined>(provider: ProviderToken<T>, options?: UseInjectOptions<IsOptional, AsList>): _adimm_x_injection.ProviderModuleGetReturn<T, IsOptional, AsList>;
|
|
19
|
+
type UseInjectOptions<IsOptional extends boolean | undefined = undefined, AsList extends boolean | undefined = undefined> = {
|
|
20
|
+
/**
|
|
21
|
+
* When set to `false` an exception will be thrown when the supplied `ProviderToken` isn't bound.
|
|
22
|
+
*
|
|
23
|
+
* Defaults to `false`.
|
|
24
|
+
*/
|
|
25
|
+
isOptional?: IsOptional;
|
|
26
|
+
/**
|
|
27
|
+
* Set to `true` if you need to retrieve _all_ the bound identifiers of the supplied `ProviderToken`.
|
|
28
|
+
*
|
|
29
|
+
* Defaults to `false`.
|
|
30
|
+
*/
|
|
31
|
+
asList?: AsList;
|
|
84
32
|
};
|
|
85
33
|
|
|
86
34
|
/**
|
|
87
|
-
* Low-level hook which can be used to resolve multiple dependencies at once
|
|
88
|
-
* context module.
|
|
35
|
+
* Low-level hook which can be used to resolve multiple dependencies at once.
|
|
89
36
|
*
|
|
90
37
|
* **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_
|
|
91
38
|
* _`hookFactory` method to compose a custom hook._
|
|
@@ -93,30 +40,83 @@ type UseInjectOptions = {
|
|
|
93
40
|
* @param deps Either one or more {@link ProviderToken}.
|
|
94
41
|
* @returns Tuple containing the {@link D | dependencies}.
|
|
95
42
|
*/
|
|
96
|
-
declare function useInjectMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D
|
|
43
|
+
declare function useInjectMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken | ProviderIdentifier)[]>(...deps: D): _adimm_x_injection.ProviderModuleGetManyReturn<D>;
|
|
97
44
|
|
|
98
|
-
/** Can be used to retrieve the {@link
|
|
99
|
-
declare function useComponentModule():
|
|
45
|
+
/** Can be used to retrieve the {@link IProviderModule | Module} from the current context. */
|
|
46
|
+
declare function useComponentModule(): IProviderModule;
|
|
100
47
|
|
|
101
|
-
|
|
102
|
-
declare class ComponentProviderModule extends ProviderModule implements IComponentProviderModule {
|
|
103
|
-
protected readonly originalIdentifier: symbol;
|
|
104
|
-
protected readonly hasContextualizedImports: IComponentProviderModuleNaked['hasContextualizedImports'];
|
|
105
|
-
protected readonly initializedFromComponent: IComponentProviderModuleNaked['initializedFromComponent'];
|
|
106
|
-
constructor(options: ComponentProviderModuleOptions);
|
|
107
|
-
toNaked(): IComponentProviderModuleNaked & IProviderModuleNaked;
|
|
108
|
-
clone(options?: Partial<ComponentProviderModuleOptions>): IComponentProviderModule;
|
|
109
|
-
dispose(): Promise<void>;
|
|
48
|
+
type PropsWithModule<P extends Record<string, any>> = P & {
|
|
110
49
|
/**
|
|
111
|
-
*
|
|
50
|
+
* The {@link ModuleOrBlueprint} which this component should consume.
|
|
112
51
|
*
|
|
113
|
-
*
|
|
52
|
+
* **Note:** _Can be used to easily mock an entire module._
|
|
53
|
+
*
|
|
54
|
+
* example:
|
|
55
|
+
* ```tsx
|
|
56
|
+
* const CarModuleBp = ProviderModule.blueprint({
|
|
57
|
+
* id: 'CarModule',
|
|
58
|
+
* imports: [CarEngineModule, CarDashboardModule],
|
|
59
|
+
* providers: [CarService],
|
|
60
|
+
* exports: [CarService],
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* const cbMock = jest.fn();
|
|
64
|
+
*
|
|
65
|
+
* const CarModulBpeMocked = CarModuleBp.clone().updateDefinition({
|
|
66
|
+
* providers: [
|
|
67
|
+
* {
|
|
68
|
+
* provide: CarService, useValue: { startEngine: cbMock }
|
|
69
|
+
* },
|
|
70
|
+
* ]
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* await act(async () => render(<CarComponent module={CarModuleBpMocked} />));
|
|
74
|
+
*
|
|
75
|
+
* await waitFor(async () => {
|
|
76
|
+
* expect(cbMock).toHaveBeenCalled();
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
114
79
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
80
|
+
module?: ModuleOrBlueprint;
|
|
81
|
+
/**
|
|
82
|
+
* Can be used to control the dependencies consumed by this component.
|
|
83
|
+
* This is useful when you want to provide an already resolved instance of a dependency down the component tree.
|
|
84
|
+
*
|
|
85
|
+
* eg:
|
|
86
|
+
* ```tsx
|
|
87
|
+
* class InputboxService {
|
|
88
|
+
* currentValue = '';
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* class FormService {
|
|
92
|
+
* constructor(
|
|
93
|
+
* public readonly firstNameInputbox: InputboxService,
|
|
94
|
+
* public readonly lastNameInputbox: InputboxService
|
|
95
|
+
* ) {
|
|
96
|
+
* this.firstNameInputbox.currentValue = 'John';
|
|
97
|
+
* this.lastNameInputbox.currentValue = 'Doe';
|
|
98
|
+
* }
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* const Form = provideModuleToComponent(FormModule, () => {
|
|
102
|
+
* const service = useInject(FormService);
|
|
103
|
+
*
|
|
104
|
+
* return (
|
|
105
|
+
* <>
|
|
106
|
+
* <Inputbox inject={[{ provide: InputboxService, useValue: service.firstNameInputbox }]} />
|
|
107
|
+
* <Inputbox inject={[{ provide: InputboxService, useValue: service.lastNameInputbox }]} />
|
|
108
|
+
* </>
|
|
109
|
+
* );
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
inject?: DependencyProvider[];
|
|
114
|
+
};
|
|
117
115
|
|
|
118
116
|
/**
|
|
119
|
-
* Can be used to easily provide a {@link module} to
|
|
117
|
+
* Can be used to easily provide a {@link module} to a component.
|
|
118
|
+
*
|
|
119
|
+
* **Note:** _An error will be thrown if a `global` module is provided._
|
|
120
120
|
*
|
|
121
121
|
* @example
|
|
122
122
|
* ```tsx
|
|
@@ -139,57 +139,21 @@ declare class ComponentProviderModule extends ProviderModule implements ICompone
|
|
|
139
139
|
* }
|
|
140
140
|
* ```
|
|
141
141
|
*
|
|
142
|
-
* @param module The {@link
|
|
143
|
-
* @returns The provided {@link toComponent | Component}.
|
|
144
|
-
*/
|
|
145
|
-
declare function provideModuleToComponent<P extends Record<string, any>, C extends ReactElementWithProviderModule<P> = ReactElementWithProviderModule<P>>(module: IComponentProviderModule, component: ReactElementWithProviderModule<P>): C;
|
|
146
|
-
type ReactElementWithProviderModule<P extends Record<string, any>> = (p: PropsWithModule<P>) => React__default.ReactNode;
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Can be used to easily provide a {@link module} to any component.
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
152
|
-
* ```tsx
|
|
153
|
-
* interface MyComponentProps {
|
|
154
|
-
* firstName: string;
|
|
155
|
-
* lastName: string;
|
|
156
|
-
* }
|
|
157
|
-
*
|
|
158
|
-
* function MyComponent({ firstName, lastName }: MyComponentProps) {
|
|
159
|
-
* const service = useInject(MyComponentService);
|
|
160
|
-
*
|
|
161
|
-
* return <h1>Hello {service.computeUserName(firstName, lastName)}!</h1>
|
|
162
|
-
* }
|
|
163
|
-
*
|
|
164
|
-
* function App() {
|
|
165
|
-
* return (
|
|
166
|
-
* <ProvideModule module={MyComponentModule}>
|
|
167
|
-
* <MyComponent firstName={'John'} lastName={'Doe'} />
|
|
168
|
-
* </ProvideModule>
|
|
169
|
-
* );
|
|
170
|
-
* }
|
|
171
|
-
* ```
|
|
172
|
-
*
|
|
173
|
-
* @param param0 See {@link ProvideModuleFunctionParams}.
|
|
142
|
+
* @param module The {@link ModuleOrBlueprint} which should be consumed by the {@link component}.
|
|
174
143
|
* @returns The provided {@link toComponent | Component}.
|
|
175
144
|
*/
|
|
176
|
-
declare function
|
|
177
|
-
|
|
178
|
-
/** The {@link IComponentProviderModule | Module} which should be consumed by the {@link children | component}. */
|
|
179
|
-
module: IComponentProviderModule;
|
|
180
|
-
children: React__default.ReactElement;
|
|
181
|
-
}
|
|
145
|
+
declare function provideModuleToComponent<P extends Record<string, any>, C extends ReactElementWithProviderModule<P> = ReactElementWithProviderModule<P>>(module: ModuleOrBlueprint, component: ReactElementWithProviderModule<P>): C;
|
|
146
|
+
type ReactElementWithProviderModule<P extends Record<string, any>> = (p: PropsWithModule<P>) => react__default.ReactNode;
|
|
182
147
|
|
|
183
148
|
declare function hookFactory<P extends HookParams, D extends any[], T>({ use: hook, inject, }: HookFactoryParams<P, D, T>): (p: P) => T;
|
|
184
149
|
interface HookFactoryParams<P extends HookParams, D extends any[], T> {
|
|
185
|
-
use:
|
|
150
|
+
use: (p: HookWithDeps<P, D>) => T;
|
|
186
151
|
inject: ProviderToken[];
|
|
187
152
|
}
|
|
188
|
-
type HookWithProviderModuleDependencies<P extends HookParams, D extends any[], T> = (p: HookWithDeps<P, D>) => T;
|
|
189
153
|
type HookWithDeps<P extends HookParams, D extends any[]> = P & {
|
|
190
|
-
/** Array containing the resolved dependencies
|
|
154
|
+
/** Array containing the resolved dependencies. */
|
|
191
155
|
deps: D;
|
|
192
156
|
};
|
|
193
157
|
type HookParams = Record<string, any> | void;
|
|
194
158
|
|
|
195
|
-
export {
|
|
159
|
+
export { type HookFactoryParams, type HookWithDeps, type PropsWithModule, REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT, type ReactElementWithProviderModule, type UseInjectOptions, hookFactory, provideModuleToComponent, useComponentModule, useInject, useInjectMany };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,74 +1,12 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import react__default from 'react';
|
|
3
|
+
import * as _adimm_x_injection from '@adimm/x-injection';
|
|
4
|
+
import { IProviderModule, ProviderToken, ProviderModuleGetManyParam, ProviderIdentifier, ModuleOrBlueprint, DependencyProvider } from '@adimm/x-injection';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* When set to `true`, it'll automatically `contextualize` all the `imports` array.
|
|
9
|
-
*
|
|
10
|
-
* **Note:** _This is required when you have a parent component which renders one or more children
|
|
11
|
-
* and does also need to control their providers._
|
|
12
|
-
* _If not set to `true`, then the children providers would be resolved from their original `module`, therefore_
|
|
13
|
-
* _if your parent component has multiple instances of itself, all its children will actually share_
|
|
14
|
-
* _the same providers, especially when their module injection scope is set to `Singleton` (by default)._
|
|
15
|
-
*
|
|
16
|
-
* **Note2:** _If a module has {@link ProviderModuleOptions.markAsGlobal | markAsGlobal} set to `true`,_
|
|
17
|
-
* _this will automatically default to `false`._
|
|
18
|
-
*
|
|
19
|
-
* Defaults to `true`.
|
|
20
|
-
*/
|
|
21
|
-
contextualizeImports?: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface IComponentProviderModuleNaked extends IComponentProviderModule {
|
|
25
|
-
/**
|
|
26
|
-
* The original `id` which was assigned to this module.
|
|
27
|
-
*
|
|
28
|
-
* **Note:** _The module initial {@link ComponentProviderModuleOptions.identifier | id} will be suffixed with `Contextualized`._
|
|
29
|
-
*/
|
|
30
|
-
originalIdentifier: symbol;
|
|
31
|
-
/** See {@link ComponentProviderModuleOptions.contextualizeImports}. */
|
|
32
|
-
hasContextualizedImports: ComponentProviderModuleOptions['contextualizeImports'];
|
|
33
|
-
/** Indicates if this module has been initialized by a React Component or not. */
|
|
34
|
-
initializedFromComponent: boolean;
|
|
35
|
-
/**
|
|
36
|
-
* It is used internally by the `ProviderModule` to create a new cloned
|
|
37
|
-
* module which will be consumed only by that specific component instance.
|
|
38
|
-
*/
|
|
39
|
-
_createContextualizedComponentInstance(parentIdentifier?: symbol): IComponentProviderModule;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface IComponentProviderModule extends IProviderModule {
|
|
43
|
-
/**
|
|
44
|
-
* Casts the current module type to the {@link IComponentProviderModuleNaked} type.
|
|
45
|
-
*
|
|
46
|
-
* **Internally used and for testing purposes!**
|
|
47
|
-
*/
|
|
48
|
-
toNaked(): IComponentProviderModuleNaked & IProviderModuleNaked;
|
|
49
|
-
/**
|
|
50
|
-
* Should be invoked only when the component is not needed anymore.
|
|
51
|
-
*
|
|
52
|
-
* _eg: When changing page, removable components and so on._
|
|
53
|
-
*/
|
|
54
|
-
dispose(): Promise<void>;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
type PropsWithModule<P extends Record<string, any>> = P & {
|
|
58
|
-
/**
|
|
59
|
-
* The {@link IComponentProviderModule | Module} which this component should consume.
|
|
60
|
-
*
|
|
61
|
-
* **Note:** _You can easily override this in your unit tests to provide a module having mocked providers_
|
|
62
|
-
* _without the need to create a new mocked component by using the composable `ProvideModule` method._
|
|
63
|
-
*/
|
|
64
|
-
module?: IComponentProviderModule;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
declare const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT: React.Context<IComponentProviderModule>;
|
|
6
|
+
declare const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT: react.Context<IProviderModule>;
|
|
68
7
|
|
|
69
8
|
/**
|
|
70
|
-
* Low-level hook which can be used to resolve a single dependency
|
|
71
|
-
* context module.
|
|
9
|
+
* Low-level hook which can be used to resolve a single dependency.
|
|
72
10
|
*
|
|
73
11
|
* **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_
|
|
74
12
|
* _`hookFactory` method to compose a custom hook._
|
|
@@ -77,15 +15,24 @@ declare const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT: React.Context<IComponen
|
|
|
77
15
|
* @param options See {@link UseInjectOptions}.
|
|
78
16
|
* @returns The resolved {@link T | dependency}.
|
|
79
17
|
*/
|
|
80
|
-
declare function useInject<T>(provider: ProviderToken<T>, options?: UseInjectOptions): T
|
|
81
|
-
type UseInjectOptions = {
|
|
82
|
-
/**
|
|
83
|
-
|
|
18
|
+
declare function useInject<T, IsOptional extends boolean | undefined = undefined, AsList extends boolean | undefined = undefined>(provider: ProviderToken<T>, options?: UseInjectOptions<IsOptional, AsList>): _adimm_x_injection.ProviderModuleGetReturn<T, IsOptional, AsList>;
|
|
19
|
+
type UseInjectOptions<IsOptional extends boolean | undefined = undefined, AsList extends boolean | undefined = undefined> = {
|
|
20
|
+
/**
|
|
21
|
+
* When set to `false` an exception will be thrown when the supplied `ProviderToken` isn't bound.
|
|
22
|
+
*
|
|
23
|
+
* Defaults to `false`.
|
|
24
|
+
*/
|
|
25
|
+
isOptional?: IsOptional;
|
|
26
|
+
/**
|
|
27
|
+
* Set to `true` if you need to retrieve _all_ the bound identifiers of the supplied `ProviderToken`.
|
|
28
|
+
*
|
|
29
|
+
* Defaults to `false`.
|
|
30
|
+
*/
|
|
31
|
+
asList?: AsList;
|
|
84
32
|
};
|
|
85
33
|
|
|
86
34
|
/**
|
|
87
|
-
* Low-level hook which can be used to resolve multiple dependencies at once
|
|
88
|
-
* context module.
|
|
35
|
+
* Low-level hook which can be used to resolve multiple dependencies at once.
|
|
89
36
|
*
|
|
90
37
|
* **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_
|
|
91
38
|
* _`hookFactory` method to compose a custom hook._
|
|
@@ -93,30 +40,83 @@ type UseInjectOptions = {
|
|
|
93
40
|
* @param deps Either one or more {@link ProviderToken}.
|
|
94
41
|
* @returns Tuple containing the {@link D | dependencies}.
|
|
95
42
|
*/
|
|
96
|
-
declare function useInjectMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D
|
|
43
|
+
declare function useInjectMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken | ProviderIdentifier)[]>(...deps: D): _adimm_x_injection.ProviderModuleGetManyReturn<D>;
|
|
97
44
|
|
|
98
|
-
/** Can be used to retrieve the {@link
|
|
99
|
-
declare function useComponentModule():
|
|
45
|
+
/** Can be used to retrieve the {@link IProviderModule | Module} from the current context. */
|
|
46
|
+
declare function useComponentModule(): IProviderModule;
|
|
100
47
|
|
|
101
|
-
|
|
102
|
-
declare class ComponentProviderModule extends ProviderModule implements IComponentProviderModule {
|
|
103
|
-
protected readonly originalIdentifier: symbol;
|
|
104
|
-
protected readonly hasContextualizedImports: IComponentProviderModuleNaked['hasContextualizedImports'];
|
|
105
|
-
protected readonly initializedFromComponent: IComponentProviderModuleNaked['initializedFromComponent'];
|
|
106
|
-
constructor(options: ComponentProviderModuleOptions);
|
|
107
|
-
toNaked(): IComponentProviderModuleNaked & IProviderModuleNaked;
|
|
108
|
-
clone(options?: Partial<ComponentProviderModuleOptions>): IComponentProviderModule;
|
|
109
|
-
dispose(): Promise<void>;
|
|
48
|
+
type PropsWithModule<P extends Record<string, any>> = P & {
|
|
110
49
|
/**
|
|
111
|
-
*
|
|
50
|
+
* The {@link ModuleOrBlueprint} which this component should consume.
|
|
112
51
|
*
|
|
113
|
-
*
|
|
52
|
+
* **Note:** _Can be used to easily mock an entire module._
|
|
53
|
+
*
|
|
54
|
+
* example:
|
|
55
|
+
* ```tsx
|
|
56
|
+
* const CarModuleBp = ProviderModule.blueprint({
|
|
57
|
+
* id: 'CarModule',
|
|
58
|
+
* imports: [CarEngineModule, CarDashboardModule],
|
|
59
|
+
* providers: [CarService],
|
|
60
|
+
* exports: [CarService],
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* const cbMock = jest.fn();
|
|
64
|
+
*
|
|
65
|
+
* const CarModulBpeMocked = CarModuleBp.clone().updateDefinition({
|
|
66
|
+
* providers: [
|
|
67
|
+
* {
|
|
68
|
+
* provide: CarService, useValue: { startEngine: cbMock }
|
|
69
|
+
* },
|
|
70
|
+
* ]
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* await act(async () => render(<CarComponent module={CarModuleBpMocked} />));
|
|
74
|
+
*
|
|
75
|
+
* await waitFor(async () => {
|
|
76
|
+
* expect(cbMock).toHaveBeenCalled();
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
114
79
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
80
|
+
module?: ModuleOrBlueprint;
|
|
81
|
+
/**
|
|
82
|
+
* Can be used to control the dependencies consumed by this component.
|
|
83
|
+
* This is useful when you want to provide an already resolved instance of a dependency down the component tree.
|
|
84
|
+
*
|
|
85
|
+
* eg:
|
|
86
|
+
* ```tsx
|
|
87
|
+
* class InputboxService {
|
|
88
|
+
* currentValue = '';
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* class FormService {
|
|
92
|
+
* constructor(
|
|
93
|
+
* public readonly firstNameInputbox: InputboxService,
|
|
94
|
+
* public readonly lastNameInputbox: InputboxService
|
|
95
|
+
* ) {
|
|
96
|
+
* this.firstNameInputbox.currentValue = 'John';
|
|
97
|
+
* this.lastNameInputbox.currentValue = 'Doe';
|
|
98
|
+
* }
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* const Form = provideModuleToComponent(FormModule, () => {
|
|
102
|
+
* const service = useInject(FormService);
|
|
103
|
+
*
|
|
104
|
+
* return (
|
|
105
|
+
* <>
|
|
106
|
+
* <Inputbox inject={[{ provide: InputboxService, useValue: service.firstNameInputbox }]} />
|
|
107
|
+
* <Inputbox inject={[{ provide: InputboxService, useValue: service.lastNameInputbox }]} />
|
|
108
|
+
* </>
|
|
109
|
+
* );
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
inject?: DependencyProvider[];
|
|
114
|
+
};
|
|
117
115
|
|
|
118
116
|
/**
|
|
119
|
-
* Can be used to easily provide a {@link module} to
|
|
117
|
+
* Can be used to easily provide a {@link module} to a component.
|
|
118
|
+
*
|
|
119
|
+
* **Note:** _An error will be thrown if a `global` module is provided._
|
|
120
120
|
*
|
|
121
121
|
* @example
|
|
122
122
|
* ```tsx
|
|
@@ -139,57 +139,21 @@ declare class ComponentProviderModule extends ProviderModule implements ICompone
|
|
|
139
139
|
* }
|
|
140
140
|
* ```
|
|
141
141
|
*
|
|
142
|
-
* @param module The {@link
|
|
143
|
-
* @returns The provided {@link toComponent | Component}.
|
|
144
|
-
*/
|
|
145
|
-
declare function provideModuleToComponent<P extends Record<string, any>, C extends ReactElementWithProviderModule<P> = ReactElementWithProviderModule<P>>(module: IComponentProviderModule, component: ReactElementWithProviderModule<P>): C;
|
|
146
|
-
type ReactElementWithProviderModule<P extends Record<string, any>> = (p: PropsWithModule<P>) => React__default.ReactNode;
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Can be used to easily provide a {@link module} to any component.
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
152
|
-
* ```tsx
|
|
153
|
-
* interface MyComponentProps {
|
|
154
|
-
* firstName: string;
|
|
155
|
-
* lastName: string;
|
|
156
|
-
* }
|
|
157
|
-
*
|
|
158
|
-
* function MyComponent({ firstName, lastName }: MyComponentProps) {
|
|
159
|
-
* const service = useInject(MyComponentService);
|
|
160
|
-
*
|
|
161
|
-
* return <h1>Hello {service.computeUserName(firstName, lastName)}!</h1>
|
|
162
|
-
* }
|
|
163
|
-
*
|
|
164
|
-
* function App() {
|
|
165
|
-
* return (
|
|
166
|
-
* <ProvideModule module={MyComponentModule}>
|
|
167
|
-
* <MyComponent firstName={'John'} lastName={'Doe'} />
|
|
168
|
-
* </ProvideModule>
|
|
169
|
-
* );
|
|
170
|
-
* }
|
|
171
|
-
* ```
|
|
172
|
-
*
|
|
173
|
-
* @param param0 See {@link ProvideModuleFunctionParams}.
|
|
142
|
+
* @param module The {@link ModuleOrBlueprint} which should be consumed by the {@link component}.
|
|
174
143
|
* @returns The provided {@link toComponent | Component}.
|
|
175
144
|
*/
|
|
176
|
-
declare function
|
|
177
|
-
|
|
178
|
-
/** The {@link IComponentProviderModule | Module} which should be consumed by the {@link children | component}. */
|
|
179
|
-
module: IComponentProviderModule;
|
|
180
|
-
children: React__default.ReactElement;
|
|
181
|
-
}
|
|
145
|
+
declare function provideModuleToComponent<P extends Record<string, any>, C extends ReactElementWithProviderModule<P> = ReactElementWithProviderModule<P>>(module: ModuleOrBlueprint, component: ReactElementWithProviderModule<P>): C;
|
|
146
|
+
type ReactElementWithProviderModule<P extends Record<string, any>> = (p: PropsWithModule<P>) => react__default.ReactNode;
|
|
182
147
|
|
|
183
148
|
declare function hookFactory<P extends HookParams, D extends any[], T>({ use: hook, inject, }: HookFactoryParams<P, D, T>): (p: P) => T;
|
|
184
149
|
interface HookFactoryParams<P extends HookParams, D extends any[], T> {
|
|
185
|
-
use:
|
|
150
|
+
use: (p: HookWithDeps<P, D>) => T;
|
|
186
151
|
inject: ProviderToken[];
|
|
187
152
|
}
|
|
188
|
-
type HookWithProviderModuleDependencies<P extends HookParams, D extends any[], T> = (p: HookWithDeps<P, D>) => T;
|
|
189
153
|
type HookWithDeps<P extends HookParams, D extends any[]> = P & {
|
|
190
|
-
/** Array containing the resolved dependencies
|
|
154
|
+
/** Array containing the resolved dependencies. */
|
|
191
155
|
deps: D;
|
|
192
156
|
};
|
|
193
157
|
type HookParams = Record<string, any> | void;
|
|
194
158
|
|
|
195
|
-
export {
|
|
159
|
+
export { type HookFactoryParams, type HookWithDeps, type PropsWithModule, REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT, type ReactElementWithProviderModule, type UseInjectOptions, hookFactory, provideModuleToComponent, useComponentModule, useInject, useInjectMany };
|