@octanejs/tanstack-query 0.1.4 → 0.1.8

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/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@octanejs/tanstack-query",
3
- "version": "0.1.4",
3
+ "version": "0.1.8",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
+ "engines": {
7
+ "node": ">=22"
8
+ },
6
9
  "octane": {
7
10
  "hookSlots": {
8
11
  "manual": [
@@ -34,8 +37,10 @@
34
37
  ".": "./src/index.ts"
35
38
  },
36
39
  "dependencies": {
37
- "@tanstack/query-core": "^5.0.0",
38
- "octane": "0.1.5"
40
+ "@tanstack/query-core": "^5.0.0"
41
+ },
42
+ "peerDependencies": {
43
+ "octane": "0.1.9"
39
44
  },
40
45
  "devDependencies": {
41
46
  "@tanstack/react-query": "^5.0.0",
@@ -43,7 +48,8 @@
43
48
  "esbuild": "^0.28.1",
44
49
  "react": "^19.2.0",
45
50
  "react-dom": "^19.2.0",
46
- "vitest": "^4.1.9"
51
+ "vitest": "^4.1.9",
52
+ "octane": "0.1.9"
47
53
  },
48
54
  "scripts": {
49
55
  "test": "vitest run"
package/src/internal.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  // Shared internals for the binding hooks.
2
2
  import { shouldThrowError } from '@tanstack/query-core';
3
+ import { use, useRef } from 'octane';
3
4
 
4
5
  // Derive a stable, distinct sub-slot from a wrapper's compiler-injected slot, so
5
6
  // a hook composing multiple base hooks gives each one its own identity. Tags are
@@ -89,6 +90,47 @@ export function fetchOptimistic(
89
90
  });
90
91
  }
91
92
 
93
+ type SuspensePromise = PromiseLike<unknown> & {
94
+ status?: 'pending' | 'fulfilled' | 'rejected';
95
+ };
96
+
97
+ interface SuspensePromiseCache {
98
+ key: unknown;
99
+ promise: SuspensePromise | undefined;
100
+ }
101
+
102
+ /**
103
+ * Suspend through a stable `use()` occurrence for one query-hook call site.
104
+ *
105
+ * `use()` tracks thenables by dynamic call order. A component with two
106
+ * sequential suspense queries initially reaches only the first query; when it
107
+ * resolves, the first query no longer needs to suspend and the second query is
108
+ * reached for the first time. If the first call simply skips `use()`, the
109
+ * second query takes its call-order position and replay reuses the first
110
+ * query's fulfilled promise, exposing the second query's still-pending result.
111
+ *
112
+ * Retain the promise that suspended this query hook and keep reading it after
113
+ * it settles. That reserves the query hook's call-order position throughout
114
+ * the replay episode. A new query key or retry replaces a settled promise;
115
+ * re-renders during the same pending fetch reuse the in-flight promise.
116
+ */
117
+ export function useSuspensePromise(
118
+ shouldSuspend: boolean,
119
+ key: unknown,
120
+ createPromise: () => PromiseLike<unknown>,
121
+ slot: symbol | undefined,
122
+ ): void {
123
+ const cache = useRef<SuspensePromiseCache>({ key: undefined, promise: undefined }, slot).current;
124
+ if (
125
+ shouldSuspend &&
126
+ (cache.promise === undefined || cache.key !== key || cache.promise.status !== 'pending')
127
+ ) {
128
+ cache.key = key;
129
+ cache.promise = createPromise() as SuspensePromise;
130
+ }
131
+ if (cache.promise !== undefined) use(cache.promise);
132
+ }
133
+
92
134
  // react-query's getHasError: only throw if the query errored, the boundary isn't
93
135
  // reset, the query isn't refetching, and the options opt into throwing.
94
136
  export function getHasError({
@@ -4,7 +4,7 @@
4
4
  // changes through useEffect. The single compiler-injected slot is split into
5
5
  // distinct sub-slots for each internal base hook, the same way the zustand
6
6
  // `traditional` binding does.
7
- import { useState, useCallback, useSyncExternalStore, useEffect, use } from 'octane';
7
+ import { useState, useCallback, useSyncExternalStore, useEffect } from 'octane';
8
8
  import { environmentManager, noop, notifyManager } from '@tanstack/query-core';
9
9
  import { resolveClient } from './context';
10
10
  import { useIsRestoring } from './isRestoring';
@@ -16,6 +16,7 @@ import {
16
16
  getHasError,
17
17
  shouldSuspend,
18
18
  subSlot,
19
+ useSuspensePromise,
19
20
  willFetch,
20
21
  } from './internal';
21
22
 
@@ -104,15 +105,17 @@ export function useBaseQuery(
104
105
  oq('eff'),
105
106
  );
106
107
 
107
- // Suspense: suspend on the in-flight promise via `use(thenable)` (octane's
108
- // suspend primitive — NOT a raw `throw promise`). fetchOptimistic clears the
109
- // reset boundary on error (upstream suspense.ts), so a reset→retry that fails
110
- // again re-throws to the boundary on replay instead of falling through with
111
- // undefined data. The promise resolves even on error; the replay surfaces the
112
- // error through the error-boundary throw below.
113
- if (shouldSuspend(defaultedOptions, result)) {
114
- use(fetchOptimistic(defaultedOptions, observer, errorResetBoundary));
115
- }
108
+ // Suspend on a promise retained for this query-hook call site. Keeping the
109
+ // settled promise in the dynamic use() sequence prevents a later sequential
110
+ // query from taking this query's replay position. fetchOptimistic clears the
111
+ // reset boundary on error; the replay surfaces that error through the
112
+ // error-boundary throw below.
113
+ useSuspensePromise(
114
+ shouldSuspend(defaultedOptions, result),
115
+ defaultedOptions.queryHash,
116
+ () => fetchOptimistic(defaultedOptions, observer, errorResetBoundary),
117
+ oq('sus'),
118
+ );
116
119
 
117
120
  // Error boundary: throw so the nearest @try/@catch (or <ErrorBoundary>) handles
118
121
  // it, when the query errored and the options opt into throwing.
package/src/useQueries.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { QueriesObserver, QueryObserver, noop, notifyManager } from '@tanstack/query-core';
2
2
  import type { QueryClient } from '@tanstack/query-core';
3
- import { useState, useMemo, useSyncExternalStore, useCallback, useEffect, use } from 'octane';
3
+ import { useState, useMemo, useSyncExternalStore, useCallback, useEffect } from 'octane';
4
4
  import { resolveClient } from './context';
5
5
  import { useIsRestoring } from './isRestoring';
6
6
  import { useQueryErrorResetBoundary } from './errorResetBoundary';
@@ -12,6 +12,7 @@ import {
12
12
  shouldSuspend,
13
13
  splitSlot,
14
14
  subSlot,
15
+ useSuspensePromise,
15
16
  } from './internal';
16
17
  import type { QueriesOptions, QueriesResults } from './queries-types';
17
18
 
@@ -93,25 +94,29 @@ export function useQueries(options: any, ...rest: any[]): any {
93
94
  qs('eff'),
94
95
  );
95
96
 
96
- // Suspense: if any query should suspend, suspend on Promise.all of their
97
- // optimistic fetches. octane suspends via use(thenable), not `throw promise`.
98
- const shouldAtLeastOneSuspend = optimisticResult.some((result: any, index: number) =>
99
- shouldSuspend(defaultedQueries[index], result),
100
- );
101
- if (shouldAtLeastOneSuspend) {
102
- const suspensePromises = optimisticResult.flatMap((result: any, index: number) => {
103
- const opts = defaultedQueries[index];
104
- if (opts && shouldSuspend(opts, result)) {
105
- // fetchOptimistic clears the reset boundary on error (upstream
106
- // suspense.ts) so a reset→retry→fail-again re-throws to the boundary.
107
- return fetchOptimistic(opts, new QueryObserver(client, opts), errorResetBoundary);
108
- }
109
- return [];
110
- });
111
- if (suspensePromises.length > 0) {
112
- use(Promise.all(suspensePromises));
97
+ // If any query should suspend, await their optimistic fetches as one retained
98
+ // suspense promise. Retaining it gives a sequential useQueries call the same
99
+ // stable replay position as useBaseQuery above.
100
+ const suspenseIndexes: number[] = [];
101
+ for (let index = 0; index < optimisticResult.length; index++) {
102
+ if (shouldSuspend(defaultedQueries[index], optimisticResult[index])) {
103
+ suspenseIndexes.push(index);
113
104
  }
114
105
  }
106
+ useSuspensePromise(
107
+ suspenseIndexes.length > 0,
108
+ JSON.stringify(suspenseIndexes.map((index) => defaultedQueries[index].queryHash)),
109
+ () =>
110
+ Promise.all(
111
+ suspenseIndexes.map((index) => {
112
+ const opts = defaultedQueries[index];
113
+ // fetchOptimistic clears the reset boundary on error (upstream
114
+ // suspense.ts) so a reset→retry→fail-again re-throws to the boundary.
115
+ return fetchOptimistic(opts, new QueryObserver(client, opts), errorResetBoundary);
116
+ }),
117
+ ),
118
+ qs('sus'),
119
+ );
115
120
 
116
121
  const firstError = optimisticResult.find((result: any, index: number) => {
117
122
  const query = defaultedQueries[index];