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,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 down to React children.
8
- *
9
- * If `binder` callback is specified, it will be called for a new container
10
- * to binds it with dependencies.
11
- *
12
- * If a parent container is exist, it is connected to the current one by default.
13
- * For making a new 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 setup bindings to 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 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,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 is exist, 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
@@ -1 +0,0 @@
1
- {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/hooks.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,UAAU,EAAE,OAAO,EAAC,MAAM,OAAO,CAAC;AAC1C,OAAO,EAAC,0BAA0B,EAAC,MAAM,uBAAuB,CAAC;AAgBjE;;GAEG;AACH,MAAM,UAAU,sBAAsB,CACpC,IAA4B;IAE5B,MAAM,SAAS,GAAG,UAAU,CAAC,0BAA0B,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;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAI,KAAe;IAC9C,MAAM,SAAS,GAAG,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IACnD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;IAC1E,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAI,KAAe;IACtD,MAAM,SAAS,GAAG,sBAAsB,EAAE,CAAC;IAC3C,MAAM,KAAK,GAAG,OAAO,CAAC,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"}
@@ -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/esm/index.js DELETED
@@ -1,4 +0,0 @@
1
- export { CustomDependencyContainer, DependencyContainer, } from './DependencyContainer';
2
- export { useDependencyContainer, useDependency, useOptionalDependency, } from './hooks';
3
- export { DependencyModule } from './DependencyModule';
4
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,yBAAyB,EACzB,mBAAmB,GACpB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EACL,sBAAsB,EACtB,aAAa,EACb,qBAAqB,GACtB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC"}
package/dist/umd/index.js DELETED
@@ -1,2 +0,0 @@
1
- !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("ditox"),require("react")):"function"==typeof define&&define.amd?define(["exports","ditox","react"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).DitoxReact={},e.Ditox,e.React)}(this,(function(e,n,t){"use strict";var r=t.createContext(void 0);function o(e){var o=e.children,i=e.root,u=e.binder,c=t.useContext(r),d=t.useMemo((function(){var e=n.createContainer(i?void 0:c);return null==u||u(e),e}),[u,c,i]);return t.useEffect((function(){return function(){return d.removeAll()}}),[d]),t.createElement(r.Provider,{value:d},o)}function i(e){var n=t.useContext(r);if(!n&&"strict"===e)throw new Error("Container is not provided by DependencyContainer component");return n}e.CustomDependencyContainer=function(e){var n=e.children,o=e.container;return t.createElement(r.Provider,{value:o},n)},e.DependencyContainer=o,e.DependencyModule=function(e){var r=e.children,i=e.module,u=e.scope,c=t.useCallback((function(e){return n.bindModule(e,i,{scope:u})}),[i,u]);return t.createElement(o,{binder:c},r)},e.useDependency=function(e){var n=i("strict");return t.useMemo((function(){return n.resolve(e)}),[n,e])},e.useDependencyContainer=i,e.useOptionalDependency=function(e){var n=i();return t.useMemo((function(){return null==n?void 0:n.get(e)}),[n,e])}}));
2
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}