@mic-rexjs/usecases-react 3.1.1 → 3.1.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.
@@ -6,11 +6,18 @@ export const valueUseCase = createUseCase(() => {
6
6
  const _utilsUseCase = utilsUseCase(),
7
7
  createKey = _utilsUseCase.createKey;
8
8
  let prevKeyIndex = createKey();
9
- const hasField = (entity, field) => {
9
+ const isObjectValue = entity => {
10
10
  if (typeof entity !== 'object' || entity === null) {
11
11
  return false;
12
12
  }
13
- return Object.hasOwn(entity, field);
13
+ return true;
14
+ };
15
+ const hasField = (entity, field) => {
16
+ const isObject = isObjectValue(entity);
17
+ if (isObject) {
18
+ return Object.hasOwn(entity, field);
19
+ }
20
+ return false;
14
21
  };
15
22
  const isValueChanged = (entity, value) => {
16
23
  return entity !== value;
@@ -19,8 +26,8 @@ export const valueUseCase = createUseCase(() => {
19
26
  if (entity === value) {
20
27
  return true;
21
28
  }
22
- const isObject1 = typeof entity === 'object' && entity !== null;
23
- const isObject2 = typeof value === 'object' && value !== null;
29
+ const isObject1 = isObjectValue(entity);
30
+ const isObject2 = isObjectValue(value);
24
31
  const keys1 = isObject1 ? Object.keys(entity) : [];
25
32
  const keys2 = isObject2 ? Object.keys(value) : [];
26
33
  const hasCallback = typeof onMatchPropertyFailed === 'function';
@@ -32,21 +39,18 @@ export const valueUseCase = createUseCase(() => {
32
39
  const keys = [...keys1, ...keys2];
33
40
  for (let i = 0, length = keys.length; i < length; i++) {
34
41
  const key = keys[i];
35
- const isFromKeys2 = i >= length1;
36
42
  const isInKey1 = keys1.includes(key);
43
+ const isFromKeys2 = i >= length1;
37
44
  const isUsed = isFromKeys2 && isInKey1;
38
45
  if (isUsed) {
39
46
  continue;
40
47
  }
41
48
  const hasField1 = hasField(entity, key);
42
49
  const hasField2 = hasField(value, key);
43
- const onlyHasField1 = hasField1 && !hasField2;
44
- const onlyHasField2 = !hasField1 && hasField2;
45
- const onlyHasOneField = onlyHasField1 || onlyHasField2;
46
50
  const value1 = hasField1 ? entity[key] : void 0;
47
51
  const value2 = hasField2 ? value[key] : void 0;
48
52
  const matched = value1 === value2;
49
- if (!onlyHasOneField && matched) {
53
+ if (hasField1 && hasField2 && matched) {
50
54
  continue;
51
55
  }
52
56
  allMatched = false;
@@ -127,22 +131,6 @@ export const valueUseCase = createUseCase(() => {
127
131
  } while (list.length > 0);
128
132
  return allMatched;
129
133
  };
130
- const diffValue = (entity, value, onMatchPropertyFailed) => {
131
- const isArray1 = Array.isArray(entity);
132
- const isArray2 = Array.isArray(value);
133
- const isArray = isArray1 && isArray2;
134
- const result = isArray ? [] : {};
135
- if (isArray) {
136
- const length1 = entity.length;
137
- const length2 = value.length;
138
- result.length = Math.max(length1, length2);
139
- }
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
134
  const recordValue = function* (entity, value) {
147
135
  const changed = isValueChanged(entity, value);
148
136
  if (!changed) {
@@ -151,16 +139,6 @@ export const valueUseCase = createUseCase(() => {
151
139
  yield value;
152
140
  return true;
153
141
  };
154
- const recordValueDiff = function* (entity, value) {
155
- let matched = true;
156
- const result = diffValue(entity, value, () => {
157
- matched = false;
158
- });
159
- if (!matched) {
160
- yield value;
161
- }
162
- return result;
163
- };
164
142
  const recordValueMatch = function* (entity, value) {
165
143
  const matched = matchValue(entity, value);
166
144
  if (matched) {
@@ -178,12 +156,11 @@ export const valueUseCase = createUseCase(() => {
178
156
  };
179
157
  return {
180
158
  ...entityReducers,
181
- diffValue,
159
+ isObjectValue,
182
160
  isValueChanged,
183
161
  matchValue,
184
162
  matchProperty,
185
163
  recordValue,
186
- recordValueDiff,
187
164
  recordValueMatch,
188
165
  recordValueMatchWith
189
166
  };
@@ -8,12 +8,11 @@ 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
+ isObjectValue(entity: T): boolean;
12
12
  isValueChanged<S extends T>(entity: S, value: S): boolean;
13
13
  matchValue<S extends T>(entity: S, value: S, onMatchPropertyFailed?: MatchValuePropertyFailedCallback<S>): boolean;
14
14
  matchProperty<S extends T>(entity: S, value: S, fieldPath: string, onFailed?: MatchPropertyFailedCallback<S>): boolean;
15
15
  recordValue<S extends T>(entity: S, value: S): EntityGenerator<S, boolean>;
16
- recordValueDiff<S extends T>(entity: S, value: S): EntityGenerator<S, Partial<S>>;
17
16
  recordValueMatch<S extends T>(entity: S, value: S): EntityGenerator<S, boolean>;
18
17
  recordValueMatchWith<S extends T>(entity: S, value: S, key?: React.Key): EntityGenerator<S, React.Key>;
19
18
  }>;
@@ -57,7 +57,7 @@ var useEntity = exports.useEntity = function useEntity(entityArg, store, reducer
57
57
  var currentEntity = entityArg;
58
58
  var isFirst = isFirstRef.current;
59
59
  isFirstRef.current = false;
60
- if (isFirst) {
60
+ if (isFirst && entityRootEnabled) {
61
61
  var _bindEntity;
62
62
  currentEntity = (_bindEntity = bindEntity === null || bindEntity === void 0 ? void 0 : bindEntity()) !== null && _bindEntity !== void 0 ? _bindEntity : currentEntity;
63
63
  }
@@ -92,6 +92,9 @@ var useEntity = exports.useEntity = function useEntity(entityArg, store, reducer
92
92
  };
93
93
  }, [entityEnabled, store, onEntityChange]);
94
94
  (0, _ahooks.useUnmount)(function () {
95
+ if (!entityRootEnabled) {
96
+ return;
97
+ }
95
98
  unbindEntity === null || unbindEntity === void 0 || unbindEntity();
96
99
  });
97
100
  (0, _ahooks.useUpdateEffect)(function () {
@@ -32,17 +32,31 @@ var useStore = exports.useStore = function useStore(entityArg, context, statuses
32
32
  var store = new _usecases.EntityStore(currentEntity);
33
33
  var createEntityList = [];
34
34
  var _valueUseCase = (0, _valueUseCase2.valueUseCase)(),
35
- diffValue = _valueUseCase.diffValue;
35
+ isObjectValue = _valueUseCase.isObjectValue,
36
+ matchValue = _valueUseCase.matchValue;
36
37
  var _createEntityReducers = (0, _usecases.createEntityReducers)(store, _entityRuntimeUseCase.entityRuntimeUseCase, {
37
38
  onInit: function onInit(currentValue) {
38
39
  return entityArg(currentValue);
39
40
  },
40
41
  onCreate: function onCreate(newEntity, oldEntity) {
41
- var prevEntity = prevCreateEntityList[index];
42
- var diffEntity = diffValue(prevEntity, newEntity);
43
- createEntityList[index] = newEntity;
42
+ var currentIndex = index;
43
+ var isObject = isObjectValue(oldEntity);
44
+ var isArray = isObject && Array.isArray(oldEntity);
44
45
  index++;
45
- return (0, _usecases.createEntity)(oldEntity, diffEntity);
46
+ createEntityList[currentIndex] = newEntity;
47
+ if (!isObject || isArray) {
48
+ return (0, _usecases.createEntity)(oldEntity, newEntity);
49
+ }
50
+ var descriptors = {};
51
+ var prevEntity = prevCreateEntityList[currentIndex];
52
+ matchValue(prevEntity, newEntity, function (key) {
53
+ var descriptor = Object.getOwnPropertyDescriptor(newEntity, key) || null;
54
+ if (descriptor === null) {
55
+ return;
56
+ }
57
+ descriptors[key] = descriptor;
58
+ });
59
+ return (0, _usecases.createEntity)(oldEntity, Object.defineProperties({}, descriptors));
46
60
  },
47
61
  onGenerate: function onGenerate(entity, result) {
48
62
  if (typeof result === 'undefined') {