ditox-react 3.2.0 → 3.3.1

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 (44) hide show
  1. package/dist/index.cjs +175 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/{browser/index.d.ts → index.d.cts} +43 -39
  4. package/dist/{umd/index.d.ts → index.d.ts} +43 -39
  5. package/dist/index.js +141 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.umd.js +179 -0
  8. package/dist/index.umd.js.map +1 -0
  9. package/package.json +23 -23
  10. package/src/DependencyContainer.test.tsx +32 -35
  11. package/src/DependencyContainer.tsx +5 -5
  12. package/src/DependencyModule.test.tsx +10 -9
  13. package/src/DependencyModule.tsx +6 -6
  14. package/src/hooks.test.tsx +18 -17
  15. package/src/hooks.ts +3 -3
  16. package/src/index.ts +2 -4
  17. package/dist/browser/index.js +0 -2
  18. package/dist/browser/index.js.map +0 -1
  19. package/dist/cjs/DependencyContainer.d.ts +0 -74
  20. package/dist/cjs/DependencyContainer.js +0 -101
  21. package/dist/cjs/DependencyContainer.js.map +0 -1
  22. package/dist/cjs/DependencyModule.d.ts +0 -29
  23. package/dist/cjs/DependencyModule.js +0 -58
  24. package/dist/cjs/DependencyModule.js.map +0 -1
  25. package/dist/cjs/hooks.d.ts +0 -25
  26. package/dist/cjs/hooks.js +0 -39
  27. package/dist/cjs/hooks.js.map +0 -1
  28. package/dist/cjs/index.d.ts +0 -4
  29. package/dist/cjs/index.js +0 -13
  30. package/dist/cjs/index.js.map +0 -1
  31. package/dist/esm/DependencyContainer.d.ts +0 -74
  32. package/dist/esm/DependencyContainer.js +0 -73
  33. package/dist/esm/DependencyContainer.js.map +0 -1
  34. package/dist/esm/DependencyModule.d.ts +0 -29
  35. package/dist/esm/DependencyModule.js +0 -31
  36. package/dist/esm/DependencyModule.js.map +0 -1
  37. package/dist/esm/hooks.d.ts +0 -25
  38. package/dist/esm/hooks.js +0 -33
  39. package/dist/esm/hooks.js.map +0 -1
  40. package/dist/esm/index.d.ts +0 -4
  41. package/dist/esm/index.js +0 -4
  42. package/dist/esm/index.js.map +0 -1
  43. package/dist/umd/index.js +0 -2
  44. package/dist/umd/index.js.map +0 -1
package/src/index.ts CHANGED
@@ -6,11 +6,9 @@ export {
6
6
  CustomDependencyContainer,
7
7
  DependencyContainer,
8
8
  } from './DependencyContainer';
9
-
9
+ export { DependencyModule } from './DependencyModule';
10
10
  export {
11
- useDependencyContainer,
12
11
  useDependency,
12
+ useDependencyContainer,
13
13
  useOptionalDependency,
14
14
  } from './hooks';
15
-
16
- export {DependencyModule} from './DependencyModule';
@@ -1,2 +0,0 @@
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 a(n){var t=n.children,c=n.root,a=n.binder,l=o(v),d=i((function(){var n=r(c?void 0:l);return null==a||a(n),n}),[a,l,c]);return u((function(){return function(){return d.removeAll()}}),[d]),e.createElement(v.Provider,{value:d},t)}function l(r){var n=r.children,t=r.container;return e.createElement(v.Provider,{value:t},n)}function d(r){var n=o(v);if(!n&&"strict"===r)throw new Error("Container is not provided by DependencyContainer component");return n}function f(r){var n=d("strict");return i((function(){return n.resolve(r)}),[n,r])}function m(r){var n=d();return i((function(){return null==n?void 0:n.get(r)}),[n,r])}function p(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(a,{binder:u},t)}export{l as CustomDependencyContainer,a as DependencyContainer,p as DependencyModule,f as useDependency,d as useDependencyContainer,m as useOptionalDependency};
2
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -1,74 +0,0 @@
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 type DependencyContainerBinder = (container: Container) => unknown;
8
- /**
9
- * Specifies options for a new container:
10
- * @property binder - A callback which sets up bindings for the container.
11
- * @property root - If `true` then a new container does not depend on any parent containers
12
- */
13
- export type DependencyContainerParams = {
14
- children: ReactNode;
15
- root?: boolean;
16
- binder?: DependencyContainerBinder;
17
- };
18
- /**
19
- * Provides a new dependency container to React app
20
- *
21
- * This component creates a new container and provides it to React children.
22
- *
23
- * If a `binder` callback is specified, it will be called for the new container
24
- * to bind it with dependencies.
25
- *
26
- * If a parent container exists, it is connected to the current one by default.
27
- * To create a root container specify `root` parameter as `true`,
28
- * and the container will not depend on any parent container.
29
- *
30
- * @param params.binder - A callback which sets up bindings for the container.
31
- * @param params.root - If `true` then a new container does not depend on any parent containers
32
- *
33
- * @example
34
- *
35
- * ```tsx
36
- * const TOKEN = token();
37
- *
38
- * function appDependencyBinder(container: Container) {
39
- * container.bindValue(TOKEN, 'value');
40
- * }
41
- *
42
- * function App() {
43
- * return (
44
- * <DependencyContainer root binder={appDependencyBinder}>
45
- * <NestedComponent />
46
- * </DependencyContainer>
47
- * );
48
- * }
49
- * ```
50
- *
51
- */
52
- export declare function DependencyContainer(params: DependencyContainerParams): ReactElement;
53
- /**
54
- * Provides a custom dependency container to React app
55
- *
56
- * @param params.container - a custom container
57
- *
58
- * @example
59
- * ```tsx
60
- * const container = useMemo(() => {
61
- * return createContainer();
62
- * }
63
- *
64
- * return (
65
- * <CustomDependencyContainer container={container}>
66
- * {children}
67
- * </CustomDependencyContainer>
68
- * );
69
- * ```
70
- */
71
- export declare function CustomDependencyContainer(params: {
72
- children: ReactNode;
73
- container: Container;
74
- }): ReactElement;
@@ -1,101 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.CustomDependencyContainer = exports.DependencyContainer = exports.DependencyContainerContext = void 0;
27
- const ditox_1 = require("ditox");
28
- const react_1 = __importStar(require("react"));
29
- exports.DependencyContainerContext = (0, react_1.createContext)(undefined);
30
- /**
31
- * Provides a new dependency container to React app
32
- *
33
- * This component creates a new container and provides it to React children.
34
- *
35
- * If a `binder` callback is specified, it will be called for the new container
36
- * to bind it with dependencies.
37
- *
38
- * If a parent container exists, it is connected to the current one by default.
39
- * To create a root container specify `root` parameter as `true`,
40
- * and the container will not depend on any parent container.
41
- *
42
- * @param params.binder - A callback which sets up bindings for the container.
43
- * @param params.root - If `true` then a new container does not depend on any parent containers
44
- *
45
- * @example
46
- *
47
- * ```tsx
48
- * const TOKEN = token();
49
- *
50
- * function appDependencyBinder(container: Container) {
51
- * container.bindValue(TOKEN, 'value');
52
- * }
53
- *
54
- * function App() {
55
- * return (
56
- * <DependencyContainer root binder={appDependencyBinder}>
57
- * <NestedComponent />
58
- * </DependencyContainer>
59
- * );
60
- * }
61
- * ```
62
- *
63
- */
64
- function DependencyContainer(params) {
65
- const { children, root, binder } = params;
66
- const parentContainer = (0, react_1.useContext)(exports.DependencyContainerContext);
67
- const container = (0, react_1.useMemo)(() => {
68
- const container = (0, ditox_1.createContainer)(root ? undefined : parentContainer);
69
- binder === null || binder === void 0 ? void 0 : binder(container);
70
- return container;
71
- }, [binder, parentContainer, root]);
72
- (0, react_1.useEffect)(() => {
73
- return () => container.removeAll();
74
- }, [container]);
75
- return (react_1.default.createElement(exports.DependencyContainerContext.Provider, { value: container }, children));
76
- }
77
- exports.DependencyContainer = DependencyContainer;
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
@@ -1 +0,0 @@
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;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;;;;;;;;;;;;;;;;;GAiBG;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,29 +0,0 @@
1
- import type { Module, ModuleDeclaration } from 'ditox';
2
- import { ReactElement, ReactNode } from 'react';
3
- /**
4
- * Binds the module to a new dependency container.
5
- *
6
- * If a parent container exists, it is connected to the current one by default.
7
- *
8
- * @param params.module - Module declaration for binding
9
- * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
10
- *
11
- * @example
12
- *
13
- * ```tsx
14
- * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
15
- *
16
- * function App() {
17
- * return (
18
- * <DependencyModule module={LOGGER_MODULE}>
19
- * <NestedComponent />
20
- * </DependencyModule>
21
- * );
22
- * }
23
- * ```
24
- */
25
- export declare function DependencyModule(params: {
26
- children: ReactNode;
27
- module: ModuleDeclaration<Module<Record<string, unknown>>>;
28
- scope?: 'scoped' | 'singleton';
29
- }): ReactElement;
@@ -1,58 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.DependencyModule = void 0;
27
- const ditox_1 = require("ditox");
28
- const react_1 = __importStar(require("react"));
29
- const DependencyContainer_1 = require("./DependencyContainer");
30
- /**
31
- * Binds the module to a new dependency container.
32
- *
33
- * If a parent container exists, it is connected to the current one by default.
34
- *
35
- * @param params.module - Module declaration for binding
36
- * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
37
- *
38
- * @example
39
- *
40
- * ```tsx
41
- * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
42
- *
43
- * function App() {
44
- * return (
45
- * <DependencyModule module={LOGGER_MODULE}>
46
- * <NestedComponent />
47
- * </DependencyModule>
48
- * );
49
- * }
50
- * ```
51
- */
52
- function DependencyModule(params) {
53
- const { children, module, scope } = params;
54
- const binder = (0, react_1.useCallback)((container) => (0, ditox_1.bindModule)(container, module, { scope }), [module, scope]);
55
- return react_1.default.createElement(DependencyContainer_1.DependencyContainer, { binder: binder }, children);
56
- }
57
- exports.DependencyModule = DependencyModule;
58
- //# sourceMappingURL=DependencyModule.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DependencyModule.js","sourceRoot":"","sources":["../../src/DependencyModule.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,iCAAiC;AACjC,+CAAkE;AAClE,+DAG+B;AAE/B;;;;;;;;;;;;;;;;;;;;;GAqBG;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"}
@@ -1,25 +0,0 @@
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;
package/dist/cjs/hooks.js DELETED
@@ -1,39 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,4 +0,0 @@
1
- export type { DependencyContainerBinder, DependencyContainerParams, } from './DependencyContainer';
2
- export { CustomDependencyContainer, DependencyContainer, } from './DependencyContainer';
3
- export { useDependencyContainer, useDependency, useOptionalDependency, } from './hooks';
4
- export { DependencyModule } from './DependencyModule';
package/dist/cjs/index.js DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DependencyModule = exports.useOptionalDependency = exports.useDependency = exports.useDependencyContainer = exports.DependencyContainer = exports.CustomDependencyContainer = void 0;
4
- var DependencyContainer_1 = require("./DependencyContainer");
5
- Object.defineProperty(exports, "CustomDependencyContainer", { enumerable: true, get: function () { return DependencyContainer_1.CustomDependencyContainer; } });
6
- Object.defineProperty(exports, "DependencyContainer", { enumerable: true, get: function () { return DependencyContainer_1.DependencyContainer; } });
7
- var hooks_1 = require("./hooks");
8
- Object.defineProperty(exports, "useDependencyContainer", { enumerable: true, get: function () { return hooks_1.useDependencyContainer; } });
9
- Object.defineProperty(exports, "useDependency", { enumerable: true, get: function () { return hooks_1.useDependency; } });
10
- Object.defineProperty(exports, "useOptionalDependency", { enumerable: true, get: function () { return hooks_1.useOptionalDependency; } });
11
- var DependencyModule_1 = require("./DependencyModule");
12
- Object.defineProperty(exports, "DependencyModule", { enumerable: true, get: function () { return DependencyModule_1.DependencyModule; } });
13
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAIA,6DAG+B;AAF7B,gIAAA,yBAAyB,OAAA;AACzB,0HAAA,mBAAmB,OAAA;AAGrB,iCAIiB;AAHf,+GAAA,sBAAsB,OAAA;AACtB,sGAAA,aAAa,OAAA;AACb,8GAAA,qBAAqB,OAAA;AAGvB,uDAAoD;AAA5C,oHAAA,gBAAgB,OAAA"}
@@ -1,74 +0,0 @@
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 type DependencyContainerBinder = (container: Container) => unknown;
8
- /**
9
- * Specifies options for a new container:
10
- * @property binder - A callback which sets up bindings for the container.
11
- * @property root - If `true` then a new container does not depend on any parent containers
12
- */
13
- export type DependencyContainerParams = {
14
- children: ReactNode;
15
- root?: boolean;
16
- binder?: DependencyContainerBinder;
17
- };
18
- /**
19
- * Provides a new dependency container to React app
20
- *
21
- * This component creates a new container and provides it to React children.
22
- *
23
- * If a `binder` callback is specified, it will be called for the new container
24
- * to bind it with dependencies.
25
- *
26
- * If a parent container exists, it is connected to the current one by default.
27
- * To create a root container specify `root` parameter as `true`,
28
- * and the container will not depend on any parent container.
29
- *
30
- * @param params.binder - A callback which sets up bindings for the container.
31
- * @param params.root - If `true` then a new container does not depend on any parent containers
32
- *
33
- * @example
34
- *
35
- * ```tsx
36
- * const TOKEN = token();
37
- *
38
- * function appDependencyBinder(container: Container) {
39
- * container.bindValue(TOKEN, 'value');
40
- * }
41
- *
42
- * function App() {
43
- * return (
44
- * <DependencyContainer root binder={appDependencyBinder}>
45
- * <NestedComponent />
46
- * </DependencyContainer>
47
- * );
48
- * }
49
- * ```
50
- *
51
- */
52
- export declare function DependencyContainer(params: DependencyContainerParams): ReactElement;
53
- /**
54
- * Provides a custom dependency container to React app
55
- *
56
- * @param params.container - a custom container
57
- *
58
- * @example
59
- * ```tsx
60
- * const container = useMemo(() => {
61
- * return createContainer();
62
- * }
63
- *
64
- * return (
65
- * <CustomDependencyContainer container={container}>
66
- * {children}
67
- * </CustomDependencyContainer>
68
- * );
69
- * ```
70
- */
71
- export declare function CustomDependencyContainer(params: {
72
- children: ReactNode;
73
- container: Container;
74
- }): ReactElement;
@@ -1,73 +0,0 @@
1
- import { createContainer } from 'ditox';
2
- import React, { createContext, useContext, useEffect, useMemo, } from 'react';
3
- export const DependencyContainerContext = createContext(undefined);
4
- /**
5
- * Provides a new dependency container to React app
6
- *
7
- * This component creates a new container and provides it to React children.
8
- *
9
- * If a `binder` callback is specified, it will be called for the new container
10
- * to bind it with dependencies.
11
- *
12
- * If a parent container exists, it is connected to the current one by default.
13
- * To create a root container specify `root` parameter as `true`,
14
- * and the container will not depend on any parent container.
15
- *
16
- * @param params.binder - A callback which sets up bindings for the container.
17
- * @param params.root - If `true` then a new container does not depend on any parent containers
18
- *
19
- * @example
20
- *
21
- * ```tsx
22
- * const TOKEN = token();
23
- *
24
- * function appDependencyBinder(container: Container) {
25
- * container.bindValue(TOKEN, 'value');
26
- * }
27
- *
28
- * function App() {
29
- * return (
30
- * <DependencyContainer root binder={appDependencyBinder}>
31
- * <NestedComponent />
32
- * </DependencyContainer>
33
- * );
34
- * }
35
- * ```
36
- *
37
- */
38
- export function DependencyContainer(params) {
39
- const { children, root, binder } = params;
40
- const parentContainer = useContext(DependencyContainerContext);
41
- const container = useMemo(() => {
42
- const container = createContainer(root ? undefined : parentContainer);
43
- binder === null || binder === void 0 ? void 0 : binder(container);
44
- return container;
45
- }, [binder, parentContainer, root]);
46
- useEffect(() => {
47
- return () => container.removeAll();
48
- }, [container]);
49
- return (React.createElement(DependencyContainerContext.Provider, { value: container }, children));
50
- }
51
- /**
52
- * Provides a custom dependency container to React app
53
- *
54
- * @param params.container - a custom container
55
- *
56
- * @example
57
- * ```tsx
58
- * const container = useMemo(() => {
59
- * return createContainer();
60
- * }
61
- *
62
- * return (
63
- * <CustomDependencyContainer container={container}>
64
- * {children}
65
- * </CustomDependencyContainer>
66
- * );
67
- * ```
68
- */
69
- export function CustomDependencyContainer(params) {
70
- const { children, container } = params;
71
- return (React.createElement(DependencyContainerContext.Provider, { value: container }, children));
72
- }
73
- //# sourceMappingURL=DependencyContainer.js.map
@@ -1 +0,0 @@
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;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;;;;;;;;;;;;;;;;;GAiBG;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"}
@@ -1,29 +0,0 @@
1
- import type { Module, ModuleDeclaration } from 'ditox';
2
- import { ReactElement, ReactNode } from 'react';
3
- /**
4
- * Binds the module to a new dependency container.
5
- *
6
- * If a parent container exists, it is connected to the current one by default.
7
- *
8
- * @param params.module - Module declaration for binding
9
- * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
10
- *
11
- * @example
12
- *
13
- * ```tsx
14
- * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
15
- *
16
- * function App() {
17
- * return (
18
- * <DependencyModule module={LOGGER_MODULE}>
19
- * <NestedComponent />
20
- * </DependencyModule>
21
- * );
22
- * }
23
- * ```
24
- */
25
- export declare function DependencyModule(params: {
26
- children: ReactNode;
27
- module: ModuleDeclaration<Module<Record<string, unknown>>>;
28
- scope?: 'scoped' | 'singleton';
29
- }): ReactElement;
@@ -1,31 +0,0 @@
1
- import { bindModule } from 'ditox';
2
- import React, { useCallback } from 'react';
3
- import { DependencyContainer, } from './DependencyContainer';
4
- /**
5
- * Binds the module to a new dependency container.
6
- *
7
- * If a parent container exists, it is connected to the current one by default.
8
- *
9
- * @param params.module - Module declaration for binding
10
- * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
11
- *
12
- * @example
13
- *
14
- * ```tsx
15
- * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
16
- *
17
- * function App() {
18
- * return (
19
- * <DependencyModule module={LOGGER_MODULE}>
20
- * <NestedComponent />
21
- * </DependencyModule>
22
- * );
23
- * }
24
- * ```
25
- */
26
- export function DependencyModule(params) {
27
- const { children, module, scope } = params;
28
- const binder = useCallback((container) => bindModule(container, module, { scope }), [module, scope]);
29
- return React.createElement(DependencyContainer, { binder: binder }, children);
30
- }
31
- //# sourceMappingURL=DependencyModule.js.map
@@ -1 +0,0 @@
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;;;;;;;;;;;;;;;;;;;;;GAqBG;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"}
@@ -1,25 +0,0 @@
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;
package/dist/esm/hooks.js DELETED
@@ -1,33 +0,0 @@
1
- import { useContext, useMemo } from 'react';
2
- import { DependencyContainerContext } from './DependencyContainer';
3
- /**
4
- * @internal
5
- */
6
- export function useDependencyContainer(mode) {
7
- const container = useContext(DependencyContainerContext);
8
- if (!container && mode === 'strict') {
9
- throw new Error('Container is not provided by DependencyContainer component');
10
- }
11
- return container;
12
- }
13
- /**
14
- * @category Hook
15
- *
16
- * Returns a dependency by token, or fails with an error.
17
- */
18
- export function useDependency(token) {
19
- const container = useDependencyContainer('strict');
20
- const value = useMemo(() => container.resolve(token), [container, token]);
21
- return value;
22
- }
23
- /**
24
- * @category Hook
25
- *
26
- * Returns a dependency by token, or `undefined` in case the dependency is not provided.
27
- */
28
- export function useOptionalDependency(token) {
29
- const container = useDependencyContainer();
30
- const value = useMemo(() => container === null || container === void 0 ? void 0 : container.get(token), [container, token]);
31
- return value;
32
- }
33
- //# sourceMappingURL=hooks.js.map