@mic-rexjs/usecases-react 2.13.6 → 3.0.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.
- package/dist/index.min.js +1 -1
- package/es/hooks/useEntity/index.d.ts +5 -2
- package/es/hooks/useEntity/index.js +88 -17
- package/es/hooks/useExec/index.d.ts +2 -0
- package/es/hooks/useExec/index.js +13 -0
- package/es/hooks/useFullArguments/types.d.ts +3 -3
- package/es/hooks/useReducers/index.js +2 -1
- package/es/hooks/useStore/index.d.ts +1 -1
- package/es/hooks/useStore/index.js +51 -73
- package/es/hooks/useUseCase/index.js +2 -2
- package/es/hooks/useUseCase/index.test.js +250 -183
- package/es/hooks/useUseCase/types.d.ts +2 -2
- package/es/index.d.ts +1 -0
- package/es/types.d.ts +7 -3
- package/es/usecases/methodUseCase/index.js +2 -2
- package/es/usecases/valueUseCase/index.js +32 -23
- package/es/usecases/valueUseCase/types.d.ts +3 -2
- package/lib/hooks/useEntity/index.js +88 -17
- package/lib/hooks/useExec/index.js +19 -0
- package/lib/hooks/useReducers/index.js +2 -1
- package/lib/hooks/useStore/index.js +50 -73
- package/lib/hooks/useUseCase/index.js +2 -2
- package/lib/hooks/useUseCase/index.test.js +272 -191
- package/lib/usecases/methodUseCase/index.js +2 -2
- package/lib/usecases/valueUseCase/index.js +34 -25
- package/package.json +3 -3
- package/es/methods/initStore/index.d.ts +0 -3
- package/es/methods/initStore/index.js +0 -23
- package/lib/methods/initStore/index.js +0 -29
|
@@ -20,7 +20,7 @@ export type RootCoreCollection<T, TEntityReducers extends EntityReducerMap<T>> =
|
|
|
20
20
|
* @deprecated 请用 `CoreCollection` 代替
|
|
21
21
|
*/
|
|
22
22
|
export type ContextualCoreCollection<T, TEntityReducers extends EntityReducerMap<T>> = CoreCollection<T, TEntityReducers>;
|
|
23
|
-
export interface EntityGetter<T
|
|
23
|
+
export interface EntityGetter<T> extends EntityInitializer<T> {
|
|
24
24
|
}
|
|
25
25
|
export interface EntityWatchEvent<T, TValue = unknown> extends MatchPropertyFailedResult<T, TValue> {
|
|
26
26
|
}
|
|
@@ -56,7 +56,7 @@ export interface ContextualCoreCollectionHook {
|
|
|
56
56
|
<T, TEntityReducers extends EntityReducerMap<T>>(usecase: InferableEntityUseCase<T, TEntityReducers>, options?: UseCaseHookContextualOptions<T>, deps?: Dependencies): CoreCollection<T, TEntityReducers>;
|
|
57
57
|
}
|
|
58
58
|
export interface RootCoreCollectionHook {
|
|
59
|
-
<T, TEntityReducers extends EntityReducerMap<T>, TUseCaseOptions extends object = object
|
|
59
|
+
<T, TEntityReducers extends EntityReducerMap<T>, TUseCaseOptions extends object = object>(entity: T | EntityInitializer<T>, usecase: InferableEntityUseCase<T, TEntityReducers & EntityReducers<T>, TUseCaseOptions>, options?: UseCaseHookOptions<T, TUseCaseOptions>, deps?: Dependencies): CoreCollection<T, EntityReducers<T, TEntityReducers>>;
|
|
60
60
|
}
|
|
61
61
|
export interface CoreCollectionHook extends ContextualCoreCollectionHook, RootCoreCollectionHook {
|
|
62
62
|
}
|
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 { EntityLifeCycles } from './types';
|
|
3
4
|
export type { EntityWatchEvent, RootCoreCollection, ContextualCoreCollection, CoreCollection, } from './hooks/useUseCase/types';
|
|
4
5
|
export type { UseCaseHookOptions } from './hooks/useUseCase/types';
|
|
5
6
|
export type { UseCaseProvider } from './hooks/useProvider/types';
|
package/es/types.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { AsyncEntityCallbackGenerator, EntityGenerator, RestArguments } from '@mic-rexjs/usecases/es/types';
|
|
2
2
|
export interface Dependencies extends RestArguments {
|
|
3
3
|
}
|
|
4
|
-
export type InitEntityResult<T> = T | EntityGenerator<T
|
|
5
|
-
export interface EntityInitializer<T
|
|
6
|
-
(entity: T | undefined
|
|
4
|
+
export type InitEntityResult<T> = T | EntityGenerator<T> | AsyncEntityCallbackGenerator<T>;
|
|
5
|
+
export interface EntityInitializer<T> {
|
|
6
|
+
(entity: T | undefined): InitEntityResult<T>;
|
|
7
|
+
}
|
|
8
|
+
export interface EntityLifeCycles<T> {
|
|
9
|
+
bindEntity?<S extends T>(entity: S): S | void;
|
|
10
|
+
unbindEntity?(entity: T): void;
|
|
7
11
|
}
|
|
@@ -2,7 +2,7 @@ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
|
2
2
|
import { valueUseCase } from '../valueUseCase';
|
|
3
3
|
export const methodUseCase = () => {
|
|
4
4
|
const _valueUseCase = valueUseCase(),
|
|
5
|
-
|
|
5
|
+
matchValue = _valueUseCase.matchValue;
|
|
6
6
|
const cacheCall = (factory, options) => {
|
|
7
7
|
let cache;
|
|
8
8
|
const _ref = options || {},
|
|
@@ -12,7 +12,7 @@ export const methodUseCase = () => {
|
|
|
12
12
|
} : _ref$onShouldCache,
|
|
13
13
|
_ref$onCompare = _ref.onCompare,
|
|
14
14
|
onCompare = _ref$onCompare === void 0 ? (newArgs, oldArgs) => {
|
|
15
|
-
return
|
|
15
|
+
return matchValue(newArgs, oldArgs);
|
|
16
16
|
} : _ref$onCompare;
|
|
17
17
|
return function () {
|
|
18
18
|
const cacheable = onShouldCache();
|
|
@@ -15,21 +15,21 @@ export const valueUseCase = createUseCase(() => {
|
|
|
15
15
|
const isValueChanged = (entity, value) => {
|
|
16
16
|
return entity !== value;
|
|
17
17
|
};
|
|
18
|
-
const
|
|
18
|
+
const matchValue = (entity, value, onMatchPropertyFailed) => {
|
|
19
19
|
if (entity === value) {
|
|
20
20
|
return true;
|
|
21
21
|
}
|
|
22
22
|
const isObject1 = typeof entity === 'object' && entity !== null;
|
|
23
23
|
const isObject2 = typeof value === 'object' && value !== null;
|
|
24
|
-
|
|
24
|
+
const keys1 = isObject1 ? Object.keys(entity) : [];
|
|
25
|
+
const keys2 = isObject2 ? Object.keys(value) : [];
|
|
26
|
+
const hasCallback = typeof onMatchPropertyFailed === 'function';
|
|
27
|
+
if (!isObject1 && !isObject2) {
|
|
25
28
|
return false;
|
|
26
29
|
}
|
|
27
30
|
let allMatched = true;
|
|
28
|
-
const keys1 = Object.keys(entity);
|
|
29
|
-
const keys2 = Object.keys(value);
|
|
30
31
|
const length1 = keys1.length;
|
|
31
32
|
const keys = [...keys1, ...keys2];
|
|
32
|
-
const hasCallback = typeof onMatchFailed === 'function';
|
|
33
33
|
for (let i = 0, length = keys.length; i < length; i++) {
|
|
34
34
|
const key = keys[i];
|
|
35
35
|
const isFromKeys2 = i >= length1;
|
|
@@ -38,20 +38,20 @@ export const valueUseCase = createUseCase(() => {
|
|
|
38
38
|
if (isUsed) {
|
|
39
39
|
continue;
|
|
40
40
|
}
|
|
41
|
-
const hasField1 =
|
|
42
|
-
const hasField2 =
|
|
41
|
+
const hasField1 = hasField(entity, key);
|
|
42
|
+
const hasField2 = hasField(value, key);
|
|
43
43
|
const onlyHasField1 = hasField1 && !hasField2;
|
|
44
44
|
const onlyHasField2 = !hasField1 && hasField2;
|
|
45
45
|
const onlyHasOneField = onlyHasField1 || onlyHasField2;
|
|
46
|
-
const value1 = entity[key];
|
|
47
|
-
const value2 = value[key];
|
|
46
|
+
const value1 = hasField1 ? entity[key] : void 0;
|
|
47
|
+
const value2 = hasField2 ? value[key] : void 0;
|
|
48
48
|
const matched = value1 === value2;
|
|
49
49
|
if (!onlyHasOneField && matched) {
|
|
50
50
|
continue;
|
|
51
51
|
}
|
|
52
52
|
allMatched = false;
|
|
53
53
|
if (hasCallback) {
|
|
54
|
-
|
|
54
|
+
onMatchPropertyFailed(key, value2, value1);
|
|
55
55
|
continue;
|
|
56
56
|
}
|
|
57
57
|
break;
|
|
@@ -127,15 +127,7 @@ export const valueUseCase = createUseCase(() => {
|
|
|
127
127
|
} while (list.length > 0);
|
|
128
128
|
return allMatched;
|
|
129
129
|
};
|
|
130
|
-
const
|
|
131
|
-
const changed = isValueChanged(entity, value);
|
|
132
|
-
if (!changed) {
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
yield value;
|
|
136
|
-
return true;
|
|
137
|
-
};
|
|
138
|
-
const recordValueDiff = function* (entity, value) {
|
|
130
|
+
const diffValue = (entity, value, onMatchPropertyFailed) => {
|
|
139
131
|
const isArray1 = Array.isArray(entity);
|
|
140
132
|
const isArray2 = Array.isArray(value);
|
|
141
133
|
const isArray = isArray1 && isArray2;
|
|
@@ -145,8 +137,24 @@ export const valueUseCase = createUseCase(() => {
|
|
|
145
137
|
const length2 = value.length;
|
|
146
138
|
result.length = Math.max(length1, length2);
|
|
147
139
|
}
|
|
148
|
-
|
|
149
|
-
result[key] =
|
|
140
|
+
matchValue(entity, value, (key, newSubValue, oldSubValue) => {
|
|
141
|
+
result[key] = newSubValue;
|
|
142
|
+
onMatchPropertyFailed === null || onMatchPropertyFailed === void 0 || onMatchPropertyFailed(key, newSubValue, oldSubValue);
|
|
143
|
+
});
|
|
144
|
+
return result;
|
|
145
|
+
};
|
|
146
|
+
const recordValue = function* (entity, value) {
|
|
147
|
+
const changed = isValueChanged(entity, value);
|
|
148
|
+
if (!changed) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
yield value;
|
|
152
|
+
return true;
|
|
153
|
+
};
|
|
154
|
+
const recordValueDiff = function* (entity, value) {
|
|
155
|
+
let matched = true;
|
|
156
|
+
const result = diffValue(entity, value, () => {
|
|
157
|
+
matched = false;
|
|
150
158
|
});
|
|
151
159
|
if (!matched) {
|
|
152
160
|
yield value;
|
|
@@ -154,7 +162,7 @@ export const valueUseCase = createUseCase(() => {
|
|
|
154
162
|
return result;
|
|
155
163
|
};
|
|
156
164
|
const recordValueMatch = function* (entity, value) {
|
|
157
|
-
const matched =
|
|
165
|
+
const matched = matchValue(entity, value);
|
|
158
166
|
if (matched) {
|
|
159
167
|
return true;
|
|
160
168
|
}
|
|
@@ -170,8 +178,9 @@ export const valueUseCase = createUseCase(() => {
|
|
|
170
178
|
};
|
|
171
179
|
return {
|
|
172
180
|
...entityReducers,
|
|
181
|
+
diffValue,
|
|
173
182
|
isValueChanged,
|
|
174
|
-
|
|
183
|
+
matchValue,
|
|
175
184
|
matchProperty,
|
|
176
185
|
recordValue,
|
|
177
186
|
recordValueDiff,
|
|
@@ -4,12 +4,13 @@ import { MatchPropertyFailedResult } from '../../entities/matchPropertyFailedRes
|
|
|
4
4
|
export interface MatchPropertyFailedCallback<T> {
|
|
5
5
|
(result: MatchPropertyFailedResult<T>): void;
|
|
6
6
|
}
|
|
7
|
-
export interface
|
|
7
|
+
export interface MatchValuePropertyFailedCallback<T> {
|
|
8
8
|
<K extends keyof T>(key: K, newValue: T[K], oldValue: T[K]): void;
|
|
9
9
|
}
|
|
10
10
|
export type ValueReducers<T> = EntityReducers<T, {
|
|
11
|
+
diffValue<S extends T>(entity: S, value: S, onMatchPropertyFailed?: MatchValuePropertyFailedCallback<S>): Partial<S>;
|
|
11
12
|
isValueChanged<S extends T>(entity: S, value: S): boolean;
|
|
12
|
-
|
|
13
|
+
matchValue<S extends T>(entity: S, value: S, onMatchPropertyFailed?: MatchValuePropertyFailedCallback<S>): boolean;
|
|
13
14
|
matchProperty<S extends T>(entity: S, value: S, fieldPath: string, onFailed?: MatchPropertyFailedCallback<S>): boolean;
|
|
14
15
|
recordValue<S extends T>(entity: S, value: S): EntityGenerator<S, boolean>;
|
|
15
16
|
recordValueDiff<S extends T>(entity: S, value: S): EntityGenerator<S, Partial<S>>;
|
|
@@ -5,26 +5,97 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.useEntity = void 0;
|
|
7
7
|
var _useConstantEntityReducers = require("../useConstantEntityReducers");
|
|
8
|
+
var _useExec = require("../useExec");
|
|
9
|
+
var _ahooks = require("ahooks");
|
|
10
|
+
var _react = require("react");
|
|
8
11
|
var _Statuses = require("../../enums/Statuses");
|
|
12
|
+
var _triggerWatchers = require("../../methods/triggerWatchers");
|
|
9
13
|
var _valueUseCase = require("../../usecases/valueUseCase");
|
|
10
|
-
var useEntity = exports.useEntity = function useEntity(store,
|
|
14
|
+
var useEntity = exports.useEntity = function useEntity(entityArg, store, reducers, statuses, options) {
|
|
15
|
+
var update = (0, _ahooks.useUpdate)();
|
|
16
|
+
var isFunction = typeof entityArg === 'function';
|
|
17
|
+
var entity = store.value;
|
|
18
|
+
var isFirstRef = (0, _react.useRef)(true);
|
|
19
|
+
var optionsRef = (0, _ahooks.useLatest)(options);
|
|
20
|
+
var prevEntityRef = (0, _react.useRef)(entity);
|
|
21
|
+
var entityEnabled = (statuses & _Statuses.Statuses.EntityEnabled) === _Statuses.Statuses.EntityEnabled;
|
|
22
|
+
var entityRootEnabled = (statuses & _Statuses.Statuses.EntityRootEnabled) === _Statuses.Statuses.EntityRootEnabled;
|
|
23
|
+
var bindEntity = reducers.bindEntity,
|
|
24
|
+
unbindEntity = reducers.unbindEntity;
|
|
11
25
|
var _useConstantEntityRed = (0, _useConstantEntityReducers.useConstantEntityReducers)(entityArg, _valueUseCase.valueUseCase),
|
|
12
26
|
isValueChanged = _useConstantEntityRed.isValueChanged,
|
|
13
27
|
recordValue = _useConstantEntityRed.recordValue;
|
|
14
|
-
var
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
var onTriggerWatchers = (0, _ahooks.useMemoizedFn)(function (newEntity, oldEntity) {
|
|
29
|
+
var watch = optionsRef.current.watch;
|
|
30
|
+
if (!watch) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
(0, _triggerWatchers.triggerWatchers)(watch, newEntity, oldEntity);
|
|
34
|
+
});
|
|
35
|
+
var onEntityChange = (0, _ahooks.useMemoizedFn)(function (newEntity, oldEntity) {
|
|
36
|
+
var onChange = optionsRef.current.onChange;
|
|
37
|
+
prevEntityRef.current = newEntity;
|
|
38
|
+
onTriggerWatchers(newEntity, oldEntity);
|
|
39
|
+
onChange === null || onChange === void 0 || onChange(newEntity, oldEntity);
|
|
40
|
+
if (!entityRootEnabled) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
update();
|
|
44
|
+
});
|
|
45
|
+
var onValueChange = (0, _ahooks.useMemoizedFn)(function () {
|
|
46
|
+
if (!entityEnabled) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
var prevEntity = prevEntityRef.current;
|
|
50
|
+
if (entity === prevEntity) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
prevEntityRef.current = entity;
|
|
54
|
+
onTriggerWatchers(entity, prevEntity);
|
|
55
|
+
});
|
|
56
|
+
(0, _useExec.useExec)(function () {
|
|
57
|
+
var currentEntity = entityArg;
|
|
58
|
+
var isFirst = isFirstRef.current;
|
|
59
|
+
isFirstRef.current = false;
|
|
60
|
+
if (isFirst) {
|
|
61
|
+
var _bindEntity;
|
|
62
|
+
currentEntity = (_bindEntity = bindEntity === null || bindEntity === void 0 ? void 0 : bindEntity()) !== null && _bindEntity !== void 0 ? _bindEntity : currentEntity;
|
|
63
|
+
}
|
|
64
|
+
switch (true) {
|
|
65
|
+
case !entityRootEnabled:
|
|
66
|
+
case isFunction:
|
|
67
|
+
case !isValueChanged(currentEntity):
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
recordValue(currentEntity);
|
|
71
|
+
store.disableWatchers();
|
|
72
|
+
// 执行同步操作
|
|
73
|
+
store.setValue(currentEntity);
|
|
74
|
+
store.enableWatchers();
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* 需要马上 `watch`,因为 `useEffect` 在子组件内优先执行,
|
|
78
|
+
* 如果使用了 `setEntity` 则无法监控到变化
|
|
79
|
+
*/
|
|
80
|
+
(0, _useExec.useExec)(function () {
|
|
81
|
+
if (!entityEnabled) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
store.watch(onEntityChange);
|
|
85
|
+
}, [entityEnabled, store, onEntityChange]);
|
|
86
|
+
/**
|
|
87
|
+
* 在变化时候,组件更新前,使用 `unwatch`
|
|
88
|
+
*/
|
|
89
|
+
(0, _react.useEffect)(function () {
|
|
90
|
+
return function () {
|
|
91
|
+
store.unwatch(onEntityChange);
|
|
92
|
+
};
|
|
93
|
+
}, [entityEnabled, store, onEntityChange]);
|
|
94
|
+
(0, _ahooks.useUnmount)(function () {
|
|
95
|
+
unbindEntity === null || unbindEntity === void 0 || unbindEntity();
|
|
96
|
+
});
|
|
97
|
+
(0, _ahooks.useUpdateEffect)(function () {
|
|
98
|
+
onValueChange();
|
|
99
|
+
}, [entity, onValueChange]);
|
|
100
|
+
return store.value;
|
|
30
101
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useExec = void 0;
|
|
7
|
+
var _useDepsKey = require("../useDepsKey");
|
|
8
|
+
var _ahooks = require("ahooks");
|
|
9
|
+
var useExec = exports.useExec = function useExec(callback) {
|
|
10
|
+
var deps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [callback];
|
|
11
|
+
var depsKey = (0, _useDepsKey.useDepsKey)(deps);
|
|
12
|
+
var execCallback = (0, _ahooks.useMemoizedFn)(function () {
|
|
13
|
+
callback();
|
|
14
|
+
});
|
|
15
|
+
(0, _ahooks.useCreation)(function () {
|
|
16
|
+
void depsKey;
|
|
17
|
+
execCallback();
|
|
18
|
+
}, [depsKey, execCallback]);
|
|
19
|
+
};
|
|
@@ -27,7 +27,7 @@ var useReducers = exports.useReducers = function useReducers(usecase, context, s
|
|
|
27
27
|
contextReducers = _ref$reducers === void 0 ? null : _ref$reducers;
|
|
28
28
|
var _useConstantReducers = (0, _useConstantReducers2.useConstantReducers)(_methodUseCase.methodUseCase),
|
|
29
29
|
captureCalls = _useConstantReducers.captureCalls;
|
|
30
|
-
|
|
30
|
+
var reducers = (0, _useContextualItem.useContextualItem)(contextReducers, statuses, function () {
|
|
31
31
|
var opts = captureCalls(options, function (key, callArgs) {
|
|
32
32
|
var _optionsRef$current$k2;
|
|
33
33
|
var _optionsRef$current$k, _optionsRef$current;
|
|
@@ -47,4 +47,5 @@ var useReducers = exports.useReducers = function useReducers(usecase, context, s
|
|
|
47
47
|
}
|
|
48
48
|
}));
|
|
49
49
|
}, deps);
|
|
50
|
+
return reducers;
|
|
50
51
|
};
|
|
@@ -1,95 +1,72 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
7
6
|
exports.useStore = void 0;
|
|
8
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
9
|
-
var _useConstantEntityReducers = require("../useConstantEntityReducers");
|
|
10
7
|
var _useContextualItem = require("../useContextualItem");
|
|
11
8
|
var _usecases = require("@mic-rexjs/usecases");
|
|
12
|
-
var _ahooks = require("ahooks");
|
|
13
9
|
var _react = require("react");
|
|
14
10
|
var _Statuses = require("../../enums/Statuses");
|
|
15
|
-
var
|
|
16
|
-
var
|
|
17
|
-
var
|
|
18
|
-
var useStore = exports.useStore = function useStore(context, entity, statuses, options) {
|
|
11
|
+
var _entityRuntimeUseCase = require("../../usecases/entityRuntimeUseCase");
|
|
12
|
+
var _valueUseCase2 = require("../../usecases/valueUseCase");
|
|
13
|
+
var useStore = exports.useStore = function useStore(entityArg, context, statuses, options) {
|
|
19
14
|
var deps = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
|
|
20
|
-
var update = (0, _ahooks.useUpdate)();
|
|
21
|
-
var optionsRef = (0, _ahooks.useLatest)(options);
|
|
22
15
|
var storeRef = (0, _react.useRef)(null);
|
|
23
|
-
var
|
|
16
|
+
var createEntityListRef = (0, _react.useRef)([]);
|
|
24
17
|
var entityRootEnabled = (statuses & _Statuses.Statuses.EntityRootEnabled) === _Statuses.Statuses.EntityRootEnabled;
|
|
25
18
|
var contextValue = (0, _react.useContext)(context);
|
|
26
19
|
var _ref = contextValue || {},
|
|
27
20
|
_ref$store = _ref.store,
|
|
28
21
|
contextStore = _ref$store === void 0 ? null : _ref$store;
|
|
29
|
-
var
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
var initStore = function initStore() {
|
|
23
|
+
var isFunction = typeof entityArg === 'function';
|
|
24
|
+
if (!isFunction) {
|
|
25
|
+
return new _usecases.EntityStore(entityArg);
|
|
26
|
+
}
|
|
27
|
+
var index = 0;
|
|
28
|
+
var currentStore = storeRef.current;
|
|
29
|
+
var _ref2 = currentStore || {},
|
|
30
|
+
currentEntity = _ref2.value;
|
|
31
|
+
var prevCreateEntityList = createEntityListRef.current;
|
|
32
|
+
var store = new _usecases.EntityStore(currentEntity);
|
|
33
|
+
var createEntityList = [];
|
|
34
|
+
var _valueUseCase = (0, _valueUseCase2.valueUseCase)(),
|
|
35
|
+
diffValue = _valueUseCase.diffValue;
|
|
36
|
+
var _createEntityReducers = (0, _usecases.createEntityReducers)(store, _entityRuntimeUseCase.entityRuntimeUseCase, {
|
|
37
|
+
onInit: function onInit(currentValue) {
|
|
38
|
+
return entityArg(currentValue);
|
|
39
|
+
},
|
|
40
|
+
onCreate: function onCreate(newEntity, oldEntity) {
|
|
41
|
+
var prevEntity = prevCreateEntityList[index];
|
|
42
|
+
var diffEntity = diffValue(prevEntity, newEntity);
|
|
43
|
+
createEntityList[index] = newEntity;
|
|
44
|
+
index++;
|
|
45
|
+
return (0, _usecases.createEntity)(oldEntity, diffEntity);
|
|
46
|
+
},
|
|
47
|
+
onGenerate: function onGenerate(entity, result) {
|
|
48
|
+
if (typeof result === 'undefined') {
|
|
49
|
+
return [entity, result];
|
|
50
|
+
}
|
|
51
|
+
var newResult = (0, _usecases.createEntity)(entity, result);
|
|
52
|
+
store.value = newResult;
|
|
53
|
+
return [newResult, result];
|
|
54
|
+
},
|
|
55
|
+
onReturn: function onReturn(result) {
|
|
56
|
+
store.value = result;
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
}),
|
|
60
|
+
initEntity = _createEntityReducers.initEntity;
|
|
61
|
+
initEntity();
|
|
62
|
+
storeRef.current = store;
|
|
63
|
+
createEntityListRef.current = createEntityList;
|
|
64
|
+
return store;
|
|
65
|
+
};
|
|
66
|
+
return (0, _useContextualItem.useContextualItem)(contextStore, statuses, function () {
|
|
32
67
|
if (!entityRootEnabled) {
|
|
33
68
|
return new _usecases.EntityStore(null);
|
|
34
69
|
}
|
|
35
|
-
|
|
36
|
-
var _recordValueDiff = recordValueDiff(deps),
|
|
37
|
-
_recordValueDiff2 = (0, _slicedToArray2["default"])(_recordValueDiff, 2),
|
|
38
|
-
changedDeps = _recordValueDiff2[1];
|
|
39
|
-
var newStore = (0, _initStore.initStore)(entity, current, changedDeps);
|
|
40
|
-
storeRef.current = newStore;
|
|
41
|
-
return newStore;
|
|
70
|
+
return initStore();
|
|
42
71
|
}, deps);
|
|
43
|
-
var value = store.value;
|
|
44
|
-
var prevValueRef = (0, _react.useRef)(value);
|
|
45
|
-
var onTriggerWatchers = (0, _ahooks.useMemoizedFn)(function (newEntity, oldEntity) {
|
|
46
|
-
var watch = optionsRef.current.watch;
|
|
47
|
-
if (!watch) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
(0, _triggerWatchers.triggerWatchers)(watch, newEntity, oldEntity);
|
|
51
|
-
});
|
|
52
|
-
var onEntityChange = (0, _ahooks.useMemoizedFn)(function (newEntity, oldEntity) {
|
|
53
|
-
var onChange = optionsRef.current.onChange;
|
|
54
|
-
prevValueRef.current = newEntity;
|
|
55
|
-
onTriggerWatchers(newEntity, oldEntity);
|
|
56
|
-
onChange === null || onChange === void 0 || onChange(newEntity, oldEntity);
|
|
57
|
-
if (!entityRootEnabled) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
update();
|
|
61
|
-
});
|
|
62
|
-
var onValueChange = (0, _ahooks.useMemoizedFn)(function () {
|
|
63
|
-
if (!entityEnabled) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
var prevValue = prevValueRef.current;
|
|
67
|
-
if (value === prevValue) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
prevValueRef.current = value;
|
|
71
|
-
onTriggerWatchers(value, prevValue);
|
|
72
|
-
});
|
|
73
|
-
/**
|
|
74
|
-
* 需要马上 `watch`,因为 `useEffect` 在子组件内优先执行,
|
|
75
|
-
* 如果使用了 `setEntity` 则无法监控到变化
|
|
76
|
-
*/
|
|
77
|
-
(0, _ahooks.useCreation)(function () {
|
|
78
|
-
if (!entityEnabled) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
store.watch(onEntityChange);
|
|
82
|
-
}, [entityEnabled, store, onEntityChange]);
|
|
83
|
-
/**
|
|
84
|
-
* 在变化时候,组件更新前,使用 `unwatch`
|
|
85
|
-
*/
|
|
86
|
-
(0, _react.useEffect)(function () {
|
|
87
|
-
return function () {
|
|
88
|
-
store.unwatch(onEntityChange);
|
|
89
|
-
};
|
|
90
|
-
}, [entityEnabled, store, onEntityChange]);
|
|
91
|
-
(0, _ahooks.useUpdateEffect)(function () {
|
|
92
|
-
onValueChange();
|
|
93
|
-
}, [value, onValueChange]);
|
|
94
|
-
return store;
|
|
95
72
|
};
|
|
@@ -42,9 +42,9 @@ var useUseCase = exports.useUseCase = function useUseCase() {
|
|
|
42
42
|
var context = (0, _useContext.useContext)(usecase, argumentTypes);
|
|
43
43
|
var entityContext = context;
|
|
44
44
|
var statuses = (0, _useStatuses.useStatuses)(context, argumentTypes);
|
|
45
|
-
var store = (0, _useStore.useStore)(
|
|
46
|
-
var entity = (0, _useEntity.useEntity)(store, unsafeEntity, statuses);
|
|
45
|
+
var store = (0, _useStore.useStore)(unsafeEntity, entityContext, statuses, options, deps);
|
|
47
46
|
var reducers = (0, _useReducers.useReducers)(usecase, context, store, statuses, options, deps);
|
|
47
|
+
var entity = (0, _useEntity.useEntity)(unsafeEntity, store, reducers, statuses, options);
|
|
48
48
|
var Provider = (0, _useProvider.useProvider)(statuses, context, store, reducers);
|
|
49
49
|
return (0, _ahooks.useCreation)(function () {
|
|
50
50
|
if ((statuses & _Statuses.Statuses.EntityEnabled) !== _Statuses.Statuses.EntityEnabled) {
|