@mic-rexjs/usecases-react 3.7.0 → 4.0.0-beta.3
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.
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.LICENSE.txt +2 -0
- package/es/components/UseCaseFragment/index.js +2 -2
- package/es/enums/Statuses/index.d.ts +86 -10
- package/es/enums/Statuses/index.js +86 -10
- package/es/hooks/useAlternateStatuses/index.d.ts +2 -0
- package/es/hooks/useAlternateStatuses/index.js +16 -0
- package/es/hooks/useChanged/index.d.ts +2 -0
- package/es/hooks/useChanged/index.js +17 -0
- package/es/hooks/useChanged/types.d.ts +3 -0
- package/es/hooks/useContext/index.d.ts +3 -4
- package/es/hooks/useContext/index.js +17 -15
- package/es/hooks/useContext/index.test.js +13 -24
- package/es/hooks/useContextValue/index.d.ts +5 -0
- package/es/hooks/useContextValue/index.js +13 -0
- package/es/hooks/useDepsKey/index.js +11 -9
- package/es/hooks/useEntity/index.d.ts +3 -2
- package/es/hooks/useEntity/index.js +52 -53
- package/es/hooks/useEntityReturn/index.d.ts +5 -0
- package/es/hooks/useEntityReturn/index.js +15 -0
- package/es/hooks/useEntityReturn/types.d.ts +7 -0
- package/es/hooks/useEntityRootExec/index.d.ts +5 -0
- package/es/hooks/useEntityRootExec/index.js +7 -0
- package/es/hooks/useEntityRootExec/types.d.ts +7 -0
- package/es/hooks/useEntityRootExec/types.js +1 -0
- package/es/hooks/useEntityRootReturn/index.d.ts +5 -0
- package/es/hooks/useEntityRootReturn/index.js +14 -0
- package/es/hooks/useEntityRootReturn/types.d.ts +7 -0
- package/es/hooks/useEntityRootReturn/types.js +1 -0
- package/es/hooks/useExec/index.d.ts +2 -3
- package/es/hooks/useExec/index.js +3 -12
- package/es/hooks/useExec/types.d.ts +10 -2
- package/es/hooks/useFullArguments/index.d.ts +2 -3
- package/es/hooks/useFullArguments/index.js +4 -6
- package/es/hooks/useFullArguments/types.d.ts +2 -2
- package/es/hooks/useIfExec/index.d.ts +2 -0
- package/es/hooks/useIfExec/index.js +20 -0
- package/es/hooks/useIfExec/types.d.ts +6 -0
- package/es/hooks/useIfExec/types.js +1 -0
- package/es/hooks/useIfReturn/index.d.ts +2 -0
- package/es/hooks/useIfReturn/index.js +22 -0
- package/es/hooks/useIfReturn/types.d.ts +6 -0
- package/es/hooks/useIfReturn/types.js +1 -0
- package/es/hooks/useProvider/index.d.ts +3 -5
- package/es/hooks/useProvider/index.js +17 -23
- package/es/hooks/useProviders/index.js +4 -4
- package/es/hooks/useReducers/index.d.ts +5 -4
- package/es/hooks/useReducers/index.js +45 -27
- package/es/hooks/useReturn/index.d.ts +2 -0
- package/es/hooks/useReturn/index.js +33 -0
- package/es/hooks/useReturn/types.d.ts +11 -0
- package/es/hooks/useReturn/types.js +1 -0
- package/es/hooks/useRootInitializing/index.d.ts +2 -0
- package/es/hooks/useRootInitializing/index.js +14 -0
- package/es/hooks/useRootReturn/index.d.ts +5 -0
- package/es/hooks/useRootReturn/index.js +22 -0
- package/es/hooks/useRootReturn/types.d.ts +14 -0
- package/es/hooks/useRootReturn/types.js +1 -0
- package/es/hooks/useStatuses/index.d.ts +4 -4
- package/es/hooks/useStatuses/index.js +46 -21
- package/es/hooks/useStore/index.d.ts +3 -3
- package/es/hooks/useStore/index.js +21 -49
- package/es/hooks/useThrottleUpdate/index.d.ts +2 -0
- package/es/hooks/useThrottleUpdate/index.js +31 -0
- package/es/hooks/useThrottleUpdate/types.d.ts +3 -0
- package/es/hooks/useThrottleUpdate/types.js +1 -0
- package/es/hooks/useUseCase/index.js +21 -24
- package/es/hooks/useUseCase/index.test.js +330 -98
- package/es/hooks/useUseCase/types.d.ts +15 -5
- package/es/methods/cacheCall/index.d.ts +2 -0
- package/es/methods/cacheCall/index.js +38 -0
- package/es/methods/cacheCall/types.d.ts +12 -0
- package/es/methods/cacheCall/types.js +1 -0
- package/es/methods/cacheCalls/index.d.ts +2 -0
- package/es/methods/cacheCalls/index.js +12 -0
- package/es/types.d.ts +2 -1
- package/es/usecases/contextUseCase/index.js +21 -38
- package/es/usecases/contextUseCase/types.d.ts +5 -6
- package/es/usecases/lifecycleEventUseCase/index.js +37 -9
- package/es/usecases/lifecycleEventUseCase/types.d.ts +11 -8
- package/lib/components/UseCaseFragment/index.js +2 -2
- package/lib/enums/Statuses/index.js +86 -10
- package/lib/hooks/useAlternateStatuses/index.js +22 -0
- package/lib/hooks/useChanged/index.js +23 -0
- package/lib/hooks/useContext/index.js +19 -17
- package/lib/hooks/useContext/index.test.js +13 -24
- package/lib/hooks/useContextValue/index.js +19 -0
- package/lib/hooks/useDepsKey/index.js +10 -8
- package/lib/hooks/useEntity/index.js +81 -54
- package/lib/hooks/useEntityReturn/index.js +22 -0
- package/lib/hooks/useEntityRootExec/index.js +13 -0
- package/lib/hooks/useEntityRootExec/types.js +5 -0
- package/lib/hooks/useEntityRootReturn/index.js +21 -0
- package/lib/hooks/useEntityRootReturn/types.js +5 -0
- package/lib/hooks/useExec/index.js +3 -12
- package/lib/hooks/useFullArguments/index.js +4 -6
- package/lib/hooks/useIfExec/index.js +27 -0
- package/lib/hooks/useIfExec/types.js +5 -0
- package/lib/hooks/useIfReturn/index.js +29 -0
- package/lib/hooks/useIfReturn/types.js +5 -0
- package/lib/hooks/useProvider/index.js +16 -22
- package/lib/hooks/useProviders/index.js +3 -3
- package/lib/hooks/useReducers/index.js +46 -29
- package/lib/hooks/useReturn/index.js +40 -0
- package/lib/hooks/useReturn/types.js +5 -0
- package/lib/hooks/useRootInitializing/index.js +20 -0
- package/lib/hooks/useRootReturn/index.js +30 -0
- package/lib/hooks/useRootReturn/types.js +5 -0
- package/lib/hooks/useStatuses/index.js +47 -21
- package/lib/hooks/useStore/index.js +19 -48
- package/lib/hooks/useThrottleUpdate/index.js +37 -0
- package/lib/hooks/useThrottleUpdate/types.js +5 -0
- package/lib/hooks/useUseCase/index.js +22 -25
- package/lib/hooks/useUseCase/index.test.js +360 -97
- package/lib/methods/cacheCall/index.js +44 -0
- package/lib/methods/cacheCall/types.js +5 -0
- package/lib/methods/cacheCalls/index.js +19 -0
- package/lib/usecases/contextUseCase/index.js +21 -38
- package/lib/usecases/lifecycleEventUseCase/index.js +48 -8
- package/package.json +3 -3
- package/es/enums/ArgumentTypes/index.d.ts +0 -4
- package/es/enums/ArgumentTypes/index.js +0 -5
- package/es/hooks/useArgumentTypes/index.d.ts +0 -3
- package/es/hooks/useArgumentTypes/index.js +0 -14
- package/es/hooks/useConstantEntityReducers/index.d.ts +0 -3
- package/es/hooks/useConstantEntityReducers/index.js +0 -7
- package/es/hooks/useConstantFn/index.d.ts +0 -1
- package/es/hooks/useConstantFn/index.js +0 -10
- package/es/hooks/useConstantReducers/index.d.ts +0 -3
- package/es/hooks/useConstantReducers/index.js +0 -6
- package/es/hooks/useContextualItem/index.d.ts +0 -4
- package/es/hooks/useContextualItem/index.js +0 -31
- package/es/hooks/useContextualItem/types.d.ts +0 -3
- package/es/hooks/useEntityInitializing/index.d.ts +0 -2
- package/es/hooks/useEntityInitializing/index.js +0 -16
- package/es/usecases/methodUseCase/index.d.ts +0 -2
- package/es/usecases/methodUseCase/index.js +0 -72
- package/es/usecases/methodUseCase/types.d.ts +0 -21
- package/lib/enums/ArgumentTypes/index.js +0 -11
- package/lib/hooks/useArgumentTypes/index.js +0 -22
- package/lib/hooks/useConstantEntityReducers/index.js +0 -13
- package/lib/hooks/useConstantFn/index.js +0 -17
- package/lib/hooks/useConstantReducers/index.js +0 -12
- package/lib/hooks/useContextualItem/index.js +0 -37
- package/lib/hooks/useEntityInitializing/index.js +0 -22
- package/lib/usecases/methodUseCase/index.js +0 -79
- /package/es/hooks/{useContextualItem → useChanged}/types.js +0 -0
- /package/es/{usecases/methodUseCase → hooks/useEntityReturn}/types.js +0 -0
- /package/lib/hooks/{useContextualItem → useChanged}/types.js +0 -0
- /package/lib/{usecases/methodUseCase → hooks/useEntityReturn}/types.js +0 -0
|
@@ -1,31 +1,27 @@
|
|
|
1
1
|
import { useConstant } from '../useConstant';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { useEffect, useRef } from 'react';
|
|
2
|
+
import { useEntityReturn } from '../useEntityReturn';
|
|
3
|
+
import { useEntityRootExec } from '../useEntityRootExec';
|
|
4
|
+
import { useThrottleUpdate } from '../useThrottleUpdate';
|
|
5
|
+
import { useLatest, useMemoizedFn, useUpdateEffect } from 'ahooks';
|
|
6
|
+
import { useRef } from 'react';
|
|
8
7
|
import { Statuses } from '../../enums/Statuses';
|
|
9
8
|
import { triggerWatchers } from '../../methods/triggerWatchers';
|
|
10
9
|
import { lifecycleEventUseCase } from '../../usecases/lifecycleEventUseCase';
|
|
11
|
-
export const useEntity = (store,
|
|
10
|
+
export const useEntity = (store,
|
|
11
|
+
// 强制传入 `reducers` 参数,目的是让该 `hook` 一定在 `useReducers` 之后执行
|
|
12
|
+
reducers, statuses, options) => {
|
|
12
13
|
const entity = store.value;
|
|
13
|
-
const update =
|
|
14
|
-
const isInitializing = useEntityInitializing(statuses);
|
|
14
|
+
const update = useThrottleUpdate();
|
|
15
15
|
const optionsRef = useLatest(options);
|
|
16
16
|
const prevEntityRef = useRef(entity);
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
getLifecycleEvents = _useConstantReducers.getLifecycleEvents,
|
|
24
|
-
deleteLifecycleEvents = _useConstantReducers.deleteLifecycleEvents;
|
|
17
|
+
const hasEntity = (statuses & Statuses.Entity) === Statuses.Entity;
|
|
18
|
+
const lifecycleReducers = useConstant(lifecycleEventUseCase);
|
|
19
|
+
const createLifecycleKey = lifecycleReducers.createLifecycleKey,
|
|
20
|
+
bindLifecycleEventsKey = lifecycleReducers.bindLifecycleEventsKey,
|
|
21
|
+
getLifecycleEvents = lifecycleReducers.getLifecycleEvents,
|
|
22
|
+
deleteLifecycleEvents = lifecycleReducers.deleteLifecycleEvents;
|
|
25
23
|
const lifecycleKey = useConstant(() => {
|
|
26
|
-
|
|
27
|
-
createKey = _utilsUseCase.createKey;
|
|
28
|
-
return createKey();
|
|
24
|
+
return createLifecycleKey();
|
|
29
25
|
});
|
|
30
26
|
const onTriggerWatchers = useMemoizedFn((newEntity, oldEntity) => {
|
|
31
27
|
const watch = optionsRef.current.watch;
|
|
@@ -37,10 +33,10 @@ export const useEntity = (store, statuses, options) => {
|
|
|
37
33
|
const onEntityUpdate = useMemoizedFn(function () {
|
|
38
34
|
let newEntity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : entity;
|
|
39
35
|
let oldEntity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : prevEntityRef.current;
|
|
40
|
-
|
|
36
|
+
const prevEntity = prevEntityRef.current;
|
|
37
|
+
if (!hasEntity) {
|
|
41
38
|
return;
|
|
42
39
|
}
|
|
43
|
-
const prevEntity = prevEntityRef.current;
|
|
44
40
|
if (prevEntity === newEntity) {
|
|
45
41
|
return;
|
|
46
42
|
}
|
|
@@ -54,41 +50,45 @@ export const useEntity = (store, statuses, options) => {
|
|
|
54
50
|
onAdopt === null || onAdopt === void 0 || onAdopt(newEntity, oldEntity);
|
|
55
51
|
onUpdate === null || onUpdate === void 0 || onUpdate(newEntity, oldEntity);
|
|
56
52
|
});
|
|
57
|
-
const
|
|
58
|
-
const currentOptions = optionsRef.current;
|
|
59
|
-
const onChange1 = currentOptions.onChange;
|
|
53
|
+
const onEntityLifecycleChange = useMemoizedFn(async (newEntity, oldEntity) => {
|
|
60
54
|
const _getLifecycleEvents2 = getLifecycleEvents(lifecycleKey),
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
onChange = _getLifecycleEvents2.onChange;
|
|
56
|
+
let result = onChange === null || onChange === void 0 ? void 0 : onChange(newEntity, oldEntity);
|
|
57
|
+
if (result instanceof Promise) {
|
|
58
|
+
result = await result;
|
|
59
|
+
}
|
|
60
|
+
switch (typeof result) {
|
|
61
|
+
case 'number':
|
|
62
|
+
update(result);
|
|
63
|
+
return;
|
|
64
|
+
case 'undefined':
|
|
65
|
+
update();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (result) {
|
|
66
69
|
return;
|
|
67
70
|
}
|
|
68
71
|
update();
|
|
69
72
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
73
|
+
const onEntityOptionsChange = useMemoizedFn((newEntity, oldEntity) => {
|
|
74
|
+
const currentOptions = optionsRef.current;
|
|
75
|
+
const onChange = currentOptions.onChange;
|
|
76
|
+
onChange === null || onChange === void 0 || onChange(newEntity, oldEntity);
|
|
77
|
+
});
|
|
78
|
+
useEntityRootExec(statuses, () => {
|
|
74
79
|
const _bindLifecycleEventsK = bindLifecycleEventsKey(lifecycleKey),
|
|
75
80
|
onBind = _bindLifecycleEventsK.onBind,
|
|
76
81
|
onAdopt = _bindLifecycleEventsK.onAdopt;
|
|
77
|
-
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
82
|
+
store.watch(onEntityLifecycleChange);
|
|
80
83
|
onBind === null || onBind === void 0 || onBind(entity);
|
|
81
84
|
onAdopt === null || onAdopt === void 0 || onAdopt(entity);
|
|
82
|
-
})
|
|
83
|
-
useUnmount(() => {
|
|
84
|
-
if (!entityRootEnabled) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
85
|
+
}, () => {
|
|
87
86
|
// 要使用最新的 `entity`
|
|
88
87
|
const currentEntity = store.value;
|
|
89
88
|
const _getLifecycleEvents3 = getLifecycleEvents(lifecycleKey),
|
|
90
89
|
onUnbind = _getLifecycleEvents3.onUnbind,
|
|
91
90
|
onDiscard = _getLifecycleEvents3.onDiscard;
|
|
91
|
+
store.unwatch(onEntityLifecycleChange);
|
|
92
92
|
deleteLifecycleEvents(lifecycleKey);
|
|
93
93
|
onUnbind === null || onUnbind === void 0 || onUnbind(currentEntity);
|
|
94
94
|
onDiscard === null || onDiscard === void 0 || onDiscard(currentEntity);
|
|
@@ -97,22 +97,21 @@ export const useEntity = (store, statuses, options) => {
|
|
|
97
97
|
* 需要马上 `watch`,因为 `useEffect` 在子组件内优先执行,
|
|
98
98
|
* 如果使用了 `setEntity` 则无法监控到变化
|
|
99
99
|
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
store.watch(onEntityChange);
|
|
105
|
-
}, [store, entityEnabled, onEntityChange]);
|
|
100
|
+
useEntityReturn(statuses, () => {
|
|
101
|
+
store.watch(onEntityOptionsChange);
|
|
102
|
+
store.watch(onEntityUpdate);
|
|
103
|
+
},
|
|
106
104
|
/**
|
|
107
105
|
* 在变化时候,组件更新前,使用 `unwatch`
|
|
108
106
|
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}, [store, entityEnabled, onEntityChange]);
|
|
107
|
+
() => {
|
|
108
|
+
store.unwatch(onEntityOptionsChange);
|
|
109
|
+
store.unwatch(onEntityUpdate);
|
|
110
|
+
}, [store]);
|
|
114
111
|
useUpdateEffect(() => {
|
|
115
112
|
onEntityUpdate();
|
|
116
113
|
}, [entity, onEntityUpdate]);
|
|
114
|
+
// 强制传入 `reducers` 参数,目的是让该 `hook` 一定在 `useReducers` 之后执行
|
|
115
|
+
void reducers;
|
|
117
116
|
return entity;
|
|
118
117
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import { useIfReturn } from '../useIfReturn';
|
|
3
|
+
import { Statuses } from '../../enums/Statuses';
|
|
4
|
+
/**
|
|
5
|
+
* 只在 `RootCoreCollectionHook` 或 `ContextualCoreCollectionHook` 模式下才会执行相关回调
|
|
6
|
+
*/
|
|
7
|
+
export const useEntityReturn = (statuses, callback, arg3, arg4) => {
|
|
8
|
+
const isDepsArgs = Array.isArray(arg3);
|
|
9
|
+
const _ref = isDepsArgs ? [null, arg3] : [arg3, arg4],
|
|
10
|
+
_ref2 = _slicedToArray(_ref, 2),
|
|
11
|
+
destructor = _ref2[0],
|
|
12
|
+
deps = _ref2[1];
|
|
13
|
+
const hasEntity = (statuses & Statuses.Entity) === Statuses.Entity;
|
|
14
|
+
return useIfReturn(hasEntity, callback, destructor || null, deps);
|
|
15
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ReturnCallback, ReturnDestructor } from '../useReturn/types';
|
|
2
|
+
import { Statuses } from '../../enums/Statuses';
|
|
3
|
+
import { Dependencies } from '../../types';
|
|
4
|
+
export interface EntityReturnHook {
|
|
5
|
+
<T>(statuses: Statuses, callback: ReturnCallback<T>, deps?: Dependencies): T | null;
|
|
6
|
+
<T>(statuses: Statuses, callback: ReturnCallback<T>, destructor: ReturnDestructor | null, deps?: Dependencies): T | null;
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ExecCallback, ExecDestructor } from '../useExec/types';
|
|
2
|
+
import { Statuses } from '../../enums/Statuses';
|
|
3
|
+
import { Dependencies } from '../../types';
|
|
4
|
+
export interface EntityRootExecHook {
|
|
5
|
+
(statuses: Statuses, callback: ExecCallback, deps?: Dependencies): void;
|
|
6
|
+
(statuses: Statuses, callback: ExecCallback, destructor: ExecDestructor | null, deps?: Dependencies): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import { useRootReturn } from '../useRootReturn';
|
|
3
|
+
import { Statuses } from '../../enums/Statuses';
|
|
4
|
+
/**
|
|
5
|
+
* 只在 `RootCoreCollectionHook` 模式下才会执行相关回调
|
|
6
|
+
*/
|
|
7
|
+
export const useEntityRootReturn = (statuses, callback, arg3, arg4) => {
|
|
8
|
+
const isDepsArgs = Array.isArray(arg3);
|
|
9
|
+
const _ref = isDepsArgs ? [null, arg3] : [arg3, arg4],
|
|
10
|
+
_ref2 = _slicedToArray(_ref, 2),
|
|
11
|
+
destructor = _ref2[0],
|
|
12
|
+
deps = _ref2[1];
|
|
13
|
+
return useRootReturn(statuses, callback, destructor || null, Statuses.EntityRootRuntime, deps);
|
|
14
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { RootReturnCallback, RootReturnDestructor } from '../useRootReturn/types';
|
|
2
|
+
import { Statuses } from '../../enums/Statuses';
|
|
3
|
+
import { Dependencies } from '../../types';
|
|
4
|
+
export interface EntityRootReturnHook {
|
|
5
|
+
<T>(statuses: Statuses, callback: RootReturnCallback<T>, deps?: Dependencies): T | null;
|
|
6
|
+
<T>(statuses: Statuses, callback: RootReturnCallback<T>, destructor: RootReturnDestructor | null, deps?: Dependencies): T | null;
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare const useExec: <T>(callback: ExecCallback<T>, deps?: RestArguments) => T;
|
|
1
|
+
import { ExecHook } from './types';
|
|
2
|
+
export declare const useExec: ExecHook;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
let deps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [callback];
|
|
5
|
-
const depsKey = useDepsKey(deps);
|
|
6
|
-
const execCallback = useMemoizedFn(() => {
|
|
7
|
-
return callback();
|
|
8
|
-
});
|
|
9
|
-
return useCreation(() => {
|
|
10
|
-
void depsKey;
|
|
11
|
-
return execCallback();
|
|
12
|
-
}, [depsKey, execCallback]);
|
|
1
|
+
import { useReturn } from '../useReturn';
|
|
2
|
+
export const useExec = function () {
|
|
3
|
+
void useReturn(...arguments);
|
|
13
4
|
};
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { Dependencies } from '../../types';
|
|
2
|
+
export interface ExecCallback {
|
|
3
|
+
(): void;
|
|
4
|
+
}
|
|
5
|
+
export interface ExecDestructor {
|
|
6
|
+
(isUnmount: boolean): void;
|
|
7
|
+
}
|
|
8
|
+
export interface ExecHook {
|
|
9
|
+
(callback: ExecCallback, deps?: Dependencies): void;
|
|
10
|
+
(callback: ExecCallback, destructor: ExecDestructor | null, deps?: Dependencies): void;
|
|
3
11
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { FullParameters } from './types';
|
|
2
|
-
import { ReducerMap } from '@mic-rexjs/usecases/es/types';
|
|
3
|
-
import { ArgumentTypes } from '../../enums/ArgumentTypes';
|
|
4
2
|
import { UseCaseHookParameters } from '../../hooks/useUseCase/types';
|
|
5
|
-
|
|
3
|
+
import { UnknownReducerMap } from '../../types';
|
|
4
|
+
export declare const useFullArguments: <T, TReducers extends UnknownReducerMap<T>, TUseCaseOptions extends object>(args: UseCaseHookParameters, isEntityRoot: boolean) => FullParameters<T, TReducers, TUseCaseOptions>;
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
-
|
|
3
|
-
export const useFullArguments = (args, argumentTypes) => {
|
|
2
|
+
export const useFullArguments = (args, isEntityRoot) => {
|
|
4
3
|
const _args = _slicedToArray(args, 4),
|
|
5
4
|
arg1 = _args[0],
|
|
6
5
|
arg2 = _args[1],
|
|
7
6
|
arg3 = _args[2],
|
|
8
7
|
arg4 = _args[3];
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return [null, arg1, arg2 || {}, arg3 || []];
|
|
8
|
+
if (isEntityRoot) {
|
|
9
|
+
return [arg1, arg2, arg3 || {}, arg4 || []];
|
|
12
10
|
}
|
|
13
|
-
return [arg1, arg2
|
|
11
|
+
return [null, arg1, arg2 || {}, arg3 || []];
|
|
14
12
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { EntityReducers, EntityUseCase, UseCase } from '@mic-rexjs/usecases';
|
|
2
2
|
import { ReducerMap } from '@mic-rexjs/usecases/es/types';
|
|
3
|
-
import {
|
|
3
|
+
import { ContextualUseCaseHookOptions, UseCaseHookOptions } from '../../hooks/useUseCase/types';
|
|
4
4
|
import { Dependencies, EntityInitializer } from '../../types';
|
|
5
5
|
export type FullParameters<T, TReducers extends ReducerMap, TUseCaseOptions extends object> = [
|
|
6
6
|
entity: T | EntityInitializer<T>,
|
|
7
7
|
usecase: UseCase<TReducers, TUseCaseOptions> | EntityUseCase<T, TReducers & EntityReducers<T>, TUseCaseOptions>,
|
|
8
|
-
options: TUseCaseOptions | UseCaseHookOptions<T, TUseCaseOptions> |
|
|
8
|
+
options: TUseCaseOptions | UseCaseHookOptions<T, TUseCaseOptions> | ContextualUseCaseHookOptions<T>,
|
|
9
9
|
deps: Dependencies
|
|
10
10
|
];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import { useExec } from '../useExec';
|
|
3
|
+
export const useIfExec = (value, callback, arg3, arg4) => {
|
|
4
|
+
const hasDestructor = typeof arg3 === 'function' || arg3 === null;
|
|
5
|
+
const _ref = hasDestructor ? [arg3, arg4] : [null, arg3],
|
|
6
|
+
_ref2 = _slicedToArray(_ref, 2),
|
|
7
|
+
destructor = _ref2[0],
|
|
8
|
+
deps = _ref2[1];
|
|
9
|
+
return useExec(() => {
|
|
10
|
+
if (!value) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
callback();
|
|
14
|
+
}, isUnmount => {
|
|
15
|
+
if (!value) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
destructor === null || destructor === void 0 || destructor(isUnmount);
|
|
19
|
+
}, deps);
|
|
20
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ExecCallback, ExecDestructor } from '../useExec/types';
|
|
2
|
+
import { Dependencies } from '../../types';
|
|
3
|
+
export interface TrueExecHook {
|
|
4
|
+
<T>(value: boolean, callback: ExecCallback, deps?: Dependencies): T;
|
|
5
|
+
<T>(value: boolean, callback: ExecCallback, destructor: ExecDestructor | null, deps?: Dependencies): T;
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import { useReturn } from '../useReturn';
|
|
3
|
+
export const useIfReturn = (value, callback, arg3,
|
|
4
|
+
// 默认:如果 `callback` 没有用 `useMemorizedFn`,就会每次都执行,否则只执行一次
|
|
5
|
+
arg4) => {
|
|
6
|
+
const isDepsArg3 = Array.isArray(arg3);
|
|
7
|
+
const _ref = isDepsArg3 ? [null, arg3] : [arg3, arg4],
|
|
8
|
+
_ref2 = _slicedToArray(_ref, 2),
|
|
9
|
+
destructor = _ref2[0],
|
|
10
|
+
deps = _ref2[1];
|
|
11
|
+
return useReturn(() => {
|
|
12
|
+
if (!value) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return callback();
|
|
16
|
+
}, isUnmount => {
|
|
17
|
+
if (!value) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
destructor === null || destructor === void 0 || destructor(isUnmount);
|
|
21
|
+
}, deps);
|
|
22
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ReturnCallback, ReturnDestructor } from '../useReturn/types';
|
|
2
|
+
import { Dependencies } from '../../types';
|
|
3
|
+
export interface IfReturnHook {
|
|
4
|
+
<T>(value: boolean, callback: ReturnCallback<T>, deps?: Dependencies): T | null;
|
|
5
|
+
<T>(value: boolean, callback: ReturnCallback<T>, destructor: ReturnDestructor | null, deps?: Dependencies): T | null;
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { UseCaseProvider } from './types';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import { UseCaseContext, UseCaseContextValue } from '../../usecases/contextUseCase/types';
|
|
6
|
-
export declare const useProvider: <T, TReducers extends ReducerMap>(statuses: Statuses, context: UseCaseContext<UseCaseContextValue<TReducers>>, store: EntityStore<T>, reducers: TReducers) => UseCaseProvider;
|
|
2
|
+
import { UnknownReducerMap } from '../../types';
|
|
3
|
+
import { EntityUseCaseContextValue, UseCaseContext } from '../../usecases/contextUseCase/types';
|
|
4
|
+
export declare const useProvider: <T, TReducers extends UnknownReducerMap<T>, TContextValue extends EntityUseCaseContextValue<T, TReducers>>(context: UseCaseContext<TContextValue>, contextValue: TContextValue) => UseCaseProvider;
|
|
@@ -1,27 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useConstant } from '../useConstant';
|
|
2
|
+
import { useLatest } from 'ahooks';
|
|
3
3
|
import React from 'react';
|
|
4
|
-
|
|
5
|
-
export const useProvider = (statuses, context, store, reducers) => {
|
|
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]);
|
|
4
|
+
export const useProvider = (context, contextValue) => {
|
|
13
5
|
const contextValueRef = useLatest(contextValue);
|
|
14
|
-
return
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
6
|
+
return useConstant(() => {
|
|
7
|
+
return _ref => {
|
|
8
|
+
let children = _ref.children,
|
|
9
|
+
_ref$with = _ref.with,
|
|
10
|
+
withProviders = _ref$with === void 0 ? [] : _ref$with;
|
|
11
|
+
const ContextProvider = context.Provider;
|
|
12
|
+
const value = contextValueRef.current;
|
|
13
|
+
const providers = Array.isArray(withProviders) ? withProviders : [withProviders];
|
|
14
|
+
return React.createElement(ContextProvider, {
|
|
15
|
+
value
|
|
16
|
+
}, providers.reduceRight((currentChildren, withProvider) => {
|
|
17
|
+
return React.createElement(withProvider, {}, currentChildren);
|
|
18
|
+
}, children));
|
|
19
|
+
};
|
|
26
20
|
});
|
|
27
21
|
};
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { useDepsKey } from '../useDepsKey';
|
|
2
|
-
import {
|
|
2
|
+
import { useReturn } from '../useReturn';
|
|
3
|
+
import { useLatest } from 'ahooks';
|
|
3
4
|
export const useProviders = function () {
|
|
4
5
|
for (var _len = arguments.length, providers = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
5
6
|
providers[_key] = arguments[_key];
|
|
6
7
|
}
|
|
7
8
|
const depsKey = useDepsKey(providers);
|
|
8
9
|
const providersRef = useLatest(providers);
|
|
9
|
-
return
|
|
10
|
-
void depsKey;
|
|
10
|
+
return useReturn(() => {
|
|
11
11
|
return providersRef.current;
|
|
12
|
-
}, [depsKey
|
|
12
|
+
}, [depsKey]);
|
|
13
13
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { ContextualUseCaseHookOptions, UseCaseHookOptions } from '../useUseCase/types';
|
|
1
2
|
import { EntityStore } from '@mic-rexjs/usecases';
|
|
2
|
-
import {
|
|
3
|
+
import { EntityUseCase, UseCase } from '@mic-rexjs/usecases/es/types';
|
|
3
4
|
import { Statuses } from '../../enums/Statuses';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export declare const useReducers: <T, TReducers extends
|
|
5
|
+
import { UnknownReducerMap } from '../../types';
|
|
6
|
+
import { ContextualEntityReducers, EntityUseCaseContextValue, UseCaseContext } from '../../usecases/contextUseCase/types';
|
|
7
|
+
export declare const useReducers: <T, TReducers extends UnknownReducerMap<T>, TOptions extends object>(usecase: UseCase<TReducers, TOptions> | EntityUseCase<T, TReducers, TOptions>, store: EntityStore<T>, context: UseCaseContext<EntityUseCaseContextValue<T, TReducers>>, statuses: Statuses, options: TOptions | UseCaseHookOptions<T, TOptions> | ContextualUseCaseHookOptions<T>) => TReducers | ContextualEntityReducers<T, TReducers>;
|
|
@@ -1,43 +1,61 @@
|
|
|
1
1
|
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
2
|
-
const _excluded = ["watch", "onChange"
|
|
3
|
-
import {
|
|
2
|
+
const _excluded = ["watch", "onChange"];
|
|
3
|
+
import { useReturn } from '../useReturn';
|
|
4
4
|
import { createEntityReducers } from '@mic-rexjs/usecases';
|
|
5
5
|
import { useLatest } from 'ahooks';
|
|
6
6
|
import { useContext } from 'react';
|
|
7
7
|
import { Statuses } from '../../enums/Statuses';
|
|
8
|
-
|
|
9
|
-
export const useReducers = function (usecase, context, store, statuses, options) {
|
|
10
|
-
let deps = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
|
|
11
|
-
const optionsRef = useLatest(options);
|
|
8
|
+
export const useReducers = (usecase, store, context, statuses, options) => {
|
|
12
9
|
const contextValue = useContext(context);
|
|
13
10
|
const _ref = contextValue || {},
|
|
14
11
|
_ref$reducers = _ref.reducers,
|
|
15
12
|
contextReducers = _ref$reducers === void 0 ? null : _ref$reducers;
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
const watch = options.watch,
|
|
14
|
+
onChange = options.onChange,
|
|
15
|
+
restOptions = _objectWithoutProperties(options, _excluded);
|
|
16
|
+
const _options$options = options.options,
|
|
17
|
+
initailOptions = _options$options === void 0 ? restOptions : _options$options;
|
|
18
|
+
const initailOptionsRef = useLatest(initailOptions);
|
|
19
|
+
const reducersKey = statuses & Statuses.ReducersKey;
|
|
20
|
+
const onOption = key => {
|
|
21
|
+
return function () {
|
|
22
|
+
const currentInitailOptions = initailOptionsRef.current;
|
|
23
|
+
const currentValue = currentInitailOptions[key];
|
|
24
|
+
if (typeof currentValue !== 'function') {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
return currentValue(...arguments);
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const onOptions = () => {
|
|
31
|
+
const newOptions = {
|
|
32
|
+
...initailOptions
|
|
33
|
+
};
|
|
34
|
+
for (const key of Object.keys(newOptions)) {
|
|
35
|
+
const optionKey = key;
|
|
36
|
+
const value = newOptions[optionKey];
|
|
37
|
+
if (typeof value !== 'function') {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
newOptions[optionKey] = onOption(optionKey);
|
|
26
41
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
return newOptions;
|
|
43
|
+
};
|
|
44
|
+
return useReturn(() => {
|
|
45
|
+
const hasEntity = (statuses & Statuses.Entity) === Statuses.Entity;
|
|
46
|
+
const isEntityRootRuntime = (statuses & Statuses.EntityRootRuntime) === Statuses.EntityRootRuntime;
|
|
47
|
+
if (!hasEntity) {
|
|
48
|
+
return usecase(options);
|
|
49
|
+
}
|
|
50
|
+
if (!isEntityRootRuntime) {
|
|
51
|
+
return contextReducers;
|
|
52
|
+
}
|
|
53
|
+
const newOptions = onOptions();
|
|
32
54
|
return createEntityReducers(store, usecase, {
|
|
33
|
-
|
|
34
|
-
...usecaseOptions,
|
|
55
|
+
options: newOptions,
|
|
35
56
|
onGenerate(newEntity, result) {
|
|
36
57
|
return result;
|
|
37
58
|
}
|
|
38
59
|
});
|
|
39
|
-
};
|
|
40
|
-
return useContextualItem(contextReducers, statuses, () => {
|
|
41
|
-
return onCreate();
|
|
42
|
-
}, deps);
|
|
60
|
+
}, [reducersKey]);
|
|
43
61
|
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
import { useDepsKey } from '../useDepsKey';
|
|
3
|
+
import { useCreation as useAhooksCreation, useMemoizedFn, useUnmount } from 'ahooks';
|
|
4
|
+
import { useRef } from 'react';
|
|
5
|
+
export const useReturn = function (callback) {
|
|
6
|
+
let arg2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [callback];
|
|
7
|
+
let arg3 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [callback];
|
|
8
|
+
const hasDestructor = !Array.isArray(arg2);
|
|
9
|
+
const _ref = hasDestructor ? [arg2, arg3] : [null, arg2],
|
|
10
|
+
_ref2 = _slicedToArray(_ref, 2),
|
|
11
|
+
destructor = _ref2[0],
|
|
12
|
+
deps = _ref2[1];
|
|
13
|
+
const depsKey = useDepsKey(deps);
|
|
14
|
+
const destructorRef = useRef(null);
|
|
15
|
+
const onDestruct = function () {
|
|
16
|
+
var _destructorRef$curren;
|
|
17
|
+
let isUnmount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
18
|
+
(_destructorRef$curren = destructorRef.current) === null || _destructorRef$curren === void 0 || _destructorRef$curren.call(destructorRef, isUnmount);
|
|
19
|
+
};
|
|
20
|
+
const onCreate = useMemoizedFn(() => {
|
|
21
|
+
onDestruct();
|
|
22
|
+
// `destructor` 一定是在执行回调的时候,同步绑定的,保持两个回调内的闭包变量一致
|
|
23
|
+
destructorRef.current = destructor;
|
|
24
|
+
return callback();
|
|
25
|
+
});
|
|
26
|
+
useUnmount(() => {
|
|
27
|
+
onDestruct(true);
|
|
28
|
+
});
|
|
29
|
+
return useAhooksCreation(() => {
|
|
30
|
+
void depsKey;
|
|
31
|
+
return onCreate();
|
|
32
|
+
}, [depsKey, onCreate]);
|
|
33
|
+
};
|