@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
|
@@ -9,7 +9,7 @@ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/sli
|
|
|
9
9
|
var _valueUseCase2 = require("../valueUseCase");
|
|
10
10
|
var methodUseCase = exports.methodUseCase = function methodUseCase() {
|
|
11
11
|
var _valueUseCase = (0, _valueUseCase2.valueUseCase)(),
|
|
12
|
-
|
|
12
|
+
matchValue = _valueUseCase.matchValue;
|
|
13
13
|
var cacheCall = function cacheCall(factory, options) {
|
|
14
14
|
var cache;
|
|
15
15
|
var _ref = options || {},
|
|
@@ -19,7 +19,7 @@ var methodUseCase = exports.methodUseCase = function methodUseCase() {
|
|
|
19
19
|
} : _ref$onShouldCache,
|
|
20
20
|
_ref$onCompare = _ref.onCompare,
|
|
21
21
|
onCompare = _ref$onCompare === void 0 ? function (newArgs, oldArgs) {
|
|
22
|
-
return
|
|
22
|
+
return matchValue(newArgs, oldArgs);
|
|
23
23
|
} : _ref$onCompare;
|
|
24
24
|
return function () {
|
|
25
25
|
var cacheable = onShouldCache();
|
|
@@ -28,21 +28,21 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
28
28
|
var isValueChanged = function isValueChanged(entity, value) {
|
|
29
29
|
return entity !== value;
|
|
30
30
|
};
|
|
31
|
-
var
|
|
31
|
+
var matchValue = function matchValue(entity, value, onMatchPropertyFailed) {
|
|
32
32
|
if (entity === value) {
|
|
33
33
|
return true;
|
|
34
34
|
}
|
|
35
35
|
var isObject1 = (0, _typeof2["default"])(entity) === 'object' && entity !== null;
|
|
36
36
|
var isObject2 = (0, _typeof2["default"])(value) === 'object' && value !== null;
|
|
37
|
-
|
|
37
|
+
var keys1 = isObject1 ? Object.keys(entity) : [];
|
|
38
|
+
var keys2 = isObject2 ? Object.keys(value) : [];
|
|
39
|
+
var hasCallback = typeof onMatchPropertyFailed === 'function';
|
|
40
|
+
if (!isObject1 && !isObject2) {
|
|
38
41
|
return false;
|
|
39
42
|
}
|
|
40
43
|
var allMatched = true;
|
|
41
|
-
var keys1 = Object.keys(entity);
|
|
42
|
-
var keys2 = Object.keys(value);
|
|
43
44
|
var length1 = keys1.length;
|
|
44
|
-
var keys = [].concat(keys1, keys2);
|
|
45
|
-
var hasCallback = typeof onMatchFailed === 'function';
|
|
45
|
+
var keys = [].concat((0, _toConsumableArray2["default"])(keys1), (0, _toConsumableArray2["default"])(keys2));
|
|
46
46
|
for (var i = 0, length = keys.length; i < length; i++) {
|
|
47
47
|
var key = keys[i];
|
|
48
48
|
var isFromKeys2 = i >= length1;
|
|
@@ -51,20 +51,20 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
51
51
|
if (isUsed) {
|
|
52
52
|
continue;
|
|
53
53
|
}
|
|
54
|
-
var hasField1 =
|
|
55
|
-
var hasField2 =
|
|
54
|
+
var hasField1 = hasField(entity, key);
|
|
55
|
+
var hasField2 = hasField(value, key);
|
|
56
56
|
var onlyHasField1 = hasField1 && !hasField2;
|
|
57
57
|
var onlyHasField2 = !hasField1 && hasField2;
|
|
58
58
|
var onlyHasOneField = onlyHasField1 || onlyHasField2;
|
|
59
|
-
var value1 = entity[key];
|
|
60
|
-
var value2 = value[key];
|
|
59
|
+
var value1 = hasField1 ? entity[key] : void 0;
|
|
60
|
+
var value2 = hasField2 ? value[key] : void 0;
|
|
61
61
|
var matched = value1 === value2;
|
|
62
62
|
if (!onlyHasOneField && matched) {
|
|
63
63
|
continue;
|
|
64
64
|
}
|
|
65
65
|
allMatched = false;
|
|
66
66
|
if (hasCallback) {
|
|
67
|
-
|
|
67
|
+
onMatchPropertyFailed(key, value2, value1);
|
|
68
68
|
continue;
|
|
69
69
|
}
|
|
70
70
|
break;
|
|
@@ -140,6 +140,22 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
140
140
|
} while (list.length > 0);
|
|
141
141
|
return allMatched;
|
|
142
142
|
};
|
|
143
|
+
var diffValue = function diffValue(entity, value, onMatchPropertyFailed) {
|
|
144
|
+
var isArray1 = Array.isArray(entity);
|
|
145
|
+
var isArray2 = Array.isArray(value);
|
|
146
|
+
var isArray = isArray1 && isArray2;
|
|
147
|
+
var result = isArray ? [] : {};
|
|
148
|
+
if (isArray) {
|
|
149
|
+
var length1 = entity.length;
|
|
150
|
+
var length2 = value.length;
|
|
151
|
+
result.length = Math.max(length1, length2);
|
|
152
|
+
}
|
|
153
|
+
matchValue(entity, value, function (key, newSubValue, oldSubValue) {
|
|
154
|
+
result[key] = newSubValue;
|
|
155
|
+
onMatchPropertyFailed === null || onMatchPropertyFailed === void 0 || onMatchPropertyFailed(key, newSubValue, oldSubValue);
|
|
156
|
+
});
|
|
157
|
+
return result;
|
|
158
|
+
};
|
|
143
159
|
var recordValue = /*#__PURE__*/_regenerator["default"].mark(function recordValue(entity, value) {
|
|
144
160
|
var changed;
|
|
145
161
|
return _regenerator["default"].wrap(function (_context) {
|
|
@@ -163,21 +179,13 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
163
179
|
}, recordValue);
|
|
164
180
|
});
|
|
165
181
|
var recordValueDiff = /*#__PURE__*/_regenerator["default"].mark(function recordValueDiff(entity, value) {
|
|
166
|
-
var
|
|
182
|
+
var matched, result;
|
|
167
183
|
return _regenerator["default"].wrap(function (_context2) {
|
|
168
184
|
while (1) switch (_context2.prev = _context2.next) {
|
|
169
185
|
case 0:
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
result = isArray ? [] : {};
|
|
174
|
-
if (isArray) {
|
|
175
|
-
length1 = entity.length;
|
|
176
|
-
length2 = value.length;
|
|
177
|
-
result.length = Math.max(length1, length2);
|
|
178
|
-
}
|
|
179
|
-
matched = isValueMatched(entity, value, function (key, newValue) {
|
|
180
|
-
result[key] = newValue;
|
|
186
|
+
matched = true;
|
|
187
|
+
result = diffValue(entity, value, function () {
|
|
188
|
+
matched = false;
|
|
181
189
|
});
|
|
182
190
|
if (matched) {
|
|
183
191
|
_context2.next = 1;
|
|
@@ -198,7 +206,7 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
198
206
|
return _regenerator["default"].wrap(function (_context3) {
|
|
199
207
|
while (1) switch (_context3.prev = _context3.next) {
|
|
200
208
|
case 0:
|
|
201
|
-
matched =
|
|
209
|
+
matched = matchValue(entity, value);
|
|
202
210
|
if (!matched) {
|
|
203
211
|
_context3.next = 1;
|
|
204
212
|
break;
|
|
@@ -234,8 +242,9 @@ var valueUseCase = exports.valueUseCase = (0, _usecases.createUseCase)(function
|
|
|
234
242
|
}, recordValueMatchWith);
|
|
235
243
|
});
|
|
236
244
|
return _objectSpread(_objectSpread({}, entityReducers), {}, {
|
|
245
|
+
diffValue: diffValue,
|
|
237
246
|
isValueChanged: isValueChanged,
|
|
238
|
-
|
|
247
|
+
matchValue: matchValue,
|
|
239
248
|
matchProperty: matchProperty,
|
|
240
249
|
recordValue: recordValue,
|
|
241
250
|
recordValueDiff: recordValueDiff,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mic-rexjs/usecases-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "React-based solution for use usecases of Clean Architecture",
|
|
5
5
|
"private": false,
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@mic-rexjs/usecases": "^
|
|
38
|
+
"@mic-rexjs/usecases": "^4.01",
|
|
39
39
|
"react": ">=16.8.0"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"@eslint/js": "^10.0.1",
|
|
55
55
|
"@ianvs/prettier-plugin-sort-imports": "^4.7.1",
|
|
56
56
|
"@jest/globals": "^30.3.0",
|
|
57
|
-
"@mic-rexjs/usecases": "^
|
|
57
|
+
"@mic-rexjs/usecases": "^4.0.1",
|
|
58
58
|
"@testing-library/dom": "^10.4.1",
|
|
59
59
|
"@testing-library/react": "^16.3.2",
|
|
60
60
|
"@types/node": "^25.6.0",
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { EntityStore } from '@mic-rexjs/usecases';
|
|
2
|
-
import { Dependencies, EntityInitializer } from '../../types';
|
|
3
|
-
export declare const initStore: <T, TDependencies extends Dependencies = Dependencies>(entity: T | EntityInitializer<T>, prevStore: EntityStore<T> | null, changedDeps: Partial<TDependencies>) => EntityStore<T>;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { createEntityReducers, EntityStore } from '@mic-rexjs/usecases';
|
|
2
|
-
import { entityRuntimeUseCase } from '../../usecases/entityRuntimeUseCase';
|
|
3
|
-
export const initStore = (entity, prevStore, changedDeps) => {
|
|
4
|
-
const isFunction = typeof entity === 'function';
|
|
5
|
-
if (!isFunction) {
|
|
6
|
-
return new EntityStore(entity);
|
|
7
|
-
}
|
|
8
|
-
const _ref = prevStore || {},
|
|
9
|
-
value = _ref.value;
|
|
10
|
-
const store = new EntityStore(value);
|
|
11
|
-
const _createEntityReducers = createEntityReducers(store, entityRuntimeUseCase, {
|
|
12
|
-
onInit(currentValue) {
|
|
13
|
-
return entity(currentValue, changedDeps);
|
|
14
|
-
},
|
|
15
|
-
onReturn(result) {
|
|
16
|
-
store.setValue(result);
|
|
17
|
-
return result;
|
|
18
|
-
}
|
|
19
|
-
}),
|
|
20
|
-
initEntity = _createEntityReducers.initEntity;
|
|
21
|
-
initEntity();
|
|
22
|
-
return store;
|
|
23
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.initStore = void 0;
|
|
7
|
-
var _usecases = require("@mic-rexjs/usecases");
|
|
8
|
-
var _entityRuntimeUseCase = require("../../usecases/entityRuntimeUseCase");
|
|
9
|
-
var initStore = exports.initStore = function initStore(entity, prevStore, changedDeps) {
|
|
10
|
-
var isFunction = typeof entity === 'function';
|
|
11
|
-
if (!isFunction) {
|
|
12
|
-
return new _usecases.EntityStore(entity);
|
|
13
|
-
}
|
|
14
|
-
var _ref = prevStore || {},
|
|
15
|
-
value = _ref.value;
|
|
16
|
-
var store = new _usecases.EntityStore(value);
|
|
17
|
-
var _createEntityReducers = (0, _usecases.createEntityReducers)(store, _entityRuntimeUseCase.entityRuntimeUseCase, {
|
|
18
|
-
onInit: function onInit(currentValue) {
|
|
19
|
-
return entity(currentValue, changedDeps);
|
|
20
|
-
},
|
|
21
|
-
onReturn: function onReturn(result) {
|
|
22
|
-
store.setValue(result);
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
}),
|
|
26
|
-
initEntity = _createEntityReducers.initEntity;
|
|
27
|
-
initEntity();
|
|
28
|
-
return store;
|
|
29
|
-
};
|