@mic-rexjs/usecases-react 2.12.0 → 2.12.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 (45) hide show
  1. package/dist/index.min.js +1 -1
  2. package/es/components/SafeUseCaseFragment/index.js +17 -7
  3. package/es/components/SafeUseCaseFragment/types.js +1 -1
  4. package/es/components/UseCaseFragment/index.js +16 -6
  5. package/es/components/UseCaseFragment/types.js +1 -1
  6. package/es/entities/matchPropertyFailedResult/types.js +1 -1
  7. package/es/enums/ArgumentTypes/index.js +3 -3
  8. package/es/enums/Statuses/index.js +22 -22
  9. package/es/hooks/useArgumentTypes/index.js +12 -10
  10. package/es/hooks/useConstant/index.js +7 -4
  11. package/es/hooks/useConstant/types.js +1 -1
  12. package/es/hooks/useConstantEntityReducers/index.js +4 -4
  13. package/es/hooks/useConstantFn/index.js +9 -6
  14. package/es/hooks/useConstantReducers/index.js +4 -4
  15. package/es/hooks/useContext/index.js +14 -11
  16. package/es/hooks/useContext/index.test.js +50 -44
  17. package/es/hooks/useContextualItem/index.js +27 -25
  18. package/es/hooks/useContextualItem/types.js +1 -1
  19. package/es/hooks/useDepsKey/index.js +9 -5
  20. package/es/hooks/useEntity/index.js +65 -61
  21. package/es/hooks/useFullArguments/index.js +12 -17
  22. package/es/hooks/useFullArguments/types.js +1 -1
  23. package/es/hooks/useIsRenderingRef/index.js +6 -6
  24. package/es/hooks/useProvider/index.js +21 -15
  25. package/es/hooks/useProvider/types.js +1 -1
  26. package/es/hooks/useRuntimeEntity/index.js +18 -16
  27. package/es/hooks/useStatuses/index.js +19 -19
  28. package/es/hooks/useUseCase/index.js +78 -53
  29. package/es/hooks/useUseCase/index.test.js +2400 -1755
  30. package/es/hooks/useUseCase/types.js +1 -1
  31. package/es/index.js +1 -1
  32. package/es/methods/triggerWatchers/index.js +12 -7
  33. package/es/types.js +1 -1
  34. package/es/usecases/contextUseCase/index.js +70 -65
  35. package/es/usecases/contextUseCase/types.js +1 -1
  36. package/es/usecases/methodUseCase/index.js +69 -52
  37. package/es/usecases/methodUseCase/types.js +1 -1
  38. package/es/usecases/valueUseCase/index.js +175 -167
  39. package/es/usecases/valueUseCase/types.js +1 -1
  40. package/lib/components/UseCaseFragment/index.js +1 -1
  41. package/lib/hooks/useUseCase/index.js +4 -3
  42. package/lib/hooks/useUseCase/index.test.js +48 -49
  43. package/lib/methods/triggerWatchers/index.js +9 -7
  44. package/lib/usecases/valueUseCase/index.js +1 -1
  45. package/package.json +3 -3
@@ -1,3 +1,4 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
1
2
  import { useConstantEntityReducers } from '../useConstantEntityReducers';
2
3
  import { useContextualItem } from '../useContextualItem';
3
4
  import { useRuntimeEntity } from '../useRuntimeEntity';
@@ -8,66 +9,69 @@ import { Statuses } from '../../enums/Statuses';
8
9
  import { triggerWatchers } from '../../methods/triggerWatchers';
9
10
  import { valueUseCase } from '../../usecases/valueUseCase';
10
11
  export const useEntity = (statuses, entityArg, contextStore, options, deps) => {
11
- const update = useUpdate();
12
- const storeRef = useRef(null);
13
- const optionsRef = useLatest(options);
14
- const contextEntity = (contextStore ? contextStore.value : null);
15
- const entityEnabled = (statuses & Statuses.EntityEnabled) === Statuses.EntityEnabled;
16
- const entityRootEnabled = (statuses & Statuses.EntityRootEnabled) === Statuses.EntityRootEnabled;
17
- const { recordValueDiff } = useConstantEntityReducers(deps, (valueUseCase));
18
- const store = useContextualItem(contextStore, statuses, () => {
19
- let newStore;
20
- if (entityRootEnabled) {
21
- const isFunction = typeof entityArg === 'function';
22
- const { current } = storeRef;
23
- const { value } = current || {};
24
- const [, depsDiff] = recordValueDiff(deps);
25
- const initailEntity = isFunction
26
- ? entityArg(value, depsDiff)
27
- : entityArg;
28
- newStore = new EntityStore(initailEntity);
29
- }
30
- else {
31
- newStore = new EntityStore(null);
32
- }
33
- storeRef.current = newStore;
34
- return newStore;
35
- }, deps);
36
- const runtimeEntity = useRuntimeEntity(store, entityArg, contextEntity, statuses);
37
- const onEntityChange = useMemoizedFn((newEntity, oldEntity) => {
38
- const { onChange, watch } = optionsRef.current;
39
- if (watch) {
40
- triggerWatchers(watch, newEntity, oldEntity);
41
- }
42
- onChange?.(newEntity, oldEntity);
43
- if (!entityRootEnabled) {
44
- return;
45
- }
46
- update();
47
- });
12
+ const update = useUpdate();
13
+ const storeRef = useRef(null);
14
+ const optionsRef = useLatest(options);
15
+ const contextEntity = contextStore ? contextStore.value : null;
16
+ const entityEnabled = (statuses & Statuses.EntityEnabled) === Statuses.EntityEnabled;
17
+ const entityRootEnabled = (statuses & Statuses.EntityRootEnabled) === Statuses.EntityRootEnabled;
18
+ const _useConstantEntityRed = useConstantEntityReducers(deps, valueUseCase),
19
+ recordValueDiff = _useConstantEntityRed.recordValueDiff;
20
+ const store = useContextualItem(contextStore, statuses, () => {
21
+ let newStore;
48
22
  if (entityRootEnabled) {
49
- // 执行同步操作
50
- store.setValue(runtimeEntity, true);
23
+ const isFunction = typeof entityArg === 'function';
24
+ const current = storeRef.current;
25
+ const _ref = current || {},
26
+ value = _ref.value;
27
+ const _recordValueDiff = recordValueDiff(deps),
28
+ _recordValueDiff2 = _slicedToArray(_recordValueDiff, 2),
29
+ depsDiff = _recordValueDiff2[1];
30
+ const initailEntity = isFunction ? entityArg(value, depsDiff) : entityArg;
31
+ newStore = new EntityStore(initailEntity);
32
+ } else {
33
+ newStore = new EntityStore(null);
51
34
  }
52
- /**
53
- * 需要马上 `watch`,因为 `useEffect` 在子组件内优先执行,
54
- * 如果使用了 `setEntity` 则无法监控到变化
55
- */
56
- useCreation(() => {
57
- if (!entityEnabled) {
58
- return;
59
- }
60
- store.watch(onEntityChange);
61
- }, [entityEnabled, store, onEntityChange]);
62
- /**
63
- * 在变化时候,组件更新前,使用 `unwatch`
64
- */
65
- useEffect(() => {
66
- return () => {
67
- store.unwatch(onEntityChange);
68
- };
69
- }, [entityEnabled, store, onEntityChange]);
70
- return useCreation(() => {
71
- return [runtimeEntity, store];
72
- }, [runtimeEntity, store]);
73
- };
35
+ storeRef.current = newStore;
36
+ return newStore;
37
+ }, deps);
38
+ const runtimeEntity = useRuntimeEntity(store, entityArg, contextEntity, statuses);
39
+ const onEntityChange = useMemoizedFn((newEntity, oldEntity) => {
40
+ const _optionsRef$current = optionsRef.current,
41
+ onChange = _optionsRef$current.onChange,
42
+ watch = _optionsRef$current.watch;
43
+ if (watch) {
44
+ triggerWatchers(watch, newEntity, oldEntity);
45
+ }
46
+ onChange === null || onChange === void 0 || onChange(newEntity, oldEntity);
47
+ if (!entityRootEnabled) {
48
+ return;
49
+ }
50
+ update();
51
+ });
52
+ if (entityRootEnabled) {
53
+ // 执行同步操作
54
+ store.setValue(runtimeEntity, true);
55
+ }
56
+ /**
57
+ * 需要马上 `watch`,因为 `useEffect` 在子组件内优先执行,
58
+ * 如果使用了 `setEntity` 则无法监控到变化
59
+ */
60
+ useCreation(() => {
61
+ if (!entityEnabled) {
62
+ return;
63
+ }
64
+ store.watch(onEntityChange);
65
+ }, [entityEnabled, store, onEntityChange]);
66
+ /**
67
+ * 在变化时候,组件更新前,使用 `unwatch`
68
+ */
69
+ useEffect(() => {
70
+ return () => {
71
+ store.unwatch(onEntityChange);
72
+ };
73
+ }, [entityEnabled, store, onEntityChange]);
74
+ return useCreation(() => {
75
+ return [runtimeEntity, store];
76
+ }, [runtimeEntity, store]);
77
+ };
@@ -1,19 +1,14 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
1
2
  import { ArgumentTypes } from '../../enums/ArgumentTypes';
2
3
  export const useFullArguments = (args, argumentTypes) => {
3
- const [arg1, arg2, arg3, arg4] = args;
4
- const withoutEntity = (argumentTypes & ArgumentTypes.Entity) !== ArgumentTypes.Entity;
5
- if (withoutEntity) {
6
- return [
7
- null,
8
- arg1,
9
- (arg2 || {}),
10
- (arg3 || []),
11
- ];
12
- }
13
- return [
14
- arg1,
15
- arg2,
16
- (arg3 || {}),
17
- (arg4 || []),
18
- ];
19
- };
4
+ const _args = _slicedToArray(args, 4),
5
+ arg1 = _args[0],
6
+ arg2 = _args[1],
7
+ arg3 = _args[2],
8
+ arg4 = _args[3];
9
+ const withoutEntity = (argumentTypes & ArgumentTypes.Entity) !== ArgumentTypes.Entity;
10
+ if (withoutEntity) {
11
+ return [null, arg1, arg2 || {}, arg3 || []];
12
+ }
13
+ return [arg1, arg2, arg3 || {}, arg4 || []];
14
+ };
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -1,9 +1,9 @@
1
1
  import { useLatest } from 'ahooks';
2
2
  import { useEffect } from 'react';
3
3
  export const useIsRenderingRef = () => {
4
- const ref = useLatest(true);
5
- useEffect(() => {
6
- ref.current = false;
7
- });
8
- return ref;
9
- };
4
+ const ref = useLatest(true);
5
+ useEffect(() => {
6
+ ref.current = false;
7
+ });
8
+ return ref;
9
+ };
@@ -3,18 +3,24 @@ import { useCreation, useLatest } from 'ahooks';
3
3
  import React from 'react';
4
4
  import { contextUseCase } from '../../usecases/contextUseCase';
5
5
  export const useProvider = (statuses, context, store, reducers) => {
6
- const { value: entity } = store;
7
- const contextValue = useCreation(() => {
8
- const { createContextValue } = contextUseCase();
9
- void entity;
10
- return createContextValue(store, reducers, statuses);
11
- }, [statuses, entity, store, reducers]);
12
- const contextValueRef = useLatest(contextValue);
13
- return useConstantFn(({ children, with: withProviders = [] }) => {
14
- const { Provider: ContextProvider } = context;
15
- const { current: value } = contextValueRef;
16
- return React.createElement(ContextProvider, { value }, withProviders.reduceRight((currentChildren, withProvider) => {
17
- return React.createElement(withProvider, {}, currentChildren);
18
- }, children));
19
- });
20
- };
6
+ const entity = store.value;
7
+ const contextValue = useCreation(() => {
8
+ const _contextUseCase = contextUseCase(),
9
+ createContextValue = _contextUseCase.createContextValue;
10
+ void entity;
11
+ return createContextValue(store, reducers, statuses);
12
+ }, [statuses, entity, store, reducers]);
13
+ const contextValueRef = useLatest(contextValue);
14
+ return useConstantFn(_ref => {
15
+ let children = _ref.children,
16
+ _ref$with = _ref.with,
17
+ withProviders = _ref$with === void 0 ? [] : _ref$with;
18
+ const ContextProvider = context.Provider;
19
+ const value = contextValueRef.current;
20
+ return React.createElement(ContextProvider, {
21
+ value
22
+ }, withProviders.reduceRight((currentChildren, withProvider) => {
23
+ return React.createElement(withProvider, {}, currentChildren);
24
+ }, children));
25
+ });
26
+ };
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -2,19 +2,21 @@ import { useConstantEntityReducers } from '../useConstantEntityReducers';
2
2
  import { Statuses } from '../../enums/Statuses';
3
3
  import { valueUseCase } from '../../usecases/valueUseCase';
4
4
  export const useRuntimeEntity = (store, entityArg, contextEntity, statuses) => {
5
- const { isValueChanged, recordValue } = useConstantEntityReducers(entityArg, (valueUseCase));
6
- const changed = isValueChanged(entityArg);
7
- const entityRootDisabled = (statuses & Statuses.EntityRootEnabled) !== Statuses.EntityRootEnabled;
8
- if (entityRootDisabled) {
9
- return contextEntity;
10
- }
11
- const isFunction = typeof entityArg === 'function';
12
- const { value } = store;
13
- switch (true) {
14
- case isFunction:
15
- case !changed:
16
- return value;
17
- }
18
- recordValue(entityArg);
19
- return entityArg;
20
- };
5
+ const _useConstantEntityRed = useConstantEntityReducers(entityArg, valueUseCase),
6
+ isValueChanged = _useConstantEntityRed.isValueChanged,
7
+ recordValue = _useConstantEntityRed.recordValue;
8
+ const changed = isValueChanged(entityArg);
9
+ const entityRootDisabled = (statuses & Statuses.EntityRootEnabled) !== Statuses.EntityRootEnabled;
10
+ if (entityRootDisabled) {
11
+ return contextEntity;
12
+ }
13
+ const isFunction = typeof entityArg === 'function';
14
+ const value = store.value;
15
+ switch (true) {
16
+ case isFunction:
17
+ case !changed:
18
+ return value;
19
+ }
20
+ recordValue(entityArg);
21
+ return entityArg;
22
+ };
@@ -2,22 +2,22 @@ import { useCreation } from 'ahooks';
2
2
  import { ArgumentTypes } from '../../enums/ArgumentTypes';
3
3
  import { Statuses } from '../../enums/Statuses';
4
4
  export const useStatuses = (argumentTypes, contextValue) => {
5
- return useCreation(() => {
6
- let statuses = Statuses.None;
7
- const { store } = (contextValue || {});
8
- const hasEntity = (argumentTypes & ArgumentTypes.Entity) === ArgumentTypes.Entity;
9
- if (hasEntity) {
10
- statuses |= Statuses.EntityRootEnabled;
11
- }
12
- if (contextValue) {
13
- statuses |= Statuses.ContextEnabled;
14
- }
15
- else {
16
- statuses |= Statuses.RootEnabled;
17
- }
18
- if (store) {
19
- statuses |= Statuses.EntityEnabled;
20
- }
21
- return statuses;
22
- }, [argumentTypes, contextValue]);
23
- };
5
+ return useCreation(() => {
6
+ let statuses = Statuses.None;
7
+ const _ref = contextValue || {},
8
+ store = _ref.store;
9
+ const hasEntity = (argumentTypes & ArgumentTypes.Entity) === ArgumentTypes.Entity;
10
+ if (hasEntity) {
11
+ statuses |= Statuses.EntityRootEnabled;
12
+ }
13
+ if (contextValue) {
14
+ statuses |= Statuses.ContextEnabled;
15
+ } else {
16
+ statuses |= Statuses.RootEnabled;
17
+ }
18
+ if (store) {
19
+ statuses |= Statuses.EntityEnabled;
20
+ }
21
+ return statuses;
22
+ }, [argumentTypes, contextValue]);
23
+ };
@@ -1,3 +1,6 @@
1
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
2
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
+ const _excluded = ["watch", "onChange", "options"];
1
4
  import { useArgumentTypes } from '../useArgumentTypes';
2
5
  import { useConstantFn } from '../useConstantFn';
3
6
  import { useConstantReducers } from '../useConstantReducers';
@@ -13,56 +16,78 @@ import { useCreation, useLatest } from 'ahooks';
13
16
  import { useContext as useContextValue } from 'react';
14
17
  import { Statuses } from '../../enums/Statuses';
15
18
  import { methodUseCase } from '../../usecases/methodUseCase';
16
- export const useUseCase = ((...args) => {
17
- const isRenderingRef = useIsRenderingRef();
18
- const argumentTypes = useArgumentTypes(args);
19
- const fullArguments = useFullArguments(args, argumentTypes);
20
- const [unsafeEntity, unsafeUsecase, options, deps] = fullArguments;
21
- const unkownUsecase = useConstantFn(unsafeUsecase);
22
- const usecase = unkownUsecase;
23
- const entityUseCase = unkownUsecase;
24
- const context = useContext(unkownUsecase, argumentTypes);
25
- const contextValue = useContextValue(context);
26
- const entityContextValue = contextValue;
27
- const statuses = useStatuses(argumentTypes, contextValue);
28
- const optionsRef = useLatest(options);
29
- const { reducers: contextReducers = null } = contextValue || {};
30
- const { store: contextStore = null } = entityContextValue || {};
31
- const [entity, store] = useEntity(statuses, unsafeEntity, contextStore, options, deps);
32
- const { cacheCalls, captureCalls } = useConstantReducers(methodUseCase);
33
- const reducers = useContextualItem(contextReducers, statuses, () => {
34
- const opts = captureCalls(options, (key, callArgs) => {
35
- return optionsRef.current[key]?.(...callArgs);
36
- });
37
- if ((statuses & Statuses.EntityEnabled) !== Statuses.EntityEnabled) {
38
- return usecase(opts);
39
- }
40
- const hookOptions = opts;
41
- const { watch, onChange, options: usecaseOptions, ...restUseCaseOptions } = hookOptions;
42
- return createEntityReducers(store, entityUseCase, {
43
- ...restUseCaseOptions,
44
- ...usecaseOptions,
45
- onGenerate(newEntity, result) {
46
- return result;
47
- },
48
- });
49
- }, deps);
50
- const Provider = useProvider(statuses, context, store, reducers);
51
- return useCreation(() => {
52
- if ((statuses & Statuses.EntityEnabled) !== Statuses.EntityEnabled) {
53
- return reducers;
54
- }
55
- const cachedReducers = cacheCalls(reducers, {
56
- onShouldCache() {
57
- return isRenderingRef.current;
58
- },
59
- });
60
- const isNull = entity === null;
61
- const isArray = Array.isArray(entity);
62
- const isObjectType = typeof entity === 'object';
63
- const isObjectEntity = isObjectType && !isNull && !isArray;
64
- // 这里是为了让访问器 `getter` 的值保持唯一性,否则每次 `getter` 返回的对象都是新对象,无法用于 `deps`
65
- const newEntity = isObjectEntity ? { ...entity } : entity;
66
- return [newEntity, cachedReducers, Provider];
67
- }, [statuses, entity, reducers, Provider, isRenderingRef, cacheCalls]);
68
- });
19
+ export const useUseCase = function () {
20
+ const isRenderingRef = useIsRenderingRef();
21
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
22
+ args[_key] = arguments[_key];
23
+ }
24
+ const argumentTypes = useArgumentTypes(args);
25
+ const fullArguments = useFullArguments(args, argumentTypes);
26
+ const _fullArguments = _slicedToArray(fullArguments, 4),
27
+ unsafeEntity = _fullArguments[0],
28
+ unsafeUsecase = _fullArguments[1],
29
+ options = _fullArguments[2],
30
+ deps = _fullArguments[3];
31
+ const unkownUsecase = useConstantFn(unsafeUsecase);
32
+ const usecase = unkownUsecase;
33
+ const entityUseCase = unkownUsecase;
34
+ const context = useContext(unkownUsecase, argumentTypes);
35
+ const contextValue = useContextValue(context);
36
+ const entityContextValue = contextValue;
37
+ const statuses = useStatuses(argumentTypes, contextValue);
38
+ const optionsRef = useLatest(options);
39
+ const _ref = contextValue || {},
40
+ _ref$reducers = _ref.reducers,
41
+ contextReducers = _ref$reducers === void 0 ? null : _ref$reducers;
42
+ const _ref2 = entityContextValue || {},
43
+ _ref2$store = _ref2.store,
44
+ contextStore = _ref2$store === void 0 ? null : _ref2$store;
45
+ const _useEntity = useEntity(statuses, unsafeEntity, contextStore, options, deps),
46
+ _useEntity2 = _slicedToArray(_useEntity, 2),
47
+ entity = _useEntity2[0],
48
+ store = _useEntity2[1];
49
+ const _useConstantReducers = useConstantReducers(methodUseCase),
50
+ cacheCalls = _useConstantReducers.cacheCalls,
51
+ captureCalls = _useConstantReducers.captureCalls;
52
+ const reducers = useContextualItem(contextReducers, statuses, () => {
53
+ const opts = captureCalls(options, (key, callArgs) => {
54
+ var _optionsRef$current$k, _optionsRef$current;
55
+ return (_optionsRef$current$k = (_optionsRef$current = optionsRef.current)[key]) === null || _optionsRef$current$k === void 0 ? void 0 : _optionsRef$current$k.call(_optionsRef$current, ...callArgs);
56
+ });
57
+ if ((statuses & Statuses.EntityEnabled) !== Statuses.EntityEnabled) {
58
+ return usecase(opts);
59
+ }
60
+ const hookOptions = opts;
61
+ const watch = hookOptions.watch,
62
+ onChange = hookOptions.onChange,
63
+ usecaseOptions = hookOptions.options,
64
+ restUseCaseOptions = _objectWithoutProperties(hookOptions, _excluded);
65
+ return createEntityReducers(store, entityUseCase, {
66
+ ...restUseCaseOptions,
67
+ ...usecaseOptions,
68
+ onGenerate(newEntity, result) {
69
+ return result;
70
+ }
71
+ });
72
+ }, deps);
73
+ const Provider = useProvider(statuses, context, store, reducers);
74
+ return useCreation(() => {
75
+ if ((statuses & Statuses.EntityEnabled) !== Statuses.EntityEnabled) {
76
+ return reducers;
77
+ }
78
+ const cachedReducers = cacheCalls(reducers, {
79
+ onShouldCache() {
80
+ return isRenderingRef.current;
81
+ }
82
+ });
83
+ const isNull = entity === null;
84
+ const isArray = Array.isArray(entity);
85
+ const isObjectType = typeof entity === 'object';
86
+ const isObjectEntity = isObjectType && !isNull && !isArray;
87
+ // 这里是为了让访问器 `getter` 的值保持唯一性,否则每次 `getter` 返回的对象都是新对象,无法用于 `deps`
88
+ const newEntity = isObjectEntity ? {
89
+ ...entity
90
+ } : entity;
91
+ return [newEntity, cachedReducers, Provider];
92
+ }, [statuses, entity, reducers, Provider, isRenderingRef, cacheCalls]);
93
+ };