ditox-react 1.4.1 → 2.2.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.
Files changed (53) hide show
  1. package/README.md +1 -1
  2. package/dist/{index.d.ts → browser/index.d.ts} +0 -0
  3. package/dist/{index.browser.js → browser/index.js} +1 -1
  4. package/dist/browser/index.js.map +1 -0
  5. package/dist/cjs/DependencyContainer.d.ts +78 -0
  6. package/dist/cjs/DependencyContainer.js +101 -0
  7. package/dist/cjs/DependencyContainer.js.map +1 -0
  8. package/{src/DependencyModule.js.flow → dist/cjs/DependencyModule.d.ts} +7 -11
  9. package/dist/cjs/DependencyModule.js +56 -0
  10. package/dist/cjs/DependencyModule.js.map +1 -0
  11. package/dist/cjs/hooks.d.ts +25 -0
  12. package/dist/cjs/hooks.js +39 -0
  13. package/dist/cjs/hooks.js.map +1 -0
  14. package/dist/cjs/index.d.ts +4 -0
  15. package/dist/cjs/index.js +12 -0
  16. package/dist/cjs/index.js.map +1 -0
  17. package/dist/esm/DependencyContainer.d.ts +78 -0
  18. package/dist/esm/DependencyContainer.js +77 -0
  19. package/dist/esm/DependencyContainer.js.map +1 -0
  20. package/dist/esm/DependencyModule.d.ts +31 -0
  21. package/dist/esm/DependencyModule.js +33 -0
  22. package/dist/esm/DependencyModule.js.map +1 -0
  23. package/dist/esm/hooks.d.ts +25 -0
  24. package/dist/esm/hooks.js +33 -0
  25. package/dist/esm/hooks.js.map +1 -0
  26. package/dist/esm/index.d.ts +4 -0
  27. package/dist/esm/index.js +4 -0
  28. package/dist/esm/index.js.map +1 -0
  29. package/{lib → dist/umd}/index.d.ts +0 -0
  30. package/dist/{index.umd.js → umd/index.js} +1 -1
  31. package/dist/umd/index.js.map +1 -0
  32. package/docs/.nojekyll +1 -0
  33. package/docs/README.md +23 -65
  34. package/package.json +21 -22
  35. package/CHANGELOG.md +0 -125
  36. package/dist/index.browser.js.flow +0 -4
  37. package/dist/index.browser.js.map +0 -1
  38. package/dist/index.js +0 -129
  39. package/dist/index.js.flow +0 -4
  40. package/dist/index.js.map +0 -1
  41. package/dist/index.module.d.ts +0 -111
  42. package/dist/index.module.js +0 -117
  43. package/dist/index.module.js.flow +0 -4
  44. package/dist/index.module.js.map +0 -1
  45. package/dist/index.umd.d.ts +0 -111
  46. package/dist/index.umd.js.flow +0 -4
  47. package/dist/index.umd.js.map +0 -1
  48. package/lib/index.js +0 -117
  49. package/lib/index.js.flow +0 -4
  50. package/lib/index.js.map +0 -1
  51. package/src/DependencyContainer.js.flow +0 -18
  52. package/src/hooks.js.flow +0 -12
  53. package/src/index.js.flow +0 -5
package/README.md CHANGED
@@ -28,7 +28,7 @@ React.js tooling for [Ditox.js](https://github.com/mnasyrov/ditox) DI container.
28
28
  - Container hierarchy
29
29
  - Scoped containers
30
30
  - Multi-value tokens
31
- - Typescript and Flow typings
31
+ - Typescript typings
32
32
  - Supports Node.js, Deno and browsers
33
33
 
34
34
  ## Installation
File without changes
@@ -1,2 +1,2 @@
1
1
  import{createContainer as r,bindModule as n}from"ditox";import e,{createContext as t,useContext as o,useMemo as i,useEffect as u,useCallback as c}from"react";var v=t(void 0);function f(n){var t=n.children,c=n.root,f=n.binder,a=o(v),d=i((function(){var n=r(c?void 0:a);return null==f||f(n),n}),[f,a,c]);return u((function(){return function(){return d.removeAll()}}),[d]),e.createElement(v.Provider,{value:d},t)}function a(r){var n=o(v);if(!n&&"strict"===r)throw new Error("Container is not provided by DependencyContainer component");return n}function d(r){var n=a("strict");return i((function(){return n.resolve(r)}),[n,r])}function l(r){var n=a();return i((function(){return null==n?void 0:n.get(r)}),[n,r])}function m(r){var t=r.children,o=r.module,i=r.scope,u=c((function(r){return n(r,o,{scope:i})}),[o,i]);return e.createElement(f,{binder:u},t)}export{f as DependencyContainer,m as DependencyModule,d as useDependency,a as useDependencyContainer,l as useOptionalDependency};
2
- //# sourceMappingURL=index.browser.js.map
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.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","useDependency","token","resolve","useOptionalDependency","get","DependencyModule","module","scope","useCallback","bindModule"],"mappings":"8JAWO,IAAMA,EAA6BC,OACxCC,YAuDcC,EACdC,GAEO,IAAAC,EAA0BD,WAAhBE,EAAgBF,OAAVG,EAAUH,SAC3BI,EAAkBC,EAAWT,GAE7BU,EAAYC,GAAQ,WACxB,IAAMD,EAAYE,EAAgBN,OAAOJ,EAAYM,GAGrD,OAFAD,MAAAA,GAAAA,EAASG,GAEFA,IACN,CAACH,EAAQC,EAAiBF,IAM7B,OAJAO,GAAU,WACR,OAAO,WAAM,OAAAH,EAAUI,eACtB,CAACJ,IAGFK,gBAACf,EAA2BgB,UAASC,MAAOP,GACzCL,YCjESa,EACdC,GAEA,IAAMT,EAAYD,EAAWT,GAE7B,IAAKU,GAAsB,WAATS,EAChB,MAAM,IAAIC,MACR,8DAIJ,OAAOV,WAQOW,EAAiBC,GAC/B,IAAMZ,EAAYQ,EAAuB,UAEzC,OADcP,GAAQ,WAAM,OAAAD,EAAUa,QAAQD,KAAQ,CAACZ,EAAWY,aASpDE,EAAyBF,GACvC,IAAMZ,EAAYQ,IAElB,OADcP,GAAQ,WAAM,OAAAD,MAAAA,SAAAA,EAAWe,IAAIH,KAAQ,CAACZ,EAAWY,aCrBjDI,EAAiBtB,GAKxB,IAAAC,EAA2BD,WAAjBuB,EAAiBvB,SAATwB,EAASxB,QAE5BG,EAAoCsB,GACxC,SAACnB,GAAc,OAAAoB,EAAWpB,EAAWiB,EAAQ,CAACC,YAC9C,CAACD,EAAQC,IAGX,OAAOb,gBAACZ,GAAoBI,OAAQA,GAASF"}
@@ -0,0 +1,78 @@
1
+ import type { Container } from 'ditox';
2
+ import React, { ReactElement, ReactNode } from 'react';
3
+ export declare const DependencyContainerContext: React.Context<Container | undefined>;
4
+ /**
5
+ * A callback for binding dependencies to a container
6
+ */
7
+ export 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
+ export 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
+ export declare function DependencyContainer(params: DependencyContainerParams): ReactElement;
55
+ /**
56
+ * @category Component
57
+ *
58
+ * Provides a custom dependency container to React app
59
+ *
60
+ * @param params.container - a custom container
61
+ *
62
+ * @example
63
+ * ```tsx
64
+ * const container = useMemo(() => {
65
+ * return createContainer();
66
+ * }
67
+ *
68
+ * return (
69
+ * <CustomDependencyContainer container={container}>
70
+ * {children}
71
+ * </CustomDependencyContainer>
72
+ * );
73
+ * ```
74
+ */
75
+ export declare function CustomDependencyContainer(params: {
76
+ children: ReactNode;
77
+ container: Container;
78
+ }): ReactElement;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.CustomDependencyContainer = exports.DependencyContainer = exports.DependencyContainerContext = void 0;
23
+ const ditox_1 = require("ditox");
24
+ const react_1 = __importStar(require("react"));
25
+ exports.DependencyContainerContext = (0, react_1.createContext)(undefined);
26
+ /**
27
+ * @category Component
28
+ *
29
+ * Provides a new dependency container to React app
30
+ *
31
+ * This component creates a new container and provides it down to React children.
32
+ *
33
+ * If `binder` callback is specified, it will be called for a new container
34
+ * to binds it with dependencies.
35
+ *
36
+ * If a parent container is exist, it is connected to the current one by default.
37
+ * For making a new root container specify `root` parameter as `true`,
38
+ * and the container will not depend on any parent container.
39
+ *
40
+ * @param params.binder - A callback which setup bindings to the container.
41
+ * @param params.root - If `true` then a new container does not depend on any parent containers
42
+ *
43
+ * @example
44
+ *
45
+ * ```tsx
46
+ * const TOKEN = token();
47
+ *
48
+ * function appDependencyBinder(container: Container) {
49
+ * container.bindValue(TOKEN, 'value');
50
+ * }
51
+ *
52
+ * function App() {
53
+ * return (
54
+ * <DependencyContainer root binder={appDependencyBinder}>
55
+ * <NestedComponent />
56
+ * </DependencyContainer>
57
+ * );
58
+ * }
59
+ * ```
60
+ *
61
+ */
62
+ function DependencyContainer(params) {
63
+ const { children, root, binder } = params;
64
+ const parentContainer = (0, react_1.useContext)(exports.DependencyContainerContext);
65
+ const container = (0, react_1.useMemo)(() => {
66
+ const container = (0, ditox_1.createContainer)(root ? undefined : parentContainer);
67
+ binder === null || binder === void 0 ? void 0 : binder(container);
68
+ return container;
69
+ }, [binder, parentContainer, root]);
70
+ (0, react_1.useEffect)(() => {
71
+ return () => container.removeAll();
72
+ }, [container]);
73
+ return (react_1.default.createElement(exports.DependencyContainerContext.Provider, { value: container }, children));
74
+ }
75
+ exports.DependencyContainer = DependencyContainer;
76
+ /**
77
+ * @category Component
78
+ *
79
+ * Provides a custom dependency container to React app
80
+ *
81
+ * @param params.container - a custom container
82
+ *
83
+ * @example
84
+ * ```tsx
85
+ * const container = useMemo(() => {
86
+ * return createContainer();
87
+ * }
88
+ *
89
+ * return (
90
+ * <CustomDependencyContainer container={container}>
91
+ * {children}
92
+ * </CustomDependencyContainer>
93
+ * );
94
+ * ```
95
+ */
96
+ function CustomDependencyContainer(params) {
97
+ const { children, container } = params;
98
+ return (react_1.default.createElement(exports.DependencyContainerContext.Provider, { value: container }, children));
99
+ }
100
+ exports.CustomDependencyContainer = CustomDependencyContainer;
101
+ //# sourceMappingURL=DependencyContainer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DependencyContainer.js","sourceRoot":"","sources":["../../src/DependencyContainer.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AACA,iCAAsC;AACtC,+CAOe;AAEF,QAAA,0BAA0B,GAAG,IAAA,qBAAa,EACrD,SAAS,CACV,CAAC;AAkBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,SAAgB,mBAAmB,CACjC,MAAiC;IAEjC,MAAM,EAAC,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAC,GAAG,MAAM,CAAC;IACxC,MAAM,eAAe,GAAG,IAAA,kBAAU,EAAC,kCAA0B,CAAC,CAAC;IAE/D,MAAM,SAAS,GAAG,IAAA,eAAO,EAAC,GAAG,EAAE;QAC7B,MAAM,SAAS,GAAG,IAAA,uBAAe,EAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC;QACtE,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAG,SAAS,CAAC,CAAC;QAEpB,OAAO,SAAS,CAAC;IACnB,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,CAAC,CAAC,CAAC;IAEpC,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC;IACrC,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAEhB,OAAO,CACL,8BAAC,kCAA0B,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,IAClD,QAAQ,CAC2B,CACvC,CAAC;AACJ,CAAC;AAtBD,kDAsBC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,yBAAyB,CAAC,MAGzC;IACC,MAAM,EAAC,QAAQ,EAAE,SAAS,EAAC,GAAG,MAAM,CAAC;IAErC,OAAO,CACL,8BAAC,kCAA0B,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,IAClD,QAAQ,CAC2B,CACvC,CAAC;AACJ,CAAC;AAXD,8DAWC"}
@@ -1,9 +1,5 @@
1
- // @flow strict
2
-
3
- import type {Module, ModuleDeclaration} from 'ditox';
4
- import type {Node} from 'react';
5
- import React from 'react';
6
-
1
+ import type { Module, ModuleDeclaration } from 'ditox';
2
+ import { ReactElement, ReactNode } from 'react';
7
3
  /**
8
4
  * @category Component
9
5
  *
@@ -28,8 +24,8 @@ import React from 'react';
28
24
  * }
29
25
  * ```
30
26
  */
31
- declare export function DependencyModule(params: {
32
- children: Node,
33
- module: ModuleDeclaration<Module<*>>,
34
- scope?: 'scoped' | 'singleton',
35
- }): Node;
27
+ export declare function DependencyModule(params: {
28
+ children: ReactNode;
29
+ module: ModuleDeclaration<Module<Record<string, unknown>>>;
30
+ scope?: 'scoped' | 'singleton';
31
+ }): ReactElement;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.DependencyModule = void 0;
23
+ const ditox_1 = require("ditox");
24
+ const react_1 = __importStar(require("react"));
25
+ const DependencyContainer_1 = require("./DependencyContainer");
26
+ /**
27
+ * @category Component
28
+ *
29
+ * Binds the module to a new dependency container.
30
+ *
31
+ * If a parent container is exist, it is connected to the current one by default.
32
+ *
33
+ * @param params.module - Module declaration for binding
34
+ * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
35
+ *
36
+ * @example
37
+ *
38
+ * ```tsx
39
+ * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
40
+ *
41
+ * function App() {
42
+ * return (
43
+ * <DependencyModule module={LOGGER_MODULE}>
44
+ * <NestedComponent />
45
+ * </DependencyModule>
46
+ * );
47
+ * }
48
+ * ```
49
+ */
50
+ function DependencyModule(params) {
51
+ const { children, module, scope } = params;
52
+ const binder = (0, react_1.useCallback)((container) => (0, ditox_1.bindModule)(container, module, { scope }), [module, scope]);
53
+ return react_1.default.createElement(DependencyContainer_1.DependencyContainer, { binder: binder }, children);
54
+ }
55
+ exports.DependencyModule = DependencyModule;
56
+ //# sourceMappingURL=DependencyModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DependencyModule.js","sourceRoot":"","sources":["../../src/DependencyModule.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AACA,iCAAiC;AACjC,+CAAkE;AAClE,+DAG+B;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,SAAgB,gBAAgB,CAAC,MAIhC;IACC,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAC,GAAG,MAAM,CAAC;IAEzC,MAAM,MAAM,GAA8B,IAAA,mBAAW,EACnD,CAAC,SAAS,EAAE,EAAE,CAAC,IAAA,kBAAU,EAAC,SAAS,EAAE,MAAM,EAAE,EAAC,KAAK,EAAC,CAAC,EACrD,CAAC,MAAM,EAAE,KAAK,CAAC,CAChB,CAAC;IAEF,OAAO,8BAAC,yCAAmB,IAAC,MAAM,EAAE,MAAM,IAAG,QAAQ,CAAuB,CAAC;AAC/E,CAAC;AAbD,4CAaC"}
@@ -0,0 +1,25 @@
1
+ import { Container, Token } from 'ditox';
2
+ /**
3
+ * @category Hook
4
+ *
5
+ * Returns a dependency container. Throws an error in case the container is not provided.
6
+ */
7
+ export declare function useDependencyContainer(mode: 'strict'): Container;
8
+ /**
9
+ * @category Hook
10
+ *
11
+ * Returns a dependency container, or `undefined` in case the container is not provided.
12
+ */
13
+ export declare function useDependencyContainer(mode?: 'optional'): Container | undefined;
14
+ /**
15
+ * @category Hook
16
+ *
17
+ * Returns a dependency by token, or fails with an error.
18
+ */
19
+ export declare function useDependency<T>(token: Token<T>): T;
20
+ /**
21
+ * @category Hook
22
+ *
23
+ * Returns a dependency by token, or `undefined` in case the dependency is not provided.
24
+ */
25
+ export declare function useOptionalDependency<T>(token: Token<T>): T | undefined;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useOptionalDependency = exports.useDependency = exports.useDependencyContainer = void 0;
4
+ const react_1 = require("react");
5
+ const DependencyContainer_1 = require("./DependencyContainer");
6
+ /**
7
+ * @internal
8
+ */
9
+ function useDependencyContainer(mode) {
10
+ const container = (0, react_1.useContext)(DependencyContainer_1.DependencyContainerContext);
11
+ if (!container && mode === 'strict') {
12
+ throw new Error('Container is not provided by DependencyContainer component');
13
+ }
14
+ return container;
15
+ }
16
+ exports.useDependencyContainer = useDependencyContainer;
17
+ /**
18
+ * @category Hook
19
+ *
20
+ * Returns a dependency by token, or fails with an error.
21
+ */
22
+ function useDependency(token) {
23
+ const container = useDependencyContainer('strict');
24
+ const value = (0, react_1.useMemo)(() => container.resolve(token), [container, token]);
25
+ return value;
26
+ }
27
+ exports.useDependency = useDependency;
28
+ /**
29
+ * @category Hook
30
+ *
31
+ * Returns a dependency by token, or `undefined` in case the dependency is not provided.
32
+ */
33
+ function useOptionalDependency(token) {
34
+ const container = useDependencyContainer();
35
+ const value = (0, react_1.useMemo)(() => container === null || container === void 0 ? void 0 : container.get(token), [container, token]);
36
+ return value;
37
+ }
38
+ exports.useOptionalDependency = useOptionalDependency;
39
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/hooks.ts"],"names":[],"mappings":";;;AACA,iCAA0C;AAC1C,+DAAiE;AAgBjE;;GAEG;AACH,SAAgB,sBAAsB,CACpC,IAA4B;IAE5B,MAAM,SAAS,GAAG,IAAA,kBAAU,EAAC,gDAA0B,CAAC,CAAC;IAEzD,IAAI,CAAC,SAAS,IAAI,IAAI,KAAK,QAAQ,EAAE;QACnC,MAAM,IAAI,KAAK,CACb,4DAA4D,CAC7D,CAAC;KACH;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAZD,wDAYC;AAED;;;;GAIG;AACH,SAAgB,aAAa,CAAI,KAAe;IAC9C,MAAM,SAAS,GAAG,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IACnD,MAAM,KAAK,GAAG,IAAA,eAAO,EAAC,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;IAC1E,OAAO,KAAK,CAAC;AACf,CAAC;AAJD,sCAIC;AAED;;;;GAIG;AACH,SAAgB,qBAAqB,CAAI,KAAe;IACtD,MAAM,SAAS,GAAG,sBAAsB,EAAE,CAAC;IAC3C,MAAM,KAAK,GAAG,IAAA,eAAO,EAAC,GAAG,EAAE,CAAC,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;IACvE,OAAO,KAAK,CAAC;AACf,CAAC;AAJD,sDAIC"}
@@ -0,0 +1,4 @@
1
+ export type { DependencyContainerBinder, DependencyContainerParams, } from './DependencyContainer';
2
+ export { DependencyContainer } from './DependencyContainer';
3
+ export { useDependencyContainer, useDependency, useOptionalDependency, } from './hooks';
4
+ export { DependencyModule } from './DependencyModule';
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DependencyModule = exports.useOptionalDependency = exports.useDependency = exports.useDependencyContainer = exports.DependencyContainer = void 0;
4
+ var DependencyContainer_1 = require("./DependencyContainer");
5
+ Object.defineProperty(exports, "DependencyContainer", { enumerable: true, get: function () { return DependencyContainer_1.DependencyContainer; } });
6
+ var hooks_1 = require("./hooks");
7
+ Object.defineProperty(exports, "useDependencyContainer", { enumerable: true, get: function () { return hooks_1.useDependencyContainer; } });
8
+ Object.defineProperty(exports, "useDependency", { enumerable: true, get: function () { return hooks_1.useDependency; } });
9
+ Object.defineProperty(exports, "useOptionalDependency", { enumerable: true, get: function () { return hooks_1.useOptionalDependency; } });
10
+ var DependencyModule_1 = require("./DependencyModule");
11
+ Object.defineProperty(exports, "DependencyModule", { enumerable: true, get: function () { return DependencyModule_1.DependencyModule; } });
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAIA,6DAA0D;AAAlD,0HAAA,mBAAmB,OAAA;AAE3B,iCAIiB;AAHf,+GAAA,sBAAsB,OAAA;AACtB,sGAAA,aAAa,OAAA;AACb,8GAAA,qBAAqB,OAAA;AAGvB,uDAAoD;AAA5C,oHAAA,gBAAgB,OAAA"}
@@ -0,0 +1,78 @@
1
+ import type { Container } from 'ditox';
2
+ import React, { ReactElement, ReactNode } from 'react';
3
+ export declare const DependencyContainerContext: React.Context<Container | undefined>;
4
+ /**
5
+ * A callback for binding dependencies to a container
6
+ */
7
+ export 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
+ export 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
+ export declare function DependencyContainer(params: DependencyContainerParams): ReactElement;
55
+ /**
56
+ * @category Component
57
+ *
58
+ * Provides a custom dependency container to React app
59
+ *
60
+ * @param params.container - a custom container
61
+ *
62
+ * @example
63
+ * ```tsx
64
+ * const container = useMemo(() => {
65
+ * return createContainer();
66
+ * }
67
+ *
68
+ * return (
69
+ * <CustomDependencyContainer container={container}>
70
+ * {children}
71
+ * </CustomDependencyContainer>
72
+ * );
73
+ * ```
74
+ */
75
+ export declare function CustomDependencyContainer(params: {
76
+ children: ReactNode;
77
+ container: Container;
78
+ }): ReactElement;
@@ -0,0 +1,77 @@
1
+ import { createContainer } from 'ditox';
2
+ import React, { createContext, useContext, useEffect, useMemo, } from 'react';
3
+ export const DependencyContainerContext = createContext(undefined);
4
+ /**
5
+ * @category Component
6
+ *
7
+ * Provides a new dependency container to React app
8
+ *
9
+ * This component creates a new container and provides it down to React children.
10
+ *
11
+ * If `binder` callback is specified, it will be called for a new container
12
+ * to binds it with dependencies.
13
+ *
14
+ * If a parent container is exist, it is connected to the current one by default.
15
+ * For making a new root container specify `root` parameter as `true`,
16
+ * and the container will not depend on any parent container.
17
+ *
18
+ * @param params.binder - A callback which setup bindings to the container.
19
+ * @param params.root - If `true` then a new container does not depend on any parent containers
20
+ *
21
+ * @example
22
+ *
23
+ * ```tsx
24
+ * const TOKEN = token();
25
+ *
26
+ * function appDependencyBinder(container: Container) {
27
+ * container.bindValue(TOKEN, 'value');
28
+ * }
29
+ *
30
+ * function App() {
31
+ * return (
32
+ * <DependencyContainer root binder={appDependencyBinder}>
33
+ * <NestedComponent />
34
+ * </DependencyContainer>
35
+ * );
36
+ * }
37
+ * ```
38
+ *
39
+ */
40
+ export function DependencyContainer(params) {
41
+ const { children, root, binder } = params;
42
+ const parentContainer = useContext(DependencyContainerContext);
43
+ const container = useMemo(() => {
44
+ const container = createContainer(root ? undefined : parentContainer);
45
+ binder === null || binder === void 0 ? void 0 : binder(container);
46
+ return container;
47
+ }, [binder, parentContainer, root]);
48
+ useEffect(() => {
49
+ return () => container.removeAll();
50
+ }, [container]);
51
+ return (React.createElement(DependencyContainerContext.Provider, { value: container }, children));
52
+ }
53
+ /**
54
+ * @category Component
55
+ *
56
+ * Provides a custom dependency container to React app
57
+ *
58
+ * @param params.container - a custom container
59
+ *
60
+ * @example
61
+ * ```tsx
62
+ * const container = useMemo(() => {
63
+ * return createContainer();
64
+ * }
65
+ *
66
+ * return (
67
+ * <CustomDependencyContainer container={container}>
68
+ * {children}
69
+ * </CustomDependencyContainer>
70
+ * );
71
+ * ```
72
+ */
73
+ export function CustomDependencyContainer(params) {
74
+ const { children, container } = params;
75
+ return (React.createElement(DependencyContainerContext.Provider, { value: container }, children));
76
+ }
77
+ //# sourceMappingURL=DependencyContainer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DependencyContainer.js","sourceRoot":"","sources":["../../src/DependencyContainer.tsx"],"names":[],"mappings":"AACA,OAAO,EAAC,eAAe,EAAC,MAAM,OAAO,CAAC;AACtC,OAAO,KAAK,EAAE,EACZ,aAAa,EAGb,UAAU,EACV,SAAS,EACT,OAAO,GACR,MAAM,OAAO,CAAC;AAEf,MAAM,CAAC,MAAM,0BAA0B,GAAG,aAAa,CACrD,SAAS,CACV,CAAC;AAkBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,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,GAAG,EAAE;QAC7B,MAAM,SAAS,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC;QACtE,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAG,SAAS,CAAC,CAAC;QAEpB,OAAO,SAAS,CAAC;IACnB,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,CAAC,CAAC,CAAC;IAEpC,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC;IACrC,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAEhB,OAAO,CACL,oBAAC,0BAA0B,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,IAClD,QAAQ,CAC2B,CACvC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,yBAAyB,CAAC,MAGzC;IACC,MAAM,EAAC,QAAQ,EAAE,SAAS,EAAC,GAAG,MAAM,CAAC;IAErC,OAAO,CACL,oBAAC,0BAA0B,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,IAClD,QAAQ,CAC2B,CACvC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,31 @@
1
+ import type { Module, ModuleDeclaration } from 'ditox';
2
+ import { ReactElement, ReactNode } from 'react';
3
+ /**
4
+ * @category Component
5
+ *
6
+ * Binds the module to a new dependency container.
7
+ *
8
+ * If a parent container is exist, it is connected to the current one by default.
9
+ *
10
+ * @param params.module - Module declaration for binding
11
+ * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
12
+ *
13
+ * @example
14
+ *
15
+ * ```tsx
16
+ * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
17
+ *
18
+ * function App() {
19
+ * return (
20
+ * <DependencyModule module={LOGGER_MODULE}>
21
+ * <NestedComponent />
22
+ * </DependencyModule>
23
+ * );
24
+ * }
25
+ * ```
26
+ */
27
+ export declare function DependencyModule(params: {
28
+ children: ReactNode;
29
+ module: ModuleDeclaration<Module<Record<string, unknown>>>;
30
+ scope?: 'scoped' | 'singleton';
31
+ }): ReactElement;
@@ -0,0 +1,33 @@
1
+ import { bindModule } from 'ditox';
2
+ import React, { useCallback } from 'react';
3
+ import { DependencyContainer, } from './DependencyContainer';
4
+ /**
5
+ * @category Component
6
+ *
7
+ * Binds the module to a new dependency container.
8
+ *
9
+ * If a parent container is exist, it is connected to the current one by default.
10
+ *
11
+ * @param params.module - Module declaration for binding
12
+ * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
13
+ *
14
+ * @example
15
+ *
16
+ * ```tsx
17
+ * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
18
+ *
19
+ * function App() {
20
+ * return (
21
+ * <DependencyModule module={LOGGER_MODULE}>
22
+ * <NestedComponent />
23
+ * </DependencyModule>
24
+ * );
25
+ * }
26
+ * ```
27
+ */
28
+ export function DependencyModule(params) {
29
+ const { children, module, scope } = params;
30
+ const binder = useCallback((container) => bindModule(container, module, { scope }), [module, scope]);
31
+ return React.createElement(DependencyContainer, { binder: binder }, children);
32
+ }
33
+ //# sourceMappingURL=DependencyModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DependencyModule.js","sourceRoot":"","sources":["../../src/DependencyModule.tsx"],"names":[],"mappings":"AACA,OAAO,EAAC,UAAU,EAAC,MAAM,OAAO,CAAC;AACjC,OAAO,KAAK,EAAE,EAA0B,WAAW,EAAC,MAAM,OAAO,CAAC;AAClE,OAAO,EACL,mBAAmB,GAEpB,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAIhC;IACC,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAC,GAAG,MAAM,CAAC;IAEzC,MAAM,MAAM,GAA8B,WAAW,CACnD,CAAC,SAAS,EAAE,EAAE,CAAC,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,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { Container, Token } from 'ditox';
2
+ /**
3
+ * @category Hook
4
+ *
5
+ * Returns a dependency container. Throws an error in case the container is not provided.
6
+ */
7
+ export declare function useDependencyContainer(mode: 'strict'): Container;
8
+ /**
9
+ * @category Hook
10
+ *
11
+ * Returns a dependency container, or `undefined` in case the container is not provided.
12
+ */
13
+ export declare function useDependencyContainer(mode?: 'optional'): Container | undefined;
14
+ /**
15
+ * @category Hook
16
+ *
17
+ * Returns a dependency by token, or fails with an error.
18
+ */
19
+ export declare function useDependency<T>(token: Token<T>): T;
20
+ /**
21
+ * @category Hook
22
+ *
23
+ * Returns a dependency by token, or `undefined` in case the dependency is not provided.
24
+ */
25
+ export declare function useOptionalDependency<T>(token: Token<T>): T | undefined;