@mic-rexjs/usecases-react 1.3.36 → 1.4.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 (97) hide show
  1. package/README.md +2 -2
  2. package/dist/index.min.js +1 -1
  3. package/es/components/UpdateFragment/index.d.ts +2 -0
  4. package/es/components/UpdateFragment/index.js +8 -0
  5. package/es/components/UpdateFragment/types.d.ts +3 -0
  6. package/es/components/UseCaseFragment/index.d.ts +1 -1
  7. package/es/components/UseCaseFragment/types.d.ts +3 -3
  8. package/es/configs/defaultUseCaseContext/index.d.ts +3 -1
  9. package/es/configs/defaultUseCaseContext/index.js +1 -1
  10. package/es/configs/defaultUseCaseContext/types.d.ts +11 -0
  11. package/es/configs/defaultUseCaseContext/types.js +1 -0
  12. package/es/configs/usecaseContextMap/index.d.ts +1 -3
  13. package/es/configs/usecaseContextMap/types.d.ts +4 -17
  14. package/es/hooks/useCompareDeps/index.d.ts +1 -0
  15. package/es/hooks/useCompareDeps/index.js +24 -0
  16. package/es/hooks/useConstant/index.d.ts +2 -0
  17. package/es/hooks/useConstant/index.js +5 -0
  18. package/es/hooks/useConstant/types.d.ts +3 -0
  19. package/es/hooks/useConstant/types.js +1 -0
  20. package/es/hooks/useConstantFn/index.d.ts +1 -0
  21. package/es/hooks/useConstantFn/index.js +7 -0
  22. package/es/hooks/useOptionsRefCollection/index.d.ts +3 -0
  23. package/es/hooks/useOptionsRefCollection/index.js +22 -0
  24. package/es/hooks/useOptionsRefCollection/types.d.ts +2 -0
  25. package/es/hooks/useOptionsRefCollection/types.js +1 -0
  26. package/es/hooks/useProvider/index.d.ts +7 -0
  27. package/es/hooks/useProvider/index.js +17 -0
  28. package/es/hooks/useProvider/types.d.ts +3 -0
  29. package/es/hooks/useProvider/types.js +1 -0
  30. package/es/hooks/useScope/index.d.ts +2 -0
  31. package/es/hooks/useScope/index.js +3 -0
  32. package/es/hooks/useScope/types.d.ts +3 -0
  33. package/es/hooks/useScope/types.js +1 -0
  34. package/es/hooks/useUseCase/index.js +68 -89
  35. package/es/hooks/useUseCase/index.test.js +638 -156
  36. package/es/hooks/useUseCase/types.d.ts +27 -13
  37. package/es/hooks/useUseCaseContext/index.d.ts +3 -0
  38. package/es/hooks/useUseCaseContext/index.js +17 -0
  39. package/es/index.d.ts +3 -2
  40. package/es/methods/compareProperty/index.d.ts +2 -0
  41. package/es/methods/compareProperty/index.js +34 -0
  42. package/es/methods/compareProperty/types.d.ts +3 -0
  43. package/es/methods/compareProperty/types.js +1 -0
  44. package/es/methods/getRootEntity/index.d.ts +2 -0
  45. package/es/methods/getRootEntity/index.js +9 -0
  46. package/es/methods/hasField/index.d.ts +1 -0
  47. package/es/methods/hasField/index.js +6 -0
  48. package/es/methods/initEntityReducers/index.d.ts +5 -0
  49. package/es/methods/initEntityReducers/index.js +13 -0
  50. package/es/methods/initEntityReducers/types.d.ts +3 -0
  51. package/es/methods/initEntityReducers/types.js +1 -0
  52. package/es/methods/removeItem/index.d.ts +1 -0
  53. package/es/methods/removeItem/index.js +7 -0
  54. package/es/methods/triggerCallbacks/index.d.ts +3 -0
  55. package/es/methods/triggerCallbacks/index.js +14 -0
  56. package/es/methods/triggerWatchers/index.d.ts +2 -0
  57. package/es/methods/triggerWatchers/index.js +14 -0
  58. package/lib/components/UpdateFragment/index.js +16 -0
  59. package/lib/configs/defaultUseCaseContext/index.js +1 -1
  60. package/lib/configs/defaultUseCaseContext/types.js +5 -0
  61. package/lib/hooks/useCompareDeps/index.js +31 -0
  62. package/lib/hooks/useConstant/index.js +16 -0
  63. package/lib/hooks/useConstant/types.js +5 -0
  64. package/lib/hooks/useConstantFn/index.js +18 -0
  65. package/lib/hooks/useOptionsRefCollection/index.js +29 -0
  66. package/lib/hooks/useOptionsRefCollection/types.js +5 -0
  67. package/lib/hooks/useProvider/index.js +30 -0
  68. package/lib/hooks/useProvider/types.js +5 -0
  69. package/lib/hooks/useScope/index.js +10 -0
  70. package/lib/hooks/useScope/types.js +5 -0
  71. package/lib/hooks/useUseCase/index.js +73 -115
  72. package/lib/hooks/useUseCase/index.test.js +841 -240
  73. package/lib/hooks/useUseCaseContext/index.js +24 -0
  74. package/lib/methods/compareProperty/index.js +50 -0
  75. package/lib/methods/compareProperty/types.js +5 -0
  76. package/lib/methods/getRootEntity/index.js +16 -0
  77. package/lib/methods/hasField/index.js +15 -0
  78. package/lib/methods/initEntityReducers/index.js +29 -0
  79. package/lib/methods/initEntityReducers/types.js +5 -0
  80. package/lib/methods/removeItem/index.js +14 -0
  81. package/lib/methods/triggerCallbacks/index.js +22 -0
  82. package/lib/{hooks/useUseCase/methods → methods}/triggerWatchers/index.js +16 -9
  83. package/package.json +2 -2
  84. package/es/core/usecases/optionsGetterCellectionUseCase/index.d.ts +0 -2
  85. package/es/core/usecases/optionsGetterCellectionUseCase/index.js +0 -13
  86. package/es/core/usecases/optionsGetterCellectionUseCase/types.d.ts +0 -8
  87. package/es/hooks/useOptionsTransfer/index.d.ts +0 -3
  88. package/es/hooks/useOptionsTransfer/index.js +0 -15
  89. package/es/hooks/useUseCase/methods/triggerCallbacks/index.d.ts +0 -3
  90. package/es/hooks/useUseCase/methods/triggerCallbacks/index.js +0 -11
  91. package/es/hooks/useUseCase/methods/triggerWatchers/index.d.ts +0 -2
  92. package/es/hooks/useUseCase/methods/triggerWatchers/index.js +0 -10
  93. package/lib/core/usecases/optionsGetterCellectionUseCase/index.js +0 -23
  94. package/lib/hooks/useOptionsTransfer/index.js +0 -23
  95. package/lib/hooks/useUseCase/methods/triggerCallbacks/index.js +0 -32
  96. /package/es/{core/usecases/optionsGetterCellectionUseCase → components/UpdateFragment}/types.js +0 -0
  97. /package/lib/{core/usecases/optionsGetterCellectionUseCase → components/UpdateFragment}/types.js +0 -0
@@ -1,25 +1,39 @@
1
+ import { ContextualEntityReducers } from '../../configs/defaultUseCaseContext/types';
1
2
  import { EntityReducers, EntityUseCase, Reducers, UseCase } from '@mic-rexjs/usecases';
2
- import { UseCaseContext, UseCaseContextWithProvider } from '../../configs/usecaseContextMap/types';
3
+ import { UseCaseProvider } from '../useProvider/types';
4
+ export type CoreCollection<T, TEntityReducers extends EntityReducers<T>, TProvide extends UseCaseProvider | null = UseCaseProvider> = [entity: T, reducers: ContextualEntityReducers<T, TEntityReducers>, Provider: TProvide];
3
5
  export interface EntityGetter<T> {
4
6
  (): T;
5
7
  }
6
- export interface EntityWatcher<T> {
7
- (newEntity: T, prevEntity: T): void;
8
+ export interface EntityWatchEvent<T, TValue = unknown> {
9
+ fieldPaths: string[];
10
+ newEntity: T;
11
+ newValue: TValue;
12
+ oldEntity: T;
13
+ oldValue: TValue;
8
14
  }
9
- export type EntityWatchMap<T> = T extends unknown[] ? Record<number, EntityWatcher<T>> : T extends object ? {
10
- [K in keyof T]?: EntityWatcher<T>;
11
- } : object;
15
+ export interface EntityWatcher<T, TValue = unknown> {
16
+ (event: EntityWatchEvent<T, TValue>): void;
17
+ }
18
+ export type PropertyPath<T, K extends keyof T = keyof T & string> = K extends infer TKey ? `${TKey & (string | number)}${'' | DotAccessorFieldPath<T[TKey & keyof T]>}` : never;
19
+ export type DotAccessorFieldPath<T> = T extends unknown[] ? '.length' | `${'' | `.${number}`}${DotAccessorFieldPath<T[number]>}` : T extends object ? `.${PropertyPath<T>}` : '';
20
+ export type FieldPath<T> = T extends unknown[] ? FieldPath<T[number]> | 'length' : T extends object ? PropertyPath<T> : never;
21
+ export type ExtractPropertyType<T, TPath> = TPath extends `${infer TKey}.${infer TSubPath}` ? T extends unknown[] ? ExtractPropertyType<T[number], TKey extends `${number}` ? TSubPath : TPath> : ExtractPropertyType<T[TKey & keyof T], TSubPath> : T extends unknown[] ? TPath extends 'length' ? T[TPath] : TPath extends `${number}` ? T[number] : ExtractPropertyType<T[number], TPath> : T[TPath & keyof T];
22
+ export type ExtractFieldType<T, TPath> = TPath extends `${infer TKey}.${infer TSubProperty}` ? ExtractPropertyType<T[TKey & keyof T], TSubProperty> : T[TPath & keyof T];
23
+ export type EntityWatchMap<T> = {
24
+ [K in FieldPath<T>]?: EntityWatcher<T, ExtractFieldType<T, K>>;
25
+ };
12
26
  export interface UseCaseHookContextualOptions<T> {
13
27
  watch?: EntityWatchMap<T>;
14
- onChange?(newEntity: T, prevEntity: T): void;
28
+ onChange?(newEntity: T, oldEntity: T): void;
15
29
  }
16
- export interface UseCaseHookOwnOptions<T, TOptions extends object> extends UseCaseHookContextualOptions<T> {
30
+ export interface UseCaseHookOwnOptions<T, TUseCaseOptions extends object> extends UseCaseHookContextualOptions<T> {
17
31
  stateless?: boolean;
18
- options?: TOptions;
32
+ options?: TUseCaseOptions;
19
33
  }
20
- export type UseCaseHookOptions<T, TOptions extends object> = TOptions & UseCaseHookOwnOptions<T, TOptions>;
34
+ export type UseCaseHookOptions<T, TUseCaseOptions extends object = object> = TUseCaseOptions & UseCaseHookOwnOptions<T, TUseCaseOptions>;
21
35
  export interface UseCaseHook {
22
- <T, TReducers extends EntityReducers<T>, TOptions extends UseCaseHookContextualOptions<T> = UseCaseHookContextualOptions<T>>(usecase: EntityUseCase<T, TReducers & EntityReducers<T>>, options?: TOptions): UseCaseContext<T, TReducers>;
23
- <T, TReducers extends EntityReducers<T>, TUseCaseOptions extends object = object, TOptions extends UseCaseHookOptions<T, TUseCaseOptions> = UseCaseHookOptions<T, TUseCaseOptions>>(defaultEntity: T | EntityGetter<T>, usecase: EntityUseCase<T, TReducers, TUseCaseOptions> & UseCase<TReducers, TUseCaseOptions>, options?: TOptions, deps?: unknown[]): UseCaseContextWithProvider<T, TReducers>;
24
- <T extends Reducers, TOptions extends object = object>(usecase: UseCase<T, TOptions>, options?: TOptions, deps?: unknown[]): T;
36
+ <T, TEntityReducers extends EntityReducers<T>>(usecase: EntityUseCase<T, TEntityReducers & EntityReducers<T>> & UseCase<TEntityReducers>, options?: UseCaseHookContextualOptions<T>): CoreCollection<T, TEntityReducers, null>;
37
+ <T, TEntityReducers extends EntityReducers<T>, TUseCaseOptions extends object = object>(initailEntity: T | EntityGetter<T>, usecase: EntityUseCase<T, TEntityReducers, TUseCaseOptions> & UseCase<TEntityReducers, TUseCaseOptions>, options?: UseCaseHookOptions<T, TUseCaseOptions>, deps?: unknown[]): CoreCollection<T, TEntityReducers>;
38
+ <T extends Reducers, TUseCaseOptions extends object = object>(usecase: UseCase<T, TUseCaseOptions>, options?: TUseCaseOptions, deps?: unknown[]): T;
25
39
  }
@@ -0,0 +1,3 @@
1
+ import { EntityReducers, EntityUseCase } from '@mic-rexjs/usecases';
2
+ import { UseCaseMappingContext } from '../../configs/usecaseContextMap/types';
3
+ export declare const useUseCaseContext: <T, TEntityReducers extends EntityReducers<T>, TUseCaseOptions extends object, TContext extends UseCaseMappingContext<T, TEntityReducers, TUseCaseOptions> = UseCaseMappingContext<T, TEntityReducers, TUseCaseOptions>>(usecase: EntityUseCase<T, TEntityReducers, TUseCaseOptions>, defaultContext?: TContext | null | undefined) => TContext;
@@ -0,0 +1,17 @@
1
+ import { useConstant } from '../useConstant';
2
+ import { usecaseContextMap } from '../../configs/usecaseContextMap';
3
+ import { createContext } from 'react';
4
+ export const useUseCaseContext = (usecase, defaultContext) => {
5
+ return useConstant(() => {
6
+ const map = usecaseContextMap;
7
+ if (map.has(usecase)) {
8
+ return map.get(usecase);
9
+ }
10
+ if (defaultContext) {
11
+ return defaultContext;
12
+ }
13
+ const ctx = createContext({});
14
+ map.set(usecase, ctx);
15
+ return ctx;
16
+ });
17
+ };
package/es/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { UseCaseFragment } from './components/UseCaseFragment';
2
2
  export { useUseCase } from './hooks/useUseCase';
3
- export type { UseCaseFragmentProps } from './components/UseCaseFragment/types';
4
- export type { UseCaseContext, UseCaseContextWithProvider, UseCaseProvider } from './configs/usecaseContextMap/types';
3
+ export type { CoreCollection } from './hooks/useUseCase/types';
5
4
  export type { UseCaseHookOptions } from './hooks/useUseCase/types';
5
+ export type { UseCaseProvider } from './hooks/useProvider/types';
6
+ export type { UseCaseFragmentProps } from './components/UseCaseFragment/types';
@@ -0,0 +1,2 @@
1
+ import { ComparePropertyCallback } from './types';
2
+ export declare const compareProperty: <T, S>(from: T, to: S, fieldPath: string, callback: ComparePropertyCallback, fieldPathStack?: string[]) => void;
@@ -0,0 +1,34 @@
1
+ import { hasField } from '../hasField';
2
+ export const compareProperty = (from, to, fieldPath, callback, fieldPathStack = []) => {
3
+ const undef = void 0;
4
+ const [, key = fieldPath, subFieldPath = ''] = fieldPath.match(/^([^.]+)\.(.+)$/) || [];
5
+ const hasFromField = hasField(from, key);
6
+ const hasToField = hasField(to, key);
7
+ const currentFieldPathStack = [...fieldPathStack, key];
8
+ if (hasFromField || hasToField) {
9
+ const fromValue = hasFromField ? from[key] : undef;
10
+ const toValue = hasToField ? to[key] : undef;
11
+ if (subFieldPath) {
12
+ compareProperty(fromValue, toValue, subFieldPath, callback, currentFieldPathStack);
13
+ return;
14
+ }
15
+ if (fromValue === toValue) {
16
+ return;
17
+ }
18
+ callback(fromValue, toValue, currentFieldPathStack);
19
+ return;
20
+ }
21
+ if (/^\d+^/.test(key)) {
22
+ return;
23
+ }
24
+ const fromArray = Array.isArray(from) ? from : [];
25
+ const toArray = Array.isArray(to) ? to : [];
26
+ const { length: fromLength } = fromArray;
27
+ const { length: toLength } = toArray;
28
+ const length = Math.max(fromLength, toLength);
29
+ for (let i = 0; i < length; i++) {
30
+ const fromItem = fromArray[i];
31
+ const toItem = toArray[i];
32
+ compareProperty(fromItem, toItem, fieldPath, callback, [...fieldPathStack, `${i}`]);
33
+ }
34
+ };
@@ -0,0 +1,3 @@
1
+ export interface ComparePropertyCallback {
2
+ <T, S>(fromValue: T, toValue: S, fieldPaths: string[]): void;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { EntityGetter } from '../../hooks/useUseCase/types';
2
+ export declare const getRootEntity: <T>(entityState: T, entityArg: T | EntityGetter<T>, stateless?: boolean) => T;
@@ -0,0 +1,9 @@
1
+ export const getRootEntity = (entityState, entityArg, stateless) => {
2
+ if (!stateless) {
3
+ return entityState;
4
+ }
5
+ if (typeof entityArg === 'function') {
6
+ return entityArg();
7
+ }
8
+ return entityArg;
9
+ };
@@ -0,0 +1 @@
1
+ export declare const hasField: <T>(data: T, field: string) => boolean;
@@ -0,0 +1,6 @@
1
+ export const hasField = (data, field) => {
2
+ if (typeof data !== 'object' || data === null) {
3
+ return false;
4
+ }
5
+ return Object.hasOwn(data, field);
6
+ };
@@ -0,0 +1,5 @@
1
+ import { EntityReducers, EntityUseCase } from '@mic-rexjs/usecases';
2
+ import { UseCaseHookOptions } from '../../hooks/useUseCase/types';
3
+ import { ContextualEntityReducers } from '../../configs/defaultUseCaseContext/types';
4
+ import { EntityChangeEventHandler } from './types';
5
+ export declare const initEntityReducers: <T, TEntityReducers extends EntityReducers<T>, TUseCaseOptions extends object = object>(entity: T, usecase: EntityUseCase<T, TEntityReducers, TUseCaseOptions>, options: UseCaseHookOptions<T, TUseCaseOptions>, onEntityChange: EntityChangeEventHandler<T>) => ContextualEntityReducers<T, TEntityReducers>;
@@ -0,0 +1,13 @@
1
+ import { entityReducerUseCase } from '@mic-rexjs/usecases';
2
+ export const initEntityReducers = (entity, usecase, options, onEntityChange) => {
3
+ const { createEntityReducers } = entityReducerUseCase();
4
+ const { stateless, watch, onChange, options: usecaseOptions, ...restUseCaseOptions } = options;
5
+ return createEntityReducers(entity, usecase, {
6
+ ...restUseCaseOptions,
7
+ ...usecaseOptions,
8
+ onChange: onEntityChange,
9
+ onGenerate(newEntity, result) {
10
+ return result;
11
+ },
12
+ });
13
+ };
@@ -0,0 +1,3 @@
1
+ export interface EntityChangeEventHandler<T> {
2
+ (newEntity: T, oldEntity: T): void;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const removeItem: <T>(list: T[], item: T) => void;
@@ -0,0 +1,7 @@
1
+ export const removeItem = (list, item) => {
2
+ const index = list.indexOf(item);
3
+ if (index === -1) {
4
+ return;
5
+ }
6
+ list.splice(index, 1);
7
+ };
@@ -0,0 +1,3 @@
1
+ import { OptionsRefCollection } from '../../hooks/useOptionsRefCollection/types';
2
+ import { UseCaseHookOptions } from '../../hooks/useUseCase/types';
3
+ export declare const triggerCallbacks: <T, TUseCaseOptions extends object, TOptions extends UseCaseHookOptions<T, TUseCaseOptions> = UseCaseHookOptions<T, TUseCaseOptions>>(optionsRefCollection: OptionsRefCollection<TOptions>, newEntity: T, oldEntity: T) => void;
@@ -0,0 +1,14 @@
1
+ import { triggerWatchers } from '../triggerWatchers';
2
+ export const triggerCallbacks = (optionsRefCollection, newEntity, oldEntity) => {
3
+ for (let { length: i } = optionsRefCollection; i > 0; i--) {
4
+ const { current: options } = optionsRefCollection[i - 1];
5
+ if (!options) {
6
+ continue;
7
+ }
8
+ const { onChange, watch } = options;
9
+ if (watch) {
10
+ triggerWatchers(watch, newEntity, oldEntity);
11
+ }
12
+ onChange?.(newEntity, oldEntity);
13
+ }
14
+ };
@@ -0,0 +1,2 @@
1
+ import { EntityWatchMap } from '../../hooks/useUseCase/types';
2
+ export declare const triggerWatchers: <T>(watch: EntityWatchMap<T>, newEntity: T, oldEntity: T) => void;
@@ -0,0 +1,14 @@
1
+ import { compareProperty } from '../compareProperty';
2
+ export const triggerWatchers = (watch, newEntity, oldEntity) => {
3
+ for (const [fieldPath, watcher] of Object.entries(watch)) {
4
+ compareProperty(oldEntity, newEntity, fieldPath, (oldValue, newValue, fieldPaths) => {
5
+ watcher({
6
+ fieldPaths,
7
+ newEntity,
8
+ oldEntity,
9
+ newValue,
10
+ oldValue,
11
+ });
12
+ });
13
+ }
14
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.UpdateFragment = void 0;
7
+ var _jsxRuntime = require("react/jsx-runtime");
8
+ var _react = require("react");
9
+ var UpdateFragment = function UpdateFragment(_ref) {
10
+ var onUpdate = _ref.onUpdate;
11
+ (0, _react.useEffect)(function () {
12
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate();
13
+ });
14
+ return (0, _jsxRuntime.jsx)(_react.Fragment, {});
15
+ };
16
+ exports.UpdateFragment = UpdateFragment;
@@ -5,5 +5,5 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.defaultUseCaseContext = void 0;
7
7
  var _react = require("react");
8
- var defaultUseCaseContext = (0, _react.createContext)(null);
8
+ var defaultUseCaseContext = (0, _react.createContext)({});
9
9
  exports.defaultUseCaseContext = defaultUseCaseContext;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useCompareDeps = void 0;
7
+ var _react = require("react");
8
+ var _useScope = require("../useScope");
9
+ var useCompareDeps = function useCompareDeps(deps) {
10
+ var updateTimesRef = (0, _react.useRef)(0);
11
+ var prevDepsRef = (0, _react.useRef)([]);
12
+ var prevDeps = prevDepsRef.current;
13
+ var prevLength = prevDeps.length;
14
+ var length = deps.length;
15
+ var updated = (0, _useScope.useScope)(function () {
16
+ if (length !== prevLength) {
17
+ return true;
18
+ }
19
+ for (var i = 0; i < length; i++) {
20
+ if (deps[i] === prevDeps[i]) {
21
+ continue;
22
+ }
23
+ return true;
24
+ }
25
+ return false;
26
+ });
27
+ prevDepsRef.current = deps;
28
+ updateTimesRef.current += updated ? 1 : 0;
29
+ return updateTimesRef.current;
30
+ };
31
+ exports.useCompareDeps = useCompareDeps;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.useConstant = void 0;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _react = require("react");
10
+ var useConstant = function useConstant(initialValue) {
11
+ var _useState = (0, _react.useState)(initialValue),
12
+ _useState2 = (0, _slicedToArray2["default"])(_useState, 1),
13
+ value = _useState2[0];
14
+ return value;
15
+ };
16
+ exports.useConstant = useConstant;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.useConstantFn = void 0;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _react = require("react");
10
+ var useConstantFn = function useConstantFn(initialValue) {
11
+ var _useState = (0, _react.useState)(function () {
12
+ return initialValue;
13
+ }),
14
+ _useState2 = (0, _slicedToArray2["default"])(_useState, 1),
15
+ value = _useState2[0];
16
+ return value;
17
+ };
18
+ exports.useConstantFn = useConstantFn;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useOptionsRefCollection = void 0;
7
+ var _ahooks = require("ahooks");
8
+ var _removeItem = require("../../methods/removeItem");
9
+ var useOptionsRefCollection = function useOptionsRefCollection(isRoot, contextOptionsRefCollection, options) {
10
+ var optionsRef = (0, _ahooks.useLatest)(options);
11
+ var optionsRefCollection = (0, _ahooks.useCreation)(function () {
12
+ if (isRoot) {
13
+ return [];
14
+ }
15
+ return contextOptionsRefCollection;
16
+ }, [contextOptionsRefCollection, isRoot]);
17
+ var prevOptionsRefCollection = (0, _ahooks.usePrevious)(optionsRefCollection);
18
+ (0, _ahooks.useCreation)(function () {
19
+ if (prevOptionsRefCollection) {
20
+ (0, _removeItem.removeItem)(prevOptionsRefCollection, optionsRef);
21
+ }
22
+ optionsRefCollection.push(optionsRef);
23
+ }, [optionsRef, optionsRefCollection, prevOptionsRefCollection]);
24
+ (0, _ahooks.useUnmount)(function () {
25
+ (0, _removeItem.removeItem)(optionsRefCollection, optionsRef);
26
+ });
27
+ return optionsRefCollection;
28
+ };
29
+ exports.useOptionsRefCollection = useOptionsRefCollection;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.useProvider = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _useConstant = require("../useConstant");
10
+ var _ahooks = require("ahooks");
11
+ var useProvider = function useProvider(context, entity, reducers, optionsRefCollection) {
12
+ var contextValue = (0, _ahooks.useCreation)(function () {
13
+ return {
14
+ entity: entity,
15
+ reducers: reducers,
16
+ optionsRefCollection: optionsRefCollection
17
+ };
18
+ }, [entity, reducers, optionsRefCollection]);
19
+ var contextValueRef = (0, _ahooks.useLatest)(contextValue);
20
+ return (0, _useConstant.useConstant)(function () {
21
+ return function (_ref) {
22
+ var children = _ref.children;
23
+ var ContextProvider = context.Provider;
24
+ return _react["default"].createElement(ContextProvider, {
25
+ value: contextValueRef.current
26
+ }, children);
27
+ };
28
+ });
29
+ };
30
+ exports.useProvider = useProvider;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useScope = void 0;
7
+ var useScope = function useScope(scope) {
8
+ return scope();
9
+ };
10
+ exports.useScope = useScope;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });