reactjrx 1.31.1 → 1.33.0

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.cjs CHANGED
@@ -509,8 +509,9 @@ function useMutation(query, mapOperatorOrOptions, options = {}) {
509
509
  rxjs.first(),
510
510
  rxjs.map((data) => ({ data, isError: false })),
511
511
  rxjs.catchError((error) => {
512
+ console.error(error);
512
513
  if (optionsRef.current.onError != null) {
513
- optionsRef.current.onError(error);
514
+ optionsRef.current.onError(error, args);
514
515
  }
515
516
  return rxjs.of({ data: error, isError: true });
516
517
  })
@@ -520,7 +521,7 @@ function useMutation(query, mapOperatorOrOptions, options = {}) {
520
521
  rxjs.map(([{ data, isError }, isLastMutationCalled2]) => {
521
522
  if (!isError) {
522
523
  if (optionsRef.current.onSuccess != null)
523
- optionsRef.current.onSuccess(data);
524
+ optionsRef.current.onSuccess(data, args);
524
525
  }
525
526
  if (isLastMutationCalled2) {
526
527
  return isError ? {
@@ -683,6 +684,33 @@ const autoRefetch = (options = {}) => (source) => {
683
684
  })
684
685
  );
685
686
  };
687
+ function isDefined(arg) {
688
+ return arg !== null && arg !== void 0;
689
+ }
690
+ const withKeyComparison = (stream) => stream.pipe(
691
+ rxjs.startWith(void 0),
692
+ rxjs.pairwise(),
693
+ rxjs.map(([previous, current]) => {
694
+ if (current) {
695
+ if (!previous) {
696
+ return {
697
+ ...current,
698
+ previousKey: void 0,
699
+ isUsingDifferentKey: true
700
+ };
701
+ }
702
+ const serializedKey = serializeKey(current.key);
703
+ const serializedPreviousKey = serializeKey(previous.key);
704
+ return {
705
+ ...current,
706
+ previousKey: (previous == null ? void 0 : previous.key) ?? current.key,
707
+ isUsingDifferentKey: serializedPreviousKey !== serializedKey
708
+ };
709
+ }
710
+ return void 0;
711
+ }),
712
+ rxjs.filter(isDefined)
713
+ );
686
714
  function useQuery(keyOrQuery, queryOrOptionOrNothing, optionsOrNothing) {
687
715
  const query = Array.isArray(keyOrQuery) ? queryOrOptionOrNothing : keyOrQuery;
688
716
  const options = optionsOrNothing ?? (queryOrOptionOrNothing !== query ? queryOrOptionOrNothing : void 0) ?? {};
@@ -746,13 +774,15 @@ function useQuery(keyOrQuery, queryOrOptionOrNothing, optionsOrNothing) {
746
774
  options: options2,
747
775
  query: query2
748
776
  })),
777
+ withKeyComparison,
749
778
  rxjs.filter(({ enabled }) => enabled),
750
- rxjs.switchMap(({ key: key2, options: options2, query: query2 }) => {
779
+ rxjs.switchMap(({ key: key2, options: options2, isUsingDifferentKey, query: query2 }) => {
751
780
  const serializedKey = serializeKey(key2);
752
781
  return rxjs.of(null).pipe(
753
782
  rxjs.tap(() => {
754
783
  data$.current.next({
755
784
  ...data$.current.getValue(),
785
+ data: isUsingDifferentKey ? void 0 : data$.current.getValue().data,
756
786
  error: void 0,
757
787
  isLoading: true
758
788
  });
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { useRef, useMemo, useCallback, useSyncExternalStore, useEffect, createContext, memo, useContext, useState } from "react";
2
- import { distinctUntilChanged, tap, finalize, catchError, EMPTY, Subject, identity, BehaviorSubject, of, zip, from, map, merge, throttleTime, switchMap, defer, iif, timer, throwError, take, startWith, combineLatest, first, takeUntil, filter, concatMap as concatMap$1, mergeMap, skip, interval, withLatestFrom, shareReplay, repeat, share, retry } from "rxjs";
2
+ import { distinctUntilChanged, tap, finalize, catchError, EMPTY, Subject, identity, BehaviorSubject, of, zip, from, map, merge, throttleTime, switchMap, defer, iif, timer, throwError, take, startWith, combineLatest, first, takeUntil, filter, concatMap as concatMap$1, mergeMap, skip, interval, withLatestFrom, shareReplay, repeat, pairwise, share, retry } from "rxjs";
3
3
  import { jsx } from "react/jsx-runtime";
4
4
  import { retryWhen, concatMap, tap as tap$1 } from "rxjs/operators";
5
5
  const useLiveRef = (value) => {
@@ -507,8 +507,9 @@ function useMutation(query, mapOperatorOrOptions, options = {}) {
507
507
  first(),
508
508
  map((data) => ({ data, isError: false })),
509
509
  catchError((error) => {
510
+ console.error(error);
510
511
  if (optionsRef.current.onError != null) {
511
- optionsRef.current.onError(error);
512
+ optionsRef.current.onError(error, args);
512
513
  }
513
514
  return of({ data: error, isError: true });
514
515
  })
@@ -518,7 +519,7 @@ function useMutation(query, mapOperatorOrOptions, options = {}) {
518
519
  map(([{ data, isError }, isLastMutationCalled2]) => {
519
520
  if (!isError) {
520
521
  if (optionsRef.current.onSuccess != null)
521
- optionsRef.current.onSuccess(data);
522
+ optionsRef.current.onSuccess(data, args);
522
523
  }
523
524
  if (isLastMutationCalled2) {
524
525
  return isError ? {
@@ -681,6 +682,33 @@ const autoRefetch = (options = {}) => (source) => {
681
682
  })
682
683
  );
683
684
  };
685
+ function isDefined(arg) {
686
+ return arg !== null && arg !== void 0;
687
+ }
688
+ const withKeyComparison = (stream) => stream.pipe(
689
+ startWith(void 0),
690
+ pairwise(),
691
+ map(([previous, current]) => {
692
+ if (current) {
693
+ if (!previous) {
694
+ return {
695
+ ...current,
696
+ previousKey: void 0,
697
+ isUsingDifferentKey: true
698
+ };
699
+ }
700
+ const serializedKey = serializeKey(current.key);
701
+ const serializedPreviousKey = serializeKey(previous.key);
702
+ return {
703
+ ...current,
704
+ previousKey: (previous == null ? void 0 : previous.key) ?? current.key,
705
+ isUsingDifferentKey: serializedPreviousKey !== serializedKey
706
+ };
707
+ }
708
+ return void 0;
709
+ }),
710
+ filter(isDefined)
711
+ );
684
712
  function useQuery(keyOrQuery, queryOrOptionOrNothing, optionsOrNothing) {
685
713
  const query = Array.isArray(keyOrQuery) ? queryOrOptionOrNothing : keyOrQuery;
686
714
  const options = optionsOrNothing ?? (queryOrOptionOrNothing !== query ? queryOrOptionOrNothing : void 0) ?? {};
@@ -744,13 +772,15 @@ function useQuery(keyOrQuery, queryOrOptionOrNothing, optionsOrNothing) {
744
772
  options: options2,
745
773
  query: query2
746
774
  })),
775
+ withKeyComparison,
747
776
  filter(({ enabled }) => enabled),
748
- switchMap(({ key: key2, options: options2, query: query2 }) => {
777
+ switchMap(({ key: key2, options: options2, isUsingDifferentKey, query: query2 }) => {
749
778
  const serializedKey = serializeKey(key2);
750
779
  return of(null).pipe(
751
780
  tap(() => {
752
781
  data$.current.next({
753
782
  ...data$.current.getValue(),
783
+ data: isUsingDifferentKey ? void 0 : data$.current.getValue().data,
754
784
  error: void 0,
755
785
  isLoading: true
756
786
  });
@@ -0,0 +1,16 @@
1
+ import type { Observable } from "rxjs";
2
+ export declare const withKeyComparison: <T extends {
3
+ key: any[];
4
+ }>(stream: Observable<T>) => Observable<(T & {
5
+ previousKey: undefined;
6
+ isUsingDifferentKey: boolean;
7
+ } extends infer T_1 ? T_1 extends T & {
8
+ previousKey: undefined;
9
+ isUsingDifferentKey: boolean;
10
+ } ? T_1 extends null | undefined ? never : T_1 : never : never) | (T & {
11
+ previousKey: any[];
12
+ isUsingDifferentKey: boolean;
13
+ } extends infer T_2 ? T_2 extends T & {
14
+ previousKey: any[];
15
+ isUsingDifferentKey: boolean;
16
+ } ? T_2 extends null | undefined ? never : T_2 : never : never)>;
@@ -4,20 +4,20 @@ interface QueryState<R> {
4
4
  status: "idle" | "loading" | "error" | "success";
5
5
  error: unknown;
6
6
  }
7
- export interface MutationOptions<R> {
7
+ export interface MutationOptions<Result, Params> {
8
8
  retry?: false | number | ((attempt: number, error: unknown) => boolean);
9
9
  /**
10
10
  * Called for every mutation on error.
11
11
  * `merge` mapping will run callback as they happen.
12
12
  * Use `concat` if you need to run callbacks in order of calling.
13
13
  */
14
- onError?: (error: unknown) => void;
14
+ onError?: (error: unknown, params: Params) => void;
15
15
  /**
16
16
  * Called for every mutation on success.
17
17
  * `merge` mapping will run callback as they happen.
18
18
  * Use `concat` if you need to run callbacks in order of calling.
19
19
  */
20
- onSuccess?: (data: R) => void;
20
+ onSuccess?: (data: Result, params: Params) => void;
21
21
  /**
22
22
  * When true, any running mutation will be cancelled (when possible) on unmount.
23
23
  * You need to handle it yourself for promises if needed.
@@ -33,7 +33,7 @@ export interface MutationOptions<R> {
33
33
  * Only use for debugging.
34
34
  * It is not the main subscription hook, only the one following the trigger.
35
35
  */
36
- triggerHook?: MonoTypeOperatorFunction<Partial<QueryState<R>> | undefined>;
36
+ triggerHook?: MonoTypeOperatorFunction<Partial<QueryState<Result>> | undefined>;
37
37
  }
38
38
  interface Result<A, R> {
39
39
  status: "idle" | "loading" | "error" | "success";
@@ -77,6 +77,6 @@ interface Result<A, R> {
77
77
  * Result correspond to the current running mutation.
78
78
  */
79
79
  type MapOperator = "switch" | "concat" | "merge";
80
- export declare function useMutation<A = void, R = undefined>(query: (args: A) => Promise<R> | Observable<R>, mapOperatorOrOptions?: MapOperator, options?: MutationOptions<R>): Result<A, R>;
81
- export declare function useMutation<A = void, R = undefined>(query: (args: A) => Promise<R> | Observable<R>, mapOperatorOrOptions?: MutationOptions<R>): Result<A, R>;
80
+ export declare function useMutation<A = void, R = undefined>(query: (args: A) => Promise<R> | Observable<R>, mapOperatorOrOptions?: MapOperator, options?: MutationOptions<R, A>): Result<A, R>;
81
+ export declare function useMutation<A = void, R = undefined>(query: (args: A) => Promise<R> | Observable<R>, mapOperatorOrOptions?: MutationOptions<R, A>): Result<A, R>;
82
82
  export {};
@@ -0,0 +1 @@
1
+ export declare function isDefined<T>(arg: T | null | undefined): arg is T extends null | undefined ? never : T;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "reactjrx",
3
3
  "private": false,
4
- "version": "1.31.1",
4
+ "version": "1.33.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"