@choksheak/react-utils 0.3.9 → 0.4.1
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/NoSSR.cjs +7 -30
- package/NoSSR.min.cjs +1 -1
- package/NoSSR.min.cjs.map +1 -1
- package/NoSSR.min.mjs +1 -1
- package/NoSSR.min.mjs.map +1 -1
- package/NoSSR.mjs +2 -0
- package/chunk-33UMQLKY.mjs +43 -0
- package/chunk-3X25NN6X.cjs +195 -0
- package/chunk-5DO7KC3L.cjs +43 -0
- package/chunk-5PDGU37E.cjs +104 -0
- package/chunk-74BS2FAO.cjs +12 -0
- package/chunk-F5L32IUC.min.cjs +2 -0
- package/chunk-F5L32IUC.min.cjs.map +1 -0
- package/chunk-GPV6JIQK.mjs +17 -0
- package/chunk-KE3RXVMS.min.cjs +2 -0
- package/chunk-KE3RXVMS.min.cjs.map +1 -0
- package/chunk-KILGR7YA.min.mjs +2 -0
- package/chunk-KILGR7YA.min.mjs.map +1 -0
- package/chunk-KRXENFK3.mjs +195 -0
- package/chunk-MWDH7YON.min.mjs +2 -0
- package/chunk-MWDH7YON.min.mjs.map +1 -0
- package/chunk-NTFFK7ZH.min.cjs +2 -0
- package/chunk-NTFFK7ZH.min.cjs.map +1 -0
- package/chunk-PHLY3LR4.mjs +104 -0
- package/chunk-RM4ZGGZ2.min.mjs +2 -0
- package/chunk-RM4ZGGZ2.min.mjs.map +1 -0
- package/chunk-TEZPF6BG.min.cjs +2 -0
- package/chunk-TEZPF6BG.min.cjs.map +1 -0
- package/chunk-UFJQLQFI.min.cjs +2 -0
- package/chunk-UFJQLQFI.min.cjs.map +1 -0
- package/chunk-V7L3UICC.min.mjs +2 -0
- package/chunk-V7L3UICC.min.mjs.map +1 -0
- package/chunk-VHMO75EY.min.mjs +2 -0
- package/chunk-VHMO75EY.min.mjs.map +1 -0
- package/chunk-WVLPJPWG.cjs +17 -0
- package/chunk-X6BYQHVC.mjs +12 -0
- package/package.json +104 -43
- package/sharedQuery.cjs +130 -391
- package/sharedQuery.d.mts +55 -10
- package/sharedQuery.d.ts +54 -9
- package/sharedQuery.min.cjs +1 -1
- package/sharedQuery.min.cjs.map +1 -1
- package/sharedQuery.min.mjs +1 -1
- package/sharedQuery.min.mjs.map +1 -1
- package/sharedQuery.mjs +92 -321
- package/sharedState.cjs +15 -304
- package/sharedState.d.mts +4 -4
- package/sharedState.d.ts +4 -4
- package/sharedState.min.cjs +1 -1
- package/sharedState.min.cjs.map +1 -1
- package/sharedState.min.mjs +1 -1
- package/sharedState.min.mjs.map +1 -1
- package/sharedState.mjs +10 -269
- package/storage.cjs +10 -123
- package/storage.d.mts +1 -0
- package/storage.d.ts +1 -0
- package/storage.min.cjs +1 -1
- package/storage.min.cjs.map +1 -1
- package/storage.min.mjs +1 -1
- package/storage.min.mjs.map +1 -1
- package/storage.mjs +6 -92
- package/stringify.cjs +8 -57
- package/stringify.min.cjs +1 -1
- package/stringify.min.cjs.map +1 -1
- package/stringify.min.mjs +1 -1
- package/stringify.min.mjs.map +1 -1
- package/stringify.mjs +6 -30
- package/useDeepMemo.cjs +6 -50
- package/useDeepMemo.min.cjs +1 -1
- package/useDeepMemo.min.cjs.map +1 -1
- package/useDeepMemo.min.mjs +1 -1
- package/useDeepMemo.min.mjs.map +1 -1
- package/useDeepMemo.mjs +4 -13
- package/fetcher.cjs +0 -51
- package/fetcher.d.mts +0 -36
- package/fetcher.d.ts +0 -32
- package/fetcher.min.cjs +0 -2
- package/fetcher.min.cjs.map +0 -1
- package/fetcher.min.mjs +0 -2
- package/fetcher.min.mjs.map +0 -1
- package/fetcher.mjs +0 -15
package/sharedQuery.mjs
CHANGED
|
@@ -1,325 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sharedState,
|
|
3
|
+
useSharedState
|
|
4
|
+
} from "./chunk-KRXENFK3.mjs";
|
|
5
|
+
import "./chunk-PHLY3LR4.mjs";
|
|
6
|
+
import {
|
|
7
|
+
init_stringify,
|
|
8
|
+
stringifyDeterministicForKeys
|
|
9
|
+
} from "./chunk-33UMQLKY.mjs";
|
|
10
|
+
import {
|
|
11
|
+
useDeepMemo
|
|
12
|
+
} from "./chunk-GPV6JIQK.mjs";
|
|
13
|
+
import "./chunk-X6BYQHVC.mjs";
|
|
14
|
+
|
|
1
15
|
// src/sharedQuery.ts
|
|
2
|
-
import {
|
|
16
|
+
import { http, retries } from "@choksheak/ts-utils/http";
|
|
3
17
|
import getByteSize from "object-sizeof";
|
|
4
18
|
import {
|
|
5
19
|
useCallback,
|
|
6
20
|
useEffect,
|
|
7
|
-
useMemo
|
|
21
|
+
useMemo,
|
|
8
22
|
useRef,
|
|
9
|
-
useState
|
|
23
|
+
useState
|
|
10
24
|
} from "react";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import { MS_PER_DAY } from "@choksheak/ts-utils/timeConstants";
|
|
14
|
-
import { useSyncExternalStore } from "react";
|
|
15
|
-
|
|
16
|
-
// src/storage.ts
|
|
17
|
-
import { kvStore } from "@choksheak/ts-utils/kvStore";
|
|
18
|
-
import { createLocalStore } from "@choksheak/ts-utils/localStore";
|
|
19
|
-
var IS_NOT_BROWSER = typeof window === "undefined";
|
|
20
|
-
function getStorageAdapter(options, defaultStoreExpiryMs) {
|
|
21
|
-
const { store } = options ?? {};
|
|
22
|
-
if (!store) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
if ("persistTo" in store && typeof store.persistTo === "string" && "key" in store && typeof store.key === "string") {
|
|
26
|
-
const expiryMs = store.expiryMs || defaultStoreExpiryMs;
|
|
27
|
-
const config = { ...store, expiryMs };
|
|
28
|
-
switch (store.persistTo) {
|
|
29
|
-
case "localStorage":
|
|
30
|
-
return localStoreAdapter(config);
|
|
31
|
-
case "indexedDb":
|
|
32
|
-
return kvStoreAdapter(config);
|
|
33
|
-
default:
|
|
34
|
-
store.persistTo;
|
|
35
|
-
throw new Error(`Unknown store.adapter ${store.persistTo}`);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return store;
|
|
39
|
-
}
|
|
40
|
-
function localStoreAdapter({
|
|
41
|
-
key,
|
|
42
|
-
expiryMs,
|
|
43
|
-
isValid
|
|
44
|
-
}) {
|
|
45
|
-
const adapter = createLocalStore(key, { defaultExpiryMs: expiryMs });
|
|
46
|
-
return {
|
|
47
|
-
set: (key2, value) => {
|
|
48
|
-
if (IS_NOT_BROWSER) return;
|
|
49
|
-
adapter.set(key2, value);
|
|
50
|
-
},
|
|
51
|
-
get: (key2) => {
|
|
52
|
-
if (IS_NOT_BROWSER) return void 0;
|
|
53
|
-
const value = adapter.get(key2);
|
|
54
|
-
if (isValid && !isValid(value)) {
|
|
55
|
-
console.warn(
|
|
56
|
-
`localStorage adapter: Auto-discard invalid value for ${key2}: ${JSON.stringify(value)}`
|
|
57
|
-
);
|
|
58
|
-
adapter.delete(key2);
|
|
59
|
-
return void 0;
|
|
60
|
-
}
|
|
61
|
-
return value;
|
|
62
|
-
},
|
|
63
|
-
delete: (key2) => {
|
|
64
|
-
if (IS_NOT_BROWSER) return;
|
|
65
|
-
adapter.delete(key2);
|
|
66
|
-
},
|
|
67
|
-
clear: () => {
|
|
68
|
-
if (IS_NOT_BROWSER) return;
|
|
69
|
-
adapter.clear();
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
function kvStoreAdapter({
|
|
74
|
-
key,
|
|
75
|
-
expiryMs,
|
|
76
|
-
isValid
|
|
77
|
-
}) {
|
|
78
|
-
const keyPrefix = key + ":";
|
|
79
|
-
return {
|
|
80
|
-
set: async (key2, value) => {
|
|
81
|
-
if (IS_NOT_BROWSER) return;
|
|
82
|
-
await kvStore.set(keyPrefix + key2, value, expiryMs);
|
|
83
|
-
},
|
|
84
|
-
get: async (key2) => {
|
|
85
|
-
if (IS_NOT_BROWSER) return void 0;
|
|
86
|
-
const fullKey = keyPrefix + key2;
|
|
87
|
-
const value = await kvStore.get(fullKey);
|
|
88
|
-
if (value !== void 0 && isValid && !isValid(value)) {
|
|
89
|
-
console.warn(
|
|
90
|
-
`indexedDb adapter: Auto-discard invalid value for ${key2}:`,
|
|
91
|
-
value
|
|
92
|
-
);
|
|
93
|
-
await kvStore.delete(fullKey);
|
|
94
|
-
return void 0;
|
|
95
|
-
}
|
|
96
|
-
return value;
|
|
97
|
-
},
|
|
98
|
-
delete: async (key2) => {
|
|
99
|
-
if (IS_NOT_BROWSER) return;
|
|
100
|
-
await kvStore.delete(keyPrefix + key2);
|
|
101
|
-
},
|
|
102
|
-
clear: async () => {
|
|
103
|
-
if (IS_NOT_BROWSER) return;
|
|
104
|
-
await kvStore.clear();
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// src/sharedState.ts
|
|
110
|
-
var SharedStateConfig = {
|
|
111
|
-
/**
|
|
112
|
-
* Default duration for storage expiration in milliseconds. Initial value
|
|
113
|
-
* is 30 days, but can be changed to anything.
|
|
114
|
-
*/
|
|
115
|
-
storeExpiryMs: 30 * MS_PER_DAY,
|
|
116
|
-
/**
|
|
117
|
-
* True to load from storage only on mount, false (default) to load in
|
|
118
|
-
* the top level scope when the shared state is defined.
|
|
119
|
-
*/
|
|
120
|
-
lazyLoad: false
|
|
121
|
-
};
|
|
122
|
-
function createPubSubStore() {
|
|
123
|
-
const dataByKey = /* @__PURE__ */ new Map();
|
|
124
|
-
const subscribersByKey = /* @__PURE__ */ new Map();
|
|
125
|
-
function notify(key, next, prev) {
|
|
126
|
-
const subscribers = subscribersByKey.get(key);
|
|
127
|
-
if (!subscribers) return;
|
|
128
|
-
const copy = Array.from(subscribers.values());
|
|
129
|
-
for (const subscriber of copy) {
|
|
130
|
-
try {
|
|
131
|
-
subscriber(next, prev);
|
|
132
|
-
} catch (e) {
|
|
133
|
-
console.error(`Error invoking subscriber:`, e);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return {
|
|
138
|
-
// This could be undefined if T includes undefined, else it should never
|
|
139
|
-
// return undefined because the user has to always specify the default value.
|
|
140
|
-
get(key) {
|
|
141
|
-
return dataByKey.get(key);
|
|
142
|
-
},
|
|
143
|
-
set(key, value) {
|
|
144
|
-
const prev = dataByKey.get(key);
|
|
145
|
-
if (prev === value) return;
|
|
146
|
-
dataByKey.set(key, value);
|
|
147
|
-
notify(key, value, prev);
|
|
148
|
-
},
|
|
149
|
-
delete(key) {
|
|
150
|
-
dataByKey.delete(key);
|
|
151
|
-
},
|
|
152
|
-
setNoNotify(key, value) {
|
|
153
|
-
dataByKey.set(key, value);
|
|
154
|
-
},
|
|
155
|
-
// Subscribe to changes of a specific key
|
|
156
|
-
subscribe(key, subscriber) {
|
|
157
|
-
let subscribers = subscribersByKey.get(key);
|
|
158
|
-
if (!subscribers) {
|
|
159
|
-
subscribers = /* @__PURE__ */ new Set();
|
|
160
|
-
subscribersByKey.set(key, subscribers);
|
|
161
|
-
}
|
|
162
|
-
subscribers.add(subscriber);
|
|
163
|
-
return () => {
|
|
164
|
-
subscribers.delete(subscriber);
|
|
165
|
-
if (subscribers.size === 0) subscribersByKey.delete(key);
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
var pubSubStore = createPubSubStore();
|
|
171
|
-
var STORAGE_KEY = "state";
|
|
172
|
-
var stateKey = 0;
|
|
173
|
-
function sharedState(defaultValue, options) {
|
|
174
|
-
let readyResolve = () => {
|
|
175
|
-
};
|
|
176
|
-
const readyPromise = new Promise((resolve) => {
|
|
177
|
-
readyResolve = resolve;
|
|
178
|
-
});
|
|
179
|
-
const resolveReadyPromise = readyResolve;
|
|
180
|
-
const pubSubKey = String(stateKey++);
|
|
181
|
-
const storageAdapter = getStorageAdapter(
|
|
182
|
-
options,
|
|
183
|
-
SharedStateConfig.storeExpiryMs
|
|
184
|
-
);
|
|
185
|
-
pubSubStore.setNoNotify(pubSubKey, defaultValue);
|
|
186
|
-
const lazyLoad = options?.lazyLoad;
|
|
187
|
-
const lazy = lazyLoad !== void 0 ? lazyLoad : SharedStateConfig.lazyLoad;
|
|
188
|
-
const obj = {
|
|
189
|
-
/** Allow users to await for the completion of data init. */
|
|
190
|
-
readyPromise,
|
|
191
|
-
lazy,
|
|
192
|
-
initStarted: false,
|
|
193
|
-
initDone: false,
|
|
194
|
-
/** `subscribe` function required by useSyncExternalStore. */
|
|
195
|
-
subscribe(subscriber) {
|
|
196
|
-
return pubSubStore.subscribe(pubSubKey, () => subscriber());
|
|
197
|
-
},
|
|
198
|
-
/** Returns true if there is a backing store. */
|
|
199
|
-
hasStore() {
|
|
200
|
-
return Boolean(storageAdapter);
|
|
201
|
-
},
|
|
202
|
-
/**
|
|
203
|
-
* Set a value directly into the store. Does nothing if there is no
|
|
204
|
-
* backing store.
|
|
205
|
-
*
|
|
206
|
-
* Warning: If you want to set the value properly, you should use
|
|
207
|
-
* `setValue()` instead which also updates the in-memory copy.
|
|
208
|
-
*/
|
|
209
|
-
async setInStore(value) {
|
|
210
|
-
return await storageAdapter?.set(STORAGE_KEY, value);
|
|
211
|
-
},
|
|
212
|
-
/**
|
|
213
|
-
* Get a value directly from the store. Returns undefined if there is no
|
|
214
|
-
* backing store.
|
|
215
|
-
*/
|
|
216
|
-
async getFromStore() {
|
|
217
|
-
return await storageAdapter?.get(STORAGE_KEY);
|
|
218
|
-
},
|
|
219
|
-
/**
|
|
220
|
-
* Get the value of the shared state.
|
|
221
|
-
* `getSnapshot` function — must return same ref if value unchanged.
|
|
222
|
-
* Note that this would return the default value if data init from store is
|
|
223
|
-
* not completed yet.
|
|
224
|
-
*/
|
|
225
|
-
getSnapshot() {
|
|
226
|
-
return pubSubStore.get(pubSubKey);
|
|
227
|
-
},
|
|
228
|
-
/** Set the value of the shared state. */
|
|
229
|
-
setValue(next) {
|
|
230
|
-
if (typeof next === "function") {
|
|
231
|
-
const prev = pubSubStore.get(pubSubKey);
|
|
232
|
-
next = next(prev);
|
|
233
|
-
}
|
|
234
|
-
pubSubStore.set(pubSubKey, next);
|
|
235
|
-
void obj.setInStore(next);
|
|
236
|
-
},
|
|
237
|
-
/**
|
|
238
|
-
* Remove the value of the shared state. Actually this just sets the value
|
|
239
|
-
* back to the given default value.
|
|
240
|
-
*/
|
|
241
|
-
delete() {
|
|
242
|
-
pubSubStore.set(pubSubKey, defaultValue);
|
|
243
|
-
void storageAdapter?.set(STORAGE_KEY, defaultValue);
|
|
244
|
-
},
|
|
245
|
-
/** Initial default handling. */
|
|
246
|
-
async initDefaultValueOnce() {
|
|
247
|
-
if (obj.initDone || obj.initStarted) return;
|
|
248
|
-
obj.initStarted = true;
|
|
249
|
-
const stored = await obj.getFromStore();
|
|
250
|
-
const value = stored !== void 0 ? stored : defaultValue;
|
|
251
|
-
pubSubStore.set(pubSubKey, value);
|
|
252
|
-
obj.initDone = true;
|
|
253
|
-
resolveReadyPromise();
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
if (!lazy) {
|
|
257
|
-
void obj.initDefaultValueOnce();
|
|
258
|
-
}
|
|
259
|
-
return obj;
|
|
260
|
-
}
|
|
261
|
-
function useSharedState(state) {
|
|
262
|
-
void state.initDefaultValueOnce();
|
|
263
|
-
const value = useSyncExternalStore(
|
|
264
|
-
(onStoreChange) => state.subscribe(onStoreChange),
|
|
265
|
-
() => state.getSnapshot(),
|
|
266
|
-
// getServerSnapshot is needed to prevent SSR dev errors in nextjs.
|
|
267
|
-
() => state.getSnapshot()
|
|
268
|
-
);
|
|
269
|
-
return [value, state.setValue];
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// src/stringify.ts
|
|
273
|
-
function stringifyDeterministicForKeys(params) {
|
|
274
|
-
const cache = /* @__PURE__ */ new Set();
|
|
275
|
-
return JSON.stringify(params, (_, value) => {
|
|
276
|
-
if (typeof value === "function") {
|
|
277
|
-
throw new Error(`Functions should not be passed into query parameters`);
|
|
278
|
-
}
|
|
279
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
280
|
-
return value;
|
|
281
|
-
}
|
|
282
|
-
if (cache.has(value)) {
|
|
283
|
-
return "[Circular]";
|
|
284
|
-
}
|
|
285
|
-
cache.add(value);
|
|
286
|
-
if (value !== params) {
|
|
287
|
-
try {
|
|
288
|
-
JSON.stringify(value);
|
|
289
|
-
} catch {
|
|
290
|
-
return "[Unserializable]";
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return Object.keys(value).sort().reduce(
|
|
294
|
-
(result, key) => {
|
|
295
|
-
result[key] = value[key];
|
|
296
|
-
return result;
|
|
297
|
-
},
|
|
298
|
-
{}
|
|
299
|
-
);
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// src/useDeepMemo.ts
|
|
304
|
-
import isEqual from "lodash/isEqual";
|
|
305
|
-
import { useMemo, useState } from "react";
|
|
306
|
-
function useDeepMemo(value) {
|
|
307
|
-
const [state, setState] = useState(value);
|
|
308
|
-
return useMemo(() => {
|
|
309
|
-
if (isEqual(state, value)) {
|
|
310
|
-
return state;
|
|
311
|
-
}
|
|
312
|
-
setState(value);
|
|
313
|
-
return value;
|
|
314
|
-
}, [state, value]);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// src/sharedQuery.ts
|
|
25
|
+
import XXH from "xxhashjs";
|
|
26
|
+
init_stringify();
|
|
318
27
|
var SharedQueryConfig = {
|
|
319
28
|
/** Refetch on every page load. */
|
|
320
29
|
staleMs: 0,
|
|
321
30
|
/** Keep in cache for a long time. */
|
|
322
|
-
expiryMs: 30 *
|
|
31
|
+
expiryMs: 30 * 864e5,
|
|
323
32
|
/** Always refetch automatically if the data is stale. */
|
|
324
33
|
refetchOnStale: true,
|
|
325
34
|
/** No default persistence configured. */
|
|
@@ -336,14 +45,57 @@ function configureSharedQuery(config) {
|
|
|
336
45
|
}
|
|
337
46
|
var seenQueryNames = /* @__PURE__ */ new Set();
|
|
338
47
|
function sharedQuery(options) {
|
|
339
|
-
|
|
340
|
-
|
|
48
|
+
const url = options.url || "";
|
|
49
|
+
const queryName = options.queryName || url;
|
|
50
|
+
if (!queryName) {
|
|
51
|
+
throw new Error(`Either queryName or url must be specified.`);
|
|
52
|
+
}
|
|
53
|
+
if (seenQueryNames.has(queryName)) {
|
|
54
|
+
throw new Error(`Duplicate shared query name "${queryName}"`);
|
|
341
55
|
}
|
|
342
|
-
seenQueryNames.add(
|
|
56
|
+
seenQueryNames.add(queryName);
|
|
343
57
|
const inflightQueries = /* @__PURE__ */ new Map();
|
|
344
58
|
const mountedKeys = /* @__PURE__ */ new Map();
|
|
345
|
-
|
|
346
|
-
|
|
59
|
+
let queryFn;
|
|
60
|
+
if (options.queryFn) {
|
|
61
|
+
queryFn = options.queryFn;
|
|
62
|
+
} else if (url) {
|
|
63
|
+
const fetcher = http.use(retries());
|
|
64
|
+
queryFn = async (...args) => {
|
|
65
|
+
let params = "";
|
|
66
|
+
const firstArg = args?.[0];
|
|
67
|
+
if (firstArg) {
|
|
68
|
+
if (typeof firstArg === "string") {
|
|
69
|
+
params = firstArg;
|
|
70
|
+
} else if (typeof firstArg === "object") {
|
|
71
|
+
const entries = [];
|
|
72
|
+
for (const [k, v] of Object.entries(firstArg)) {
|
|
73
|
+
if (v === void 0) continue;
|
|
74
|
+
if (Array.isArray(v)) {
|
|
75
|
+
for (const item of v) {
|
|
76
|
+
if (item === void 0) continue;
|
|
77
|
+
entries.push([k, String(item)]);
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
entries.push([k, String(v)]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
params = new URLSearchParams(entries).toString();
|
|
84
|
+
}
|
|
85
|
+
if (params) {
|
|
86
|
+
if (url.includes("?")) {
|
|
87
|
+
params = "&" + params;
|
|
88
|
+
} else {
|
|
89
|
+
params = "?" + params;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const response = await fetcher.get(url + params);
|
|
94
|
+
return await response.json();
|
|
95
|
+
};
|
|
96
|
+
} else {
|
|
97
|
+
throw new Error(`Either queryFn or url must be specified`);
|
|
98
|
+
}
|
|
347
99
|
const refetchFn = options.refetchFn;
|
|
348
100
|
const staleMs = options?.staleMs ?? SharedQueryConfig.staleMs;
|
|
349
101
|
const expiryMs = options?.expiryMs ?? SharedQueryConfig.expiryMs;
|
|
@@ -523,7 +275,11 @@ function sharedQuery(options) {
|
|
|
523
275
|
queryState,
|
|
524
276
|
/** Returns a key to identify requests based on the given args. */
|
|
525
277
|
getQueryKey(args) {
|
|
526
|
-
|
|
278
|
+
let s = stringifyDeterministicForKeys(args);
|
|
279
|
+
if (s.length > 30) {
|
|
280
|
+
s = XXH.h32(s, 39406).toString(16).slice(2);
|
|
281
|
+
}
|
|
282
|
+
return queryName + ":" + s;
|
|
527
283
|
},
|
|
528
284
|
/**
|
|
529
285
|
* Get the AbortController to abort the inflight query (if any).
|
|
@@ -542,7 +298,7 @@ function sharedQuery(options) {
|
|
|
542
298
|
/**
|
|
543
299
|
* Get the AbortSignal to check for query abortions.
|
|
544
300
|
*
|
|
545
|
-
* Example:
|
|
301
|
+
* Example using fetch:
|
|
546
302
|
* ```
|
|
547
303
|
* const getUserQuery = sharedQuery({
|
|
548
304
|
* queryName: "getUser",
|
|
@@ -563,6 +319,21 @@ function sharedQuery(options) {
|
|
|
563
319
|
* },
|
|
564
320
|
* });
|
|
565
321
|
* ```
|
|
322
|
+
*
|
|
323
|
+
* Example using `http.ts`:
|
|
324
|
+
* ```
|
|
325
|
+
* const getUserQuery = sharedQuery({
|
|
326
|
+
* queryName: "getUser",
|
|
327
|
+
* queryFn: (userId: string) => {
|
|
328
|
+
* // Get the signal for this current execution.
|
|
329
|
+
* const signal = getUserQuery.getAbortSignal([userId]);
|
|
330
|
+
*
|
|
331
|
+
* // Pass the signal to fetch so that it can be aborted.
|
|
332
|
+
* const response = await http.get(`/users/${userId}`, { signal });
|
|
333
|
+
* return await response.json();
|
|
334
|
+
* },
|
|
335
|
+
* });
|
|
336
|
+
* ```
|
|
566
337
|
*/
|
|
567
338
|
getAbortSignal(args) {
|
|
568
339
|
const queryKey = obj.getQueryKey(args);
|
|
@@ -685,7 +456,7 @@ function useSharedQuery(query, ...args) {
|
|
|
685
456
|
const stableArgs = useDeepMemo(args);
|
|
686
457
|
const [queryState, setQueryState] = useSharedState(query.queryState);
|
|
687
458
|
const isMounted = useRef(true);
|
|
688
|
-
const queryKey =
|
|
459
|
+
const queryKey = useMemo(
|
|
689
460
|
() => query.getQueryKey(stableArgs),
|
|
690
461
|
[query, stableArgs]
|
|
691
462
|
);
|
|
@@ -700,7 +471,7 @@ function useSharedQuery(query, ...args) {
|
|
|
700
471
|
},
|
|
701
472
|
[queryKey, setQueryState]
|
|
702
473
|
);
|
|
703
|
-
const [lastNonEmptyData, setLastNonEmptyData] =
|
|
474
|
+
const [lastNonEmptyData, setLastNonEmptyData] = useState();
|
|
704
475
|
useEffect(() => {
|
|
705
476
|
if (query.keepLastNonEmptyData && queryStateValue.data !== void 0) {
|
|
706
477
|
setLastNonEmptyData(queryStateValue.data);
|
|
@@ -759,7 +530,7 @@ function useSharedQuery(query, ...args) {
|
|
|
759
530
|
query.unmount(queryKey);
|
|
760
531
|
};
|
|
761
532
|
}, [execute, query, queryKey]);
|
|
762
|
-
return
|
|
533
|
+
return useMemo(() => {
|
|
763
534
|
return {
|
|
764
535
|
...queryStateValue,
|
|
765
536
|
// Fill with lastNonEmptyData when data is undefined.
|