@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.
- package/dist/index.min.js +1 -1
- package/es/components/SafeUseCaseFragment/index.js +17 -7
- package/es/components/SafeUseCaseFragment/types.js +1 -1
- package/es/components/UseCaseFragment/index.js +16 -6
- package/es/components/UseCaseFragment/types.js +1 -1
- package/es/entities/matchPropertyFailedResult/types.js +1 -1
- package/es/enums/ArgumentTypes/index.js +3 -3
- package/es/enums/Statuses/index.js +22 -22
- package/es/hooks/useArgumentTypes/index.js +12 -10
- package/es/hooks/useConstant/index.js +7 -4
- package/es/hooks/useConstant/types.js +1 -1
- package/es/hooks/useConstantEntityReducers/index.js +4 -4
- package/es/hooks/useConstantFn/index.js +9 -6
- package/es/hooks/useConstantReducers/index.js +4 -4
- package/es/hooks/useContext/index.js +14 -11
- package/es/hooks/useContext/index.test.js +50 -44
- package/es/hooks/useContextualItem/index.js +27 -25
- package/es/hooks/useContextualItem/types.js +1 -1
- package/es/hooks/useDepsKey/index.js +9 -5
- package/es/hooks/useEntity/index.js +65 -61
- package/es/hooks/useFullArguments/index.js +12 -17
- package/es/hooks/useFullArguments/types.js +1 -1
- package/es/hooks/useIsRenderingRef/index.js +6 -6
- package/es/hooks/useProvider/index.js +21 -15
- package/es/hooks/useProvider/types.js +1 -1
- package/es/hooks/useRuntimeEntity/index.js +18 -16
- package/es/hooks/useStatuses/index.js +19 -19
- package/es/hooks/useUseCase/index.js +78 -53
- package/es/hooks/useUseCase/index.test.js +2400 -1755
- package/es/hooks/useUseCase/types.js +1 -1
- package/es/index.js +1 -1
- package/es/methods/triggerWatchers/index.js +12 -7
- package/es/types.js +1 -1
- package/es/usecases/contextUseCase/index.js +70 -65
- package/es/usecases/contextUseCase/types.js +1 -1
- package/es/usecases/methodUseCase/index.js +69 -52
- package/es/usecases/methodUseCase/types.js +1 -1
- package/es/usecases/valueUseCase/index.js +175 -167
- package/es/usecases/valueUseCase/types.js +1 -1
- package/lib/components/UseCaseFragment/index.js +1 -1
- package/lib/hooks/useUseCase/index.js +4 -3
- package/lib/hooks/useUseCase/index.test.js +48 -49
- package/lib/methods/triggerWatchers/index.js +9 -7
- package/lib/usecases/valueUseCase/index.js +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
package/es/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { UseCaseFragment } from './components/UseCaseFragment';
|
|
2
|
-
export { useUseCase } from './hooks/useUseCase';
|
|
2
|
+
export { useUseCase } from './hooks/useUseCase';
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
1
2
|
import { valueUseCase } from '../../usecases/valueUseCase';
|
|
2
3
|
export const triggerWatchers = (watch, newEntity, oldEntity) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
const _valueUseCase = valueUseCase(),
|
|
5
|
+
matchProperty = _valueUseCase.matchProperty;
|
|
6
|
+
for (const _ref of Object.entries(watch)) {
|
|
7
|
+
var _ref2 = _slicedToArray(_ref, 2);
|
|
8
|
+
const fieldPath = _ref2[0];
|
|
9
|
+
const watcher = _ref2[1];
|
|
10
|
+
matchProperty(oldEntity, newEntity, fieldPath, result => {
|
|
11
|
+
watcher(result);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
package/es/types.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
@@ -3,71 +3,76 @@ import { createContext } from 'react';
|
|
|
3
3
|
import { ArgumentTypes } from '../../enums/ArgumentTypes';
|
|
4
4
|
import { Statuses } from '../../enums/Statuses';
|
|
5
5
|
export const contextUseCase = createUseCase(() => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return {
|
|
12
|
-
store,
|
|
13
|
-
reducers,
|
|
14
|
-
statuses,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
return { reducers, statuses };
|
|
18
|
-
};
|
|
19
|
-
const getUseCaseContext = (usecase) => {
|
|
20
|
-
if (!referenceMap.has(usecase)) {
|
|
21
|
-
return defaultUseCaseContext;
|
|
22
|
-
}
|
|
23
|
-
const { value } = referenceMap.get(usecase);
|
|
24
|
-
return value;
|
|
25
|
-
};
|
|
26
|
-
const registerUseCase = (usecase, argumentTypes) => {
|
|
27
|
-
if (referenceMap.has(usecase)) {
|
|
28
|
-
const reference = referenceMap.get(usecase);
|
|
29
|
-
const { value, times } = reference;
|
|
30
|
-
referenceMap.set(usecase, {
|
|
31
|
-
value,
|
|
32
|
-
times: times + 1,
|
|
33
|
-
});
|
|
34
|
-
return value;
|
|
35
|
-
}
|
|
36
|
-
if ((argumentTypes & ArgumentTypes.Entity) !== ArgumentTypes.Entity) {
|
|
37
|
-
return defaultUseCaseContext;
|
|
38
|
-
}
|
|
39
|
-
const context = createContext(null);
|
|
40
|
-
referenceMap.set(usecase, {
|
|
41
|
-
value: context,
|
|
42
|
-
times: 1,
|
|
43
|
-
});
|
|
44
|
-
return context;
|
|
45
|
-
};
|
|
46
|
-
const unregisterUseCase = (usecase, argumentTypes) => {
|
|
47
|
-
if ((argumentTypes & ArgumentTypes.Entity) !== ArgumentTypes.Entity) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
if (!referenceMap.has(usecase)) {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
const reference = referenceMap.get(usecase);
|
|
54
|
-
const { value, times } = reference;
|
|
55
|
-
if (times === 1) {
|
|
56
|
-
referenceMap.delete(usecase);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
referenceMap.set(usecase, {
|
|
60
|
-
value,
|
|
61
|
-
times: times - 1,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
return true;
|
|
65
|
-
};
|
|
6
|
+
const referenceMap = new Map();
|
|
7
|
+
const defaultUseCaseContext = createContext(null);
|
|
8
|
+
return () => {
|
|
9
|
+
const createContextValue = (store, reducers, statuses) => {
|
|
10
|
+
if ((statuses & Statuses.EntityEnabled) === Statuses.EntityEnabled) {
|
|
66
11
|
return {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
unregisterUseCase,
|
|
12
|
+
store,
|
|
13
|
+
reducers,
|
|
14
|
+
statuses
|
|
71
15
|
};
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
reducers,
|
|
19
|
+
statuses
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
const getUseCaseContext = usecase => {
|
|
23
|
+
if (!referenceMap.has(usecase)) {
|
|
24
|
+
return defaultUseCaseContext;
|
|
25
|
+
}
|
|
26
|
+
const _referenceMap$get = referenceMap.get(usecase),
|
|
27
|
+
value = _referenceMap$get.value;
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
const registerUseCase = (usecase, argumentTypes) => {
|
|
31
|
+
if (referenceMap.has(usecase)) {
|
|
32
|
+
const reference = referenceMap.get(usecase);
|
|
33
|
+
const value = reference.value,
|
|
34
|
+
times = reference.times;
|
|
35
|
+
referenceMap.set(usecase, {
|
|
36
|
+
value,
|
|
37
|
+
times: times + 1
|
|
38
|
+
});
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
if ((argumentTypes & ArgumentTypes.Entity) !== ArgumentTypes.Entity) {
|
|
42
|
+
return defaultUseCaseContext;
|
|
43
|
+
}
|
|
44
|
+
const context = createContext(null);
|
|
45
|
+
referenceMap.set(usecase, {
|
|
46
|
+
value: context,
|
|
47
|
+
times: 1
|
|
48
|
+
});
|
|
49
|
+
return context;
|
|
50
|
+
};
|
|
51
|
+
const unregisterUseCase = (usecase, argumentTypes) => {
|
|
52
|
+
if ((argumentTypes & ArgumentTypes.Entity) !== ArgumentTypes.Entity) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
if (!referenceMap.has(usecase)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const reference = referenceMap.get(usecase);
|
|
59
|
+
const value = reference.value,
|
|
60
|
+
times = reference.times;
|
|
61
|
+
if (times === 1) {
|
|
62
|
+
referenceMap.delete(usecase);
|
|
63
|
+
} else {
|
|
64
|
+
referenceMap.set(usecase, {
|
|
65
|
+
value,
|
|
66
|
+
times: times - 1
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
createContextValue,
|
|
73
|
+
getUseCaseContext,
|
|
74
|
+
registerUseCase,
|
|
75
|
+
unregisterUseCase
|
|
72
76
|
};
|
|
73
|
-
}
|
|
77
|
+
};
|
|
78
|
+
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
@@ -1,55 +1,72 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
1
2
|
import { valueUseCase } from '../valueUseCase';
|
|
2
3
|
export const methodUseCase = () => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
4
|
+
const _valueUseCase = valueUseCase(),
|
|
5
|
+
isValueMatched = _valueUseCase.isValueMatched;
|
|
6
|
+
const cacheCall = (factory, options) => {
|
|
7
|
+
let cache;
|
|
8
|
+
const _ref = options || {},
|
|
9
|
+
_ref$onShouldCache = _ref.onShouldCache,
|
|
10
|
+
onShouldCache = _ref$onShouldCache === void 0 ? () => {
|
|
11
|
+
return true;
|
|
12
|
+
} : _ref$onShouldCache,
|
|
13
|
+
_ref$onCompare = _ref.onCompare,
|
|
14
|
+
onCompare = _ref$onCompare === void 0 ? (newArgs, oldArgs) => {
|
|
15
|
+
return isValueMatched(newArgs, oldArgs);
|
|
16
|
+
} : _ref$onCompare;
|
|
17
|
+
return function () {
|
|
18
|
+
const cacheable = onShouldCache();
|
|
19
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
20
|
+
args[_key] = arguments[_key];
|
|
21
|
+
}
|
|
22
|
+
if (cache && cacheable) {
|
|
23
|
+
const _cache = cache,
|
|
24
|
+
parameters = _cache.parameters,
|
|
25
|
+
returnValue = _cache.returnValue;
|
|
26
|
+
if (onCompare(parameters, args)) {
|
|
27
|
+
return returnValue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const returnValue = factory(...args);
|
|
31
|
+
if (cacheable) {
|
|
32
|
+
cache = {
|
|
33
|
+
parameters: args,
|
|
34
|
+
returnValue
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return returnValue;
|
|
28
38
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
|
|
39
|
+
};
|
|
40
|
+
const cacheCalls = (sourceFactories, options) => {
|
|
41
|
+
const cachedFactories = {};
|
|
42
|
+
Object.entries(sourceFactories).forEach(_ref2 => {
|
|
43
|
+
let _ref3 = _slicedToArray(_ref2, 2),
|
|
44
|
+
key = _ref3[0],
|
|
45
|
+
factory = _ref3[1];
|
|
46
|
+
cachedFactories[key] = cacheCall(factory, options);
|
|
47
|
+
});
|
|
48
|
+
return cachedFactories;
|
|
49
|
+
};
|
|
50
|
+
const captureCalls = (obj, callback) => {
|
|
51
|
+
const newObj = {};
|
|
52
|
+
Object.keys(obj).forEach(key => {
|
|
53
|
+
const value = obj[key];
|
|
54
|
+
if (typeof value === 'function') {
|
|
55
|
+
newObj[key] = function () {
|
|
56
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
57
|
+
args[_key2] = arguments[_key2];
|
|
58
|
+
}
|
|
59
|
+
return callback(key, args);
|
|
60
|
+
};
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
newObj[key] = value;
|
|
64
|
+
});
|
|
65
|
+
return newObj;
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
cacheCall,
|
|
69
|
+
cacheCalls,
|
|
70
|
+
captureCalls
|
|
71
|
+
};
|
|
72
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
@@ -1,170 +1,178 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
1
2
|
import { createUseCase, entityUseCase, utilsUseCase } from '@mic-rexjs/usecases';
|
|
2
3
|
export const valueUseCase = createUseCase(() => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const isValueChanged = (entity, value) => {
|
|
14
|
-
return entity !== value;
|
|
15
|
-
};
|
|
16
|
-
const isValueMatched = (entity, value, onMatchFailed) => {
|
|
17
|
-
if (entity === value) {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
const isObject1 = typeof entity === 'object' && entity !== null;
|
|
21
|
-
const isObject2 = typeof value === 'object' && value !== null;
|
|
22
|
-
if (!isObject1 || !isObject2) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
let allMatched = true;
|
|
26
|
-
const keys1 = Object.keys(entity);
|
|
27
|
-
const keys2 = Object.keys(value);
|
|
28
|
-
const { length: length1 } = keys1;
|
|
29
|
-
const keys = [...keys1, ...keys2];
|
|
30
|
-
const hasCallback = typeof onMatchFailed === 'function';
|
|
31
|
-
for (let i = 0, { length } = keys; i < length; i++) {
|
|
32
|
-
const key = keys[i];
|
|
33
|
-
const isFromKeys2 = i >= length1;
|
|
34
|
-
const isInKey1 = keys1.includes(key);
|
|
35
|
-
const isUsed = isFromKeys2 && isInKey1;
|
|
36
|
-
if (isUsed) {
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
const hasField1 = Object.hasOwn(entity, key);
|
|
40
|
-
const hasField2 = Object.hasOwn(value, key);
|
|
41
|
-
const onlyHasField1 = hasField1 && !hasField2;
|
|
42
|
-
const onlyHasField2 = !hasField1 && hasField2;
|
|
43
|
-
const onlyHasOneField = onlyHasField1 || onlyHasField2;
|
|
44
|
-
const value1 = entity[key];
|
|
45
|
-
const value2 = value[key];
|
|
46
|
-
const matched = value1 === value2;
|
|
47
|
-
if (!onlyHasOneField && matched) {
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
allMatched = false;
|
|
51
|
-
if (hasCallback) {
|
|
52
|
-
onMatchFailed(key, value2, value1);
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
break;
|
|
56
|
-
}
|
|
57
|
-
return allMatched;
|
|
58
|
-
};
|
|
59
|
-
const matchProperty = (entity, value, fieldPath, onFailed) => {
|
|
60
|
-
let allMatched = true;
|
|
61
|
-
let currentFieldPath = fieldPath;
|
|
62
|
-
const list = [
|
|
63
|
-
{
|
|
64
|
-
fieldPaths: [],
|
|
65
|
-
newEntity: value,
|
|
66
|
-
newValue: value,
|
|
67
|
-
oldEntity: entity,
|
|
68
|
-
oldValue: entity,
|
|
69
|
-
},
|
|
70
|
-
];
|
|
71
|
-
do {
|
|
72
|
-
const { oldValue, newValue, fieldPaths } = list.shift();
|
|
73
|
-
const [, key = currentFieldPath, subFieldPath = ''] = currentFieldPath.match(/^([^.]+)\.(.+)$/) || [];
|
|
74
|
-
const hasOldField = hasField(oldValue, key);
|
|
75
|
-
const hasNewField = hasField(newValue, key);
|
|
76
|
-
if (hasOldField || hasNewField) {
|
|
77
|
-
const subOldValue = hasOldField ? oldValue[key] : void 0;
|
|
78
|
-
const subNewValue = hasNewField ? newValue[key] : void 0;
|
|
79
|
-
const result = {
|
|
80
|
-
fieldPaths: [...fieldPaths, key],
|
|
81
|
-
newEntity: value,
|
|
82
|
-
newValue: subNewValue,
|
|
83
|
-
oldEntity: entity,
|
|
84
|
-
oldValue: subOldValue,
|
|
85
|
-
};
|
|
86
|
-
if (subFieldPath) {
|
|
87
|
-
currentFieldPath = subFieldPath;
|
|
88
|
-
list.push(result);
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
if (subOldValue === subNewValue) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
allMatched = false;
|
|
95
|
-
onFailed?.(result);
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (/^\d+^/.test(key)) {
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
const oldArray = Array.isArray(oldValue) ? oldValue : [];
|
|
102
|
-
const newArray = Array.isArray(newValue) ? newValue : [];
|
|
103
|
-
const { length: oldLength } = oldArray;
|
|
104
|
-
const { length: newLength } = newArray;
|
|
105
|
-
const length = Math.max(oldLength, newLength);
|
|
106
|
-
for (let i = 0; i < length; i++) {
|
|
107
|
-
list.push({
|
|
108
|
-
fieldPaths: [...fieldPaths, `${i}`],
|
|
109
|
-
newEntity: value,
|
|
110
|
-
newValue: newArray[i],
|
|
111
|
-
oldEntity: entity,
|
|
112
|
-
oldValue: oldArray[i],
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
} while (list.length > 0);
|
|
116
|
-
return allMatched;
|
|
117
|
-
};
|
|
118
|
-
const recordValue = function* (entity, value) {
|
|
119
|
-
const changed = isValueChanged(entity, value);
|
|
120
|
-
if (!changed) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
yield value;
|
|
124
|
-
return true;
|
|
125
|
-
};
|
|
126
|
-
const recordValueDiff = function* (entity, value) {
|
|
127
|
-
const isArray1 = Array.isArray(entity);
|
|
128
|
-
const isArray2 = Array.isArray(value);
|
|
129
|
-
const isArray = isArray1 && isArray2;
|
|
130
|
-
const result = (isArray ? [] : {});
|
|
131
|
-
if (isArray) {
|
|
132
|
-
const { length: length1 } = entity;
|
|
133
|
-
const { length: length2 } = value;
|
|
134
|
-
result.length = Math.max(length1, length2);
|
|
135
|
-
}
|
|
136
|
-
const matched = isValueMatched(entity, value, (key, newValue) => {
|
|
137
|
-
result[key] = newValue;
|
|
138
|
-
});
|
|
139
|
-
if (!matched) {
|
|
140
|
-
yield value;
|
|
141
|
-
}
|
|
142
|
-
return result;
|
|
143
|
-
};
|
|
144
|
-
const recordValueMatch = function* (entity, value) {
|
|
145
|
-
const matched = isValueMatched(entity, value);
|
|
146
|
-
if (matched) {
|
|
147
|
-
return true;
|
|
148
|
-
}
|
|
149
|
-
yield value;
|
|
150
|
-
return false;
|
|
151
|
-
};
|
|
152
|
-
const recordValueMatchWith = function* (entity, value, key) {
|
|
153
|
-
const matched = yield* recordValueMatch(entity, value);
|
|
154
|
-
if (!matched) {
|
|
155
|
-
prevKeyIndex = key ?? createKey();
|
|
156
|
-
}
|
|
157
|
-
return prevKeyIndex;
|
|
158
|
-
};
|
|
159
|
-
return {
|
|
160
|
-
...entityReducers,
|
|
161
|
-
isValueChanged,
|
|
162
|
-
isValueMatched,
|
|
163
|
-
matchProperty,
|
|
164
|
-
recordValue,
|
|
165
|
-
recordValueDiff,
|
|
166
|
-
recordValueMatch,
|
|
167
|
-
recordValueMatchWith,
|
|
168
|
-
};
|
|
4
|
+
return () => {
|
|
5
|
+
const _utilsUseCase = utilsUseCase(),
|
|
6
|
+
createKey = _utilsUseCase.createKey;
|
|
7
|
+
let prevKeyIndex = createKey();
|
|
8
|
+
const entityReducers = entityUseCase();
|
|
9
|
+
const hasField = (entity, field) => {
|
|
10
|
+
if (typeof entity !== 'object' || entity === null) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return Object.hasOwn(entity, field);
|
|
169
14
|
};
|
|
170
|
-
|
|
15
|
+
const isValueChanged = (entity, value) => {
|
|
16
|
+
return entity !== value;
|
|
17
|
+
};
|
|
18
|
+
const isValueMatched = (entity, value, onMatchFailed) => {
|
|
19
|
+
if (entity === value) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
const isObject1 = typeof entity === 'object' && entity !== null;
|
|
23
|
+
const isObject2 = typeof value === 'object' && value !== null;
|
|
24
|
+
if (!isObject1 || !isObject2) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
let allMatched = true;
|
|
28
|
+
const keys1 = Object.keys(entity);
|
|
29
|
+
const keys2 = Object.keys(value);
|
|
30
|
+
const length1 = keys1.length;
|
|
31
|
+
const keys = [...keys1, ...keys2];
|
|
32
|
+
const hasCallback = typeof onMatchFailed === 'function';
|
|
33
|
+
for (let i = 0, length = keys.length; i < length; i++) {
|
|
34
|
+
const key = keys[i];
|
|
35
|
+
const isFromKeys2 = i >= length1;
|
|
36
|
+
const isInKey1 = keys1.includes(key);
|
|
37
|
+
const isUsed = isFromKeys2 && isInKey1;
|
|
38
|
+
if (isUsed) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const hasField1 = Object.hasOwn(entity, key);
|
|
42
|
+
const hasField2 = Object.hasOwn(value, key);
|
|
43
|
+
const onlyHasField1 = hasField1 && !hasField2;
|
|
44
|
+
const onlyHasField2 = !hasField1 && hasField2;
|
|
45
|
+
const onlyHasOneField = onlyHasField1 || onlyHasField2;
|
|
46
|
+
const value1 = entity[key];
|
|
47
|
+
const value2 = value[key];
|
|
48
|
+
const matched = value1 === value2;
|
|
49
|
+
if (!onlyHasOneField && matched) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
allMatched = false;
|
|
53
|
+
if (hasCallback) {
|
|
54
|
+
onMatchFailed(key, value2, value1);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
return allMatched;
|
|
60
|
+
};
|
|
61
|
+
const matchProperty = (entity, value, fieldPath, onFailed) => {
|
|
62
|
+
let allMatched = true;
|
|
63
|
+
let currentFieldPath = fieldPath;
|
|
64
|
+
const list = [{
|
|
65
|
+
fieldPaths: [],
|
|
66
|
+
newEntity: value,
|
|
67
|
+
newValue: value,
|
|
68
|
+
oldEntity: entity,
|
|
69
|
+
oldValue: entity
|
|
70
|
+
}];
|
|
71
|
+
do {
|
|
72
|
+
const _list$shift = list.shift(),
|
|
73
|
+
oldValue = _list$shift.oldValue,
|
|
74
|
+
newValue = _list$shift.newValue,
|
|
75
|
+
fieldPaths = _list$shift.fieldPaths;
|
|
76
|
+
const _ref = currentFieldPath.match(/^([^.]+)\.(.+)$/) || [],
|
|
77
|
+
_ref2 = _slicedToArray(_ref, 3),
|
|
78
|
+
_ref2$ = _ref2[1],
|
|
79
|
+
key = _ref2$ === void 0 ? currentFieldPath : _ref2$,
|
|
80
|
+
_ref2$2 = _ref2[2],
|
|
81
|
+
subFieldPath = _ref2$2 === void 0 ? '' : _ref2$2;
|
|
82
|
+
const hasOldField = hasField(oldValue, key);
|
|
83
|
+
const hasNewField = hasField(newValue, key);
|
|
84
|
+
if (hasOldField || hasNewField) {
|
|
85
|
+
const subOldValue = hasOldField ? oldValue[key] : void 0;
|
|
86
|
+
const subNewValue = hasNewField ? newValue[key] : void 0;
|
|
87
|
+
const result = {
|
|
88
|
+
fieldPaths: [...fieldPaths, key],
|
|
89
|
+
newEntity: value,
|
|
90
|
+
newValue: subNewValue,
|
|
91
|
+
oldEntity: entity,
|
|
92
|
+
oldValue: subOldValue
|
|
93
|
+
};
|
|
94
|
+
if (subFieldPath) {
|
|
95
|
+
currentFieldPath = subFieldPath;
|
|
96
|
+
list.push(result);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (subOldValue === subNewValue) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
allMatched = false;
|
|
103
|
+
onFailed === null || onFailed === void 0 || onFailed(result);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (/^\d+^/.test(key)) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const oldArray = Array.isArray(oldValue) ? oldValue : [];
|
|
110
|
+
const newArray = Array.isArray(newValue) ? newValue : [];
|
|
111
|
+
const oldLength = oldArray.length;
|
|
112
|
+
const newLength = newArray.length;
|
|
113
|
+
const length = Math.max(oldLength, newLength);
|
|
114
|
+
for (let i = 0; i < length; i++) {
|
|
115
|
+
list.push({
|
|
116
|
+
fieldPaths: [...fieldPaths, `${i}`],
|
|
117
|
+
newEntity: value,
|
|
118
|
+
newValue: newArray[i],
|
|
119
|
+
oldEntity: entity,
|
|
120
|
+
oldValue: oldArray[i]
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
} while (list.length > 0);
|
|
124
|
+
return allMatched;
|
|
125
|
+
};
|
|
126
|
+
const recordValue = function* (entity, value) {
|
|
127
|
+
const changed = isValueChanged(entity, value);
|
|
128
|
+
if (!changed) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
yield value;
|
|
132
|
+
return true;
|
|
133
|
+
};
|
|
134
|
+
const recordValueDiff = function* (entity, value) {
|
|
135
|
+
const isArray1 = Array.isArray(entity);
|
|
136
|
+
const isArray2 = Array.isArray(value);
|
|
137
|
+
const isArray = isArray1 && isArray2;
|
|
138
|
+
const result = isArray ? [] : {};
|
|
139
|
+
if (isArray) {
|
|
140
|
+
const length1 = entity.length;
|
|
141
|
+
const length2 = value.length;
|
|
142
|
+
result.length = Math.max(length1, length2);
|
|
143
|
+
}
|
|
144
|
+
const matched = isValueMatched(entity, value, (key, newValue) => {
|
|
145
|
+
result[key] = newValue;
|
|
146
|
+
});
|
|
147
|
+
if (!matched) {
|
|
148
|
+
yield value;
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
};
|
|
152
|
+
const recordValueMatch = function* (entity, value) {
|
|
153
|
+
const matched = isValueMatched(entity, value);
|
|
154
|
+
if (matched) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
yield value;
|
|
158
|
+
return false;
|
|
159
|
+
};
|
|
160
|
+
const recordValueMatchWith = function* (entity, value, key) {
|
|
161
|
+
const matched = yield* recordValueMatch(entity, value);
|
|
162
|
+
if (!matched) {
|
|
163
|
+
prevKeyIndex = key !== null && key !== void 0 ? key : createKey();
|
|
164
|
+
}
|
|
165
|
+
return prevKeyIndex;
|
|
166
|
+
};
|
|
167
|
+
return {
|
|
168
|
+
...entityReducers,
|
|
169
|
+
isValueChanged,
|
|
170
|
+
isValueMatched,
|
|
171
|
+
matchProperty,
|
|
172
|
+
recordValue,
|
|
173
|
+
recordValueDiff,
|
|
174
|
+
recordValueMatch,
|
|
175
|
+
recordValueMatchWith
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
|
@@ -12,9 +12,9 @@ var _useUseCase2 = require("../../hooks/useUseCase");
|
|
|
12
12
|
var _SafeUseCaseFragment = require("../SafeUseCaseFragment");
|
|
13
13
|
var _react = require("react");
|
|
14
14
|
var _contextUseCase = require("../../usecases/contextUseCase");
|
|
15
|
-
var _excluded = ["usecase"];
|
|
16
15
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
17
16
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
17
|
+
var _excluded = ["usecase"];
|
|
18
18
|
var UseCaseFragment = exports.UseCaseFragment = function UseCaseFragment(_ref) {
|
|
19
19
|
var usecase = _ref.usecase,
|
|
20
20
|
props = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
|