ditox-react 3.1.0 → 3.3.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 (45) hide show
  1. package/README.md +94 -26
  2. package/dist/index.cjs +175 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/{browser/index.d.ts → index.d.cts} +51 -47
  5. package/dist/{umd/index.d.ts → index.d.ts} +51 -47
  6. package/dist/index.js +141 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/index.umd.js +179 -0
  9. package/dist/index.umd.js.map +1 -0
  10. package/package.json +23 -22
  11. package/src/DependencyContainer.test.tsx +32 -35
  12. package/src/DependencyContainer.tsx +13 -13
  13. package/src/DependencyModule.test.tsx +10 -9
  14. package/src/DependencyModule.tsx +7 -7
  15. package/src/hooks.test.tsx +18 -17
  16. package/src/hooks.ts +3 -3
  17. package/src/index.ts +2 -4
  18. package/dist/browser/index.js +0 -2
  19. package/dist/browser/index.js.map +0 -1
  20. package/dist/cjs/DependencyContainer.d.ts +0 -74
  21. package/dist/cjs/DependencyContainer.js +0 -101
  22. package/dist/cjs/DependencyContainer.js.map +0 -1
  23. package/dist/cjs/DependencyModule.d.ts +0 -29
  24. package/dist/cjs/DependencyModule.js +0 -58
  25. package/dist/cjs/DependencyModule.js.map +0 -1
  26. package/dist/cjs/hooks.d.ts +0 -25
  27. package/dist/cjs/hooks.js +0 -39
  28. package/dist/cjs/hooks.js.map +0 -1
  29. package/dist/cjs/index.d.ts +0 -4
  30. package/dist/cjs/index.js +0 -13
  31. package/dist/cjs/index.js.map +0 -1
  32. package/dist/esm/DependencyContainer.d.ts +0 -74
  33. package/dist/esm/DependencyContainer.js +0 -73
  34. package/dist/esm/DependencyContainer.js.map +0 -1
  35. package/dist/esm/DependencyModule.d.ts +0 -29
  36. package/dist/esm/DependencyModule.js +0 -31
  37. package/dist/esm/DependencyModule.js.map +0 -1
  38. package/dist/esm/hooks.d.ts +0 -25
  39. package/dist/esm/hooks.js +0 -33
  40. package/dist/esm/hooks.js.map +0 -1
  41. package/dist/esm/index.d.ts +0 -4
  42. package/dist/esm/index.js +0 -4
  43. package/dist/esm/index.js.map +0 -1
  44. package/dist/umd/index.js +0 -2
  45. package/dist/umd/index.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import {renderHook} from '@testing-library/react';
1
+ import { renderHook } from '@testing-library/react';
2
2
  import {
3
3
  bindModule,
4
4
  createContainer,
@@ -7,12 +7,13 @@ import {
7
7
  token,
8
8
  } from 'ditox';
9
9
  import React from 'react';
10
- import {CustomDependencyContainer} from './DependencyContainer';
11
- import {DependencyModule} from './DependencyModule';
12
- import {useDependency} from './hooks';
10
+ import { describe, expect, it } from 'vitest';
11
+ import { CustomDependencyContainer } from './DependencyContainer';
12
+ import { DependencyModule } from './DependencyModule';
13
+ import { useDependency } from './hooks';
13
14
 
14
15
  describe('DependencyModule', () => {
15
- type TestModule = Module<{value: number}>;
16
+ type TestModule = Module<{ value: number }>;
16
17
 
17
18
  const MODULE_TOKEN = token<TestModule>();
18
19
  const VALUE_TOKEN = token<number>();
@@ -28,8 +29,8 @@ describe('DependencyModule', () => {
28
29
  };
29
30
 
30
31
  it('should bind a module and its provided values', () => {
31
- const {result} = renderHook(() => useDependency(VALUE_TOKEN), {
32
- wrapper: ({children}) => (
32
+ const { result } = renderHook(() => useDependency(VALUE_TOKEN), {
33
+ wrapper: ({ children }) => (
33
34
  <DependencyModule module={MODULE}>{children}</DependencyModule>
34
35
  ),
35
36
  });
@@ -42,8 +43,8 @@ describe('DependencyModule', () => {
42
43
  bindModule(parent, MODULE);
43
44
  const parentModule = parent.resolve(MODULE_TOKEN);
44
45
 
45
- const {result} = renderHook(() => useDependency(MODULE_TOKEN), {
46
- wrapper: ({children}) => (
46
+ const { result } = renderHook(() => useDependency(MODULE_TOKEN), {
47
+ wrapper: ({ children }) => (
47
48
  <CustomDependencyContainer container={parent}>
48
49
  <DependencyModule module={MODULE} scope="scoped">
49
50
  {children}
@@ -1,6 +1,6 @@
1
- import type {Module, ModuleDeclaration} from 'ditox';
2
- import {bindModule} from 'ditox';
3
- import React, {ReactElement, ReactNode, useCallback} from 'react';
1
+ import type { Module, ModuleDeclaration } from 'ditox';
2
+ import { bindModule } from 'ditox';
3
+ import React, { ReactElement, ReactNode, useCallback } from 'react';
4
4
  import {
5
5
  DependencyContainer,
6
6
  DependencyContainerBinder,
@@ -9,7 +9,7 @@ import {
9
9
  /**
10
10
  * Binds the module to a new dependency container.
11
11
  *
12
- * If a parent container is exist, it is connected to the current one by default.
12
+ * If a parent container exists, it is connected to the current one by default.
13
13
  *
14
14
  * @param params.module - Module declaration for binding
15
15
  * @param params.scope - Optional scope for binding: `singleton` (default) or `scoped`.
@@ -17,7 +17,7 @@ import {
17
17
  * @example
18
18
  *
19
19
  * ```tsx
20
- * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
20
+ * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {};
21
21
  *
22
22
  * function App() {
23
23
  * return (
@@ -33,10 +33,10 @@ export function DependencyModule(params: {
33
33
  module: ModuleDeclaration<Module<Record<string, unknown>>>;
34
34
  scope?: 'scoped' | 'singleton';
35
35
  }): ReactElement {
36
- const {children, module, scope} = params;
36
+ const { children, module, scope } = params;
37
37
 
38
38
  const binder: DependencyContainerBinder = useCallback(
39
- (container) => bindModule(container, module, {scope}),
39
+ (container) => bindModule(container, module, { scope }),
40
40
  [module, scope],
41
41
  );
42
42
 
@@ -1,7 +1,8 @@
1
- import {renderHook} from '@testing-library/react';
2
- import {createContainer, token} from 'ditox';
1
+ import { renderHook } from '@testing-library/react';
2
+ import { createContainer, token } from 'ditox';
3
3
  import React from 'react';
4
- import {CustomDependencyContainer} from './DependencyContainer';
4
+ import { beforeAll, describe, expect, it, vi } from 'vitest';
5
+ import { CustomDependencyContainer } from './DependencyContainer';
5
6
  import {
6
7
  useDependency,
7
8
  useDependencyContainer,
@@ -9,15 +10,15 @@ import {
9
10
  } from './hooks';
10
11
 
11
12
  beforeAll(() => {
12
- jest.spyOn(console, 'error').mockImplementation();
13
+ vi.spyOn(console, 'error').mockImplementation(() => {});
13
14
  });
14
15
 
15
16
  describe('useDependencyContainer()', () => {
16
17
  it('should return a provided container in "strict" mode', () => {
17
18
  const container = createContainer();
18
19
 
19
- const {result} = renderHook(() => useDependencyContainer('strict'), {
20
- wrapper: ({children}) => (
20
+ const { result } = renderHook(() => useDependencyContainer('strict'), {
21
+ wrapper: ({ children }) => (
21
22
  <CustomDependencyContainer container={container}>
22
23
  {children}
23
24
  </CustomDependencyContainer>
@@ -36,8 +37,8 @@ describe('useDependencyContainer()', () => {
36
37
  it('should return a provided container in "optional" mode', () => {
37
38
  const container = createContainer();
38
39
 
39
- const {result} = renderHook(() => useDependencyContainer(), {
40
- wrapper: ({children}) => (
40
+ const { result } = renderHook(() => useDependencyContainer(), {
41
+ wrapper: ({ children }) => (
41
42
  <CustomDependencyContainer container={container}>
42
43
  {children}
43
44
  </CustomDependencyContainer>
@@ -48,7 +49,7 @@ describe('useDependencyContainer()', () => {
48
49
  });
49
50
 
50
51
  it('should return "undefined" in "optional" mode in case a container is not provided', () => {
51
- const {result} = renderHook(() => useDependencyContainer());
52
+ const { result } = renderHook(() => useDependencyContainer());
52
53
  expect(result.current).toBeUndefined();
53
54
  });
54
55
  });
@@ -59,8 +60,8 @@ describe('useDependency()', () => {
59
60
  const container = createContainer();
60
61
  container.bindValue(TOKEN, 'value');
61
62
 
62
- const {result} = renderHook(() => useDependency(TOKEN), {
63
- wrapper: ({children}) => (
63
+ const { result } = renderHook(() => useDependency(TOKEN), {
64
+ wrapper: ({ children }) => (
64
65
  <CustomDependencyContainer container={container}>
65
66
  {children}
66
67
  </CustomDependencyContainer>
@@ -84,7 +85,7 @@ describe('useDependency()', () => {
84
85
 
85
86
  expect(() => {
86
87
  renderHook(() => useDependency(TOKEN), {
87
- wrapper: ({children}) => (
88
+ wrapper: ({ children }) => (
88
89
  <CustomDependencyContainer container={container}>
89
90
  {children}
90
91
  </CustomDependencyContainer>
@@ -100,8 +101,8 @@ describe('useOptionalDependency()', () => {
100
101
  const container = createContainer();
101
102
  container.bindValue(TOKEN, 'value');
102
103
 
103
- const {result} = renderHook(() => useOptionalDependency(TOKEN), {
104
- wrapper: ({children}) => (
104
+ const { result } = renderHook(() => useOptionalDependency(TOKEN), {
105
+ wrapper: ({ children }) => (
105
106
  <CustomDependencyContainer container={container}>
106
107
  {children}
107
108
  </CustomDependencyContainer>
@@ -114,7 +115,7 @@ describe('useOptionalDependency()', () => {
114
115
  it('should return "undefined" in case a container is not provided', () => {
115
116
  const TOKEN = token('token');
116
117
 
117
- const {result} = renderHook(() => useOptionalDependency(TOKEN));
118
+ const { result } = renderHook(() => useOptionalDependency(TOKEN));
118
119
 
119
120
  expect(result.current).toBeUndefined();
120
121
  });
@@ -123,8 +124,8 @@ describe('useOptionalDependency()', () => {
123
124
  const TOKEN = token('token');
124
125
  const container = createContainer();
125
126
 
126
- const {result} = renderHook(() => useOptionalDependency(TOKEN), {
127
- wrapper: ({children}) => (
127
+ const { result } = renderHook(() => useOptionalDependency(TOKEN), {
128
+ wrapper: ({ children }) => (
128
129
  <CustomDependencyContainer container={container}>
129
130
  {children}
130
131
  </CustomDependencyContainer>
package/src/hooks.ts CHANGED
@@ -1,6 +1,6 @@
1
- import {Container, Token} from 'ditox';
2
- import {useContext, useMemo} from 'react';
3
- import {DependencyContainerContext} from './DependencyContainer';
1
+ import { Container, Token } from 'ditox';
2
+ import { useContext, useMemo } from 'react';
3
+ import { DependencyContainerContext } from './DependencyContainer';
4
4
 
5
5
  /**
6
6
  * @category Hook
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 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 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 down to React children.
22
- *
23
- * If `binder` callback is specified, it will be called for a new container
24
- * to binds it with dependencies.
25
- *
26
- * If a parent container is exist, it is connected to the current one by default.
27
- * For making a new 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 setup bindings to 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 down to React children.
34
- *
35
- * If `binder` callback is specified, it will be called for a new container
36
- * to binds it with dependencies.
37
- *
38
- * If a parent container is exist, it is connected to the current one by default.
39
- * For making a new 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 setup bindings to 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 is exist, 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 is exist, 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 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 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 down to React children.
22
- *
23
- * If `binder` callback is specified, it will be called for a new container
24
- * to binds it with dependencies.
25
- *
26
- * If a parent container is exist, it is connected to the current one by default.
27
- * For making a new 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 setup bindings to 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;