gemi 0.205.4 → 0.207.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/{chunk-qjk68wda.js → chunk-53fv4c39.js} +2 -2
- package/dist/{chunk-qjk68wda.js.map → chunk-53fv4c39.js.map} +4 -4
- package/dist/{chunk-1fqn9rwv.js → chunk-b1v4ync4.js} +1 -1
- package/dist/{chunk-6xh70rae.js → chunk-j683qspe.js} +1 -1
- package/dist/{chunk-j9ga7b74.js → chunk-k8a41gkt.js} +1 -1
- package/dist/{chunk-4gkhwmg2.js → chunk-yqgyywpq.js} +2 -2
- package/dist/{chunk-4gkhwmg2.js.map → chunk-yqgyywpq.js.map} +1 -1
- package/dist/chunks/{RealtimeClient-CLXePUcE.js → RealtimeClient-D4qIHPf1.js} +2 -2
- package/dist/chunks/{RealtimeClient-CLXePUcE.js.map → RealtimeClient-D4qIHPf1.js.map} +1 -1
- package/dist/chunks/{ThemeProvider-COjYIalw.js → ThemeProvider-w_axEu6B.js} +48 -9
- package/dist/chunks/ThemeProvider-w_axEu6B.js.map +1 -0
- package/dist/chunks/{fetchFileRequest-BaufNZpI.js → fetchFileRequest-Bhh5BC_l.js} +2 -2
- package/dist/chunks/{fetchFileRequest-BaufNZpI.js.map → fetchFileRequest-Bhh5BC_l.js.map} +1 -1
- package/dist/chunks/{live-Dz_rk7fm.js → live-9IXFtoGy.js} +3 -3
- package/dist/chunks/{live-Dz_rk7fm.js.map → live-9IXFtoGy.js.map} +1 -1
- package/dist/chunks/{mutationExtras-DtSw6BcX.js → mutationExtras-bNF9JOSk.js} +2 -2
- package/dist/chunks/{mutationExtras-DtSw6BcX.js.map → mutationExtras-bNF9JOSk.js.map} +1 -1
- package/dist/chunks/{subscriptionStream-CqhJOfPn.js → subscriptionStream-D42DpDiZ.js} +2 -2
- package/dist/chunks/{subscriptionStream-CqhJOfPn.js.map → subscriptionStream-D42DpDiZ.js.map} +1 -1
- package/dist/client/QueryManagerContext.d.ts +5 -1
- package/dist/client/QueryManagerContext.d.ts.map +1 -1
- package/dist/client/QueryResource.d.ts +27 -1
- package/dist/client/QueryResource.d.ts.map +1 -1
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +574 -155
- package/dist/client/index.js.map +1 -1
- package/dist/client/queryEffects.d.ts +106 -0
- package/dist/client/queryEffects.d.ts.map +1 -0
- package/dist/client/useMultipleQuery.d.ts +93 -0
- package/dist/client/useMultipleQuery.d.ts.map +1 -0
- package/dist/client/useQuery.d.ts +23 -1
- package/dist/client/useQuery.d.ts.map +1 -1
- package/dist/facades/index.js +1 -1
- package/dist/http/index.js +1 -1
- package/dist/i18n/index.js +1 -1
- package/dist/kernel/index.js +1 -1
- package/dist/server/index.js +1 -1
- package/dist/services/index.js +1 -1
- package/dist/testing/index.js +2 -2
- package/package.json +1 -1
- package/skills/gemi-react-best-practices/SKILL.md +3 -1
- package/skills/gemi-react-best-practices/rules/query-multiple-query-per-component.md +90 -0
- package/dist/chunks/ThemeProvider-COjYIalw.js.map +0 -1
- /package/dist/{chunk-1fqn9rwv.js.map → chunk-b1v4ync4.js.map} +0 -0
- /package/dist/{chunk-6xh70rae.js.map → chunk-j683qspe.js.map} +0 -0
- /package/dist/{chunk-j9ga7b74.js.map → chunk-k8a41gkt.js.map} +0 -0
package/dist/client/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $ as applyParams$1, A as useSearchParams, B as loadURLPattern, C as loadDictionary, D as readRoutePayload, E as setActiveLocale, F as ServerDataProvider, G as configureRealtime, H as applyDocumentNonce, I as useLocation, J as realtime, K as followLive, L as domainUrl, M as I18nContext, N as I18nProvider, O as routeDataUrl, P as ServerDataContext, Q as toVariantKey, R as isAbsoluteUrl, S as recoverFromChunkLoadError, T as registerDictionary, U as useCspNonce, V as CspNonceContext, W as RealtimeContext, X as loadLazy, Y as reconnectRealtime, Z as loadedModule, _ as configureChunkLoadRecovery, _t as retryDelayFor, a as useRouteTransition, at as DEFAULT_STALE_TIME, b as otherBuild, bt as Subject, c as ClientRouterContext, ct as resetAuthenticationIncident, d as ComponentsContext, dt as isExternalRedirect, et as QueryConfigContext, f as ComponentsProvider, ft as safeRedirectPath, g as CHUNK_RELOAD_MARKER, h as subscribeViewModules, ht as isRetryableQueryError, i as RouteTransitionProvider, it as pickDefined, j as useNavigate, k as useRoute, l as ClientRouterProvider, lt as setAuthenticationNavigator, m as loadViewModule, n as useTheme, nt as QueryManagerProvider, o as useChannelSubscription, ot as configureAuthenticationErrors, p as initialViewModulesReady, pt as readResponseBody, r as m, rt as USER_QUERY_CONFIG_KEYS, s as useCoalesced, st as reportAuthenticationRefusal, t as ThemeProvider, tt as QueryManagerContext, u as restoreScroll, ut as INTENDED_URL_PARAM, v as isChunkLoadError, vt as shouldRetryQuery, w as loadDictionaryForRender, x as recoverFromBuildMismatch, y as navigationStarted, yt as QueryError } from "../chunks/ThemeProvider-
|
|
1
|
+
import { $ as applyParams$1, A as useSearchParams, B as loadURLPattern, C as loadDictionary, D as readRoutePayload, E as setActiveLocale, F as ServerDataProvider, G as configureRealtime, H as applyDocumentNonce, I as useLocation, J as realtime, K as followLive, L as domainUrl, M as I18nContext, N as I18nProvider, O as routeDataUrl, P as ServerDataContext, Q as toVariantKey, R as isAbsoluteUrl, S as recoverFromChunkLoadError, T as registerDictionary, U as useCspNonce, V as CspNonceContext, W as RealtimeContext, X as loadLazy, Y as reconnectRealtime, Z as loadedModule, _ as configureChunkLoadRecovery, _t as retryDelayFor, a as useRouteTransition, at as DEFAULT_STALE_TIME, b as otherBuild, bt as Subject, c as ClientRouterContext, ct as resetAuthenticationIncident, d as ComponentsContext, dt as isExternalRedirect, et as QueryConfigContext, f as ComponentsProvider, ft as safeRedirectPath, g as CHUNK_RELOAD_MARKER, h as subscribeViewModules, ht as isRetryableQueryError, i as RouteTransitionProvider, it as pickDefined, j as useNavigate, k as useRoute, l as ClientRouterProvider, lt as setAuthenticationNavigator, m as loadViewModule, n as useTheme, nt as QueryManagerProvider, o as useChannelSubscription, ot as configureAuthenticationErrors, p as initialViewModulesReady, pt as readResponseBody, r as m, rt as USER_QUERY_CONFIG_KEYS, s as useCoalesced, st as reportAuthenticationRefusal, t as ThemeProvider, tt as QueryManagerContext, u as restoreScroll, ut as INTENDED_URL_PARAM, v as isChunkLoadError, vt as shouldRetryQuery, w as loadDictionaryForRender, x as recoverFromBuildMismatch, y as navigationStarted, yt as QueryError } from "../chunks/ThemeProvider-w_axEu6B.js";
|
|
2
2
|
import { n as RouteStateContext, r as RouteStateProvider, t as useParams } from "../chunks/useParams-BN3XXfmG.js";
|
|
3
3
|
import { a as isNetworkError, c as isPermissionError, d as isValidationError, f as mutationErrorField, h as mutationErrorList, i as isHttpError, l as isRateLimitError, m as mutationErrorKind, n as isCsrfError, o as isNotFoundError, p as mutationErrorFromBody, r as isFormError, s as isObject, t as isAuthenticationError, u as isServerError } from "../chunks/MutationError-DuKqBW8o.js";
|
|
4
4
|
import { Fragment, StrictMode, Suspense, createContext, createElement, isValidElement, lazy, memo, use, useCallback, useContext, useDeferredValue, useEffect, useInsertionEffect, useMemo, useRef, useState, useSyncExternalStore, useTransition } from "react";
|
|
@@ -29,7 +29,174 @@ function isPlainObject(value) {
|
|
|
29
29
|
return value !== null && typeof value === "object" && !Array.isArray(value) && Object.getPrototypeOf(value) === Object.prototype;
|
|
30
30
|
}
|
|
31
31
|
//#endregion
|
|
32
|
-
//#region client/
|
|
32
|
+
//#region client/queryEffects.ts
|
|
33
|
+
/**
|
|
34
|
+
* Where a query reads from: the resolved path (route params from the URL,
|
|
35
|
+
* overridden by `options.params`) and the variant key of its `search`.
|
|
36
|
+
*/
|
|
37
|
+
function resolveQueryTarget(url, options, routeParams) {
|
|
38
|
+
const params = "params" in options ? {
|
|
39
|
+
...routeParams,
|
|
40
|
+
...options.params
|
|
41
|
+
} : routeParams;
|
|
42
|
+
const search = "search" in options ? options.search ?? {} : {};
|
|
43
|
+
return {
|
|
44
|
+
params,
|
|
45
|
+
search,
|
|
46
|
+
path: applyParams$1(url, params),
|
|
47
|
+
variantKey: toVariantKey(search)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Per-call config over the provider's `queryConfig`. Only *defined* per-call
|
|
52
|
+
* keys participate: a key explicitly set to `undefined` (`{ suspense: cond ?
|
|
53
|
+
* true : undefined }`, forwarding an optional prop) falls through to the
|
|
54
|
+
* provider default instead of clobbering it.
|
|
55
|
+
*/
|
|
56
|
+
function mergeDefinedConfig(base, ...overrides) {
|
|
57
|
+
const merged = { ...base };
|
|
58
|
+
for (const override of overrides) {
|
|
59
|
+
if (!override) continue;
|
|
60
|
+
for (const key of Object.keys(override)) {
|
|
61
|
+
const value = override[key];
|
|
62
|
+
if (value !== void 0) merged[key] = value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return merged;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A failed `suspense: false` query: schedule the background retry its retry
|
|
69
|
+
* policy allows. The resource owns the timer, one per variant however many
|
|
70
|
+
* readers render it.
|
|
71
|
+
*/
|
|
72
|
+
function retryFailedQuery(resource, variantKey, snapshot, cfg) {
|
|
73
|
+
if (!snapshot?.error || snapshot.loading) return;
|
|
74
|
+
const error = snapshot.error;
|
|
75
|
+
const failureCount = Math.max(1, resource.failureCount(variantKey));
|
|
76
|
+
if (!shouldRetryQuery(failureCount, error, cfg.retry)) return;
|
|
77
|
+
const delay = retryDelayFor(failureCount, error, cfg.retryDelay, cfg.retryIntervalOnError);
|
|
78
|
+
if (cfg.debug) console.log("retrying", variantKey, "in", delay);
|
|
79
|
+
resource.scheduleRetry(variantKey, delay);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Coming back online retries a query that failed in a way another attempt
|
|
83
|
+
* might fix, as long as its retry policy would retry that error at all.
|
|
84
|
+
* Silent, and it starts a fresh run of failures. Returns the cleanup.
|
|
85
|
+
*/
|
|
86
|
+
function retryWhenOnline(resource, variantKey, getConfig) {
|
|
87
|
+
if (typeof window === "undefined") return () => {};
|
|
88
|
+
const onOnline = () => {
|
|
89
|
+
const state = resource.peek(variantKey);
|
|
90
|
+
if (!state?.error) return;
|
|
91
|
+
if (!shouldRetryQuery(1, state.error, getConfig().retry)) return;
|
|
92
|
+
resource.retryNow(variantKey);
|
|
93
|
+
};
|
|
94
|
+
window.addEventListener("online", onOnline);
|
|
95
|
+
return () => window.removeEventListener("online", onOnline);
|
|
96
|
+
}
|
|
97
|
+
function createPollState() {
|
|
98
|
+
return {
|
|
99
|
+
timer: null,
|
|
100
|
+
duration: 0,
|
|
101
|
+
snapshot: void 0,
|
|
102
|
+
resource: null,
|
|
103
|
+
variantKey: null,
|
|
104
|
+
awaiting: false
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* One step of the `refetchUntil` poll, run after every commit so the
|
|
109
|
+
* predicate always sees the current render's closure. New data (or another
|
|
110
|
+
* resource/variant) clears the pending timer and asks the predicate again; on
|
|
111
|
+
* a plain re-render a pending timer is kept, so frequent renders never push
|
|
112
|
+
* the next refetch back. `paused` (a live channel is open) stops it.
|
|
113
|
+
*/
|
|
114
|
+
function stepPoll(poll, resource, variantKey, snapshot, refetchUntil, paused) {
|
|
115
|
+
const clear = () => {
|
|
116
|
+
if (poll.timer) clearTimeout(poll.timer);
|
|
117
|
+
poll.timer = null;
|
|
118
|
+
};
|
|
119
|
+
if (poll.snapshot !== snapshot || poll.resource !== resource || poll.variantKey !== variantKey) {
|
|
120
|
+
clear();
|
|
121
|
+
poll.awaiting = false;
|
|
122
|
+
poll.snapshot = snapshot;
|
|
123
|
+
poll.resource = resource;
|
|
124
|
+
poll.variantKey = variantKey;
|
|
125
|
+
}
|
|
126
|
+
if (!refetchUntil || paused || !snapshot || snapshot.loading || !snapshot.hasData || snapshot.error) {
|
|
127
|
+
clear();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (poll.awaiting) return;
|
|
131
|
+
const nextDuration = refetchUntil(snapshot.data, poll.duration);
|
|
132
|
+
if (!(nextDuration > 0)) {
|
|
133
|
+
clear();
|
|
134
|
+
poll.duration = 0;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (poll.timer) return;
|
|
138
|
+
poll.duration = nextDuration;
|
|
139
|
+
poll.timer = setTimeout(() => {
|
|
140
|
+
poll.timer = null;
|
|
141
|
+
poll.awaiting = true;
|
|
142
|
+
resource.refetch(variantKey);
|
|
143
|
+
}, nextDuration);
|
|
144
|
+
}
|
|
145
|
+
/** Unmount: drop the pending poll. */
|
|
146
|
+
function stopPoll(poll) {
|
|
147
|
+
if (poll.timer) clearTimeout(poll.timer);
|
|
148
|
+
poll.timer = null;
|
|
149
|
+
poll.snapshot = void 0;
|
|
150
|
+
poll.awaiting = false;
|
|
151
|
+
}
|
|
152
|
+
/** The `refreshInterval` / HMR `http-reload` reload of one query. */
|
|
153
|
+
function reloadQuery(resource, variantKey, cfg) {
|
|
154
|
+
if (cfg.debug) console.log("Reloading query for", variantKey);
|
|
155
|
+
const data = resource.getVariant(variantKey, cfg.staleTime).data;
|
|
156
|
+
resource.mutate(variantKey, () => data);
|
|
157
|
+
}
|
|
158
|
+
/** Subscribe to Vite's `http-reload` event. Returns the cleanup. */
|
|
159
|
+
function onHttpReload(handler) {
|
|
160
|
+
if (typeof import.meta.hot?.on === "function") import.meta.hot.on("http-reload", handler);
|
|
161
|
+
return () => {
|
|
162
|
+
if (typeof import.meta.hot?.off === "function") import.meta.hot.off("http-reload", handler);
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Revalidate when the tab comes back to the foreground, gated by `staleTime`
|
|
167
|
+
* and always silent. Three guards decide whether a return is owed a request:
|
|
168
|
+
* eligibility, the away latch (`focus` also fires for a dismissed `alert`),
|
|
169
|
+
* and the throttle (an embedded iframe blurs and focuses the window on every
|
|
170
|
+
* click). Both `focus` and `visibilitychange` are listened for because
|
|
171
|
+
* neither covers the other. Returns the cleanup.
|
|
172
|
+
*/
|
|
173
|
+
function revalidateOnFocus(resource, variantKey, focus, getConfig) {
|
|
174
|
+
const markAway = () => {
|
|
175
|
+
focus.wasAway = true;
|
|
176
|
+
};
|
|
177
|
+
const revalidate = () => {
|
|
178
|
+
if (!focus.eligible) return;
|
|
179
|
+
if (document.visibilityState === "hidden") return;
|
|
180
|
+
if (!focus.wasAway) return;
|
|
181
|
+
const now = Date.now();
|
|
182
|
+
if (now - focus.last < (getConfig().focusThrottleInterval ?? 0)) return;
|
|
183
|
+
focus.wasAway = false;
|
|
184
|
+
focus.last = now;
|
|
185
|
+
resource.revalidate(variantKey, getConfig().staleTime);
|
|
186
|
+
};
|
|
187
|
+
const onVisibilityChange = () => {
|
|
188
|
+
if (document.visibilityState === "hidden") markAway();
|
|
189
|
+
else revalidate();
|
|
190
|
+
};
|
|
191
|
+
window.addEventListener("blur", markAway);
|
|
192
|
+
window.addEventListener("focus", revalidate);
|
|
193
|
+
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
194
|
+
return () => {
|
|
195
|
+
window.removeEventListener("blur", markAway);
|
|
196
|
+
window.removeEventListener("focus", revalidate);
|
|
197
|
+
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
198
|
+
};
|
|
199
|
+
}
|
|
33
200
|
/**
|
|
34
201
|
* A DOM event or React's synthetic one. Never a plain object, so a cached
|
|
35
202
|
* value that happens to have a `nativeEvent` key is not mistaken for one.
|
|
@@ -39,6 +206,45 @@ function isEventLike(value) {
|
|
|
39
206
|
if (typeof Event !== "undefined" && value instanceof Event) return true;
|
|
40
207
|
return "nativeEvent" in value && typeof value.preventDefault === "function";
|
|
41
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* `mutate(value | updater)` for one query; a bare `mutate()` refetches.
|
|
211
|
+
* Returns whether it went to the wire for a refetch (the caller marks the
|
|
212
|
+
* query as fetched).
|
|
213
|
+
*/
|
|
214
|
+
function mutateQuery(resource, variantKey, fn) {
|
|
215
|
+
if (isEventLike(fn)) fn = void 0;
|
|
216
|
+
if (!fn) {
|
|
217
|
+
resource.refetch(variantKey);
|
|
218
|
+
return "refetch";
|
|
219
|
+
}
|
|
220
|
+
resource.mutate(variantKey, (data) => {
|
|
221
|
+
if (data === void 0) {
|
|
222
|
+
console.warn("Mutate function called before the query.");
|
|
223
|
+
return data;
|
|
224
|
+
}
|
|
225
|
+
const updatedData = typeof fn === "function" ? fn(data) : fn;
|
|
226
|
+
if (isPlainObject(data)) {
|
|
227
|
+
if (isPlainObject(updatedData)) return updatedData;
|
|
228
|
+
throw new Error("Mutate function must return an object when the current data is an object.");
|
|
229
|
+
}
|
|
230
|
+
if (Array.isArray(data)) {
|
|
231
|
+
if (Array.isArray(updatedData)) return updatedData;
|
|
232
|
+
throw new Error("Mutate function must return an array when the current data is an array.");
|
|
233
|
+
}
|
|
234
|
+
if (typeof data !== typeof updatedData) throw new Error("Mutate function must return the same type as the current data.");
|
|
235
|
+
return updatedData;
|
|
236
|
+
});
|
|
237
|
+
return "update";
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* The dev warning for a suspense query a server render had no data for.
|
|
241
|
+
*/
|
|
242
|
+
function warnUnprefetched(url, variantKey, hook = "useQuery") {
|
|
243
|
+
const searchHint = variantKey ? `, { search: ${JSON.stringify(Object.fromEntries(new URLSearchParams(variantKey)))} }` : "";
|
|
244
|
+
console.warn(`[gemi] ${hook}("${url}") rendered on the server without data. The server never fetches, so this page ships without it and the client suspends after hydration. Add \`Query.prefetch("${url}"${searchHint})\` to the route's view handler.`);
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region client/useQuery.ts
|
|
42
248
|
var defaultConfig = {
|
|
43
249
|
fallbackData: null,
|
|
44
250
|
keepPreviousData: true,
|
|
@@ -58,11 +264,7 @@ var defaultOptions$1 = {
|
|
|
58
264
|
function useQuery(url, ...args) {
|
|
59
265
|
const [options, config] = args;
|
|
60
266
|
const { user: _user, ...queryConfig } = useContext(QueryConfigContext) ?? {};
|
|
61
|
-
|
|
62
|
-
if (config) {
|
|
63
|
-
for (const key of Object.keys(config)) if (config[key] !== void 0) merged[key] = config[key];
|
|
64
|
-
}
|
|
65
|
-
return useFrameworkQuery(url, options, merged);
|
|
267
|
+
return useFrameworkQuery(url, options, mergeDefinedConfig(queryConfig, config), { startOnServer: true });
|
|
66
268
|
}
|
|
67
269
|
/**
|
|
68
270
|
* `useQuery` minus the app's provider-level `queryConfig`: per-call config
|
|
@@ -70,10 +272,15 @@ function useQuery(url, ...args) {
|
|
|
70
272
|
* (`useUser`'s `suspense: false`, `useSignIn`'s `lazy`) build on this so an
|
|
71
273
|
* app-wide `queryConfig` can never change their semantics. Deliberately not
|
|
72
274
|
* exported from the package root.
|
|
275
|
+
*
|
|
276
|
+
* `startOnServer`: a `suspense: false` query starts its request on a
|
|
277
|
+
* streaming server render too (#1253). Only `useQuery` asks for it: `useUser`
|
|
278
|
+
* of an anonymous visitor would put a 401 for `/auth/me` on every render, and
|
|
279
|
+
* report it through `onRequestFail`.
|
|
73
280
|
*/
|
|
74
281
|
function useFrameworkQuery(url, ...args) {
|
|
75
282
|
const _params = useParams();
|
|
76
|
-
const [_options = defaultOptions$1, _config = defaultConfig] = args;
|
|
283
|
+
const [_options = defaultOptions$1, _config = defaultConfig, internal] = args;
|
|
77
284
|
const options = {
|
|
78
285
|
...defaultOptions$1,
|
|
79
286
|
..._options
|
|
@@ -83,15 +290,9 @@ function useFrameworkQuery(url, ...args) {
|
|
|
83
290
|
..._config
|
|
84
291
|
};
|
|
85
292
|
const suspense = config.suspense !== false && !config.lazy;
|
|
86
|
-
const params =
|
|
87
|
-
..._params,
|
|
88
|
-
...options.params
|
|
89
|
-
} : _params;
|
|
90
|
-
const search = "search" in options ? options.search ?? {} : {};
|
|
293
|
+
const { params, search, path: currentPath, variantKey: currentVariantKey } = resolveQueryTarget(url, options, _params);
|
|
91
294
|
const { getResource } = useContext(QueryManagerContext);
|
|
92
295
|
const serverQueries = useContext(ServerQueryContext);
|
|
93
|
-
const currentPath = applyParams$1(url, params);
|
|
94
|
-
const currentVariantKey = toVariantKey(search);
|
|
95
296
|
const deferredPath = useDeferredValue(currentPath);
|
|
96
297
|
const deferredVariantKey = useDeferredValue(currentVariantKey);
|
|
97
298
|
const deferVariant = suspense && config.keepPreviousData;
|
|
@@ -114,18 +315,14 @@ function useFrameworkQuery(url, ...args) {
|
|
|
114
315
|
}, [realtime, liveKey]);
|
|
115
316
|
const refreshIntervalRef = useRef(null);
|
|
116
317
|
const fetchedRef = useRef(!lazy);
|
|
117
|
-
const pollRef = useRef(
|
|
118
|
-
|
|
119
|
-
duration: 0,
|
|
120
|
-
snapshot: void 0,
|
|
121
|
-
resource: null,
|
|
122
|
-
variantKey: null,
|
|
123
|
-
awaiting: false
|
|
124
|
-
});
|
|
318
|
+
const pollRef = useRef(null);
|
|
319
|
+
pollRef.current ??= createPollState();
|
|
125
320
|
const prefetchedRef = useRef(false);
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
321
|
+
const focusRef = useRef({
|
|
322
|
+
eligible: !lazy,
|
|
323
|
+
wasAway: false,
|
|
324
|
+
last: 0
|
|
325
|
+
});
|
|
129
326
|
const subscribe = useCallback((onStoreChange) => {
|
|
130
327
|
const release = resource.retain(variantKey);
|
|
131
328
|
const unsubscribe = resource.store.subscribe(onStoreChange);
|
|
@@ -135,13 +332,14 @@ function useFrameworkQuery(url, ...args) {
|
|
|
135
332
|
};
|
|
136
333
|
}, [resource, variantKey]);
|
|
137
334
|
const getSnapshot = useCallback(() => resource.peek(variantKey), [resource, variantKey]);
|
|
138
|
-
const
|
|
335
|
+
const getServerSnapshot = useCallback(() => resource.peekForHydration(variantKey), [resource, variantKey]);
|
|
336
|
+
const snapshot = useSyncExternalStore(subscribe, getSnapshot, suspense ? getSnapshot : getServerSnapshot);
|
|
139
337
|
const lastDataRef = useRef(snapshot?.hasData ? snapshot : null);
|
|
140
338
|
useEffect(() => {
|
|
141
339
|
if (snapshot?.hasData) lastDataRef.current = snapshot;
|
|
142
340
|
}, [snapshot]);
|
|
143
341
|
let state = snapshot;
|
|
144
|
-
if (config.keepPreviousData && !snapshot?.hasData && snapshot
|
|
342
|
+
if (config.keepPreviousData && !snapshot?.hasData && (snapshot ? snapshot.loading : !lazy) && lastDataRef.current) state = {
|
|
145
343
|
...lastDataRef.current,
|
|
146
344
|
loading: true
|
|
147
345
|
};
|
|
@@ -164,10 +362,13 @@ function useFrameworkQuery(url, ...args) {
|
|
|
164
362
|
else if (entry.status === "rejected") serverError = entry.error;
|
|
165
363
|
else readPromise = entry.promise;
|
|
166
364
|
} else readPromise = resource.read(variantKey, config.staleTime).promise;
|
|
167
|
-
if (suspense &&
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
365
|
+
if (!suspense && !lazy && !snapshot?.hasData && !snapshot?.error) if (typeof window === "undefined") {
|
|
366
|
+
if (internal?.startOnServer) serverQueries?.ensure(url, {
|
|
367
|
+
params,
|
|
368
|
+
search
|
|
369
|
+
});
|
|
370
|
+
} else resource.start(variantKey);
|
|
371
|
+
if (suspense && typeof window === "undefined" && unserved && !state?.hasData && process.env.NODE_ENV !== "production") warnUnprefetched(url, variantKey);
|
|
171
372
|
useEffect(() => {
|
|
172
373
|
if (!isDeferred) return;
|
|
173
374
|
const targetSeed = configRef.current.fallbackData != null ? { [currentVariantKey]: configRef.current.fallbackData } : prefetchedData?.[currentPath];
|
|
@@ -183,76 +384,20 @@ function useFrameworkQuery(url, ...args) {
|
|
|
183
384
|
if (fetchedRef.current) resource.getVariant(variantKey, configRef.current.staleTime);
|
|
184
385
|
}, [variantKey, resource]);
|
|
185
386
|
useEffect(() => {
|
|
186
|
-
if (suspense
|
|
187
|
-
|
|
188
|
-
const error = snapshot.error;
|
|
189
|
-
const failureCount = Math.max(1, resource.failureCount(variantKey));
|
|
190
|
-
if (!shouldRetryQuery(failureCount, error, cfg.retry)) return;
|
|
191
|
-
const delay = retryDelayFor(failureCount, error, cfg.retryDelay, cfg.retryIntervalOnError);
|
|
192
|
-
if (cfg.debug) console.log("retrying", variantKey, "in", delay);
|
|
193
|
-
resource.scheduleRetry(variantKey, delay);
|
|
387
|
+
if (suspense) return;
|
|
388
|
+
retryFailedQuery(resource, variantKey, snapshot, configRef.current);
|
|
194
389
|
}, [
|
|
195
390
|
snapshot,
|
|
196
391
|
suspense,
|
|
197
392
|
resource,
|
|
198
393
|
variantKey
|
|
199
394
|
]);
|
|
395
|
+
useEffect(() => retryWhenOnline(resource, variantKey, () => configRef.current), [resource, variantKey]);
|
|
200
396
|
useEffect(() => {
|
|
201
|
-
|
|
202
|
-
const onOnline = () => {
|
|
203
|
-
const state = resource.peek(variantKey);
|
|
204
|
-
if (!state?.error) return;
|
|
205
|
-
if (!shouldRetryQuery(1, state.error, configRef.current.retry)) return;
|
|
206
|
-
resource.retryNow(variantKey);
|
|
207
|
-
};
|
|
208
|
-
window.addEventListener("online", onOnline);
|
|
209
|
-
return () => window.removeEventListener("online", onOnline);
|
|
210
|
-
}, [resource, variantKey]);
|
|
211
|
-
useEffect(() => {
|
|
212
|
-
const poll = pollRef.current;
|
|
213
|
-
const clear = () => {
|
|
214
|
-
if (poll.timer) clearTimeout(poll.timer);
|
|
215
|
-
poll.timer = null;
|
|
216
|
-
};
|
|
217
|
-
if (poll.snapshot !== snapshot || poll.resource !== resource || poll.variantKey !== variantKey) {
|
|
218
|
-
clear();
|
|
219
|
-
poll.awaiting = false;
|
|
220
|
-
poll.snapshot = snapshot;
|
|
221
|
-
poll.resource = resource;
|
|
222
|
-
poll.variantKey = variantKey;
|
|
223
|
-
}
|
|
224
|
-
const refetchUntil = configRef.current.refetchUntil;
|
|
225
|
-
if (!refetchUntil || liveOpen || !snapshot || snapshot.loading || !snapshot.hasData || snapshot.error) {
|
|
226
|
-
clear();
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
if (poll.awaiting) return;
|
|
230
|
-
const nextDuration = refetchUntil(snapshot.data, poll.duration);
|
|
231
|
-
if (!(nextDuration > 0)) {
|
|
232
|
-
clear();
|
|
233
|
-
poll.duration = 0;
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
if (poll.timer) return;
|
|
237
|
-
poll.duration = nextDuration;
|
|
238
|
-
poll.timer = setTimeout(() => {
|
|
239
|
-
poll.timer = null;
|
|
240
|
-
poll.awaiting = true;
|
|
241
|
-
resource.refetch(variantKey);
|
|
242
|
-
}, nextDuration);
|
|
397
|
+
stepPoll(pollRef.current, resource, variantKey, snapshot, configRef.current.refetchUntil, liveOpen);
|
|
243
398
|
});
|
|
244
|
-
useEffect(() => () =>
|
|
245
|
-
|
|
246
|
-
if (poll.timer) clearTimeout(poll.timer);
|
|
247
|
-
poll.timer = null;
|
|
248
|
-
poll.snapshot = void 0;
|
|
249
|
-
poll.awaiting = false;
|
|
250
|
-
}, []);
|
|
251
|
-
const handleReload = useCallback(() => {
|
|
252
|
-
if (configRef.current.debug) console.log("Reloading query for", variantKey);
|
|
253
|
-
const data = resource.getVariant(variantKey, configRef.current.staleTime).data;
|
|
254
|
-
resource.mutate(variantKey, () => data);
|
|
255
|
-
}, [variantKey, resource]);
|
|
399
|
+
useEffect(() => () => stopPoll(pollRef.current), []);
|
|
400
|
+
const handleReload = useCallback(() => reloadQuery(resource, variantKey, configRef.current), [variantKey, resource]);
|
|
256
401
|
useEffect(() => {
|
|
257
402
|
if (!fetchedRef.current || liveOpen) return;
|
|
258
403
|
refreshIntervalRef.current = setInterval(() => {
|
|
@@ -268,46 +413,18 @@ function useFrameworkQuery(url, ...args) {
|
|
|
268
413
|
]);
|
|
269
414
|
useEffect(() => {
|
|
270
415
|
if (!config.revalidateOnFocus) return;
|
|
271
|
-
|
|
272
|
-
wasAwayRef.current = true;
|
|
273
|
-
};
|
|
274
|
-
const revalidate = () => {
|
|
275
|
-
if (!focusEligibleRef.current) return;
|
|
276
|
-
if (document.visibilityState === "hidden") return;
|
|
277
|
-
if (!wasAwayRef.current) return;
|
|
278
|
-
const now = Date.now();
|
|
279
|
-
if (now - lastFocusRevalidationRef.current < configRef.current.focusThrottleInterval) return;
|
|
280
|
-
wasAwayRef.current = false;
|
|
281
|
-
lastFocusRevalidationRef.current = now;
|
|
282
|
-
resource.revalidate(variantKey, configRef.current.staleTime);
|
|
283
|
-
};
|
|
284
|
-
const onVisibilityChange = () => {
|
|
285
|
-
if (document.visibilityState === "hidden") markAway();
|
|
286
|
-
else revalidate();
|
|
287
|
-
};
|
|
288
|
-
window.addEventListener("blur", markAway);
|
|
289
|
-
window.addEventListener("focus", revalidate);
|
|
290
|
-
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
291
|
-
return () => {
|
|
292
|
-
window.removeEventListener("blur", markAway);
|
|
293
|
-
window.removeEventListener("focus", revalidate);
|
|
294
|
-
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
295
|
-
};
|
|
416
|
+
return revalidateOnFocus(resource, variantKey, focusRef.current, () => configRef.current);
|
|
296
417
|
}, [
|
|
297
418
|
config.revalidateOnFocus,
|
|
298
419
|
resource,
|
|
299
420
|
variantKey
|
|
300
421
|
]);
|
|
301
|
-
useEffect(() =>
|
|
302
|
-
if (typeof import.meta.hot?.on === "function") import.meta.hot.on("http-reload", handleReload);
|
|
303
|
-
return () => {
|
|
304
|
-
if (typeof import.meta.hot?.off === "function") import.meta.hot.off("http-reload", handleReload);
|
|
305
|
-
};
|
|
306
|
-
}, [handleReload]);
|
|
422
|
+
useEffect(() => onHttpReload(handleReload), [handleReload]);
|
|
307
423
|
const trigger = useCallback(() => {
|
|
308
424
|
fetchedRef.current = true;
|
|
309
|
-
|
|
425
|
+
focusRef.current.eligible = true;
|
|
310
426
|
const variant = resource.store.getValue().get(variantKey);
|
|
427
|
+
if (resource.isFetching(variantKey)) return;
|
|
311
428
|
if (!variant || !variant.loading && !variant.hasData) resource.refetch(variantKey);
|
|
312
429
|
}, [resource, variantKey]);
|
|
313
430
|
const prefetch = useCallback(() => {
|
|
@@ -318,34 +435,12 @@ function useFrameworkQuery(url, ...args) {
|
|
|
318
435
|
}, [resource, variantKey]);
|
|
319
436
|
const refetch = useCallback(() => {
|
|
320
437
|
fetchedRef.current = true;
|
|
321
|
-
|
|
438
|
+
focusRef.current.eligible = true;
|
|
322
439
|
resource.refetch(variantKey);
|
|
323
440
|
}, [resource, variantKey]);
|
|
324
441
|
function mutate(fn) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
if (!fn) {
|
|
328
|
-
fetchedRef.current = true;
|
|
329
|
-
resource.refetch(variantKey);
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
return resource.mutate(variantKey, (data) => {
|
|
333
|
-
if (data === void 0) {
|
|
334
|
-
console.warn("Mutate function called before the query.");
|
|
335
|
-
return data;
|
|
336
|
-
}
|
|
337
|
-
const updatedData = typeof fn === "function" ? fn(data) : fn;
|
|
338
|
-
if (isPlainObject(data)) {
|
|
339
|
-
if (isPlainObject(updatedData)) return updatedData;
|
|
340
|
-
throw new Error("Mutate function must return an object when the current data is an object.");
|
|
341
|
-
}
|
|
342
|
-
if (Array.isArray(data)) {
|
|
343
|
-
if (Array.isArray(updatedData)) return updatedData;
|
|
344
|
-
throw new Error("Mutate function must return an array when the current data is an array.");
|
|
345
|
-
}
|
|
346
|
-
if (typeof data !== typeof updatedData) throw new Error("Mutate function must return the same type as the current data.");
|
|
347
|
-
return updatedData;
|
|
348
|
-
});
|
|
442
|
+
focusRef.current.eligible = true;
|
|
443
|
+
if (mutateQuery(resource, variantKey, fn) === "refetch") fetchedRef.current = true;
|
|
349
444
|
}
|
|
350
445
|
if (suspense) {
|
|
351
446
|
if (serverError) throw serverError;
|
|
@@ -364,6 +459,330 @@ function useFrameworkQuery(url, ...args) {
|
|
|
364
459
|
};
|
|
365
460
|
}
|
|
366
461
|
//#endregion
|
|
462
|
+
//#region client/useMultipleQuery.ts
|
|
463
|
+
/**
|
|
464
|
+
* A key whose first answer is still on the wire: nothing to show for it yet.
|
|
465
|
+
* A variant with no state at all counts: its request was started silently in
|
|
466
|
+
* the render, or goes out in the mount effect.
|
|
467
|
+
*/
|
|
468
|
+
function awaitingFirstAnswer(target) {
|
|
469
|
+
const state = target.resource.peek(target.variantKey);
|
|
470
|
+
return !state || state.loading && !state.hasData;
|
|
471
|
+
}
|
|
472
|
+
function useMultipleQuery(queries, sharedConfig) {
|
|
473
|
+
const { user: _user, ...appConfig } = useContext(QueryConfigContext) ?? {};
|
|
474
|
+
const routeParams = useParams();
|
|
475
|
+
const { getResource } = useContext(QueryManagerContext);
|
|
476
|
+
const serverQueries = useContext(ServerQueryContext);
|
|
477
|
+
const realtime = useContext(RealtimeContext);
|
|
478
|
+
const { prefetchedData } = useRouteData();
|
|
479
|
+
const current = Object.keys(queries).map((key) => {
|
|
480
|
+
const [url, options, callConfig] = queries[key];
|
|
481
|
+
const config = {
|
|
482
|
+
...defaultConfig,
|
|
483
|
+
...mergeDefinedConfig(appConfig, sharedConfig, callConfig),
|
|
484
|
+
lazy: false
|
|
485
|
+
};
|
|
486
|
+
const target = resolveQueryTarget(url, {
|
|
487
|
+
...defaultOptions$1,
|
|
488
|
+
...options
|
|
489
|
+
}, routeParams);
|
|
490
|
+
return {
|
|
491
|
+
key,
|
|
492
|
+
url,
|
|
493
|
+
config,
|
|
494
|
+
suspense: config.suspense !== false,
|
|
495
|
+
...target
|
|
496
|
+
};
|
|
497
|
+
});
|
|
498
|
+
const signature = JSON.stringify(current.filter((e) => e.suspense && e.config.keepPreviousData).map((e) => [
|
|
499
|
+
e.key,
|
|
500
|
+
e.url,
|
|
501
|
+
e.path,
|
|
502
|
+
e.variantKey,
|
|
503
|
+
e.params,
|
|
504
|
+
e.search
|
|
505
|
+
]));
|
|
506
|
+
const deferredSignature = useDeferredValue(signature);
|
|
507
|
+
const deferred = useMemo(() => {
|
|
508
|
+
const map = /* @__PURE__ */ new Map();
|
|
509
|
+
if (deferredSignature === signature) return map;
|
|
510
|
+
for (const [key, url, path, variantKey, params, search] of JSON.parse(deferredSignature)) map.set(key, {
|
|
511
|
+
url,
|
|
512
|
+
path,
|
|
513
|
+
variantKey,
|
|
514
|
+
params,
|
|
515
|
+
search
|
|
516
|
+
});
|
|
517
|
+
return map;
|
|
518
|
+
}, [deferredSignature, signature]);
|
|
519
|
+
const entries = current.map((e) => {
|
|
520
|
+
const visible = (e.suspense && e.config.keepPreviousData ? deferred.get(e.key) : void 0) ?? e;
|
|
521
|
+
const isDeferred = visible.path !== e.path || visible.variantKey !== e.variantKey;
|
|
522
|
+
const seed = e.config.fallbackData != null ? { [visible.variantKey]: e.config.fallbackData } : prefetchedData?.[visible.path];
|
|
523
|
+
return {
|
|
524
|
+
...e,
|
|
525
|
+
url: visible.url,
|
|
526
|
+
path: visible.path,
|
|
527
|
+
variantKey: visible.variantKey,
|
|
528
|
+
params: visible.params,
|
|
529
|
+
search: visible.search,
|
|
530
|
+
currentPath: e.path,
|
|
531
|
+
currentVariantKey: e.variantKey,
|
|
532
|
+
isDeferred,
|
|
533
|
+
resource: getResource(visible.path, seed ?? void 0)
|
|
534
|
+
};
|
|
535
|
+
});
|
|
536
|
+
const entriesRef = useRef(entries);
|
|
537
|
+
entriesRef.current = entries;
|
|
538
|
+
const targets = useMemo(() => entriesRef.current.map(({ key, path, variantKey, suspense, resource }) => ({
|
|
539
|
+
key,
|
|
540
|
+
path,
|
|
541
|
+
variantKey,
|
|
542
|
+
suspense,
|
|
543
|
+
resource
|
|
544
|
+
})), [entries.map((e) => `${e.key}\u0000${e.path}\u0000${e.variantKey}\u0000${e.suspense}`).join("\n"), getResource]);
|
|
545
|
+
const subscribe = useCallback((onStoreChange) => {
|
|
546
|
+
const releases = targets.map((t) => t.resource.retain(t.variantKey));
|
|
547
|
+
let notified = targets.map((t) => t.resource.peek(t.variantKey));
|
|
548
|
+
let active = true;
|
|
549
|
+
let scheduled = false;
|
|
550
|
+
const flush = () => {
|
|
551
|
+
scheduled = false;
|
|
552
|
+
if (!active) return;
|
|
553
|
+
const states = targets.map((t) => t.resource.peek(t.variantKey));
|
|
554
|
+
if (!states.some((state, i) => state !== notified[i] && notified[i]?.hasData) && targets.some(awaitingFirstAnswer)) return;
|
|
555
|
+
notified = states;
|
|
556
|
+
onStoreChange();
|
|
557
|
+
};
|
|
558
|
+
const onChange = () => {
|
|
559
|
+
if (scheduled) return;
|
|
560
|
+
scheduled = true;
|
|
561
|
+
queueMicrotask(flush);
|
|
562
|
+
};
|
|
563
|
+
const unsubscribes = [...new Set(targets.map((t) => t.resource))].map((resource) => resource.store.subscribe(onChange));
|
|
564
|
+
return () => {
|
|
565
|
+
active = false;
|
|
566
|
+
for (const unsubscribe of unsubscribes) unsubscribe();
|
|
567
|
+
for (const release of releases) release();
|
|
568
|
+
};
|
|
569
|
+
}, [targets]);
|
|
570
|
+
const cacheRef = useRef(null);
|
|
571
|
+
const getSnapshot = useCallback(() => {
|
|
572
|
+
const states = targets.map((t) => t.resource.peek(t.variantKey));
|
|
573
|
+
const cached = cacheRef.current;
|
|
574
|
+
if (cached && cached.targets === targets && states.every((state, i) => state === cached.states[i])) return cached.states;
|
|
575
|
+
cacheRef.current = {
|
|
576
|
+
targets,
|
|
577
|
+
states
|
|
578
|
+
};
|
|
579
|
+
return states;
|
|
580
|
+
}, [targets]);
|
|
581
|
+
const serverCacheRef = useRef(null);
|
|
582
|
+
const snapshot = useSyncExternalStore(subscribe, getSnapshot, useCallback(() => {
|
|
583
|
+
const states = targets.map((t) => t.suspense ? t.resource.peek(t.variantKey) : t.resource.peekForHydration(t.variantKey));
|
|
584
|
+
const cached = serverCacheRef.current;
|
|
585
|
+
if (cached && cached.targets === targets && states.every((state, i) => state === cached.states[i])) return cached.states;
|
|
586
|
+
serverCacheRef.current = {
|
|
587
|
+
targets,
|
|
588
|
+
states
|
|
589
|
+
};
|
|
590
|
+
return states;
|
|
591
|
+
}, [targets]));
|
|
592
|
+
const lastDataRef = useRef(/* @__PURE__ */ new Map());
|
|
593
|
+
useEffect(() => {
|
|
594
|
+
snapshot.forEach((state, i) => {
|
|
595
|
+
if (state?.hasData) lastDataRef.current.set(targets[i].key, state);
|
|
596
|
+
});
|
|
597
|
+
}, [snapshot, targets]);
|
|
598
|
+
const states = entries.map((e, i) => {
|
|
599
|
+
const state = snapshot[i];
|
|
600
|
+
const last = lastDataRef.current.get(e.key);
|
|
601
|
+
if (e.config.keepPreviousData && !state?.hasData && (state ? state.loading : true) && last) return {
|
|
602
|
+
...last,
|
|
603
|
+
loading: true
|
|
604
|
+
};
|
|
605
|
+
return state;
|
|
606
|
+
});
|
|
607
|
+
const promises = [];
|
|
608
|
+
let thrown = null;
|
|
609
|
+
entries.forEach((e, i) => {
|
|
610
|
+
if (!e.suspense) {
|
|
611
|
+
if (snapshot[i]?.hasData || snapshot[i]?.error) return;
|
|
612
|
+
if (typeof window === "undefined") serverQueries?.ensure(e.url, {
|
|
613
|
+
params: e.params,
|
|
614
|
+
search: e.search
|
|
615
|
+
});
|
|
616
|
+
else e.resource.start(e.variantKey);
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
const state = states[i];
|
|
620
|
+
if (state?.hasData || state?.error) return;
|
|
621
|
+
if (typeof window === "undefined") {
|
|
622
|
+
const entry = serverQueries?.ensure(e.url, {
|
|
623
|
+
params: e.params,
|
|
624
|
+
search: e.search
|
|
625
|
+
}) ?? null;
|
|
626
|
+
if (entry?.status === "resolved") {
|
|
627
|
+
states[i] = {
|
|
628
|
+
loading: false,
|
|
629
|
+
data: entry.data,
|
|
630
|
+
hasData: true,
|
|
631
|
+
error: null,
|
|
632
|
+
version: 0
|
|
633
|
+
};
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
if (entry?.status === "rejected") thrown ??= { error: entry.error };
|
|
637
|
+
else if (entry) promises.push(entry.promise);
|
|
638
|
+
if (!entry && process.env.NODE_ENV !== "production") warnUnprefetched(e.url, e.variantKey, "useMultipleQuery");
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const { promise } = e.resource.read(e.variantKey, e.config.staleTime);
|
|
642
|
+
if (promise) promises.push(promise);
|
|
643
|
+
});
|
|
644
|
+
const [liveOpen, setLiveOpen] = useState({});
|
|
645
|
+
const liveKey = JSON.stringify(entries.filter((e) => e.config.live).map((e) => [e.key, e.config.live]));
|
|
646
|
+
useEffect(() => {
|
|
647
|
+
const stops = JSON.parse(liveKey).map(([key, live]) => {
|
|
648
|
+
const initial = entriesRef.current.find((e) => e.key === key).resource;
|
|
649
|
+
return followLive(realtime, live, () => entriesRef.current.find((e) => e.key === key)?.resource ?? initial, (open) => setLiveOpen((prev) => prev[key] === open ? prev : {
|
|
650
|
+
...prev,
|
|
651
|
+
[key]: open
|
|
652
|
+
}));
|
|
653
|
+
});
|
|
654
|
+
return () => {
|
|
655
|
+
for (const stop of stops) stop();
|
|
656
|
+
};
|
|
657
|
+
}, [realtime, liveKey]);
|
|
658
|
+
const pendingKey = JSON.stringify(entries.filter((e) => e.isDeferred).map((e) => [
|
|
659
|
+
e.key,
|
|
660
|
+
e.currentPath,
|
|
661
|
+
e.currentVariantKey
|
|
662
|
+
]));
|
|
663
|
+
useEffect(() => {
|
|
664
|
+
const releases = JSON.parse(pendingKey).map(([key, path, variantKey]) => {
|
|
665
|
+
const config = entriesRef.current.find((e) => e.key === key)?.config;
|
|
666
|
+
const seed = config?.fallbackData != null ? { [variantKey]: config.fallbackData } : prefetchedData?.[path];
|
|
667
|
+
return getResource(path, seed ?? void 0).retain(variantKey);
|
|
668
|
+
});
|
|
669
|
+
return () => {
|
|
670
|
+
for (const release of releases) release();
|
|
671
|
+
};
|
|
672
|
+
}, [
|
|
673
|
+
pendingKey,
|
|
674
|
+
getResource,
|
|
675
|
+
prefetchedData
|
|
676
|
+
]);
|
|
677
|
+
useEffect(() => {
|
|
678
|
+
for (const t of targets) {
|
|
679
|
+
const config = entriesRef.current.find((e) => e.key === t.key)?.config;
|
|
680
|
+
t.resource.getVariant(t.variantKey, config?.staleTime);
|
|
681
|
+
}
|
|
682
|
+
}, [targets]);
|
|
683
|
+
const configOf = (key) => entriesRef.current.find((e) => e.key === key)?.config ?? defaultConfig;
|
|
684
|
+
const retriedRef = useRef(/* @__PURE__ */ new Map());
|
|
685
|
+
useEffect(() => {
|
|
686
|
+
targets.forEach((t, i) => {
|
|
687
|
+
const state = snapshot[i];
|
|
688
|
+
if (retriedRef.current.get(t.key) === state) return;
|
|
689
|
+
retriedRef.current.set(t.key, state);
|
|
690
|
+
const config = configOf(t.key);
|
|
691
|
+
if (config.suspense !== false) return;
|
|
692
|
+
retryFailedQuery(t.resource, t.variantKey, state, config);
|
|
693
|
+
});
|
|
694
|
+
});
|
|
695
|
+
useEffect(() => {
|
|
696
|
+
const cleanups = targets.map((t) => retryWhenOnline(t.resource, t.variantKey, () => configOf(t.key)));
|
|
697
|
+
return () => {
|
|
698
|
+
for (const cleanup of cleanups) cleanup();
|
|
699
|
+
};
|
|
700
|
+
}, [targets]);
|
|
701
|
+
const pollsRef = useRef(/* @__PURE__ */ new Map());
|
|
702
|
+
useEffect(() => {
|
|
703
|
+
const polls = pollsRef.current;
|
|
704
|
+
const keys = new Set(targets.map((t) => t.key));
|
|
705
|
+
for (const [key, poll] of polls) if (!keys.has(key)) {
|
|
706
|
+
stopPoll(poll);
|
|
707
|
+
polls.delete(key);
|
|
708
|
+
}
|
|
709
|
+
targets.forEach((t, i) => {
|
|
710
|
+
let poll = polls.get(t.key);
|
|
711
|
+
if (!poll) {
|
|
712
|
+
poll = createPollState();
|
|
713
|
+
polls.set(t.key, poll);
|
|
714
|
+
}
|
|
715
|
+
stepPoll(poll, t.resource, t.variantKey, snapshot[i], configOf(t.key).refetchUntil, !!liveOpen[t.key]);
|
|
716
|
+
});
|
|
717
|
+
});
|
|
718
|
+
useEffect(() => () => {
|
|
719
|
+
for (const poll of pollsRef.current.values()) stopPoll(poll);
|
|
720
|
+
}, []);
|
|
721
|
+
const intervalsKey = JSON.stringify(entries.map((e) => liveOpen[e.key] ? null : e.config.refreshInterval));
|
|
722
|
+
useEffect(() => {
|
|
723
|
+
const intervals = JSON.parse(intervalsKey);
|
|
724
|
+
const timers = targets.map((t, i) => intervals[i] == null ? null : setInterval(() => reloadQuery(t.resource, t.variantKey, configOf(t.key)), intervals[i]));
|
|
725
|
+
return () => {
|
|
726
|
+
for (const timer of timers) if (timer) clearInterval(timer);
|
|
727
|
+
};
|
|
728
|
+
}, [targets, intervalsKey]);
|
|
729
|
+
const focusRef = useRef(/* @__PURE__ */ new Map());
|
|
730
|
+
const focusKey = entries.map((e) => e.config.revalidateOnFocus ? 1 : 0).join();
|
|
731
|
+
useEffect(() => {
|
|
732
|
+
const flags = focusKey.split(",");
|
|
733
|
+
const cleanups = targets.map((t, i) => {
|
|
734
|
+
if (flags[i] !== "1") return null;
|
|
735
|
+
let focus = focusRef.current.get(t.key);
|
|
736
|
+
if (!focus) {
|
|
737
|
+
focus = {
|
|
738
|
+
eligible: true,
|
|
739
|
+
wasAway: false,
|
|
740
|
+
last: 0
|
|
741
|
+
};
|
|
742
|
+
focusRef.current.set(t.key, focus);
|
|
743
|
+
}
|
|
744
|
+
return revalidateOnFocus(t.resource, t.variantKey, focus, () => configOf(t.key));
|
|
745
|
+
});
|
|
746
|
+
return () => {
|
|
747
|
+
for (const cleanup of cleanups) cleanup?.();
|
|
748
|
+
};
|
|
749
|
+
}, [targets, focusKey]);
|
|
750
|
+
useEffect(() => onHttpReload(() => {
|
|
751
|
+
for (const t of targets) reloadQuery(t.resource, t.variantKey, configOf(t.key));
|
|
752
|
+
}), [targets]);
|
|
753
|
+
if (thrown) throw thrown.error;
|
|
754
|
+
entries.forEach((e, i) => {
|
|
755
|
+
const state = states[i];
|
|
756
|
+
if (e.suspense && state?.error && !state?.hasData) throw state.error;
|
|
757
|
+
});
|
|
758
|
+
if (promises.length > 0) throw promises.length === 1 ? promises[0] : Promise.all(promises);
|
|
759
|
+
const data = {};
|
|
760
|
+
const error = {};
|
|
761
|
+
const mutate = {};
|
|
762
|
+
const refetch = {};
|
|
763
|
+
const loadingByKey = {};
|
|
764
|
+
let loading = false;
|
|
765
|
+
entries.forEach((e, i) => {
|
|
766
|
+
const state = states[i];
|
|
767
|
+
data[e.key] = state?.data;
|
|
768
|
+
error[e.key] = state?.error ?? null;
|
|
769
|
+
loadingByKey[e.key] = e.isDeferred || (state?.loading ?? true);
|
|
770
|
+
if (loadingByKey[e.key]) loading = true;
|
|
771
|
+
mutate[e.key] = (fn) => {
|
|
772
|
+
mutateQuery(e.resource, e.variantKey, fn);
|
|
773
|
+
};
|
|
774
|
+
refetch[e.key] = () => e.resource.refetch(e.variantKey);
|
|
775
|
+
});
|
|
776
|
+
return {
|
|
777
|
+
data,
|
|
778
|
+
loading,
|
|
779
|
+
loadingByKey,
|
|
780
|
+
error,
|
|
781
|
+
mutate,
|
|
782
|
+
refetch
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
//#endregion
|
|
367
786
|
//#region client/useInfiniteQuery.ts
|
|
368
787
|
/**
|
|
369
788
|
* Follows the cursor chain through the cache: the first page, then
|
|
@@ -562,7 +981,7 @@ var SOCKET_ID_HEADER = "x-gemi-socket";
|
|
|
562
981
|
*/
|
|
563
982
|
var mutationExtras = {
|
|
564
983
|
current: null,
|
|
565
|
-
load: () => import("../chunks/mutationExtras-
|
|
984
|
+
load: () => import("../chunks/mutationExtras-bNF9JOSk.js")
|
|
566
985
|
};
|
|
567
986
|
/** Starts loading `./mutationExtras`: the hooks' mount effect. */
|
|
568
987
|
function preloadMutationExtras() {
|
|
@@ -920,7 +1339,7 @@ function useMutate() {
|
|
|
920
1339
|
//#region client/fetchFile.ts
|
|
921
1340
|
var request = {
|
|
922
1341
|
current: null,
|
|
923
|
-
load: () => import("../chunks/fetchFileRequest-
|
|
1342
|
+
load: () => import("../chunks/fetchFileRequest-Bhh5BC_l.js")
|
|
924
1343
|
};
|
|
925
1344
|
/**
|
|
926
1345
|
* Downloads a file route's body as a `Blob`.
|
|
@@ -986,7 +1405,7 @@ function useSetQueryData() {
|
|
|
986
1405
|
//#endregion
|
|
987
1406
|
//#region client/useSubscription.ts
|
|
988
1407
|
/** `./subscriptionStream`, loaded on first use. Replaceable in tests. */
|
|
989
|
-
var streamModule = { load: () => import("../chunks/subscriptionStream-
|
|
1408
|
+
var streamModule = { load: () => import("../chunks/subscriptionStream-D42DpDiZ.js") };
|
|
990
1409
|
/**
|
|
991
1410
|
* Follows a route that answers with `ChangeFeed.stream`, as long as the
|
|
992
1411
|
* component is mounted: each change reaches `onChange`, in order, exactly
|
|
@@ -3281,6 +3700,6 @@ function onStaticNavigate(callback) {
|
|
|
3281
3700
|
return () => document.removeEventListener("gemi:page-load", listener);
|
|
3282
3701
|
}
|
|
3283
3702
|
//#endregion
|
|
3284
|
-
export { CHUNK_RELOAD_MARKER, Form, FormError, FormFieldContainer, Head, Image, Link, OpenGraphImage, QueryError, QueryManagerProvider, Redirect, ValidationErrors, configureRealtime, create, createRoot, defineDictionary, fetchBlob, fetchText, init, isAuthenticationError, isChunkLoadError, isCsrfError, isFormError, isHttpError, isNetworkError, isNotFoundError, isPermissionError, isRateLimitError, isRetryableQueryError, isServerError, isValidationError, island, mutationErrorField, mutationErrorKind, mutationErrorList, onStaticNavigate, recoverFromChunkLoadError, textDirection, useAppIdMissmatch, useBreadcrumbs, useChannel, useChannelInvalidate, useCspNonce, useDelete, useDictionary, useDomain, useEmailCode, useFeature, useFeatures, useForgotPassword, useFormData, useFormStatus, useFormatter, useInfiniteQuery, useIntendedUrl, useIsNavigationPending, useLocale, useLocation, useMutate, useMutation, useMutationStatus, useNavigate, useNavigationProgress, useParams, usePatch, usePost, usePrefetch, usePut, useQuery, useResetPassword, useRoute, useRouteTransition, useSearchParams, useSetQueryData, useSignIn, useSignOut, useSignUp, useSubscription, useTheme, useTranslator, useUpload, useUser };
|
|
3703
|
+
export { CHUNK_RELOAD_MARKER, Form, FormError, FormFieldContainer, Head, Image, Link, OpenGraphImage, QueryError, QueryManagerProvider, Redirect, ValidationErrors, configureRealtime, create, createRoot, defineDictionary, fetchBlob, fetchText, init, isAuthenticationError, isChunkLoadError, isCsrfError, isFormError, isHttpError, isNetworkError, isNotFoundError, isPermissionError, isRateLimitError, isRetryableQueryError, isServerError, isValidationError, island, mutationErrorField, mutationErrorKind, mutationErrorList, onStaticNavigate, recoverFromChunkLoadError, textDirection, useAppIdMissmatch, useBreadcrumbs, useChannel, useChannelInvalidate, useCspNonce, useDelete, useDictionary, useDomain, useEmailCode, useFeature, useFeatures, useForgotPassword, useFormData, useFormStatus, useFormatter, useInfiniteQuery, useIntendedUrl, useIsNavigationPending, useLocale, useLocation, useMultipleQuery, useMutate, useMutation, useMutationStatus, useNavigate, useNavigationProgress, useParams, usePatch, usePost, usePrefetch, usePut, useQuery, useResetPassword, useRoute, useRouteTransition, useSearchParams, useSetQueryData, useSignIn, useSignOut, useSignUp, useSubscription, useTheme, useTranslator, useUpload, useUser };
|
|
3285
3704
|
|
|
3286
3705
|
//# sourceMappingURL=index.js.map
|