@mic-rexjs/usecases-react 1.4.0 → 1.4.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/README.md +2 -2
- package/dist/index.min.js +1 -1
- package/es/components/UseCaseFragment/types.d.ts +1 -1
- package/es/hooks/useUseCase/index.js +2 -2
- package/es/hooks/useUseCase/index.test.js +493 -41
- package/es/hooks/useUseCase/types.d.ts +18 -6
- package/es/methods/compareProperty/index.d.ts +2 -0
- package/es/methods/compareProperty/index.js +34 -0
- package/es/methods/compareProperty/types.d.ts +3 -0
- package/es/methods/compareProperty/types.js +1 -0
- package/es/methods/hasField/index.d.ts +1 -0
- package/es/methods/hasField/index.js +6 -0
- package/es/methods/initEntityReducers/types.d.ts +1 -1
- package/es/methods/triggerCallbacks/index.d.ts +1 -1
- package/es/methods/triggerCallbacks/index.js +3 -3
- package/es/methods/triggerWatchers/index.d.ts +1 -1
- package/es/methods/triggerWatchers/index.js +12 -8
- package/lib/hooks/useUseCase/index.js +2 -2
- package/lib/hooks/useUseCase/index.test.js +643 -91
- package/lib/methods/compareProperty/index.js +50 -0
- package/lib/methods/compareProperty/types.js +5 -0
- package/lib/methods/hasField/index.js +15 -0
- package/lib/methods/triggerCallbacks/index.js +3 -3
- package/lib/methods/triggerWatchers/index.js +16 -9
- package/package.json +1 -1
|
@@ -5,15 +5,27 @@ export type CoreCollection<T, TEntityReducers extends EntityReducers<T>, TProvid
|
|
|
5
5
|
export interface EntityGetter<T> {
|
|
6
6
|
(): T;
|
|
7
7
|
}
|
|
8
|
-
export interface
|
|
9
|
-
|
|
8
|
+
export interface EntityWatchEvent<T, TValue = unknown> {
|
|
9
|
+
fieldPaths: string[];
|
|
10
|
+
newEntity: T;
|
|
11
|
+
newValue: TValue;
|
|
12
|
+
oldEntity: T;
|
|
13
|
+
oldValue: TValue;
|
|
10
14
|
}
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
}
|
|
15
|
+
export interface EntityWatcher<T, TValue = unknown> {
|
|
16
|
+
(event: EntityWatchEvent<T, TValue>): void;
|
|
17
|
+
}
|
|
18
|
+
export type PropertyPath<T, K extends keyof T = keyof T & string> = K extends infer TKey ? `${TKey & (string | number)}${'' | DotAccessorFieldPath<T[TKey & keyof T]>}` : never;
|
|
19
|
+
export type DotAccessorFieldPath<T> = T extends unknown[] ? '.length' | `${'' | `.${number}`}${DotAccessorFieldPath<T[number]>}` : T extends object ? `.${PropertyPath<T>}` : '';
|
|
20
|
+
export type FieldPath<T> = T extends unknown[] ? FieldPath<T[number]> | 'length' : T extends object ? PropertyPath<T> : never;
|
|
21
|
+
export type ExtractPropertyType<T, TPath> = TPath extends `${infer TKey}.${infer TSubPath}` ? T extends unknown[] ? ExtractPropertyType<T[number], TKey extends `${number}` ? TSubPath : TPath> : ExtractPropertyType<T[TKey & keyof T], TSubPath> : T extends unknown[] ? TPath extends 'length' ? T[TPath] : TPath extends `${number}` ? T[number] : ExtractPropertyType<T[number], TPath> : T[TPath & keyof T];
|
|
22
|
+
export type ExtractFieldType<T, TPath> = TPath extends `${infer TKey}.${infer TSubProperty}` ? ExtractPropertyType<T[TKey & keyof T], TSubProperty> : T[TPath & keyof T];
|
|
23
|
+
export type EntityWatchMap<T> = {
|
|
24
|
+
[K in FieldPath<T>]?: EntityWatcher<T, ExtractFieldType<T, K>>;
|
|
25
|
+
};
|
|
14
26
|
export interface UseCaseHookContextualOptions<T> {
|
|
15
27
|
watch?: EntityWatchMap<T>;
|
|
16
|
-
onChange?(newEntity: T,
|
|
28
|
+
onChange?(newEntity: T, oldEntity: T): void;
|
|
17
29
|
}
|
|
18
30
|
export interface UseCaseHookOwnOptions<T, TUseCaseOptions extends object> extends UseCaseHookContextualOptions<T> {
|
|
19
31
|
stateless?: boolean;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { hasField } from '../hasField';
|
|
2
|
+
export const compareProperty = (from, to, fieldPath, callback, fieldPathStack = []) => {
|
|
3
|
+
const undef = void 0;
|
|
4
|
+
const [, key = fieldPath, subFieldPath = ''] = fieldPath.match(/^([^.]+)\.(.+)$/) || [];
|
|
5
|
+
const hasFromField = hasField(from, key);
|
|
6
|
+
const hasToField = hasField(to, key);
|
|
7
|
+
const currentFieldPathStack = [...fieldPathStack, key];
|
|
8
|
+
if (hasFromField || hasToField) {
|
|
9
|
+
const fromValue = hasFromField ? from[key] : undef;
|
|
10
|
+
const toValue = hasToField ? to[key] : undef;
|
|
11
|
+
if (subFieldPath) {
|
|
12
|
+
compareProperty(fromValue, toValue, subFieldPath, callback, currentFieldPathStack);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (fromValue === toValue) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
callback(fromValue, toValue, currentFieldPathStack);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (/^\d+^/.test(key)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const fromArray = Array.isArray(from) ? from : [];
|
|
25
|
+
const toArray = Array.isArray(to) ? to : [];
|
|
26
|
+
const { length: fromLength } = fromArray;
|
|
27
|
+
const { length: toLength } = toArray;
|
|
28
|
+
const length = Math.max(fromLength, toLength);
|
|
29
|
+
for (let i = 0; i < length; i++) {
|
|
30
|
+
const fromItem = fromArray[i];
|
|
31
|
+
const toItem = toArray[i];
|
|
32
|
+
compareProperty(fromItem, toItem, fieldPath, callback, [...fieldPathStack, `${i}`]);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const hasField: <T>(data: T, field: string) => boolean;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { OptionsRefCollection } from '../../hooks/useOptionsRefCollection/types';
|
|
2
2
|
import { UseCaseHookOptions } from '../../hooks/useUseCase/types';
|
|
3
|
-
export declare const triggerCallbacks: <T, TUseCaseOptions extends object, TOptions extends UseCaseHookOptions<T, TUseCaseOptions> = UseCaseHookOptions<T, TUseCaseOptions>>(optionsRefCollection: OptionsRefCollection<TOptions>, newEntity: T,
|
|
3
|
+
export declare const triggerCallbacks: <T, TUseCaseOptions extends object, TOptions extends UseCaseHookOptions<T, TUseCaseOptions> = UseCaseHookOptions<T, TUseCaseOptions>>(optionsRefCollection: OptionsRefCollection<TOptions>, newEntity: T, oldEntity: T) => void;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { triggerWatchers } from '../triggerWatchers';
|
|
2
|
-
export const triggerCallbacks = (optionsRefCollection, newEntity,
|
|
2
|
+
export const triggerCallbacks = (optionsRefCollection, newEntity, oldEntity) => {
|
|
3
3
|
for (let { length: i } = optionsRefCollection; i > 0; i--) {
|
|
4
4
|
const { current: options } = optionsRefCollection[i - 1];
|
|
5
5
|
if (!options) {
|
|
@@ -7,8 +7,8 @@ export const triggerCallbacks = (optionsRefCollection, newEntity, prevEntity) =>
|
|
|
7
7
|
}
|
|
8
8
|
const { onChange, watch } = options;
|
|
9
9
|
if (watch) {
|
|
10
|
-
triggerWatchers(watch, newEntity,
|
|
10
|
+
triggerWatchers(watch, newEntity, oldEntity);
|
|
11
11
|
}
|
|
12
|
-
onChange?.(newEntity,
|
|
12
|
+
onChange?.(newEntity, oldEntity);
|
|
13
13
|
}
|
|
14
14
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { EntityWatchMap } from '../../hooks/useUseCase/types';
|
|
2
|
-
export declare const triggerWatchers: <T>(watch: EntityWatchMap<T>, newEntity: T,
|
|
2
|
+
export declare const triggerWatchers: <T>(watch: EntityWatchMap<T>, newEntity: T, oldEntity: T) => void;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { compareProperty } from '../compareProperty';
|
|
2
|
+
export const triggerWatchers = (watch, newEntity, oldEntity) => {
|
|
3
|
+
for (const [fieldPath, watcher] of Object.entries(watch)) {
|
|
4
|
+
compareProperty(oldEntity, newEntity, fieldPath, (oldValue, newValue, fieldPaths) => {
|
|
5
|
+
watcher({
|
|
6
|
+
fieldPaths,
|
|
7
|
+
newEntity,
|
|
8
|
+
oldEntity,
|
|
9
|
+
newValue,
|
|
10
|
+
oldValue,
|
|
11
|
+
});
|
|
12
|
+
});
|
|
9
13
|
}
|
|
10
14
|
};
|
|
@@ -51,11 +51,11 @@ var useUseCase = function useUseCase(arg1, arg2, arg3, arg4) {
|
|
|
51
51
|
}
|
|
52
52
|
return (0, _getRootEntity.getRootEntity)(entityState, arg1, stateless);
|
|
53
53
|
}, [hasInitialEntity, contextEntity, stateless, entityState, arg1]);
|
|
54
|
-
var onEntityChange = (0, _ahooks.useMemoizedFn)(function (newEntity,
|
|
54
|
+
var onEntityChange = (0, _ahooks.useMemoizedFn)(function (newEntity, oldEntity) {
|
|
55
55
|
if (!stateless) {
|
|
56
56
|
setEntityState(newEntity);
|
|
57
57
|
}
|
|
58
|
-
(0, _triggerCallbacks.triggerCallbacks)(optionsRefCollection, newEntity,
|
|
58
|
+
(0, _triggerCallbacks.triggerCallbacks)(optionsRefCollection, newEntity, oldEntity);
|
|
59
59
|
});
|
|
60
60
|
var createReducers = (0, _ahooks.useMemoizedFn)(function () {
|
|
61
61
|
var reducers = {};
|