ditox-react 1.4.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{index.d.ts → browser/index.d.ts} +0 -0
- package/dist/{index.browser.js → browser/index.js} +1 -1
- package/dist/browser/index.js.map +1 -0
- package/dist/cjs/DependencyContainer.d.ts +78 -0
- package/dist/cjs/DependencyContainer.js +101 -0
- package/dist/cjs/DependencyContainer.js.map +1 -0
- package/{src/DependencyModule.js.flow → dist/cjs/DependencyModule.d.ts} +7 -11
- package/dist/cjs/DependencyModule.js +56 -0
- package/dist/cjs/DependencyModule.js.map +1 -0
- package/dist/cjs/hooks.d.ts +25 -0
- package/dist/cjs/hooks.js +39 -0
- package/dist/cjs/hooks.js.map +1 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/DependencyContainer.d.ts +78 -0
- package/dist/esm/DependencyContainer.js +77 -0
- package/dist/esm/DependencyContainer.js.map +1 -0
- package/dist/esm/DependencyModule.d.ts +31 -0
- package/dist/esm/DependencyModule.js +33 -0
- package/dist/esm/DependencyModule.js.map +1 -0
- package/dist/esm/hooks.d.ts +25 -0
- package/dist/esm/hooks.js +33 -0
- package/dist/esm/hooks.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/{lib → dist/umd}/index.d.ts +0 -0
- package/dist/{index.umd.js → umd/index.js} +1 -1
- package/dist/umd/index.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +23 -65
- package/package.json +21 -22
- package/CHANGELOG.md +0 -133
- package/dist/index.browser.js.flow +0 -4
- package/dist/index.browser.js.map +0 -1
- package/dist/index.js +0 -129
- package/dist/index.js.flow +0 -4
- package/dist/index.js.map +0 -1
- package/dist/index.module.d.ts +0 -111
- package/dist/index.module.js +0 -117
- package/dist/index.module.js.flow +0 -4
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.d.ts +0 -111
- package/dist/index.umd.js.flow +0 -4
- package/dist/index.umd.js.map +0 -1
- package/lib/index.js +0 -117
- package/lib/index.js.flow +0 -4
- package/lib/index.js.map +0 -1
- package/src/DependencyContainer.js.flow +0 -18
- package/src/hooks.js.flow +0 -12
- package/src/index.js.flow +0 -5
package/dist/index.module.d.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { Container, Token, ModuleDeclaration, Module } from 'ditox';
|
|
2
|
-
import { ReactNode, ReactElement } from 'react';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A callback for binding dependencies to a container
|
|
6
|
-
*/
|
|
7
|
-
declare type DependencyContainerBinder = (container: Container) => unknown;
|
|
8
|
-
/**
|
|
9
|
-
* Specifies an existed container or options for a new container:
|
|
10
|
-
* @property binder - A callback which setup bindings to the container.
|
|
11
|
-
* @property root - If `true` then a new container does not depend on any parent containers
|
|
12
|
-
*/
|
|
13
|
-
declare type DependencyContainerParams = {
|
|
14
|
-
children: ReactNode;
|
|
15
|
-
root?: boolean;
|
|
16
|
-
binder?: DependencyContainerBinder;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* @category Component
|
|
20
|
-
*
|
|
21
|
-
* Provides a new dependency container to React app
|
|
22
|
-
*
|
|
23
|
-
* This component creates a new container and provides it down to React children.
|
|
24
|
-
*
|
|
25
|
-
* If `binder` callback is specified, it will be called for a new container
|
|
26
|
-
* to binds it with dependencies.
|
|
27
|
-
*
|
|
28
|
-
* If a parent container is exist, it is connected to the current one by default.
|
|
29
|
-
* For making a new root container specify `root` parameter as `true`,
|
|
30
|
-
* and the container will not depend on any parent container.
|
|
31
|
-
*
|
|
32
|
-
* @param params.binder - A callback which setup bindings to the container.
|
|
33
|
-
* @param params.root - If `true` then a new container does not depend on any parent containers
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
*
|
|
37
|
-
* ```tsx
|
|
38
|
-
* const TOKEN = token();
|
|
39
|
-
*
|
|
40
|
-
* function appDependencyBinder(container: Container) {
|
|
41
|
-
* container.bindValue(TOKEN, 'value');
|
|
42
|
-
* }
|
|
43
|
-
*
|
|
44
|
-
* function App() {
|
|
45
|
-
* return (
|
|
46
|
-
* <DependencyContainer root binder={appDependencyBinder}>
|
|
47
|
-
* <NestedComponent />
|
|
48
|
-
* </DependencyContainer>
|
|
49
|
-
* );
|
|
50
|
-
* }
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
*/
|
|
54
|
-
declare function DependencyContainer(params: DependencyContainerParams): ReactElement;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @category Hook
|
|
58
|
-
*
|
|
59
|
-
* Returns a dependency container. Throws an error in case the container is not provided.
|
|
60
|
-
*/
|
|
61
|
-
declare function useDependencyContainer(mode: 'strict'): Container;
|
|
62
|
-
/**
|
|
63
|
-
* @category Hook
|
|
64
|
-
*
|
|
65
|
-
* Returns a dependency container, or `undefined` in case the container is not provided.
|
|
66
|
-
*/
|
|
67
|
-
declare function useDependencyContainer(mode?: 'optional'): Container | undefined;
|
|
68
|
-
/**
|
|
69
|
-
* @category Hook
|
|
70
|
-
*
|
|
71
|
-
* Returns a dependency by token, or fails with an error.
|
|
72
|
-
*/
|
|
73
|
-
declare function useDependency<T>(token: Token<T>): T;
|
|
74
|
-
/**
|
|
75
|
-
* @category Hook
|
|
76
|
-
*
|
|
77
|
-
* Returns a dependency by token, or `undefined` in case the dependency is not provided.
|
|
78
|
-
*/
|
|
79
|
-
declare function useOptionalDependency<T>(token: Token<T>): T | undefined;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @category Component
|
|
83
|
-
*
|
|
84
|
-
* Binds the module to a new dependency container.
|
|
85
|
-
*
|
|
86
|
-
* If a parent container is exist, it is connected to the current one by default.
|
|
87
|
-
*
|
|
88
|
-
* @param params.module - Module declaration for binding
|
|
89
|
-
* @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
*
|
|
93
|
-
* ```tsx
|
|
94
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
95
|
-
*
|
|
96
|
-
* function App() {
|
|
97
|
-
* return (
|
|
98
|
-
* <DependencyModule module={LOGGER_MODULE}>
|
|
99
|
-
* <NestedComponent />
|
|
100
|
-
* </DependencyModule>
|
|
101
|
-
* );
|
|
102
|
-
* }
|
|
103
|
-
* ```
|
|
104
|
-
*/
|
|
105
|
-
declare function DependencyModule(params: {
|
|
106
|
-
children: ReactNode;
|
|
107
|
-
module: ModuleDeclaration<Module<Record<string, unknown>>>;
|
|
108
|
-
scope?: 'scoped' | 'singleton';
|
|
109
|
-
}): ReactElement;
|
|
110
|
-
|
|
111
|
-
export { DependencyContainer, DependencyContainerBinder, DependencyContainerParams, DependencyModule, useDependency, useDependencyContainer, useOptionalDependency };
|
package/dist/index.module.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { createContainer, bindModule } from 'ditox';
|
|
2
|
-
import React, { createContext, useContext, useMemo, useEffect, useCallback } from 'react';
|
|
3
|
-
|
|
4
|
-
const DependencyContainerContext = createContext(undefined);
|
|
5
|
-
/**
|
|
6
|
-
* @category Component
|
|
7
|
-
*
|
|
8
|
-
* Provides a new dependency container to React app
|
|
9
|
-
*
|
|
10
|
-
* This component creates a new container and provides it down to React children.
|
|
11
|
-
*
|
|
12
|
-
* If `binder` callback is specified, it will be called for a new container
|
|
13
|
-
* to binds it with dependencies.
|
|
14
|
-
*
|
|
15
|
-
* If a parent container is exist, it is connected to the current one by default.
|
|
16
|
-
* For making a new root container specify `root` parameter as `true`,
|
|
17
|
-
* and the container will not depend on any parent container.
|
|
18
|
-
*
|
|
19
|
-
* @param params.binder - A callback which setup bindings to the container.
|
|
20
|
-
* @param params.root - If `true` then a new container does not depend on any parent containers
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
*
|
|
24
|
-
* ```tsx
|
|
25
|
-
* const TOKEN = token();
|
|
26
|
-
*
|
|
27
|
-
* function appDependencyBinder(container: Container) {
|
|
28
|
-
* container.bindValue(TOKEN, 'value');
|
|
29
|
-
* }
|
|
30
|
-
*
|
|
31
|
-
* function App() {
|
|
32
|
-
* return (
|
|
33
|
-
* <DependencyContainer root binder={appDependencyBinder}>
|
|
34
|
-
* <NestedComponent />
|
|
35
|
-
* </DependencyContainer>
|
|
36
|
-
* );
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
*/
|
|
41
|
-
function DependencyContainer(params) {
|
|
42
|
-
const { children, root, binder } = params;
|
|
43
|
-
const parentContainer = useContext(DependencyContainerContext);
|
|
44
|
-
const container = useMemo(() => {
|
|
45
|
-
const container = createContainer(root ? undefined : parentContainer);
|
|
46
|
-
binder === null || binder === void 0 ? void 0 : binder(container);
|
|
47
|
-
return container;
|
|
48
|
-
}, [binder, parentContainer, root]);
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
return () => container.removeAll();
|
|
51
|
-
}, [container]);
|
|
52
|
-
return (React.createElement(DependencyContainerContext.Provider, { value: container }, children));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @internal
|
|
57
|
-
*/
|
|
58
|
-
function useDependencyContainer(mode) {
|
|
59
|
-
const container = useContext(DependencyContainerContext);
|
|
60
|
-
if (!container && mode === 'strict') {
|
|
61
|
-
throw new Error('Container is not provided by DependencyContainer component');
|
|
62
|
-
}
|
|
63
|
-
return container;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* @category Hook
|
|
67
|
-
*
|
|
68
|
-
* Returns a dependency by token, or fails with an error.
|
|
69
|
-
*/
|
|
70
|
-
function useDependency(token) {
|
|
71
|
-
const container = useDependencyContainer('strict');
|
|
72
|
-
const value = useMemo(() => container.resolve(token), [container, token]);
|
|
73
|
-
return value;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* @category Hook
|
|
77
|
-
*
|
|
78
|
-
* Returns a dependency by token, or `undefined` in case the dependency is not provided.
|
|
79
|
-
*/
|
|
80
|
-
function useOptionalDependency(token) {
|
|
81
|
-
const container = useDependencyContainer();
|
|
82
|
-
const value = useMemo(() => container === null || container === void 0 ? void 0 : container.get(token), [container, token]);
|
|
83
|
-
return value;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @category Component
|
|
88
|
-
*
|
|
89
|
-
* Binds the module to a new dependency container.
|
|
90
|
-
*
|
|
91
|
-
* If a parent container is exist, it is connected to the current one by default.
|
|
92
|
-
*
|
|
93
|
-
* @param params.module - Module declaration for binding
|
|
94
|
-
* @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
*
|
|
98
|
-
* ```tsx
|
|
99
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
100
|
-
*
|
|
101
|
-
* function App() {
|
|
102
|
-
* return (
|
|
103
|
-
* <DependencyModule module={LOGGER_MODULE}>
|
|
104
|
-
* <NestedComponent />
|
|
105
|
-
* </DependencyModule>
|
|
106
|
-
* );
|
|
107
|
-
* }
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
function DependencyModule(params) {
|
|
111
|
-
const { children, module, scope } = params;
|
|
112
|
-
const binder = useCallback((container) => bindModule(container, module, { scope }), [module, scope]);
|
|
113
|
-
return React.createElement(DependencyContainer, { binder: binder }, children);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export { DependencyContainer, DependencyModule, useDependency, useDependencyContainer, useOptionalDependency };
|
|
117
|
-
//# sourceMappingURL=index.module.js.map
|
package/dist/index.module.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.module.js","sources":["../src/DependencyContainer.tsx","../src/hooks.ts","../src/DependencyModule.tsx"],"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AAC;IAC1E,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;SAKgB,qBAAqB,CAAI,KAAe;IACtD,MAAM,SAAS,GAAG,sBAAsB,EAAE,CAAC;IAC3C,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;IACvE,OAAO,KAAK,CAAC;AACf;;AC/CA;;;;;;;;;;;;;;;;;;;;;;;;SAwBgB,gBAAgB,CAAC,MAIhC;IACC,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAC,GAAG,MAAM,CAAC;IAEzC,MAAM,MAAM,GAA8B,WAAW,CACnD,CAAC,SAAS,KAAK,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,EAAC,KAAK,EAAC,CAAC,EACrD,CAAC,MAAM,EAAE,KAAK,CAAC,CAChB,CAAC;IAEF,OAAO,oBAAC,mBAAmB,IAAC,MAAM,EAAE,MAAM,IAAG,QAAQ,CAAuB,CAAC;AAC/E;;;;"}
|
package/dist/index.umd.d.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { Container, Token, ModuleDeclaration, Module } from 'ditox';
|
|
2
|
-
import { ReactNode, ReactElement } from 'react';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A callback for binding dependencies to a container
|
|
6
|
-
*/
|
|
7
|
-
declare type DependencyContainerBinder = (container: Container) => unknown;
|
|
8
|
-
/**
|
|
9
|
-
* Specifies an existed container or options for a new container:
|
|
10
|
-
* @property binder - A callback which setup bindings to the container.
|
|
11
|
-
* @property root - If `true` then a new container does not depend on any parent containers
|
|
12
|
-
*/
|
|
13
|
-
declare type DependencyContainerParams = {
|
|
14
|
-
children: ReactNode;
|
|
15
|
-
root?: boolean;
|
|
16
|
-
binder?: DependencyContainerBinder;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* @category Component
|
|
20
|
-
*
|
|
21
|
-
* Provides a new dependency container to React app
|
|
22
|
-
*
|
|
23
|
-
* This component creates a new container and provides it down to React children.
|
|
24
|
-
*
|
|
25
|
-
* If `binder` callback is specified, it will be called for a new container
|
|
26
|
-
* to binds it with dependencies.
|
|
27
|
-
*
|
|
28
|
-
* If a parent container is exist, it is connected to the current one by default.
|
|
29
|
-
* For making a new root container specify `root` parameter as `true`,
|
|
30
|
-
* and the container will not depend on any parent container.
|
|
31
|
-
*
|
|
32
|
-
* @param params.binder - A callback which setup bindings to the container.
|
|
33
|
-
* @param params.root - If `true` then a new container does not depend on any parent containers
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
*
|
|
37
|
-
* ```tsx
|
|
38
|
-
* const TOKEN = token();
|
|
39
|
-
*
|
|
40
|
-
* function appDependencyBinder(container: Container) {
|
|
41
|
-
* container.bindValue(TOKEN, 'value');
|
|
42
|
-
* }
|
|
43
|
-
*
|
|
44
|
-
* function App() {
|
|
45
|
-
* return (
|
|
46
|
-
* <DependencyContainer root binder={appDependencyBinder}>
|
|
47
|
-
* <NestedComponent />
|
|
48
|
-
* </DependencyContainer>
|
|
49
|
-
* );
|
|
50
|
-
* }
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
*/
|
|
54
|
-
declare function DependencyContainer(params: DependencyContainerParams): ReactElement;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @category Hook
|
|
58
|
-
*
|
|
59
|
-
* Returns a dependency container. Throws an error in case the container is not provided.
|
|
60
|
-
*/
|
|
61
|
-
declare function useDependencyContainer(mode: 'strict'): Container;
|
|
62
|
-
/**
|
|
63
|
-
* @category Hook
|
|
64
|
-
*
|
|
65
|
-
* Returns a dependency container, or `undefined` in case the container is not provided.
|
|
66
|
-
*/
|
|
67
|
-
declare function useDependencyContainer(mode?: 'optional'): Container | undefined;
|
|
68
|
-
/**
|
|
69
|
-
* @category Hook
|
|
70
|
-
*
|
|
71
|
-
* Returns a dependency by token, or fails with an error.
|
|
72
|
-
*/
|
|
73
|
-
declare function useDependency<T>(token: Token<T>): T;
|
|
74
|
-
/**
|
|
75
|
-
* @category Hook
|
|
76
|
-
*
|
|
77
|
-
* Returns a dependency by token, or `undefined` in case the dependency is not provided.
|
|
78
|
-
*/
|
|
79
|
-
declare function useOptionalDependency<T>(token: Token<T>): T | undefined;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @category Component
|
|
83
|
-
*
|
|
84
|
-
* Binds the module to a new dependency container.
|
|
85
|
-
*
|
|
86
|
-
* If a parent container is exist, it is connected to the current one by default.
|
|
87
|
-
*
|
|
88
|
-
* @param params.module - Module declaration for binding
|
|
89
|
-
* @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
*
|
|
93
|
-
* ```tsx
|
|
94
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
95
|
-
*
|
|
96
|
-
* function App() {
|
|
97
|
-
* return (
|
|
98
|
-
* <DependencyModule module={LOGGER_MODULE}>
|
|
99
|
-
* <NestedComponent />
|
|
100
|
-
* </DependencyModule>
|
|
101
|
-
* );
|
|
102
|
-
* }
|
|
103
|
-
* ```
|
|
104
|
-
*/
|
|
105
|
-
declare function DependencyModule(params: {
|
|
106
|
-
children: ReactNode;
|
|
107
|
-
module: ModuleDeclaration<Module<Record<string, unknown>>>;
|
|
108
|
-
scope?: 'scoped' | 'singleton';
|
|
109
|
-
}): ReactElement;
|
|
110
|
-
|
|
111
|
-
export { DependencyContainer, DependencyContainerBinder, DependencyContainerParams, DependencyModule, useDependency, useDependencyContainer, useOptionalDependency };
|
package/dist/index.umd.js.flow
DELETED
package/dist/index.umd.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/DependencyContainer.tsx","../src/hooks.ts","../src/DependencyModule.tsx"],"sourcesContent":[null,null,null],"names":["DependencyContainerContext","createContext","undefined","DependencyContainer","params","children","root","binder","parentContainer","useContext","container","useMemo","createContainer","useEffect","removeAll","React","Provider","value","useDependencyContainer","mode","Error","module","scope","useCallback","bindModule","token","resolve","get"],"mappings":"2YAWaA,EAA6BC,qBACxCC,YAuDcC,EACdC,GAEO,IAAAC,EAA0BD,WAAhBE,EAAgBF,OAAVG,EAAUH,SAC3BI,EAAkBC,aAAWT,GAE7BU,EAAYC,WAAQ,WACxB,IAAMD,EAAYE,kBAAgBN,OAAOJ,EAAYM,GAGrD,OAFAD,MAAAA,GAAAA,EAASG,GAEFA,IACN,CAACH,EAAQC,EAAiBF,IAM7B,OAJAO,aAAU,WACR,OAAO,WAAM,OAAAH,EAAUI,eACtB,CAACJ,IAGFK,wBAACf,EAA2BgB,UAASC,MAAOP,GACzCL,YCjESa,EACdC,GAEA,IAAMT,EAAYD,aAAWT,GAE7B,IAAKU,GAAsB,WAATS,EAChB,MAAM,IAAIC,MACR,8DAIJ,OAAOV,sDCAwBN,GAKxB,IAAAC,EAA2BD,WAAjBiB,EAAiBjB,SAATkB,EAASlB,QAE5BG,EAAoCgB,eACxC,SAACb,GAAc,OAAAc,aAAWd,EAAWW,EAAQ,CAACC,YAC9C,CAACD,EAAQC,IAGX,OAAOP,wBAACZ,GAAoBI,OAAQA,GAASF,6BDJdoB,GAC/B,IAAMf,EAAYQ,EAAuB,UAEzC,OADcP,WAAQ,WAAM,OAAAD,EAAUgB,QAAQD,KAAQ,CAACf,EAAWe,iEAS3BA,GACvC,IAAMf,EAAYQ,IAElB,OADcP,WAAQ,WAAM,OAAAD,MAAAA,SAAAA,EAAWiB,IAAIF,KAAQ,CAACf,EAAWe"}
|
package/lib/index.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { createContainer, bindModule } from 'ditox';
|
|
2
|
-
import React, { createContext, useContext, useMemo, useEffect, useCallback } from 'react';
|
|
3
|
-
|
|
4
|
-
const DependencyContainerContext = createContext(undefined);
|
|
5
|
-
/**
|
|
6
|
-
* @category Component
|
|
7
|
-
*
|
|
8
|
-
* Provides a new dependency container to React app
|
|
9
|
-
*
|
|
10
|
-
* This component creates a new container and provides it down to React children.
|
|
11
|
-
*
|
|
12
|
-
* If `binder` callback is specified, it will be called for a new container
|
|
13
|
-
* to binds it with dependencies.
|
|
14
|
-
*
|
|
15
|
-
* If a parent container is exist, it is connected to the current one by default.
|
|
16
|
-
* For making a new root container specify `root` parameter as `true`,
|
|
17
|
-
* and the container will not depend on any parent container.
|
|
18
|
-
*
|
|
19
|
-
* @param params.binder - A callback which setup bindings to the container.
|
|
20
|
-
* @param params.root - If `true` then a new container does not depend on any parent containers
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
*
|
|
24
|
-
* ```tsx
|
|
25
|
-
* const TOKEN = token();
|
|
26
|
-
*
|
|
27
|
-
* function appDependencyBinder(container: Container) {
|
|
28
|
-
* container.bindValue(TOKEN, 'value');
|
|
29
|
-
* }
|
|
30
|
-
*
|
|
31
|
-
* function App() {
|
|
32
|
-
* return (
|
|
33
|
-
* <DependencyContainer root binder={appDependencyBinder}>
|
|
34
|
-
* <NestedComponent />
|
|
35
|
-
* </DependencyContainer>
|
|
36
|
-
* );
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
*/
|
|
41
|
-
function DependencyContainer(params) {
|
|
42
|
-
const { children, root, binder } = params;
|
|
43
|
-
const parentContainer = useContext(DependencyContainerContext);
|
|
44
|
-
const container = useMemo(() => {
|
|
45
|
-
const container = createContainer(root ? undefined : parentContainer);
|
|
46
|
-
binder === null || binder === void 0 ? void 0 : binder(container);
|
|
47
|
-
return container;
|
|
48
|
-
}, [binder, parentContainer, root]);
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
return () => container.removeAll();
|
|
51
|
-
}, [container]);
|
|
52
|
-
return (React.createElement(DependencyContainerContext.Provider, { value: container }, children));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @internal
|
|
57
|
-
*/
|
|
58
|
-
function useDependencyContainer(mode) {
|
|
59
|
-
const container = useContext(DependencyContainerContext);
|
|
60
|
-
if (!container && mode === 'strict') {
|
|
61
|
-
throw new Error('Container is not provided by DependencyContainer component');
|
|
62
|
-
}
|
|
63
|
-
return container;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* @category Hook
|
|
67
|
-
*
|
|
68
|
-
* Returns a dependency by token, or fails with an error.
|
|
69
|
-
*/
|
|
70
|
-
function useDependency(token) {
|
|
71
|
-
const container = useDependencyContainer('strict');
|
|
72
|
-
const value = useMemo(() => container.resolve(token), [container, token]);
|
|
73
|
-
return value;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* @category Hook
|
|
77
|
-
*
|
|
78
|
-
* Returns a dependency by token, or `undefined` in case the dependency is not provided.
|
|
79
|
-
*/
|
|
80
|
-
function useOptionalDependency(token) {
|
|
81
|
-
const container = useDependencyContainer();
|
|
82
|
-
const value = useMemo(() => container === null || container === void 0 ? void 0 : container.get(token), [container, token]);
|
|
83
|
-
return value;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @category Component
|
|
88
|
-
*
|
|
89
|
-
* Binds the module to a new dependency container.
|
|
90
|
-
*
|
|
91
|
-
* If a parent container is exist, it is connected to the current one by default.
|
|
92
|
-
*
|
|
93
|
-
* @param params.module - Module declaration for binding
|
|
94
|
-
* @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
*
|
|
98
|
-
* ```tsx
|
|
99
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
100
|
-
*
|
|
101
|
-
* function App() {
|
|
102
|
-
* return (
|
|
103
|
-
* <DependencyModule module={LOGGER_MODULE}>
|
|
104
|
-
* <NestedComponent />
|
|
105
|
-
* </DependencyModule>
|
|
106
|
-
* );
|
|
107
|
-
* }
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
function DependencyModule(params) {
|
|
111
|
-
const { children, module, scope } = params;
|
|
112
|
-
const binder = useCallback((container) => bindModule(container, module, { scope }), [module, scope]);
|
|
113
|
-
return React.createElement(DependencyContainer, { binder: binder }, children);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export { DependencyContainer, DependencyModule, useDependency, useDependencyContainer, useOptionalDependency };
|
|
117
|
-
//# sourceMappingURL=index.js.map
|
package/lib/index.js.flow
DELETED
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/DependencyContainer.tsx","../src/hooks.ts","../src/DependencyModule.tsx"],"sourcesContent":[null,null,null],"names":[],"mappings":";;;AAWO,MAAM,0BAA0B,GAAG,aAAa,CACrD,SAAS,CACV,CAAC;AAkBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAoCgB,mBAAmB,CACjC,MAAiC;IAEjC,MAAM,EAAC,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAC,GAAG,MAAM,CAAC;IACxC,MAAM,eAAe,GAAG,UAAU,CAAC,0BAA0B,CAAC,CAAC;IAE/D,MAAM,SAAS,GAAG,OAAO,CAAC;QACxB,MAAM,SAAS,GAAG,eAAe,CAAC,IAAI,GAAG,SAAS,GAAG,eAAe,CAAC,CAAC;QACtE,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAG,SAAS,CAAC,CAAC;QAEpB,OAAO,SAAS,CAAC;KAClB,EAAE,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,CAAC,CAAC,CAAC;IAEpC,SAAS,CAAC;QACR,OAAO,MAAM,SAAS,CAAC,SAAS,EAAE,CAAC;KACpC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAEhB,QACE,oBAAC,0BAA0B,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,IAClD,QAAQ,CAC2B,EACtC;AACJ;;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Container} from 'ditox';
|
|
4
|
-
import type {Node} from 'react';
|
|
5
|
-
import React from 'react';
|
|
6
|
-
|
|
7
|
-
export type DependencyContainerBinder = (container: Container) => mixed;
|
|
8
|
-
|
|
9
|
-
declare export function DependencyContainer(params: {
|
|
10
|
-
children: Node,
|
|
11
|
-
root?: boolean,
|
|
12
|
-
binder?: DependencyContainerBinder,
|
|
13
|
-
}): Node;
|
|
14
|
-
|
|
15
|
-
declare export function CustomDependencyContainer(params: {
|
|
16
|
-
children: Node,
|
|
17
|
-
container: Container,
|
|
18
|
-
}): Node;
|
package/src/hooks.js.flow
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type {Container, Token} from 'ditox';
|
|
4
|
-
|
|
5
|
-
declare export function useDependencyContainer(mode: 'strict'): Container;
|
|
6
|
-
declare export function useDependencyContainer(
|
|
7
|
-
mode?: 'optional',
|
|
8
|
-
): Container | void;
|
|
9
|
-
|
|
10
|
-
declare export function useDependency<T>(token: Token<T>): T;
|
|
11
|
-
|
|
12
|
-
declare export function useOptionalDependency<T>(token: Token<T>): T | void;
|