@tanstack/query-core 4.0.11-beta.0 → 4.0.11-beta.5

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.
@@ -27,12 +27,17 @@ interface NotifyEventMutationObserverRemoved {
27
27
  mutation: Mutation<any, any, any, any>;
28
28
  observer: MutationObserver<any, any, any>;
29
29
  }
30
+ interface NotifyEventMutationObserverOptionsUpdated {
31
+ type: 'observerOptionsUpdated';
32
+ mutation?: Mutation<any, any, any, any>;
33
+ observer: MutationObserver<any, any, any, any>;
34
+ }
30
35
  interface NotifyEventMutationUpdated {
31
36
  type: 'updated';
32
37
  mutation: Mutation<any, any, any, any>;
33
38
  action: Action<any, any, any, any>;
34
39
  }
35
- declare type MutationCacheNotifyEvent = NotifyEventMutationAdded | NotifyEventMutationRemoved | NotifyEventMutationObserverAdded | NotifyEventMutationObserverRemoved | NotifyEventMutationUpdated;
40
+ declare type MutationCacheNotifyEvent = NotifyEventMutationAdded | NotifyEventMutationRemoved | NotifyEventMutationObserverAdded | NotifyEventMutationObserverRemoved | NotifyEventMutationObserverOptionsUpdated | NotifyEventMutationUpdated;
36
41
  declare type MutationCacheListener = (event: MutationCacheNotifyEvent) => void;
37
42
  export declare class MutationCache extends Subscribable<MutationCacheListener> {
38
43
  config: MutationCacheConfig;
@@ -35,7 +35,12 @@ interface NotifyEventQueryObserverResultsUpdated {
35
35
  type: 'observerResultsUpdated';
36
36
  query: Query<any, any, any, any>;
37
37
  }
38
- declare type QueryCacheNotifyEvent = NotifyEventQueryAdded | NotifyEventQueryRemoved | NotifyEventQueryUpdated | NotifyEventQueryObserverAdded | NotifyEventQueryObserverRemoved | NotifyEventQueryObserverResultsUpdated;
38
+ interface NotifyEventQueryObserverOptionsUpdated {
39
+ type: 'observerOptionsUpdated';
40
+ query: Query<any, any, any, any>;
41
+ observer: QueryObserver<any, any, any, any, any>;
42
+ }
43
+ declare type QueryCacheNotifyEvent = NotifyEventQueryAdded | NotifyEventQueryRemoved | NotifyEventQueryUpdated | NotifyEventQueryObserverAdded | NotifyEventQueryObserverRemoved | NotifyEventQueryObserverResultsUpdated | NotifyEventQueryObserverOptionsUpdated;
39
44
  declare type QueryCacheListener = (event: QueryCacheNotifyEvent) => void;
40
45
  export declare class QueryCache extends Subscribable<QueryCacheListener> {
41
46
  config: QueryCacheConfig;
@@ -45,9 +45,10 @@ export interface QueryOptions<TQueryFnData = unknown, TError = unknown, TData =
45
45
  behavior?: QueryBehavior<TQueryFnData, TError, TData>;
46
46
  /**
47
47
  * Set this to `false` to disable structural sharing between query results.
48
+ * Set this to a function which accepts the old and new data and returns resolved data of the same type to implement custom structural sharing logic.
48
49
  * Defaults to `true`.
49
50
  */
50
- structuralSharing?: boolean;
51
+ structuralSharing?: boolean | ((oldData: TData | undefined, newData: TData) => TData);
51
52
  /**
52
53
  * This function can be set to automatically get the previous cursor for infinite queries.
53
54
  * The result will also be used to determine the value of `hasPreviousPage`.
@@ -274,7 +275,8 @@ export interface QueryObserverSuccessResult<TData = unknown, TError = unknown> e
274
275
  isSuccess: true;
275
276
  status: 'success';
276
277
  }
277
- export declare type QueryObserverResult<TData = unknown, TError = unknown> = QueryObserverLoadingErrorResult<TData, TError> | QueryObserverLoadingResult<TData, TError> | QueryObserverRefetchErrorResult<TData, TError> | QueryObserverSuccessResult<TData, TError>;
278
+ export declare type DefinedQueryObserverResult<TData = unknown, TError = unknown> = QueryObserverRefetchErrorResult<TData, TError> | QueryObserverSuccessResult<TData, TError>;
279
+ export declare type QueryObserverResult<TData = unknown, TError = unknown> = DefinedQueryObserverResult<TData, TError> | QueryObserverLoadingErrorResult<TData, TError> | QueryObserverLoadingResult<TData, TError>;
278
280
  export interface InfiniteQueryObserverBaseResult<TData = unknown, TError = unknown> extends QueryObserverBaseResult<InfiniteData<TData>, TError> {
279
281
  fetchNextPage: (options?: FetchNextPageOptions) => Promise<InfiniteQueryObserverResult<TData, TError>>;
280
282
  fetchPreviousPage: (options?: FetchPreviousPageOptions) => Promise<InfiniteQueryObserverResult<TData, TError>>;
@@ -333,9 +335,9 @@ export interface MutationOptions<TData = unknown, TError = unknown, TVariables =
333
335
  mutationKey?: MutationKey;
334
336
  variables?: TVariables;
335
337
  onMutate?: (variables: TVariables) => Promise<TContext | undefined> | TContext | undefined;
336
- onSuccess?: (data: TData, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
337
- onError?: (error: TError, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
338
- onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
338
+ onSuccess?: (data: TData, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
339
+ onError?: (error: TError, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
340
+ onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
339
341
  retry?: RetryValue<TError>;
340
342
  retryDelay?: RetryDelayValue<TError>;
341
343
  networkMode?: NetworkMode;
@@ -347,9 +349,9 @@ export interface MutationObserverOptions<TData = unknown, TError = unknown, TVar
347
349
  useErrorBoundary?: boolean | ((error: TError) => boolean);
348
350
  }
349
351
  export interface MutateOptions<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> {
350
- onSuccess?: (data: TData, variables: TVariables, context: TContext) => Promise<unknown> | void;
351
- onError?: (error: TError, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
352
- onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
352
+ onSuccess?: (data: TData, variables: TVariables, context: TContext) => Promise<unknown> | unknown;
353
+ onError?: (error: TError, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
354
+ onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
353
355
  }
354
356
  export declare type MutateFunction<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> = (variables: TVariables, options?: MutateOptions<TData, TError, TVariables, TContext>) => Promise<TData>;
355
357
  export interface MutationObserverBaseResult<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> extends MutationState<TData, TError, TVariables, TContext> {
@@ -347,6 +347,8 @@
347
347
  // Use prev data if an isDataEqual function is defined and returns `true`
348
348
  if (options.isDataEqual != null && options.isDataEqual(prevData, data)) {
349
349
  return prevData;
350
+ } else if (typeof options.structuralSharing === 'function') {
351
+ return options.structuralSharing(prevData, data);
350
352
  } else if (options.structuralSharing !== false) {
351
353
  // Structurally share data between prev and new data if needed
352
354
  return replaceEqualDeep(prevData, data);
@@ -2203,6 +2205,14 @@
2203
2205
  const prevQuery = this.currentQuery;
2204
2206
  this.options = this.client.defaultQueryOptions(options);
2205
2207
 
2208
+ if (!shallowEqualObjects(prevOptions, this.options)) {
2209
+ this.client.getQueryCache().notify({
2210
+ type: 'observerOptionsUpdated',
2211
+ query: this.currentQuery,
2212
+ observer: this
2213
+ });
2214
+ }
2215
+
2206
2216
  if (typeof this.options.enabled !== 'undefined' && typeof this.options.enabled !== 'boolean') {
2207
2217
  throw new Error('Expected enabled to be a boolean');
2208
2218
  } // Keep previous query key if the user does not supply one
@@ -2886,7 +2896,16 @@
2886
2896
  }
2887
2897
 
2888
2898
  setOptions(options) {
2899
+ const prevOptions = this.options;
2889
2900
  this.options = this.client.defaultMutationOptions(options);
2901
+
2902
+ if (!shallowEqualObjects(prevOptions, this.options)) {
2903
+ this.client.getMutationCache().notify({
2904
+ type: 'observerOptionsUpdated',
2905
+ mutation: this.currentMutation,
2906
+ observer: this
2907
+ });
2908
+ }
2890
2909
  }
2891
2910
 
2892
2911
  onUnsubscribe() {