@mic-rexjs/usecases-react 2.14.0 → 3.0.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.
- package/dist/index.min.js +1 -1
- package/es/hooks/useFullArguments/types.d.ts +3 -3
- package/es/hooks/useStore/index.js +50 -13
- package/es/hooks/useUseCase/index.test.js +134 -115
- 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/useStore/index.js +50 -14
- package/lib/hooks/useUseCase/index.test.js +158 -123
- 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>>;
|
|
@@ -1,38 +1,74 @@
|
|
|
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
9
|
var _react = require("react");
|
|
13
10
|
var _Statuses = require("../../enums/Statuses");
|
|
14
|
-
var
|
|
15
|
-
var
|
|
11
|
+
var _entityRuntimeUseCase = require("../../usecases/entityRuntimeUseCase");
|
|
12
|
+
var _valueUseCase2 = require("../../usecases/valueUseCase");
|
|
16
13
|
var useStore = exports.useStore = function useStore(entityArg, context, statuses, options) {
|
|
17
14
|
var deps = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
|
|
18
15
|
var storeRef = (0, _react.useRef)(null);
|
|
16
|
+
var createEntityListRef = (0, _react.useRef)([]);
|
|
19
17
|
var entityRootEnabled = (statuses & _Statuses.Statuses.EntityRootEnabled) === _Statuses.Statuses.EntityRootEnabled;
|
|
20
18
|
var contextValue = (0, _react.useContext)(context);
|
|
21
19
|
var _ref = contextValue || {},
|
|
22
20
|
_ref$store = _ref.store,
|
|
23
21
|
contextStore = _ref$store === void 0 ? null : _ref$store;
|
|
24
|
-
var
|
|
25
|
-
|
|
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`,因为可能存在异步,异步更新是需要触发组件更新的
|
|
53
|
+
store.setValue(newResult);
|
|
54
|
+
return [newResult, result];
|
|
55
|
+
},
|
|
56
|
+
onReturn: function onReturn(result) {
|
|
57
|
+
// 不能用 `store.value`,因为可能存在异步,异步更新是需要触发组件更新的
|
|
58
|
+
store.setValue(result);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
}),
|
|
62
|
+
initEntity = _createEntityReducers.initEntity;
|
|
63
|
+
initEntity();
|
|
64
|
+
storeRef.current = store;
|
|
65
|
+
createEntityListRef.current = createEntityList;
|
|
66
|
+
return store;
|
|
67
|
+
};
|
|
26
68
|
return (0, _useContextualItem.useContextualItem)(contextStore, statuses, function () {
|
|
27
69
|
if (!entityRootEnabled) {
|
|
28
70
|
return new _usecases.EntityStore(null);
|
|
29
71
|
}
|
|
30
|
-
|
|
31
|
-
var _recordValueDiff = recordValueDiff(deps),
|
|
32
|
-
_recordValueDiff2 = (0, _slicedToArray2["default"])(_recordValueDiff, 2),
|
|
33
|
-
changedDeps = _recordValueDiff2[1];
|
|
34
|
-
var newStore = (0, _initStore.initStore)(entityArg, current, changedDeps);
|
|
35
|
-
storeRef.current = newStore;
|
|
36
|
-
return newStore;
|
|
72
|
+
return initStore();
|
|
37
73
|
}, deps);
|
|
38
74
|
};
|