@tanstack/react-query 5.0.0-alpha.19 → 5.0.0-alpha.21

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.
@@ -1043,7 +1043,7 @@
1043
1043
  this.state = reducer(this.state);
1044
1044
  notifyManager.batch(() => {
1045
1045
  this.#observers.forEach(observer => {
1046
- observer.onQueryUpdate(action);
1046
+ observer.onQueryUpdate();
1047
1047
  });
1048
1048
  this.#cache.notify({
1049
1049
  query: this,
@@ -2239,30 +2239,15 @@
2239
2239
  query.addObserver(this);
2240
2240
  }
2241
2241
  }
2242
- onQueryUpdate(action) {
2243
- const notifyOptions = {};
2244
- if (action.type === 'success') {
2245
- notifyOptions.onSuccess = !action.manual;
2246
- } else if (action.type === 'error' && !isCancelledError(action.error)) {
2247
- notifyOptions.onError = true;
2248
- }
2249
- this.#updateResult(notifyOptions);
2242
+ onQueryUpdate() {
2243
+ this.#updateResult();
2250
2244
  if (this.hasListeners()) {
2251
2245
  this.#updateTimers();
2252
2246
  }
2253
2247
  }
2254
2248
  #notify(notifyOptions) {
2255
2249
  notifyManager.batch(() => {
2256
- // First trigger the configuration callbacks
2257
- if (notifyOptions.onSuccess) {
2258
- this.options.onSuccess?.(this.#currentResult.data);
2259
- this.options.onSettled?.(this.#currentResult.data, null);
2260
- } else if (notifyOptions.onError) {
2261
- this.options.onError?.(this.#currentResult.error);
2262
- this.options.onSettled?.(undefined, this.#currentResult.error);
2263
- }
2264
-
2265
- // Then trigger the listeners
2250
+ // First, trigger the listeners
2266
2251
  if (notifyOptions.listeners) {
2267
2252
  this.listeners.forEach(listener => {
2268
2253
  listener(this.#currentResult);
@@ -2384,9 +2369,10 @@
2384
2369
  }
2385
2370
  #findMatchingObservers(queries) {
2386
2371
  const prevObservers = this.#observers;
2372
+ const prevObserversMap = new Map(prevObservers.map(observer => [observer.options.queryHash, observer]));
2387
2373
  const defaultedQueryOptions = queries.map(options => this.#client.defaultQueryOptions(options));
2388
2374
  const matchingObservers = defaultedQueryOptions.flatMap(defaultedOptions => {
2389
- const match = prevObservers.find(observer => observer.options.queryHash === defaultedOptions.queryHash);
2375
+ const match = prevObserversMap.get(defaultedOptions.queryHash);
2390
2376
  if (match != null) {
2391
2377
  return [{
2392
2378
  defaultedQueryOptions: defaultedOptions,
@@ -2395,8 +2381,8 @@
2395
2381
  }
2396
2382
  return [];
2397
2383
  });
2398
- const matchedQueryHashes = matchingObservers.map(match => match.defaultedQueryOptions.queryHash);
2399
- const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.includes(defaultedOptions.queryHash));
2384
+ const matchedQueryHashes = new Set(matchingObservers.map(match => match.defaultedQueryOptions.queryHash));
2385
+ const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.has(defaultedOptions.queryHash));
2400
2386
  const getObserver = options => {
2401
2387
  const defaultedOptions = this.#client.defaultQueryOptions(options);
2402
2388
  const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
@@ -2772,15 +2758,8 @@
2772
2758
  };
2773
2759
  const willFetch = (result, isRestoring) => result.isLoading && result.isFetching && !isRestoring;
2774
2760
  const shouldSuspend = (defaultedOptions, result, isRestoring) => defaultedOptions?.suspense && willFetch(result, isRestoring);
2775
- const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).then(({
2776
- data
2777
- }) => {
2778
- defaultedOptions.onSuccess?.(data);
2779
- defaultedOptions.onSettled?.(data, null);
2780
- }).catch(error => {
2761
+ const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).catch(() => {
2781
2762
  errorResetBoundary.clearReset();
2782
- defaultedOptions.onError?.(error);
2783
- defaultedOptions.onSettled?.(undefined, error);
2784
2763
  });
2785
2764
 
2786
2765
  // This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
@@ -2791,6 +2770,7 @@
2791
2770
  }, queryClient) {
2792
2771
  const client = useQueryClient(queryClient);
2793
2772
  const isRestoring = useIsRestoring();
2773
+ const errorResetBoundary = useQueryErrorResetBoundary();
2794
2774
  const defaultedQueries = React__namespace.useMemo(() => queries.map(options => {
2795
2775
  const defaultedOptions = client.defaultQueryOptions(options);
2796
2776
 
@@ -2798,6 +2778,11 @@
2798
2778
  defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic';
2799
2779
  return defaultedOptions;
2800
2780
  }), [queries, client, isRestoring]);
2781
+ defaultedQueries.forEach(query => {
2782
+ ensureStaleTime(query);
2783
+ ensurePreventErrorBoundaryRetry(query, errorResetBoundary);
2784
+ });
2785
+ useClearResetErrorBoundary(errorResetBoundary);
2801
2786
  const [observer] = React__namespace.useState(() => new QueriesObserver(client, defaultedQueries));
2802
2787
  const optimisticResult = observer.getOptimisticResult(defaultedQueries);
2803
2788
  React__namespace.useSyncExternalStore(React__namespace.useCallback(onStoreChange => isRestoring ? () => undefined : observer.subscribe(notifyManager.batchCalls(onStoreChange)), [observer, isRestoring]), () => observer.getCurrentResult(), () => observer.getCurrentResult());
@@ -2808,12 +2793,6 @@
2808
2793
  listeners: false
2809
2794
  });
2810
2795
  }, [defaultedQueries, observer]);
2811
- const errorResetBoundary = useQueryErrorResetBoundary();
2812
- defaultedQueries.forEach(query => {
2813
- ensurePreventErrorBoundaryRetry(query, errorResetBoundary);
2814
- ensureStaleTime(query);
2815
- });
2816
- useClearResetErrorBoundary(errorResetBoundary);
2817
2796
  const shouldAtLeastOneSuspend = optimisticResult.some((result, index) => shouldSuspend(defaultedQueries[index], result, isRestoring));
2818
2797
  const suspensePromises = shouldAtLeastOneSuspend ? optimisticResult.flatMap((result, index) => {
2819
2798
  const options = defaultedQueries[index];
@@ -2830,11 +2809,12 @@
2830
2809
  if (suspensePromises.length > 0) {
2831
2810
  throw Promise.all(suspensePromises);
2832
2811
  }
2812
+ const observerQueries = observer.getQueries();
2833
2813
  const firstSingleResultWhichShouldThrow = optimisticResult.find((result, index) => getHasError({
2834
2814
  result,
2835
2815
  errorResetBoundary,
2836
2816
  throwErrors: defaultedQueries[index]?.throwErrors ?? false,
2837
- query: observer.getQueries()[index]
2817
+ query: observerQueries[index]
2838
2818
  }));
2839
2819
  if (firstSingleResultWhichShouldThrow?.error) {
2840
2820
  throw firstSingleResultWhichShouldThrow.error;
@@ -2850,17 +2830,6 @@
2850
2830
 
2851
2831
  // Make sure results are optimistically set in fetching state before subscribing or updating options
2852
2832
  defaultedOptions._optimisticResults = isRestoring ? 'isRestoring' : 'optimistic';
2853
-
2854
- // Include callbacks in batch renders
2855
- if (defaultedOptions.onError) {
2856
- defaultedOptions.onError = notifyManager.batchCalls(defaultedOptions.onError);
2857
- }
2858
- if (defaultedOptions.onSuccess) {
2859
- defaultedOptions.onSuccess = notifyManager.batchCalls(defaultedOptions.onSuccess);
2860
- }
2861
- if (defaultedOptions.onSettled) {
2862
- defaultedOptions.onSettled = notifyManager.batchCalls(defaultedOptions.onSettled);
2863
- }
2864
2833
  ensureStaleTime(defaultedOptions);
2865
2834
  ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary);
2866
2835
  useClearResetErrorBoundary(errorResetBoundary);