@octanejs/tanstack-query 0.1.5 → 0.1.9
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 +3 -3
- package/src/internal.ts +42 -0
- package/src/useBaseQuery.ts +13 -10
- package/src/useQueries.ts +23 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octanejs/tanstack-query",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"@tanstack/query-core": "^5.0.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"octane": "0.1.
|
|
43
|
+
"octane": "0.1.10"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@tanstack/react-query": "^5.0.0",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"react": "^19.2.0",
|
|
50
50
|
"react-dom": "^19.2.0",
|
|
51
51
|
"vitest": "^4.1.9",
|
|
52
|
-
"octane": "0.1.
|
|
52
|
+
"octane": "0.1.10"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
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({
|
package/src/useBaseQuery.ts
CHANGED
|
@@ -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
|
|
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
|
-
//
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
|
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
|
-
//
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
|
|
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];
|