@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.
@@ -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 EntityWatcher<T> {
9
- (newEntity: T, prevEntity: T): void;
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 type EntityWatchMap<T> = T extends unknown[] ? Record<number, EntityWatcher<T>> : T extends object ? {
12
- [K in keyof T]?: EntityWatcher<T>;
13
- } : object;
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, prevEntity: T): void;
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,2 @@
1
+ import { ComparePropertyCallback } from './types';
2
+ export declare const compareProperty: <T, S>(from: T, to: S, fieldPath: string, callback: ComparePropertyCallback, fieldPathStack?: string[]) => void;
@@ -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,3 @@
1
+ export interface ComparePropertyCallback {
2
+ <T, S>(fromValue: T, toValue: S, fieldPaths: string[]): void;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const hasField: <T>(data: T, field: string) => boolean;
@@ -0,0 +1,6 @@
1
+ export const hasField = (data, field) => {
2
+ if (typeof data !== 'object' || data === null) {
3
+ return false;
4
+ }
5
+ return Object.hasOwn(data, field);
6
+ };
@@ -1,3 +1,3 @@
1
1
  export interface EntityChangeEventHandler<T> {
2
- (newEntity: T, prevEntity: T): void;
2
+ (newEntity: T, oldEntity: T): void;
3
3
  }
@@ -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, prevEntity: T) => void;
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, prevEntity) => {
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, prevEntity);
10
+ triggerWatchers(watch, newEntity, oldEntity);
11
11
  }
12
- onChange?.(newEntity, prevEntity);
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, prevEntity: T) => void;
2
+ export declare const triggerWatchers: <T>(watch: EntityWatchMap<T>, newEntity: T, oldEntity: T) => void;
@@ -1,10 +1,14 @@
1
- export const triggerWatchers = (watch, newEntity, prevEntity) => {
2
- for (const [property, watcher] of Object.entries(watch)) {
3
- const newValue = newEntity?.[property];
4
- const prevValue = prevEntity?.[property];
5
- if (newValue === prevValue) {
6
- continue;
7
- }
8
- watcher(newEntity, prevEntity);
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, prevEntity) {
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, prevEntity);
58
+ (0, _triggerCallbacks.triggerCallbacks)(optionsRefCollection, newEntity, oldEntity);
59
59
  });
60
60
  var createReducers = (0, _ahooks.useMemoizedFn)(function () {
61
61
  var reducers = {};