tsdf 16.0.2 → 17.0.0-alpha.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/README.md +2 -0
- package/dist/main.d.ts +2694 -366
- package/dist/main.js +18790 -2473
- package/docs/README.md +94 -0
- package/docs/batch-fetching.md +125 -0
- package/docs/browser-tabs-sync.md +59 -0
- package/docs/cache-limits.md +105 -0
- package/docs/collection-store.md +170 -0
- package/docs/document-store.md +147 -0
- package/docs/fetch-scheduling.md +93 -0
- package/docs/hooks.md +416 -0
- package/docs/invalidation.md +133 -0
- package/docs/list-query-store.md +348 -0
- package/docs/mutations.md +261 -0
- package/docs/offline.md +207 -0
- package/docs/offset-pagination.md +111 -0
- package/docs/optimistic-list-updates.md +180 -0
- package/docs/partial-resources.md +168 -0
- package/docs/persistent-storage.md +282 -0
- package/docs/real-time-updates.md +156 -0
- package/docs/shared-types.md +169 -0
- package/docs/store-manager.md +118 -0
- package/package.json +63 -46
- package/dist/main.cjs +0 -2510
- package/dist/main.d.cts +0 -400
package/dist/main.cjs
DELETED
|
@@ -1,2510 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/main.ts
|
|
21
|
-
var main_exports = {};
|
|
22
|
-
__export(main_exports, {
|
|
23
|
-
getCacheId: () => getCacheId,
|
|
24
|
-
newTSDFCollectionStore: () => newTSDFCollectionStore,
|
|
25
|
-
newTSDFDocumentStore: () => newTSDFDocumentStore,
|
|
26
|
-
newTSDFListQueryStore: () => newTSDFListQueryStore,
|
|
27
|
-
useListItemIsDeleted: () => useListItemIsDeleted,
|
|
28
|
-
useListItemIsLoading: () => useListItemIsLoading
|
|
29
|
-
});
|
|
30
|
-
module.exports = __toCommonJS(main_exports);
|
|
31
|
-
|
|
32
|
-
// src/documentStore.ts
|
|
33
|
-
var import_evtmitter2 = require("evtmitter");
|
|
34
|
-
var import_react4 = require("evtmitter/react");
|
|
35
|
-
var import_immer = require("immer");
|
|
36
|
-
var import_react5 = require("react");
|
|
37
|
-
var import_t_state3 = require("t-state");
|
|
38
|
-
|
|
39
|
-
// src/fetchOrquestrator.ts
|
|
40
|
-
function createFetchOrquestrator({
|
|
41
|
-
fetchFn,
|
|
42
|
-
on,
|
|
43
|
-
mediumPriorityThrottleMs = 10,
|
|
44
|
-
lowPriorityThrottleMs = 200,
|
|
45
|
-
dynamicRealtimeThrottleMs
|
|
46
|
-
}) {
|
|
47
|
-
const fetchs = {
|
|
48
|
-
a: null,
|
|
49
|
-
c: null,
|
|
50
|
-
b: null
|
|
51
|
-
};
|
|
52
|
-
let lastMutationIdStarted = 0;
|
|
53
|
-
let lastFetchIdStarted = 0;
|
|
54
|
-
let mutationIsInProgress = false;
|
|
55
|
-
let lastFetchStartTime = 0;
|
|
56
|
-
let lastFetchDuration = 0;
|
|
57
|
-
let onMutationEnd = null;
|
|
58
|
-
let lastFetchWasAborted = false;
|
|
59
|
-
let abortFetchsBeforeOrEqual = 0;
|
|
60
|
-
function setLastFetchDurantion(duration) {
|
|
61
|
-
lastFetchDuration = duration;
|
|
62
|
-
}
|
|
63
|
-
function setLastFetchStartTime(startTime) {
|
|
64
|
-
lastFetchStartTime = startTime;
|
|
65
|
-
}
|
|
66
|
-
function flushScheduledFetch() {
|
|
67
|
-
if (fetchs.c) {
|
|
68
|
-
on?.("scheduled-fetch-started");
|
|
69
|
-
const params = fetchs.c.params;
|
|
70
|
-
fetchs.c = null;
|
|
71
|
-
startFetch(params, Date.now());
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
function startMutation() {
|
|
75
|
-
mutationIsInProgress = true;
|
|
76
|
-
abortFetchsBeforeOrEqual = lastFetchIdStarted;
|
|
77
|
-
fetchs.a = null;
|
|
78
|
-
const id = getAutoIncrementId();
|
|
79
|
-
lastMutationIdStarted = id;
|
|
80
|
-
return () => endMutation(id);
|
|
81
|
-
}
|
|
82
|
-
function endMutation(id) {
|
|
83
|
-
if (id === lastMutationIdStarted) {
|
|
84
|
-
mutationIsInProgress = false;
|
|
85
|
-
if (onMutationEnd) {
|
|
86
|
-
onMutationEnd();
|
|
87
|
-
onMutationEnd = null;
|
|
88
|
-
}
|
|
89
|
-
flushScheduledFetch();
|
|
90
|
-
return true;
|
|
91
|
-
}
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
function getFetchs() {
|
|
95
|
-
return fetchs;
|
|
96
|
-
}
|
|
97
|
-
async function startFetch(params, startTime) {
|
|
98
|
-
if (fetchs.a) {
|
|
99
|
-
throw new Error("[tsdf] Fetch already in progress");
|
|
100
|
-
}
|
|
101
|
-
const id = getAutoIncrementId();
|
|
102
|
-
lastFetchIdStarted = id;
|
|
103
|
-
lastFetchWasAborted = false;
|
|
104
|
-
fetchs.a = { startTime, onEnd: [], rtuOnEnd: null };
|
|
105
|
-
const prevFetchStartTime = lastFetchStartTime;
|
|
106
|
-
lastFetchStartTime = startTime;
|
|
107
|
-
function shouldAbort() {
|
|
108
|
-
const abort = id !== lastFetchIdStarted || mutationIsInProgress || id <= abortFetchsBeforeOrEqual;
|
|
109
|
-
lastFetchWasAborted = abort;
|
|
110
|
-
return abort;
|
|
111
|
-
}
|
|
112
|
-
if (fetchs.b) {
|
|
113
|
-
clearTimeout(fetchs.b.timeoutId);
|
|
114
|
-
fetchs.b = null;
|
|
115
|
-
}
|
|
116
|
-
const success = await fetchFn(
|
|
117
|
-
{
|
|
118
|
-
shouldAbort,
|
|
119
|
-
getStartTime: () => startTime
|
|
120
|
-
},
|
|
121
|
-
params
|
|
122
|
-
);
|
|
123
|
-
const currentFetchs = getFetchs();
|
|
124
|
-
if (!currentFetchs.a) {
|
|
125
|
-
lastFetchStartTime = prevFetchStartTime;
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
if (success) {
|
|
129
|
-
lastFetchDuration = Date.now() - startTime;
|
|
130
|
-
}
|
|
131
|
-
const rtScheduled = fetchs.b;
|
|
132
|
-
if (rtScheduled) {
|
|
133
|
-
clearTimeout(rtScheduled.timeoutId);
|
|
134
|
-
fetchs.b = null;
|
|
135
|
-
}
|
|
136
|
-
const onEnd = currentFetchs.a.onEnd;
|
|
137
|
-
const rtuOnEnd = currentFetchs.a.rtuOnEnd;
|
|
138
|
-
currentFetchs.a = null;
|
|
139
|
-
if (onEnd.length > 0) {
|
|
140
|
-
onEnd.forEach((cb) => cb());
|
|
141
|
-
}
|
|
142
|
-
if (rtuOnEnd) {
|
|
143
|
-
rtuOnEnd();
|
|
144
|
-
}
|
|
145
|
-
flushScheduledFetch();
|
|
146
|
-
return true;
|
|
147
|
-
}
|
|
148
|
-
function scheduleFetch(fetchType, params) {
|
|
149
|
-
const startTime = Date.now();
|
|
150
|
-
const fetchTypeToUse = !lastFetchStartTime ? "highPriority" : fetchType;
|
|
151
|
-
if (dynamicRealtimeThrottleMs && fetchTypeToUse === "realtimeUpdate") {
|
|
152
|
-
if (scheduleRTU(startTime, params)) {
|
|
153
|
-
return "rt-scheduled";
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (shouldSkipFetch(fetchTypeToUse, startTime)) {
|
|
157
|
-
return "skipped";
|
|
158
|
-
}
|
|
159
|
-
if (shouldScheduleFetch(fetchTypeToUse, params)) {
|
|
160
|
-
return "scheduled";
|
|
161
|
-
}
|
|
162
|
-
startFetch(params, startTime);
|
|
163
|
-
return "started";
|
|
164
|
-
}
|
|
165
|
-
function shouldSkipFetch(fetchType, startTime) {
|
|
166
|
-
if (fetchType === "highPriority") {
|
|
167
|
-
if (fetchs.a) {
|
|
168
|
-
const timeSinceLastFetch = startTime - fetchs.a.startTime;
|
|
169
|
-
if (timeSinceLastFetch < mediumPriorityThrottleMs) {
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
if (fetchType === "lowPriority") {
|
|
175
|
-
if (fetchs.a || fetchs.c || mutationIsInProgress) {
|
|
176
|
-
return true;
|
|
177
|
-
}
|
|
178
|
-
if (lastFetchStartTime) {
|
|
179
|
-
const timeSinceLastFetch = startTime - lastFetchStartTime;
|
|
180
|
-
if (timeSinceLastFetch < lowPriorityThrottleMs) {
|
|
181
|
-
return true;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
function shouldScheduleFetch(priority, params) {
|
|
188
|
-
const shouldSchedule = (() => {
|
|
189
|
-
if (priority === "lowPriority") {
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
return !!fetchs.a || !!mutationIsInProgress;
|
|
193
|
-
})();
|
|
194
|
-
if (shouldSchedule) {
|
|
195
|
-
fetchs.c = { params };
|
|
196
|
-
}
|
|
197
|
-
return shouldSchedule;
|
|
198
|
-
}
|
|
199
|
-
function addDelayedRTU(startTime, params) {
|
|
200
|
-
if (!dynamicRealtimeThrottleMs)
|
|
201
|
-
return false;
|
|
202
|
-
const timeSinceLastFetch = startTime - (lastFetchStartTime + lastFetchDuration);
|
|
203
|
-
const minimumRealtimeInterval = dynamicRealtimeThrottleMs(lastFetchDuration);
|
|
204
|
-
if (timeSinceLastFetch >= minimumRealtimeInterval)
|
|
205
|
-
return false;
|
|
206
|
-
const delay = minimumRealtimeInterval - timeSinceLastFetch;
|
|
207
|
-
fetchs.b = {
|
|
208
|
-
timeoutId: window.setTimeout(() => {
|
|
209
|
-
fetchs.b = null;
|
|
210
|
-
on?.("scheduled-rt-fetch-started");
|
|
211
|
-
startFetch(params, Date.now());
|
|
212
|
-
}, delay)
|
|
213
|
-
};
|
|
214
|
-
return true;
|
|
215
|
-
}
|
|
216
|
-
function scheduleRTU(startTime, params) {
|
|
217
|
-
if (!lastFetchDuration || !lastFetchStartTime || !dynamicRealtimeThrottleMs) {
|
|
218
|
-
return false;
|
|
219
|
-
}
|
|
220
|
-
if (fetchs.b) {
|
|
221
|
-
return true;
|
|
222
|
-
}
|
|
223
|
-
if (fetchs.a) {
|
|
224
|
-
fetchs.a.rtuOnEnd = () => {
|
|
225
|
-
addDelayedRTU(Date.now(), params);
|
|
226
|
-
};
|
|
227
|
-
return true;
|
|
228
|
-
} else if (mutationIsInProgress) {
|
|
229
|
-
onMutationEnd = () => {
|
|
230
|
-
const added = addDelayedRTU(Date.now(), params);
|
|
231
|
-
if (!added) {
|
|
232
|
-
on?.("scheduled-rt-fetch-started");
|
|
233
|
-
scheduleFetch("highPriority", params);
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
return true;
|
|
237
|
-
} else {
|
|
238
|
-
if (!addDelayedRTU(startTime, params)) {
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
return true;
|
|
243
|
-
}
|
|
244
|
-
function addOnFetchEnd(cb) {
|
|
245
|
-
if (fetchs.a) {
|
|
246
|
-
fetchs.a.onEnd.push(cb);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
async function awaitFetch(params) {
|
|
250
|
-
scheduleFetch("highPriority", params);
|
|
251
|
-
if (fetchs.a) {
|
|
252
|
-
await new Promise((resolve) => {
|
|
253
|
-
addOnFetchEnd(() => resolve(true));
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
return lastFetchWasAborted;
|
|
257
|
-
}
|
|
258
|
-
function reset() {
|
|
259
|
-
fetchs.a = null;
|
|
260
|
-
fetchs.c = null;
|
|
261
|
-
fetchs.b = null;
|
|
262
|
-
lastFetchStartTime = 0;
|
|
263
|
-
lastFetchDuration = 0;
|
|
264
|
-
lastFetchIdStarted = 0;
|
|
265
|
-
lastMutationIdStarted = 0;
|
|
266
|
-
mutationIsInProgress = false;
|
|
267
|
-
lastFetchWasAborted = false;
|
|
268
|
-
abortFetchsBeforeOrEqual = 0;
|
|
269
|
-
}
|
|
270
|
-
return {
|
|
271
|
-
reset,
|
|
272
|
-
scheduleFetch,
|
|
273
|
-
awaitFetch,
|
|
274
|
-
startMutation,
|
|
275
|
-
get hasPendingFetch() {
|
|
276
|
-
return !!fetchs.a || !!fetchs.c || !!fetchs.b;
|
|
277
|
-
},
|
|
278
|
-
get fetchIsInProgress() {
|
|
279
|
-
return !!fetchs.a;
|
|
280
|
-
},
|
|
281
|
-
get mutationIsInProgress() {
|
|
282
|
-
return mutationIsInProgress;
|
|
283
|
-
},
|
|
284
|
-
setLastFetchDurantion,
|
|
285
|
-
setLastFetchStartTime
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
var autoIncrementId = 0;
|
|
289
|
-
function getAutoIncrementId() {
|
|
290
|
-
return ++autoIncrementId;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// src/storeShared.ts
|
|
294
|
-
var fetchTypePriority = {
|
|
295
|
-
lowPriority: 0,
|
|
296
|
-
realtimeUpdate: 1,
|
|
297
|
-
highPriority: 2
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
// src/useEnsureIsLoaded.ts
|
|
301
|
-
var import_evtmitter = require("evtmitter");
|
|
302
|
-
var import_react2 = require("evtmitter/react");
|
|
303
|
-
var import_react3 = require("react");
|
|
304
|
-
|
|
305
|
-
// src/utils/hooks.ts
|
|
306
|
-
var import_react = require("react");
|
|
307
|
-
var import_t_state = require("t-state");
|
|
308
|
-
function useDeepCompareMemoize(value, equalityFn = import_t_state.deepEqual) {
|
|
309
|
-
const ref = (0, import_react.useRef)();
|
|
310
|
-
const signalRef = (0, import_react.useRef)(0);
|
|
311
|
-
if (!equalityFn(value, ref.current)) {
|
|
312
|
-
ref.current = value;
|
|
313
|
-
signalRef.current += 1;
|
|
314
|
-
}
|
|
315
|
-
return [signalRef.current];
|
|
316
|
-
}
|
|
317
|
-
function useDeepMemo(callback, deeps) {
|
|
318
|
-
return (0, import_react.useMemo)(callback, useDeepCompareMemoize(deeps));
|
|
319
|
-
}
|
|
320
|
-
function useLatestValue(value) {
|
|
321
|
-
const ref = (0, import_react.useRef)({ insideEffect: value, insideMemo: value });
|
|
322
|
-
ref.current.insideMemo = value;
|
|
323
|
-
(0, import_react.useLayoutEffect)(() => {
|
|
324
|
-
ref.current.insideEffect = value;
|
|
325
|
-
});
|
|
326
|
-
return ref.current;
|
|
327
|
-
}
|
|
328
|
-
function usePrevious(value, initial) {
|
|
329
|
-
const ref = (0, import_react.useRef)(initial);
|
|
330
|
-
(0, import_react.useEffect)(() => {
|
|
331
|
-
ref.current = value;
|
|
332
|
-
});
|
|
333
|
-
return ref.current;
|
|
334
|
-
}
|
|
335
|
-
function useOnChange(value, callBack, {
|
|
336
|
-
equalityFn: areEqual = import_t_state.shallowEqual,
|
|
337
|
-
callOnMount,
|
|
338
|
-
layoutEffect
|
|
339
|
-
} = {}) {
|
|
340
|
-
const useEffectFn = layoutEffect ? import_react.useLayoutEffect : import_react.useEffect;
|
|
341
|
-
const prev = usePrevious(value, !callOnMount ? value : void 0);
|
|
342
|
-
useEffectFn(() => {
|
|
343
|
-
if (!areEqual(value, prev)) {
|
|
344
|
-
return callBack({ prev, current: value });
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
function useConst(getValue) {
|
|
349
|
-
const store = (0, import_react.useRef)();
|
|
350
|
-
if (store.current === void 0) {
|
|
351
|
-
store.current = getValue();
|
|
352
|
-
}
|
|
353
|
-
return store.current;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// src/useEnsureIsLoaded.ts
|
|
357
|
-
function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
358
|
-
const isLoadedEvtEmitter = useConst(() => (0, import_evtmitter.evtmitter)());
|
|
359
|
-
const [isForceLoading, setIsForceLoading] = (0, import_react3.useState)(true);
|
|
360
|
-
useOnChange(
|
|
361
|
-
ensureIsLoaded && isForceLoading && enabled,
|
|
362
|
-
({ current }) => {
|
|
363
|
-
if (current) {
|
|
364
|
-
forceFetch();
|
|
365
|
-
}
|
|
366
|
-
},
|
|
367
|
-
{ callOnMount: true }
|
|
368
|
-
);
|
|
369
|
-
(0, import_react2.useOnEvtmitterEvent)(isLoadedEvtEmitter, "isLoaded", (isLoaded) => {
|
|
370
|
-
if (ensureIsLoaded && enabled && isLoaded) {
|
|
371
|
-
setIsForceLoading(false);
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
function useModifyResult(result) {
|
|
375
|
-
return (0, import_react3.useMemo)(() => {
|
|
376
|
-
if (ensureIsLoaded) {
|
|
377
|
-
const newStatus = enabled && isForceLoading ? "loading" : result.status;
|
|
378
|
-
return {
|
|
379
|
-
...result,
|
|
380
|
-
isLoading: newStatus === "loading",
|
|
381
|
-
status: newStatus
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
return result;
|
|
385
|
-
}, [ensureIsLoaded, isForceLoading, result]);
|
|
386
|
-
}
|
|
387
|
-
return [useModifyResult, isLoadedEvtEmitter.emit];
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// src/utils/reusePrevIfEqual.ts
|
|
391
|
-
var import_t_state2 = require("t-state");
|
|
392
|
-
function reusePrevIfEqual({
|
|
393
|
-
prev,
|
|
394
|
-
current,
|
|
395
|
-
d: equalityFn_ = import_t_state2.deepEqual
|
|
396
|
-
}) {
|
|
397
|
-
if (prev === void 0)
|
|
398
|
-
return current;
|
|
399
|
-
if (equalityFn_(prev, current)) {
|
|
400
|
-
return prev;
|
|
401
|
-
}
|
|
402
|
-
return current;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// src/documentStore.ts
|
|
406
|
-
function newTSDFDocumentStore({
|
|
407
|
-
debugName,
|
|
408
|
-
fetchFn,
|
|
409
|
-
getInitialData,
|
|
410
|
-
disableRefetchOnMount: globalDisableRefetchOnMount,
|
|
411
|
-
lowPriorityThrottleMs,
|
|
412
|
-
mediumPriorityThrottleMs,
|
|
413
|
-
dynamicRealtimeThrottleMs,
|
|
414
|
-
disableInitialDataInvalidation,
|
|
415
|
-
errorNormalizer,
|
|
416
|
-
onInvalidate
|
|
417
|
-
}) {
|
|
418
|
-
const store = new import_t_state3.Store({
|
|
419
|
-
debugName,
|
|
420
|
-
state: () => {
|
|
421
|
-
const initialData = getInitialData?.();
|
|
422
|
-
return {
|
|
423
|
-
data: initialData ?? null,
|
|
424
|
-
error: null,
|
|
425
|
-
status: initialData ? "success" : "idle",
|
|
426
|
-
refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
|
|
427
|
-
};
|
|
428
|
-
}
|
|
429
|
-
});
|
|
430
|
-
async function fetch(fetchCtx, _) {
|
|
431
|
-
store.setPartialState(
|
|
432
|
-
{
|
|
433
|
-
status: store.state.status === "success" ? "refetching" : "loading",
|
|
434
|
-
error: null,
|
|
435
|
-
refetchOnMount: false
|
|
436
|
-
},
|
|
437
|
-
{
|
|
438
|
-
action: store.state.status === "success" ? "fetch-start-refetching" : "fetch-start-loading"
|
|
439
|
-
}
|
|
440
|
-
);
|
|
441
|
-
try {
|
|
442
|
-
const data = await fetchFn();
|
|
443
|
-
if (fetchCtx.shouldAbort())
|
|
444
|
-
return false;
|
|
445
|
-
store.setPartialState(
|
|
446
|
-
{
|
|
447
|
-
data: reusePrevIfEqual({ prev: store.state.data, current: data }),
|
|
448
|
-
status: "success"
|
|
449
|
-
},
|
|
450
|
-
{ action: "fetch-success" }
|
|
451
|
-
);
|
|
452
|
-
return true;
|
|
453
|
-
} catch (exception) {
|
|
454
|
-
if (fetchCtx.shouldAbort())
|
|
455
|
-
return false;
|
|
456
|
-
store.setPartialState(
|
|
457
|
-
{
|
|
458
|
-
error: errorNormalizer(exception),
|
|
459
|
-
status: "error"
|
|
460
|
-
},
|
|
461
|
-
{ action: "fetch-error" }
|
|
462
|
-
);
|
|
463
|
-
return false;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
const fetchOrquestrator = createFetchOrquestrator({
|
|
467
|
-
fetchFn: fetch,
|
|
468
|
-
lowPriorityThrottleMs,
|
|
469
|
-
dynamicRealtimeThrottleMs,
|
|
470
|
-
mediumPriorityThrottleMs
|
|
471
|
-
});
|
|
472
|
-
function scheduleFetch(fetchType) {
|
|
473
|
-
return fetchOrquestrator.scheduleFetch(fetchType, null);
|
|
474
|
-
}
|
|
475
|
-
async function awaitFetch() {
|
|
476
|
-
const wasAborted = await fetchOrquestrator.awaitFetch(null);
|
|
477
|
-
if (wasAborted) {
|
|
478
|
-
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
479
|
-
}
|
|
480
|
-
if (store.state.error) {
|
|
481
|
-
return { data: null, error: store.state.error };
|
|
482
|
-
}
|
|
483
|
-
if (!store.state.data) {
|
|
484
|
-
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
485
|
-
}
|
|
486
|
-
return { data: store.state.data, error: null };
|
|
487
|
-
}
|
|
488
|
-
const storeEvents = (0, import_evtmitter2.evtmitter)();
|
|
489
|
-
let invalidationWasTriggered = false;
|
|
490
|
-
function invalidateData(priority = "highPriority") {
|
|
491
|
-
const currentInvalidationPriority = store.state.refetchOnMount ? fetchTypePriority[store.state.refetchOnMount] : -1;
|
|
492
|
-
const newInvalidationPriority = fetchTypePriority[priority];
|
|
493
|
-
if (currentInvalidationPriority >= newInvalidationPriority)
|
|
494
|
-
return;
|
|
495
|
-
store.setKey("refetchOnMount", priority, { action: "invalidate-data" });
|
|
496
|
-
invalidationWasTriggered = false;
|
|
497
|
-
storeEvents.emit("invalidateData", priority);
|
|
498
|
-
onInvalidate?.(priority);
|
|
499
|
-
}
|
|
500
|
-
function useDocument({
|
|
501
|
-
selector,
|
|
502
|
-
isOffScreen,
|
|
503
|
-
disabled = isOffScreen,
|
|
504
|
-
returnRefetchingStatus,
|
|
505
|
-
disableRefetchOnMount = globalDisableRefetchOnMount,
|
|
506
|
-
returnIdleStatus = !!disabled,
|
|
507
|
-
ensureIsLoaded,
|
|
508
|
-
selectorUsesExternalDeps
|
|
509
|
-
} = {}) {
|
|
510
|
-
const memoizedSelector = (0, import_react5.useMemo)(
|
|
511
|
-
() => selector,
|
|
512
|
-
// eslint-disable-next-line @lucasols/extended-lint/exhaustive-deps
|
|
513
|
-
[selectorUsesExternalDeps ? selector : 0]
|
|
514
|
-
);
|
|
515
|
-
const storeStateSelector = (0, import_react5.useCallback)(
|
|
516
|
-
(state) => {
|
|
517
|
-
const { error } = state;
|
|
518
|
-
const data = memoizedSelector ? memoizedSelector(state.data) : state.data;
|
|
519
|
-
let status = state.status;
|
|
520
|
-
if (!returnIdleStatus && status === "idle") {
|
|
521
|
-
status = "loading";
|
|
522
|
-
}
|
|
523
|
-
if (!returnRefetchingStatus && status === "refetching") {
|
|
524
|
-
status = "success";
|
|
525
|
-
}
|
|
526
|
-
return {
|
|
527
|
-
data,
|
|
528
|
-
error,
|
|
529
|
-
status,
|
|
530
|
-
isLoading: status === "loading"
|
|
531
|
-
};
|
|
532
|
-
},
|
|
533
|
-
[returnRefetchingStatus, returnIdleStatus, memoizedSelector]
|
|
534
|
-
);
|
|
535
|
-
const storeState = store.useSelector(storeStateSelector, {
|
|
536
|
-
useExternalDeps: true
|
|
537
|
-
});
|
|
538
|
-
(0, import_react4.useOnEvtmitterEvent)(storeEvents, "invalidateData", (priority) => {
|
|
539
|
-
if (disabled)
|
|
540
|
-
return;
|
|
541
|
-
if (!invalidationWasTriggered) {
|
|
542
|
-
store.setKey("refetchOnMount", false);
|
|
543
|
-
scheduleFetch(priority);
|
|
544
|
-
invalidationWasTriggered = true;
|
|
545
|
-
}
|
|
546
|
-
});
|
|
547
|
-
(0, import_react5.useEffect)(() => {
|
|
548
|
-
if (disabled)
|
|
549
|
-
return;
|
|
550
|
-
const fetchType = store.state.refetchOnMount || "lowPriority";
|
|
551
|
-
if (disableRefetchOnMount) {
|
|
552
|
-
const shouldFetch = store.state.refetchOnMount || store.state.status === "idle";
|
|
553
|
-
if (shouldFetch) {
|
|
554
|
-
scheduleFetch(fetchType);
|
|
555
|
-
}
|
|
556
|
-
} else {
|
|
557
|
-
scheduleFetch(fetchType);
|
|
558
|
-
}
|
|
559
|
-
}, [disableRefetchOnMount, disabled]);
|
|
560
|
-
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
561
|
-
ensureIsLoaded,
|
|
562
|
-
!disabled,
|
|
563
|
-
() => {
|
|
564
|
-
scheduleFetch("highPriority");
|
|
565
|
-
}
|
|
566
|
-
);
|
|
567
|
-
(0, import_t_state3.useSubscribeToStore)(store, ({ observe }) => {
|
|
568
|
-
observe.ifSelector((state) => state.status).change.then(({ current }) => {
|
|
569
|
-
if (current === "success" || current === "error") {
|
|
570
|
-
emitIsLoadedEvt("isLoaded", true);
|
|
571
|
-
}
|
|
572
|
-
});
|
|
573
|
-
});
|
|
574
|
-
return useModifyResult(storeState);
|
|
575
|
-
}
|
|
576
|
-
function updateState(produceNewData) {
|
|
577
|
-
if (!store.state.data)
|
|
578
|
-
return false;
|
|
579
|
-
store.setKey(
|
|
580
|
-
"data",
|
|
581
|
-
(current) => {
|
|
582
|
-
if (!current)
|
|
583
|
-
return current;
|
|
584
|
-
return (0, import_immer.produce)(current, produceNewData);
|
|
585
|
-
},
|
|
586
|
-
{ action: "update-state" }
|
|
587
|
-
);
|
|
588
|
-
return true;
|
|
589
|
-
}
|
|
590
|
-
function reset() {
|
|
591
|
-
fetchOrquestrator.reset();
|
|
592
|
-
store.setState({
|
|
593
|
-
data: null,
|
|
594
|
-
error: null,
|
|
595
|
-
status: "idle",
|
|
596
|
-
refetchOnMount: "lowPriority"
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
return {
|
|
600
|
-
store,
|
|
601
|
-
reset,
|
|
602
|
-
awaitFetch,
|
|
603
|
-
scheduleFetch,
|
|
604
|
-
invalidateData,
|
|
605
|
-
useDocument,
|
|
606
|
-
updateState,
|
|
607
|
-
startMutation: fetchOrquestrator.startMutation
|
|
608
|
-
};
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
// src/collectionStore.ts
|
|
612
|
-
var import_evtmitter3 = require("evtmitter");
|
|
613
|
-
var import_react6 = require("evtmitter/react");
|
|
614
|
-
var import_json = require("klona/json");
|
|
615
|
-
var import_react7 = require("react");
|
|
616
|
-
var import_t_state4 = require("t-state");
|
|
617
|
-
|
|
618
|
-
// src/collectionFetchOrquestrator.ts
|
|
619
|
-
function createCollectionFetchOrquestrator(props) {
|
|
620
|
-
const fetchOrquestrators = /* @__PURE__ */ new Map();
|
|
621
|
-
function getFetchOrquestrator(key) {
|
|
622
|
-
if (!fetchOrquestrators.has(key)) {
|
|
623
|
-
fetchOrquestrators.set(key, createFetchOrquestrator(props));
|
|
624
|
-
}
|
|
625
|
-
return fetchOrquestrators.get(key);
|
|
626
|
-
}
|
|
627
|
-
function reset() {
|
|
628
|
-
fetchOrquestrators.clear();
|
|
629
|
-
}
|
|
630
|
-
return {
|
|
631
|
-
get: getFetchOrquestrator,
|
|
632
|
-
reset
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
// src/utils/filterAndMap.ts
|
|
637
|
-
function filterAndMap(arr, fn) {
|
|
638
|
-
const result = [];
|
|
639
|
-
let i = 0;
|
|
640
|
-
for (const item of arr) {
|
|
641
|
-
const mappedItem = fn(item, false, i);
|
|
642
|
-
if (mappedItem !== false) {
|
|
643
|
-
result.push(mappedItem);
|
|
644
|
-
}
|
|
645
|
-
i++;
|
|
646
|
-
}
|
|
647
|
-
return result;
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
// src/utils/isObject.ts
|
|
651
|
-
function isObject(value) {
|
|
652
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
// src/utils/getCacheId.ts
|
|
656
|
-
function getCacheId(input, maxDepth = 3) {
|
|
657
|
-
if (typeof input === "string")
|
|
658
|
-
return String(input);
|
|
659
|
-
if (!input || typeof input !== "object")
|
|
660
|
-
return `#$${input}$#`;
|
|
661
|
-
return JSON.stringify(sortValues(input, maxDepth, 0));
|
|
662
|
-
}
|
|
663
|
-
function sortValues(input, maxDepth, depth) {
|
|
664
|
-
if (depth >= maxDepth)
|
|
665
|
-
return input;
|
|
666
|
-
if (Array.isArray(input)) {
|
|
667
|
-
return input.map((v) => sortValues(v, maxDepth, depth + 1));
|
|
668
|
-
}
|
|
669
|
-
if (isObject(input)) {
|
|
670
|
-
return orderedProps(input, (v) => sortValues(v, maxDepth, depth + 1));
|
|
671
|
-
}
|
|
672
|
-
return input;
|
|
673
|
-
}
|
|
674
|
-
var emptyObject = {};
|
|
675
|
-
function orderedProps(obj, mapValue) {
|
|
676
|
-
const keys = Object.keys(obj);
|
|
677
|
-
if (keys.length === 0)
|
|
678
|
-
return emptyObject;
|
|
679
|
-
if (keys.length === 1) {
|
|
680
|
-
const value = obj[keys[0]];
|
|
681
|
-
if (value === void 0)
|
|
682
|
-
return emptyObject;
|
|
683
|
-
return { [keys[0]]: mapValue(value) };
|
|
684
|
-
}
|
|
685
|
-
const mappedValues = filterAndMap(keys.sort(), (k, ignore) => {
|
|
686
|
-
const value = obj[k];
|
|
687
|
-
if (value === void 0)
|
|
688
|
-
return ignore;
|
|
689
|
-
return { [k]: mapValue(value) };
|
|
690
|
-
});
|
|
691
|
-
if (mappedValues.length === 0)
|
|
692
|
-
return emptyObject;
|
|
693
|
-
if (mappedValues.length === 1)
|
|
694
|
-
return mappedValues[0];
|
|
695
|
-
return mappedValues;
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
// src/collectionStore.ts
|
|
699
|
-
function newTSDFCollectionStore({
|
|
700
|
-
debugName,
|
|
701
|
-
fetchFn,
|
|
702
|
-
lowPriorityThrottleMs,
|
|
703
|
-
errorNormalizer,
|
|
704
|
-
disableInitialDataInvalidation,
|
|
705
|
-
mediumPriorityThrottleMs,
|
|
706
|
-
getInitialData,
|
|
707
|
-
disableRefetchOnMount: globalDisableRefetchOnMount,
|
|
708
|
-
dynamicRealtimeThrottleMs,
|
|
709
|
-
getCollectionItemKey: filterCollectionItemObjKey,
|
|
710
|
-
onInvalidate
|
|
711
|
-
}) {
|
|
712
|
-
const store = new import_t_state4.Store({
|
|
713
|
-
debugName,
|
|
714
|
-
state: () => {
|
|
715
|
-
const initialState = {};
|
|
716
|
-
const initialStateItems = getInitialData?.();
|
|
717
|
-
if (initialStateItems) {
|
|
718
|
-
for (const item of initialStateItems) {
|
|
719
|
-
const itemKey = getItemKey(item.payload);
|
|
720
|
-
initialState[itemKey] = {
|
|
721
|
-
data: item.data,
|
|
722
|
-
error: null,
|
|
723
|
-
status: "success",
|
|
724
|
-
payload: item.payload,
|
|
725
|
-
refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
|
|
726
|
-
wasLoaded: true
|
|
727
|
-
};
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
return initialState;
|
|
731
|
-
}
|
|
732
|
-
});
|
|
733
|
-
function getItemKey(params) {
|
|
734
|
-
return getCacheId(
|
|
735
|
-
filterCollectionItemObjKey ? filterCollectionItemObjKey(params) : params
|
|
736
|
-
);
|
|
737
|
-
}
|
|
738
|
-
async function fetch(fetchCtx, itemPayload) {
|
|
739
|
-
const itemId = getItemKey(itemPayload);
|
|
740
|
-
const payload = (0, import_json.klona)(itemPayload);
|
|
741
|
-
const itemState = store.state[itemId];
|
|
742
|
-
store.produceState(
|
|
743
|
-
(draft) => {
|
|
744
|
-
const item = draft[itemId];
|
|
745
|
-
if (!item) {
|
|
746
|
-
draft[itemId] = {
|
|
747
|
-
data: null,
|
|
748
|
-
error: null,
|
|
749
|
-
status: "loading",
|
|
750
|
-
payload,
|
|
751
|
-
refetchOnMount: false,
|
|
752
|
-
wasLoaded: false
|
|
753
|
-
};
|
|
754
|
-
return;
|
|
755
|
-
}
|
|
756
|
-
item.status = item.data !== null ? "refetching" : "loading";
|
|
757
|
-
item.payload = payload;
|
|
758
|
-
item.error = null;
|
|
759
|
-
item.refetchOnMount = false;
|
|
760
|
-
},
|
|
761
|
-
{
|
|
762
|
-
equalityCheck: import_t_state4.deepEqual,
|
|
763
|
-
action: {
|
|
764
|
-
type: !itemState?.data ? "fetch-start-loading" : "fetch-start-refetching",
|
|
765
|
-
payload
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
);
|
|
769
|
-
try {
|
|
770
|
-
const data = await fetchFn(payload);
|
|
771
|
-
if (fetchCtx.shouldAbort())
|
|
772
|
-
return false;
|
|
773
|
-
store.produceState(
|
|
774
|
-
(draft) => {
|
|
775
|
-
const item = draft[itemId];
|
|
776
|
-
if (!item)
|
|
777
|
-
return;
|
|
778
|
-
item.data = reusePrevIfEqual({ current: data, prev: item.data });
|
|
779
|
-
item.status = "success";
|
|
780
|
-
item.wasLoaded = true;
|
|
781
|
-
},
|
|
782
|
-
{ action: { type: "fetch-success", payload } }
|
|
783
|
-
);
|
|
784
|
-
return true;
|
|
785
|
-
} catch (exception) {
|
|
786
|
-
if (fetchCtx.shouldAbort())
|
|
787
|
-
return false;
|
|
788
|
-
const error = errorNormalizer(exception);
|
|
789
|
-
store.produceState(
|
|
790
|
-
(draft) => {
|
|
791
|
-
const item = draft[itemId];
|
|
792
|
-
if (!item)
|
|
793
|
-
return;
|
|
794
|
-
item.error = error;
|
|
795
|
-
item.status = "error";
|
|
796
|
-
},
|
|
797
|
-
{ action: { type: "fetch-error", payload, error } }
|
|
798
|
-
);
|
|
799
|
-
return false;
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
const fetchOrquestrator = createCollectionFetchOrquestrator({
|
|
803
|
-
fetchFn: fetch,
|
|
804
|
-
lowPriorityThrottleMs,
|
|
805
|
-
dynamicRealtimeThrottleMs,
|
|
806
|
-
mediumPriorityThrottleMs
|
|
807
|
-
});
|
|
808
|
-
function scheduleFetch(fetchType, payload) {
|
|
809
|
-
const multiplePayloads = Array.isArray(payload);
|
|
810
|
-
const payloads = multiplePayloads ? payload : [payload];
|
|
811
|
-
const results = [];
|
|
812
|
-
for (const param of payloads) {
|
|
813
|
-
const itemKey = getItemKey(param);
|
|
814
|
-
results.push(
|
|
815
|
-
fetchOrquestrator.get(itemKey).scheduleFetch(fetchType, param)
|
|
816
|
-
);
|
|
817
|
-
}
|
|
818
|
-
return multiplePayloads ? results : results[0];
|
|
819
|
-
}
|
|
820
|
-
async function awaitFetch(params) {
|
|
821
|
-
const itemId = getItemKey(params);
|
|
822
|
-
const wasAborted = await fetchOrquestrator.get(itemId).awaitFetch(params);
|
|
823
|
-
if (wasAborted) {
|
|
824
|
-
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
825
|
-
}
|
|
826
|
-
const item = store.state[itemId];
|
|
827
|
-
if (item?.error) {
|
|
828
|
-
return { data: null, error: item.error };
|
|
829
|
-
}
|
|
830
|
-
if (!item?.data) {
|
|
831
|
-
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
832
|
-
}
|
|
833
|
-
return { data: item.data, error: null };
|
|
834
|
-
}
|
|
835
|
-
function getItemsKeyArray(params) {
|
|
836
|
-
const items = store.state;
|
|
837
|
-
if (Array.isArray(params)) {
|
|
838
|
-
return params.map(getItemKey);
|
|
839
|
-
} else if (typeof params === "function") {
|
|
840
|
-
return filterAndMap(Object.entries(items), ([itemKey, item], ignore) => {
|
|
841
|
-
return item && params(item.payload, item.data) ? itemKey : ignore;
|
|
842
|
-
});
|
|
843
|
-
} else {
|
|
844
|
-
return [getItemKey(params)];
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
const storeEvents = (0, import_evtmitter3.evtmitter)();
|
|
848
|
-
const invalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
849
|
-
function invalidateItem(itemPayload, priority = "highPriority") {
|
|
850
|
-
const itemsKey = getItemsKeyArray(itemPayload);
|
|
851
|
-
for (const itemKey of itemsKey) {
|
|
852
|
-
const item = store.state[itemKey];
|
|
853
|
-
if (!item)
|
|
854
|
-
continue;
|
|
855
|
-
const currentInvalidationPriority = item.refetchOnMount ? fetchTypePriority[item.refetchOnMount] : -1;
|
|
856
|
-
const newInvalidationPriority = fetchTypePriority[priority];
|
|
857
|
-
if (currentInvalidationPriority >= newInvalidationPriority)
|
|
858
|
-
continue;
|
|
859
|
-
store.produceState(
|
|
860
|
-
(draft) => {
|
|
861
|
-
const draftItem = draft[itemKey];
|
|
862
|
-
if (!draftItem)
|
|
863
|
-
return;
|
|
864
|
-
draftItem.refetchOnMount = priority;
|
|
865
|
-
},
|
|
866
|
-
{ action: { type: "invalidate-data", itemKey } }
|
|
867
|
-
);
|
|
868
|
-
invalidationWasTriggered.delete(itemKey);
|
|
869
|
-
storeEvents.emit("invalidateData", { priority, itemKey });
|
|
870
|
-
if (item.data) {
|
|
871
|
-
onInvalidate?.({
|
|
872
|
-
priority,
|
|
873
|
-
payload: item.payload,
|
|
874
|
-
itemState: item.data
|
|
875
|
-
});
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
function getItemState(params) {
|
|
880
|
-
if (typeof params === "function" || Array.isArray(params)) {
|
|
881
|
-
const itemsId = getItemsKeyArray(params);
|
|
882
|
-
return filterAndMap(itemsId, (itemId, ignore) => {
|
|
883
|
-
return store.state[itemId] || ignore;
|
|
884
|
-
});
|
|
885
|
-
}
|
|
886
|
-
return store.state[getItemKey(params)];
|
|
887
|
-
}
|
|
888
|
-
function useMultipleItems(items, {
|
|
889
|
-
selector,
|
|
890
|
-
selectorUsesExternalDeps,
|
|
891
|
-
returnIdleStatus: allItemsReturnIdleStatus,
|
|
892
|
-
returnRefetchingStatus: allItemsReturnRefetchingStatus,
|
|
893
|
-
omitPayload: allItemsOmitPayload,
|
|
894
|
-
disableRefetchOnMount: allItemsDisableRefetchOnMount,
|
|
895
|
-
isOffScreen: allItemsIsOffScreen
|
|
896
|
-
} = {}) {
|
|
897
|
-
const queriesWithId = useDeepMemo(() => {
|
|
898
|
-
return items.map((queryProps) => {
|
|
899
|
-
return {
|
|
900
|
-
itemKey: getItemKey(queryProps.payload),
|
|
901
|
-
payload: queryProps.payload,
|
|
902
|
-
disableRefetchOnMount: queryProps.disableRefetchOnMount ?? allItemsDisableRefetchOnMount ?? globalDisableRefetchOnMount,
|
|
903
|
-
returnIdleStatus: queryProps.returnIdleStatus ?? allItemsReturnIdleStatus,
|
|
904
|
-
returnRefetchingStatus: queryProps.returnRefetchingStatus ?? allItemsReturnRefetchingStatus,
|
|
905
|
-
omitPayload: queryProps.omitPayload ?? allItemsOmitPayload,
|
|
906
|
-
isOffScreen: queryProps.isOffScreen ?? allItemsIsOffScreen,
|
|
907
|
-
queryMetadata: queryProps.queryMetadata
|
|
908
|
-
};
|
|
909
|
-
});
|
|
910
|
-
}, [
|
|
911
|
-
items,
|
|
912
|
-
allItemsDisableRefetchOnMount,
|
|
913
|
-
allItemsIsOffScreen,
|
|
914
|
-
allItemsOmitPayload,
|
|
915
|
-
allItemsReturnIdleStatus,
|
|
916
|
-
allItemsReturnRefetchingStatus
|
|
917
|
-
]);
|
|
918
|
-
const dataSelector = (0, import_react7.useCallback)(
|
|
919
|
-
(itemState) => {
|
|
920
|
-
if (selector)
|
|
921
|
-
return selector(itemState);
|
|
922
|
-
return itemState;
|
|
923
|
-
},
|
|
924
|
-
// eslint-disable-next-line @lucasols/extended-lint/exhaustive-deps
|
|
925
|
-
[selectorUsesExternalDeps ? selector : 0]
|
|
926
|
-
);
|
|
927
|
-
const resultSelector = (0, import_react7.useCallback)(
|
|
928
|
-
(state) => {
|
|
929
|
-
return queriesWithId.map(
|
|
930
|
-
({
|
|
931
|
-
itemKey,
|
|
932
|
-
payload,
|
|
933
|
-
omitPayload,
|
|
934
|
-
returnIdleStatus,
|
|
935
|
-
returnRefetchingStatus,
|
|
936
|
-
queryMetadata
|
|
937
|
-
}) => {
|
|
938
|
-
const item = state[itemKey];
|
|
939
|
-
if (item === null) {
|
|
940
|
-
return {
|
|
941
|
-
itemStateKey: itemKey,
|
|
942
|
-
status: "deleted",
|
|
943
|
-
data: dataSelector(null),
|
|
944
|
-
error: null,
|
|
945
|
-
payload: omitPayload ? void 0 : payload,
|
|
946
|
-
isLoading: false,
|
|
947
|
-
queryMetadata
|
|
948
|
-
};
|
|
949
|
-
}
|
|
950
|
-
if (!item) {
|
|
951
|
-
return {
|
|
952
|
-
itemStateKey: itemKey,
|
|
953
|
-
status: returnIdleStatus ? "idle" : "loading",
|
|
954
|
-
data: dataSelector(null),
|
|
955
|
-
error: null,
|
|
956
|
-
payload: omitPayload ? void 0 : payload,
|
|
957
|
-
isLoading: returnIdleStatus ? false : true,
|
|
958
|
-
queryMetadata
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
let status = item.status;
|
|
962
|
-
if (!returnRefetchingStatus && item.status === "refetching") {
|
|
963
|
-
status = "success";
|
|
964
|
-
}
|
|
965
|
-
return {
|
|
966
|
-
itemStateKey: itemKey,
|
|
967
|
-
status,
|
|
968
|
-
data: dataSelector(item.data),
|
|
969
|
-
error: item.error,
|
|
970
|
-
isLoading: status === "loading",
|
|
971
|
-
payload: omitPayload ? void 0 : item.payload,
|
|
972
|
-
queryMetadata
|
|
973
|
-
};
|
|
974
|
-
}
|
|
975
|
-
);
|
|
976
|
-
},
|
|
977
|
-
[dataSelector, queriesWithId]
|
|
978
|
-
);
|
|
979
|
-
const storeState = store.useSelector(resultSelector, {
|
|
980
|
-
equalityFn: import_t_state4.deepEqual,
|
|
981
|
-
useExternalDeps: true
|
|
982
|
-
});
|
|
983
|
-
(0, import_react6.useOnEvtmitterEvent)(storeEvents, "invalidateData", (event) => {
|
|
984
|
-
for (const { itemKey, payload, isOffScreen } of queriesWithId) {
|
|
985
|
-
if (isOffScreen)
|
|
986
|
-
continue;
|
|
987
|
-
if (itemKey !== event.itemKey)
|
|
988
|
-
continue;
|
|
989
|
-
if (!invalidationWasTriggered.has(itemKey)) {
|
|
990
|
-
store.produceState((draft) => {
|
|
991
|
-
const draftItem = draft[itemKey];
|
|
992
|
-
if (!draftItem)
|
|
993
|
-
return;
|
|
994
|
-
draftItem.refetchOnMount = false;
|
|
995
|
-
});
|
|
996
|
-
scheduleFetch(event.priority, payload);
|
|
997
|
-
invalidationWasTriggered.add(itemKey);
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
});
|
|
1001
|
-
const ignoreItemsInRefetchOnMount = useConst(() => /* @__PURE__ */ new Set());
|
|
1002
|
-
(0, import_react7.useEffect)(() => {
|
|
1003
|
-
const removedQueries = new Set(ignoreItemsInRefetchOnMount);
|
|
1004
|
-
for (const {
|
|
1005
|
-
itemKey: itemId,
|
|
1006
|
-
payload,
|
|
1007
|
-
isOffScreen,
|
|
1008
|
-
disableRefetchOnMount
|
|
1009
|
-
} of queriesWithId) {
|
|
1010
|
-
removedQueries.delete(itemId);
|
|
1011
|
-
if (isOffScreen)
|
|
1012
|
-
continue;
|
|
1013
|
-
if (itemId) {
|
|
1014
|
-
const itemState = getItemState(payload);
|
|
1015
|
-
const fetchType = itemState?.refetchOnMount || "lowPriority";
|
|
1016
|
-
const shouldFetch = !itemState?.wasLoaded || itemState.refetchOnMount;
|
|
1017
|
-
if (!shouldFetch && ignoreItemsInRefetchOnMount.has(itemId)) {
|
|
1018
|
-
continue;
|
|
1019
|
-
}
|
|
1020
|
-
ignoreItemsInRefetchOnMount.add(itemId);
|
|
1021
|
-
if (disableRefetchOnMount) {
|
|
1022
|
-
if (shouldFetch) {
|
|
1023
|
-
scheduleFetch(fetchType, payload);
|
|
1024
|
-
continue;
|
|
1025
|
-
}
|
|
1026
|
-
} else {
|
|
1027
|
-
scheduleFetch(fetchType, payload);
|
|
1028
|
-
}
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
for (const itemId of removedQueries) {
|
|
1032
|
-
ignoreItemsInRefetchOnMount.delete(itemId);
|
|
1033
|
-
}
|
|
1034
|
-
}, [ignoreItemsInRefetchOnMount, queriesWithId]);
|
|
1035
|
-
return storeState;
|
|
1036
|
-
}
|
|
1037
|
-
function useItem(payload, {
|
|
1038
|
-
omitPayload,
|
|
1039
|
-
selector,
|
|
1040
|
-
selectorUsesExternalDeps,
|
|
1041
|
-
ensureIsLoaded,
|
|
1042
|
-
returnRefetchingStatus,
|
|
1043
|
-
disableRefetchOnMount,
|
|
1044
|
-
returnIdleStatus,
|
|
1045
|
-
isOffScreen
|
|
1046
|
-
} = {}) {
|
|
1047
|
-
const query = (0, import_react7.useMemo)(
|
|
1048
|
-
() => payload === false || payload === null || payload === void 0 ? [] : [
|
|
1049
|
-
{
|
|
1050
|
-
payload,
|
|
1051
|
-
omitPayload,
|
|
1052
|
-
returnRefetchingStatus,
|
|
1053
|
-
disableRefetchOnMount,
|
|
1054
|
-
returnIdleStatus,
|
|
1055
|
-
isOffScreen
|
|
1056
|
-
}
|
|
1057
|
-
],
|
|
1058
|
-
[
|
|
1059
|
-
disableRefetchOnMount,
|
|
1060
|
-
isOffScreen,
|
|
1061
|
-
omitPayload,
|
|
1062
|
-
returnIdleStatus,
|
|
1063
|
-
returnRefetchingStatus,
|
|
1064
|
-
payload
|
|
1065
|
-
]
|
|
1066
|
-
);
|
|
1067
|
-
const item = useMultipleItems(query, {
|
|
1068
|
-
selector,
|
|
1069
|
-
selectorUsesExternalDeps
|
|
1070
|
-
});
|
|
1071
|
-
const result = (0, import_react7.useMemo)(
|
|
1072
|
-
() => item[0] ?? {
|
|
1073
|
-
payload: void 0,
|
|
1074
|
-
data: selector ? selector(null) : null,
|
|
1075
|
-
error: null,
|
|
1076
|
-
status: "idle",
|
|
1077
|
-
itemStateKey: "",
|
|
1078
|
-
isLoading: false,
|
|
1079
|
-
queryMetadata: void 0
|
|
1080
|
-
},
|
|
1081
|
-
// eslint-disable-next-line @lucasols/extended-lint/exhaustive-deps
|
|
1082
|
-
[item]
|
|
1083
|
-
);
|
|
1084
|
-
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
1085
|
-
ensureIsLoaded,
|
|
1086
|
-
!!payload,
|
|
1087
|
-
() => {
|
|
1088
|
-
if (payload) {
|
|
1089
|
-
scheduleFetch("highPriority", payload);
|
|
1090
|
-
}
|
|
1091
|
-
}
|
|
1092
|
-
);
|
|
1093
|
-
(0, import_t_state4.useSubscribeToStore)(store, ({ observe }) => {
|
|
1094
|
-
if (!ensureIsLoaded)
|
|
1095
|
-
return;
|
|
1096
|
-
observe.ifSelector((state) => state[result.itemStateKey]?.status).change.then(({ current }) => {
|
|
1097
|
-
if (current === "success" || current === "error") {
|
|
1098
|
-
emitIsLoadedEvt("isLoaded", true);
|
|
1099
|
-
}
|
|
1100
|
-
});
|
|
1101
|
-
});
|
|
1102
|
-
return useModifyResult(result);
|
|
1103
|
-
}
|
|
1104
|
-
function startMutation(fetchParams) {
|
|
1105
|
-
const itemKeys = getItemsKeyArray(fetchParams);
|
|
1106
|
-
const endMutations = [];
|
|
1107
|
-
for (const itemKey of itemKeys) {
|
|
1108
|
-
endMutations.push(fetchOrquestrator.get(itemKey).startMutation());
|
|
1109
|
-
}
|
|
1110
|
-
return () => {
|
|
1111
|
-
for (const endMutation of endMutations) {
|
|
1112
|
-
endMutation();
|
|
1113
|
-
}
|
|
1114
|
-
};
|
|
1115
|
-
}
|
|
1116
|
-
function addItemToState(fetchParams, data) {
|
|
1117
|
-
const itemKey = getItemKey(fetchParams);
|
|
1118
|
-
store.produceState(
|
|
1119
|
-
(draftState) => {
|
|
1120
|
-
draftState[itemKey] = {
|
|
1121
|
-
data,
|
|
1122
|
-
status: "success",
|
|
1123
|
-
wasLoaded: true,
|
|
1124
|
-
refetchOnMount: false,
|
|
1125
|
-
error: null,
|
|
1126
|
-
payload: (0, import_json.klona)(fetchParams)
|
|
1127
|
-
};
|
|
1128
|
-
},
|
|
1129
|
-
{ action: { type: "create-item-state", payload: fetchParams } }
|
|
1130
|
-
);
|
|
1131
|
-
}
|
|
1132
|
-
function deleteItemState(fetchParams) {
|
|
1133
|
-
const itemKeys = getItemsKeyArray(fetchParams);
|
|
1134
|
-
store.produceState(
|
|
1135
|
-
(draftState) => {
|
|
1136
|
-
for (const itemKey of itemKeys) {
|
|
1137
|
-
draftState[itemKey] = null;
|
|
1138
|
-
}
|
|
1139
|
-
},
|
|
1140
|
-
{ action: { type: "delete-item-state", payload: fetchParams } }
|
|
1141
|
-
);
|
|
1142
|
-
}
|
|
1143
|
-
function updateItemState(fetchParams, produceNewData, ifNothingWasUpdated) {
|
|
1144
|
-
const itemKeys = getItemsKeyArray(fetchParams);
|
|
1145
|
-
let someItemWasUpdated = false;
|
|
1146
|
-
store.batch(
|
|
1147
|
-
() => {
|
|
1148
|
-
store.produceState((draftState) => {
|
|
1149
|
-
for (const itemKey of itemKeys) {
|
|
1150
|
-
const item = draftState[itemKey];
|
|
1151
|
-
if (!item)
|
|
1152
|
-
continue;
|
|
1153
|
-
if (item.data) {
|
|
1154
|
-
someItemWasUpdated = true;
|
|
1155
|
-
const newData = produceNewData(item.data, item);
|
|
1156
|
-
if (newData) {
|
|
1157
|
-
item.data = newData;
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
});
|
|
1162
|
-
if (ifNothingWasUpdated && !someItemWasUpdated) {
|
|
1163
|
-
ifNothingWasUpdated();
|
|
1164
|
-
}
|
|
1165
|
-
},
|
|
1166
|
-
{ type: "update-item-state", item: itemKeys }
|
|
1167
|
-
);
|
|
1168
|
-
return someItemWasUpdated;
|
|
1169
|
-
}
|
|
1170
|
-
function reset() {
|
|
1171
|
-
fetchOrquestrator.reset();
|
|
1172
|
-
store.setState({});
|
|
1173
|
-
}
|
|
1174
|
-
return {
|
|
1175
|
-
store,
|
|
1176
|
-
scheduleFetch,
|
|
1177
|
-
awaitFetch,
|
|
1178
|
-
useMultipleItems,
|
|
1179
|
-
useItem,
|
|
1180
|
-
reset,
|
|
1181
|
-
getItemKey,
|
|
1182
|
-
getItemState,
|
|
1183
|
-
startMutation,
|
|
1184
|
-
invalidateItem,
|
|
1185
|
-
updateItemState,
|
|
1186
|
-
addItemToState,
|
|
1187
|
-
deleteItemState
|
|
1188
|
-
};
|
|
1189
|
-
}
|
|
1190
|
-
|
|
1191
|
-
// src/listQueryStore.ts
|
|
1192
|
-
var import_evtmitter4 = require("evtmitter");
|
|
1193
|
-
var import_react8 = require("evtmitter/react");
|
|
1194
|
-
var import_json2 = require("klona/json");
|
|
1195
|
-
var import_react9 = require("react");
|
|
1196
|
-
var import_t_state5 = require("t-state");
|
|
1197
|
-
|
|
1198
|
-
// src/utils/findAndMap.ts
|
|
1199
|
-
function findAndMap(arr, findFn) {
|
|
1200
|
-
let i = 0;
|
|
1201
|
-
for (const item of arr) {
|
|
1202
|
-
const mappedItem = findFn(item, i);
|
|
1203
|
-
if (mappedItem !== false) {
|
|
1204
|
-
return mappedItem;
|
|
1205
|
-
}
|
|
1206
|
-
i++;
|
|
1207
|
-
}
|
|
1208
|
-
return void 0;
|
|
1209
|
-
}
|
|
1210
|
-
|
|
1211
|
-
// src/utils/sortBy.ts
|
|
1212
|
-
function sortBy(arr, getPriority, { order = "asc" } = {}) {
|
|
1213
|
-
return [...arr].sort((a, b) => {
|
|
1214
|
-
const _aPriority = getPriority(a);
|
|
1215
|
-
const _bPriority = getPriority(b);
|
|
1216
|
-
const aPriority = Array.isArray(_aPriority) ? _aPriority : [_aPriority];
|
|
1217
|
-
const bPriority = Array.isArray(_bPriority) ? _bPriority : [_bPriority];
|
|
1218
|
-
for (let i = 0; i < aPriority.length; i++) {
|
|
1219
|
-
const levelOrder = typeof order === "string" ? order : order[i] ?? "asc";
|
|
1220
|
-
const aP = aPriority[i] ?? 0;
|
|
1221
|
-
const bP = bPriority[i] ?? 0;
|
|
1222
|
-
if (aP === bP) {
|
|
1223
|
-
continue;
|
|
1224
|
-
}
|
|
1225
|
-
if (bP === Infinity || aP === -Infinity || aP < bP) {
|
|
1226
|
-
return levelOrder === "asc" ? -1 : 1;
|
|
1227
|
-
}
|
|
1228
|
-
if (aP === Infinity || bP === -Infinity || aP > bP) {
|
|
1229
|
-
return levelOrder === "asc" ? 1 : -1;
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
return 0;
|
|
1233
|
-
});
|
|
1234
|
-
}
|
|
1235
|
-
|
|
1236
|
-
// src/listQueryStore.ts
|
|
1237
|
-
var noFetchFnError = "No fetchItemFn was provided";
|
|
1238
|
-
function newTSDFListQueryStore({
|
|
1239
|
-
debugName,
|
|
1240
|
-
fetchListFn,
|
|
1241
|
-
fetchItemFn,
|
|
1242
|
-
errorNormalizer,
|
|
1243
|
-
defaultQuerySize = 50,
|
|
1244
|
-
getInitialData,
|
|
1245
|
-
disableRefetchOnMount: globalDisableRefetchOnMount,
|
|
1246
|
-
lowPriorityThrottleMs,
|
|
1247
|
-
disableInitialDataInvalidation,
|
|
1248
|
-
mediumPriorityThrottleMs,
|
|
1249
|
-
dynamicRealtimeThrottleMs,
|
|
1250
|
-
optimisticListUpdates,
|
|
1251
|
-
onInvalidateItem,
|
|
1252
|
-
onInvalidateQuery
|
|
1253
|
-
}) {
|
|
1254
|
-
const store = new import_t_state5.Store({
|
|
1255
|
-
debugName,
|
|
1256
|
-
state: () => {
|
|
1257
|
-
const initialState = { items: {}, queries: {}, itemQueries: {} };
|
|
1258
|
-
const initialData = getInitialData?.();
|
|
1259
|
-
if (initialData) {
|
|
1260
|
-
for (const { payload, data } of initialData.items) {
|
|
1261
|
-
const itemKey = getItemKey(payload);
|
|
1262
|
-
initialState.items[itemKey] = data;
|
|
1263
|
-
initialState.itemQueries[itemKey] = {
|
|
1264
|
-
error: null,
|
|
1265
|
-
status: "success",
|
|
1266
|
-
refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
|
|
1267
|
-
wasLoaded: true,
|
|
1268
|
-
payload
|
|
1269
|
-
};
|
|
1270
|
-
}
|
|
1271
|
-
for (const { payload, items, hasMore } of initialData.queries) {
|
|
1272
|
-
const queryKey = getQueryKey(payload);
|
|
1273
|
-
initialState.queries[queryKey] = {
|
|
1274
|
-
error: null,
|
|
1275
|
-
status: "success",
|
|
1276
|
-
refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
|
|
1277
|
-
wasLoaded: true,
|
|
1278
|
-
payload,
|
|
1279
|
-
items,
|
|
1280
|
-
hasMore
|
|
1281
|
-
};
|
|
1282
|
-
}
|
|
1283
|
-
}
|
|
1284
|
-
return initialState;
|
|
1285
|
-
}
|
|
1286
|
-
});
|
|
1287
|
-
function getQueryKey(params) {
|
|
1288
|
-
return getCacheId(params);
|
|
1289
|
-
}
|
|
1290
|
-
function getItemKey(params) {
|
|
1291
|
-
return getCacheId(params);
|
|
1292
|
-
}
|
|
1293
|
-
async function fetchQuery(fetchCtx, [fetchType, queryPayload, size = defaultQuerySize]) {
|
|
1294
|
-
const payload = (0, import_json2.klona)(queryPayload);
|
|
1295
|
-
const queryKey = getQueryKey(payload);
|
|
1296
|
-
const queryState = store.state.queries[queryKey];
|
|
1297
|
-
const isLoading = !queryState?.wasLoaded;
|
|
1298
|
-
store.produceState(
|
|
1299
|
-
(draft) => {
|
|
1300
|
-
const query = draft.queries[queryKey];
|
|
1301
|
-
if (!query) {
|
|
1302
|
-
draft.queries[queryKey] = {
|
|
1303
|
-
error: null,
|
|
1304
|
-
status: "loading",
|
|
1305
|
-
wasLoaded: false,
|
|
1306
|
-
payload,
|
|
1307
|
-
refetchOnMount: false,
|
|
1308
|
-
hasMore: false,
|
|
1309
|
-
items: []
|
|
1310
|
-
};
|
|
1311
|
-
return;
|
|
1312
|
-
}
|
|
1313
|
-
query.status = query.wasLoaded ? fetchType === "loadMore" ? "loadingMore" : "refetching" : "loading";
|
|
1314
|
-
query.error = null;
|
|
1315
|
-
query.refetchOnMount = false;
|
|
1316
|
-
},
|
|
1317
|
-
{
|
|
1318
|
-
equalityCheck: import_t_state5.deepEqual,
|
|
1319
|
-
action: {
|
|
1320
|
-
type: isLoading ? "fetch-query-start-loading" : fetchType === "loadMore" ? "fetch-query-loading-more" : "refetching-query-start",
|
|
1321
|
-
payload
|
|
1322
|
-
}
|
|
1323
|
-
}
|
|
1324
|
-
);
|
|
1325
|
-
const currentQuerySize = queryState?.items.length ?? 0;
|
|
1326
|
-
const querySize = (() => {
|
|
1327
|
-
if (fetchType === "loadMore") {
|
|
1328
|
-
return currentQuerySize + size;
|
|
1329
|
-
}
|
|
1330
|
-
if (isLoading)
|
|
1331
|
-
return size;
|
|
1332
|
-
return Math.max(currentQuerySize, size);
|
|
1333
|
-
})();
|
|
1334
|
-
try {
|
|
1335
|
-
const { items, hasMore } = await fetchListFn(payload, querySize);
|
|
1336
|
-
if (fetchCtx.shouldAbort())
|
|
1337
|
-
return false;
|
|
1338
|
-
store.produceState(
|
|
1339
|
-
(draft) => {
|
|
1340
|
-
const query = draft.queries[queryKey];
|
|
1341
|
-
if (!query)
|
|
1342
|
-
return;
|
|
1343
|
-
query.status = "success";
|
|
1344
|
-
query.wasLoaded = true;
|
|
1345
|
-
query.hasMore = hasMore;
|
|
1346
|
-
query.items = [];
|
|
1347
|
-
for (const { data, itemPayload } of items) {
|
|
1348
|
-
const itemKey = getItemKey(itemPayload);
|
|
1349
|
-
draft.items[itemKey] = reusePrevIfEqual({
|
|
1350
|
-
current: data,
|
|
1351
|
-
prev: draft.items[itemKey] ?? void 0
|
|
1352
|
-
});
|
|
1353
|
-
query.items.push(String(itemKey));
|
|
1354
|
-
const itemQuery = draft.itemQueries[itemKey];
|
|
1355
|
-
if (!itemQuery || itemQuery.status !== "loading" && itemQuery.status !== "refetching") {
|
|
1356
|
-
draft.itemQueries[itemKey] = {
|
|
1357
|
-
error: null,
|
|
1358
|
-
refetchOnMount: false,
|
|
1359
|
-
status: "success",
|
|
1360
|
-
wasLoaded: true,
|
|
1361
|
-
payload: itemPayload
|
|
1362
|
-
};
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
},
|
|
1366
|
-
{
|
|
1367
|
-
action: { type: "fetch-query-success", payload }
|
|
1368
|
-
}
|
|
1369
|
-
);
|
|
1370
|
-
for (const { itemPayload: id } of items) {
|
|
1371
|
-
const itemFetchOrquestrator = fetchItemOrquestrator?.get(String(id));
|
|
1372
|
-
if (itemFetchOrquestrator) {
|
|
1373
|
-
itemFetchOrquestrator.setLastFetchStartTime(fetchCtx.getStartTime());
|
|
1374
|
-
}
|
|
1375
|
-
}
|
|
1376
|
-
return true;
|
|
1377
|
-
} catch (exception) {
|
|
1378
|
-
if (fetchCtx.shouldAbort())
|
|
1379
|
-
return false;
|
|
1380
|
-
const error = errorNormalizer(exception);
|
|
1381
|
-
store.produceState(
|
|
1382
|
-
(draft) => {
|
|
1383
|
-
const query = draft.queries[queryKey];
|
|
1384
|
-
if (!query)
|
|
1385
|
-
return;
|
|
1386
|
-
query.status = "error";
|
|
1387
|
-
query.error = error;
|
|
1388
|
-
},
|
|
1389
|
-
{
|
|
1390
|
-
action: { type: "fetch-query-error", payload, error }
|
|
1391
|
-
}
|
|
1392
|
-
);
|
|
1393
|
-
return false;
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
function getQueryState(params) {
|
|
1397
|
-
return store.state.queries[getQueryKey(params)];
|
|
1398
|
-
}
|
|
1399
|
-
function getQueriesState(params) {
|
|
1400
|
-
const queryKeys = getQueriesKeyArray(params);
|
|
1401
|
-
return filterAndMap(queryKeys, ({ key }) => {
|
|
1402
|
-
const query = store.state.queries[key];
|
|
1403
|
-
return query ? { query, key } : false;
|
|
1404
|
-
});
|
|
1405
|
-
}
|
|
1406
|
-
function getQueriesRelatedToItem(itemPayload) {
|
|
1407
|
-
const itemKey = getItemKey(itemPayload);
|
|
1408
|
-
return getQueriesState((queryPayload) => {
|
|
1409
|
-
const queryState = store.state.queries[getQueryKey(queryPayload)];
|
|
1410
|
-
return !!queryState?.items.includes(itemKey);
|
|
1411
|
-
});
|
|
1412
|
-
}
|
|
1413
|
-
function getItemState(itemPayload) {
|
|
1414
|
-
if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
|
|
1415
|
-
const itemsId = getItemsKeyArray(itemPayload);
|
|
1416
|
-
return filterAndMap(itemsId, ({ itemKey }, ignore) => {
|
|
1417
|
-
const item = store.state.items[itemKey];
|
|
1418
|
-
const payload = store.state.itemQueries[itemKey]?.payload;
|
|
1419
|
-
return !item || !payload ? ignore : { payload, data: item };
|
|
1420
|
-
});
|
|
1421
|
-
}
|
|
1422
|
-
return store.state.items[getItemKey(itemPayload)];
|
|
1423
|
-
}
|
|
1424
|
-
const fetchQueryOrquestrator = createCollectionFetchOrquestrator({
|
|
1425
|
-
fetchFn: fetchQuery,
|
|
1426
|
-
lowPriorityThrottleMs,
|
|
1427
|
-
dynamicRealtimeThrottleMs,
|
|
1428
|
-
mediumPriorityThrottleMs
|
|
1429
|
-
});
|
|
1430
|
-
function scheduleListQueryFetch(fetchType, payload, size) {
|
|
1431
|
-
const multiplePayloads = Array.isArray(payload);
|
|
1432
|
-
const payloads = multiplePayloads ? payload : [payload];
|
|
1433
|
-
const results = payloads.map((param) => {
|
|
1434
|
-
return fetchQueryOrquestrator.get(getQueryKey(param)).scheduleFetch(fetchType, ["load", param, size]);
|
|
1435
|
-
});
|
|
1436
|
-
return multiplePayloads ? results : results[0];
|
|
1437
|
-
}
|
|
1438
|
-
function loadMore(params, size) {
|
|
1439
|
-
const queryState = getQueryState(params);
|
|
1440
|
-
if (!queryState || !queryState.hasMore)
|
|
1441
|
-
return "skipped";
|
|
1442
|
-
if (queryState.status !== "success")
|
|
1443
|
-
return "skipped";
|
|
1444
|
-
return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
|
|
1445
|
-
}
|
|
1446
|
-
function getQueryItems(query, itemDataSelector) {
|
|
1447
|
-
return filterAndMap(query.items, (itemKey, ignore) => {
|
|
1448
|
-
const item = store.state.items[itemKey];
|
|
1449
|
-
const itemPayload = store.state.itemQueries[itemKey]?.payload;
|
|
1450
|
-
return item && itemPayload ? itemDataSelector(item, itemPayload, itemKey) : ignore;
|
|
1451
|
-
});
|
|
1452
|
-
}
|
|
1453
|
-
async function awaitListQueryFetch(params, size = defaultQuerySize) {
|
|
1454
|
-
const queryKey = getQueryKey(params);
|
|
1455
|
-
const wasAborted = await fetchQueryOrquestrator.get(queryKey).awaitFetch(["load", params, size]);
|
|
1456
|
-
if (wasAborted) {
|
|
1457
|
-
return {
|
|
1458
|
-
items: [],
|
|
1459
|
-
error: errorNormalizer(new Error("Aborted")),
|
|
1460
|
-
hasMore: false
|
|
1461
|
-
};
|
|
1462
|
-
}
|
|
1463
|
-
const query = store.state.queries[queryKey];
|
|
1464
|
-
if (query?.error) {
|
|
1465
|
-
return {
|
|
1466
|
-
items: [],
|
|
1467
|
-
error: query.error,
|
|
1468
|
-
hasMore: query.hasMore
|
|
1469
|
-
};
|
|
1470
|
-
}
|
|
1471
|
-
if (!query) {
|
|
1472
|
-
return {
|
|
1473
|
-
items: [],
|
|
1474
|
-
error: errorNormalizer(new Error("Not found")),
|
|
1475
|
-
hasMore: false
|
|
1476
|
-
};
|
|
1477
|
-
}
|
|
1478
|
-
return {
|
|
1479
|
-
items: getQueryItems(query, (data, itemPayload) => ({
|
|
1480
|
-
data,
|
|
1481
|
-
itemPayload
|
|
1482
|
-
})),
|
|
1483
|
-
error: null,
|
|
1484
|
-
hasMore: query.hasMore
|
|
1485
|
-
};
|
|
1486
|
-
}
|
|
1487
|
-
async function awaitItemFetch(itemPayload) {
|
|
1488
|
-
if (!fetchItemOrquestrator)
|
|
1489
|
-
throw new Error(noFetchFnError);
|
|
1490
|
-
const itemKey = getItemKey(itemPayload);
|
|
1491
|
-
const wasAborted = await fetchItemOrquestrator.get(itemKey).awaitFetch(itemPayload);
|
|
1492
|
-
if (wasAborted) {
|
|
1493
|
-
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
1494
|
-
}
|
|
1495
|
-
const item = store.state.items[itemKey];
|
|
1496
|
-
const itemQuery = store.state.itemQueries[itemKey];
|
|
1497
|
-
if (itemQuery?.error) {
|
|
1498
|
-
return { data: null, error: itemQuery.error };
|
|
1499
|
-
}
|
|
1500
|
-
if (!itemQuery || !item) {
|
|
1501
|
-
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
1502
|
-
}
|
|
1503
|
-
return { data: item, error: null };
|
|
1504
|
-
}
|
|
1505
|
-
function getQueriesKeyArray(payloads) {
|
|
1506
|
-
if (Array.isArray(payloads)) {
|
|
1507
|
-
return payloads.map((payload) => ({
|
|
1508
|
-
key: getQueryKey(payload),
|
|
1509
|
-
payload
|
|
1510
|
-
}));
|
|
1511
|
-
} else if (typeof payloads === "function") {
|
|
1512
|
-
return filterAndMap(
|
|
1513
|
-
Object.entries(store.state.queries),
|
|
1514
|
-
([queryKey, query], ignore) => {
|
|
1515
|
-
return payloads(query.payload, query, queryKey) ? {
|
|
1516
|
-
key: queryKey,
|
|
1517
|
-
payload: query.payload
|
|
1518
|
-
} : ignore;
|
|
1519
|
-
}
|
|
1520
|
-
);
|
|
1521
|
-
} else {
|
|
1522
|
-
return [{ key: getQueryKey(payloads), payload: payloads }];
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
const storeEvents = (0, import_evtmitter4.evtmitter)();
|
|
1526
|
-
const itemInvalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
1527
|
-
function invalidateItem(itemId, priority = "highPriority") {
|
|
1528
|
-
if (!fetchItemOrquestrator) {
|
|
1529
|
-
return;
|
|
1530
|
-
}
|
|
1531
|
-
const itemsKey = getItemsKeyArray(itemId);
|
|
1532
|
-
for (const { itemKey, payload } of itemsKey) {
|
|
1533
|
-
const item = store.state.itemQueries[itemKey];
|
|
1534
|
-
if (!item)
|
|
1535
|
-
continue;
|
|
1536
|
-
const currentInvalidationPriority = item.refetchOnMount ? fetchTypePriority[item.refetchOnMount] : -1;
|
|
1537
|
-
const newInvalidationPriority = fetchTypePriority[priority];
|
|
1538
|
-
if (currentInvalidationPriority >= newInvalidationPriority)
|
|
1539
|
-
continue;
|
|
1540
|
-
store.produceState(
|
|
1541
|
-
(draft) => {
|
|
1542
|
-
const query = draft.itemQueries[itemKey];
|
|
1543
|
-
if (!query)
|
|
1544
|
-
return;
|
|
1545
|
-
query.refetchOnMount = priority;
|
|
1546
|
-
},
|
|
1547
|
-
{ action: { type: "invalidate-item", queryKey: itemKey } }
|
|
1548
|
-
);
|
|
1549
|
-
itemInvalidationWasTriggered.delete(itemKey);
|
|
1550
|
-
storeEvents.emit("invalidateItem", { priority, itemKey });
|
|
1551
|
-
if (onInvalidateItem) {
|
|
1552
|
-
const itemState = store.state.items[itemKey];
|
|
1553
|
-
if (itemState) {
|
|
1554
|
-
onInvalidateItem({ priority, itemState, payload });
|
|
1555
|
-
}
|
|
1556
|
-
}
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
1559
|
-
const queryInvalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
1560
|
-
function invalidateQueryAndItems({
|
|
1561
|
-
itemPayload,
|
|
1562
|
-
queryPayload,
|
|
1563
|
-
type: priority = "highPriority"
|
|
1564
|
-
}) {
|
|
1565
|
-
const queriesKey = queryPayload ? getQueriesKeyArray(queryPayload) : [];
|
|
1566
|
-
for (const { key, payload } of queriesKey) {
|
|
1567
|
-
const queryState = store.state.queries[key];
|
|
1568
|
-
if (!queryState)
|
|
1569
|
-
continue;
|
|
1570
|
-
const currentInvalidationPriority = queryState.refetchOnMount ? fetchTypePriority[queryState.refetchOnMount] : -1;
|
|
1571
|
-
const newInvalidationPriority = fetchTypePriority[priority];
|
|
1572
|
-
if (currentInvalidationPriority >= newInvalidationPriority)
|
|
1573
|
-
continue;
|
|
1574
|
-
store.produceState(
|
|
1575
|
-
(draft) => {
|
|
1576
|
-
const query = draft.queries[key];
|
|
1577
|
-
if (!query)
|
|
1578
|
-
return;
|
|
1579
|
-
query.refetchOnMount = priority;
|
|
1580
|
-
},
|
|
1581
|
-
{ action: { type: "invalidate-data", key } }
|
|
1582
|
-
);
|
|
1583
|
-
queryInvalidationWasTriggered.delete(key);
|
|
1584
|
-
storeEvents.emit("invalidateQuery", { priority, queryKey: key });
|
|
1585
|
-
onInvalidateQuery?.(payload, priority);
|
|
1586
|
-
}
|
|
1587
|
-
if (itemPayload) {
|
|
1588
|
-
invalidateItem(itemPayload, priority);
|
|
1589
|
-
}
|
|
1590
|
-
}
|
|
1591
|
-
function defaultItemSelector(data) {
|
|
1592
|
-
return data;
|
|
1593
|
-
}
|
|
1594
|
-
function useMultipleListQueries(queries, {
|
|
1595
|
-
itemSelector = defaultItemSelector,
|
|
1596
|
-
selectorUsesExternalDeps,
|
|
1597
|
-
returnIdleStatus: allItemsReturnIdleStatus,
|
|
1598
|
-
returnRefetchingStatus: allItemsReturnRefetchingStatus,
|
|
1599
|
-
omitPayload: allItemsOmitPayload,
|
|
1600
|
-
disableRefetchOnMount: allItemsDisableRefetchOnMount,
|
|
1601
|
-
isOffScreen: allItemsIsOffScreen,
|
|
1602
|
-
loadSize: allItemsLoadSize
|
|
1603
|
-
} = {}) {
|
|
1604
|
-
const queriesWithId = useDeepMemo(() => {
|
|
1605
|
-
return queries.map((item) => {
|
|
1606
|
-
return {
|
|
1607
|
-
key: getQueryKey(item.payload),
|
|
1608
|
-
payload: item.payload,
|
|
1609
|
-
disableRefetchOnMount: item.disableRefetchOnMount ?? allItemsDisableRefetchOnMount ?? globalDisableRefetchOnMount,
|
|
1610
|
-
returnIdleStatus: item.returnIdleStatus ?? allItemsReturnIdleStatus,
|
|
1611
|
-
returnRefetchingStatus: item.returnRefetchingStatus ?? allItemsReturnRefetchingStatus,
|
|
1612
|
-
queryMetadata: item.queryMetadata,
|
|
1613
|
-
isOffScreen: item.isOffScreen ?? allItemsIsOffScreen,
|
|
1614
|
-
omitPayload: item.omitPayload ?? allItemsOmitPayload,
|
|
1615
|
-
loadSize: item.loadSize ?? allItemsLoadSize
|
|
1616
|
-
};
|
|
1617
|
-
});
|
|
1618
|
-
}, [
|
|
1619
|
-
allItemsDisableRefetchOnMount,
|
|
1620
|
-
allItemsIsOffScreen,
|
|
1621
|
-
allItemsLoadSize,
|
|
1622
|
-
allItemsOmitPayload,
|
|
1623
|
-
allItemsReturnIdleStatus,
|
|
1624
|
-
allItemsReturnRefetchingStatus,
|
|
1625
|
-
queries
|
|
1626
|
-
]);
|
|
1627
|
-
const dataSelector = (0, import_react9.useCallback)(itemSelector, [
|
|
1628
|
-
selectorUsesExternalDeps ? itemSelector : 0
|
|
1629
|
-
]);
|
|
1630
|
-
const resultSelector = (0, import_react9.useCallback)(
|
|
1631
|
-
(state) => {
|
|
1632
|
-
return queriesWithId.map(
|
|
1633
|
-
({
|
|
1634
|
-
key: queryKey,
|
|
1635
|
-
payload,
|
|
1636
|
-
returnIdleStatus,
|
|
1637
|
-
returnRefetchingStatus,
|
|
1638
|
-
omitPayload,
|
|
1639
|
-
queryMetadata
|
|
1640
|
-
}) => {
|
|
1641
|
-
const query = state.queries[queryKey];
|
|
1642
|
-
if (!query) {
|
|
1643
|
-
return {
|
|
1644
|
-
queryKey,
|
|
1645
|
-
status: returnIdleStatus ? "idle" : "loading",
|
|
1646
|
-
items: [],
|
|
1647
|
-
error: null,
|
|
1648
|
-
hasMore: false,
|
|
1649
|
-
payload: omitPayload ? void 0 : payload,
|
|
1650
|
-
isLoading: returnIdleStatus ? false : true,
|
|
1651
|
-
isLoadingMore: false,
|
|
1652
|
-
queryMetadata
|
|
1653
|
-
};
|
|
1654
|
-
}
|
|
1655
|
-
let status = query.status;
|
|
1656
|
-
if (!returnRefetchingStatus && query.status === "refetching") {
|
|
1657
|
-
status = "success";
|
|
1658
|
-
}
|
|
1659
|
-
return {
|
|
1660
|
-
queryKey,
|
|
1661
|
-
status,
|
|
1662
|
-
items: getQueryItems(query, dataSelector),
|
|
1663
|
-
error: query.error,
|
|
1664
|
-
hasMore: query.hasMore,
|
|
1665
|
-
isLoading: status === "loading",
|
|
1666
|
-
payload: omitPayload ? void 0 : query.payload,
|
|
1667
|
-
isLoadingMore: status === "loadingMore",
|
|
1668
|
-
queryMetadata
|
|
1669
|
-
};
|
|
1670
|
-
}
|
|
1671
|
-
);
|
|
1672
|
-
},
|
|
1673
|
-
[dataSelector, queriesWithId]
|
|
1674
|
-
);
|
|
1675
|
-
const storeState = store.useSelector(resultSelector, {
|
|
1676
|
-
equalityFn: import_t_state5.deepEqual,
|
|
1677
|
-
useExternalDeps: true
|
|
1678
|
-
});
|
|
1679
|
-
(0, import_react8.useOnEvtmitterEvent)(storeEvents, "invalidateQuery", (event) => {
|
|
1680
|
-
for (const { key, payload, isOffScreen } of queriesWithId) {
|
|
1681
|
-
if (isOffScreen)
|
|
1682
|
-
continue;
|
|
1683
|
-
if (key !== event.queryKey)
|
|
1684
|
-
continue;
|
|
1685
|
-
if (!queryInvalidationWasTriggered.has(key)) {
|
|
1686
|
-
store.produceState((draft) => {
|
|
1687
|
-
const query = draft.queries[key];
|
|
1688
|
-
if (!query?.refetchOnMount)
|
|
1689
|
-
return;
|
|
1690
|
-
query.refetchOnMount = false;
|
|
1691
|
-
});
|
|
1692
|
-
scheduleListQueryFetch(event.priority, payload);
|
|
1693
|
-
queryInvalidationWasTriggered.add(key);
|
|
1694
|
-
}
|
|
1695
|
-
}
|
|
1696
|
-
});
|
|
1697
|
-
const ignoreItemsInRefetchOnMount = useConst(() => /* @__PURE__ */ new Set());
|
|
1698
|
-
(0, import_react9.useEffect)(() => {
|
|
1699
|
-
const removedItems = new Set(ignoreItemsInRefetchOnMount);
|
|
1700
|
-
for (const {
|
|
1701
|
-
key: itemId,
|
|
1702
|
-
payload: fetchParams,
|
|
1703
|
-
isOffScreen,
|
|
1704
|
-
loadSize,
|
|
1705
|
-
disableRefetchOnMount
|
|
1706
|
-
} of queriesWithId) {
|
|
1707
|
-
removedItems.delete(itemId);
|
|
1708
|
-
if (isOffScreen)
|
|
1709
|
-
continue;
|
|
1710
|
-
if (itemId) {
|
|
1711
|
-
const itemState = getQueryState(fetchParams);
|
|
1712
|
-
const fetchType = itemState?.refetchOnMount || "lowPriority";
|
|
1713
|
-
const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
|
|
1714
|
-
if (!shouldFetch && ignoreItemsInRefetchOnMount.has(itemId)) {
|
|
1715
|
-
continue;
|
|
1716
|
-
}
|
|
1717
|
-
ignoreItemsInRefetchOnMount.add(itemId);
|
|
1718
|
-
if (disableRefetchOnMount) {
|
|
1719
|
-
if (shouldFetch) {
|
|
1720
|
-
scheduleListQueryFetch(fetchType, fetchParams, loadSize);
|
|
1721
|
-
continue;
|
|
1722
|
-
}
|
|
1723
|
-
} else {
|
|
1724
|
-
scheduleListQueryFetch(fetchType, fetchParams, loadSize);
|
|
1725
|
-
}
|
|
1726
|
-
}
|
|
1727
|
-
}
|
|
1728
|
-
for (const itemKey of removedItems) {
|
|
1729
|
-
ignoreItemsInRefetchOnMount.delete(itemKey);
|
|
1730
|
-
}
|
|
1731
|
-
}, [ignoreItemsInRefetchOnMount, queriesWithId]);
|
|
1732
|
-
return storeState;
|
|
1733
|
-
}
|
|
1734
|
-
function useListQuery(payload, {
|
|
1735
|
-
isOffScreen,
|
|
1736
|
-
itemSelector,
|
|
1737
|
-
disableRefetchOnMount,
|
|
1738
|
-
returnIdleStatus,
|
|
1739
|
-
returnRefetchingStatus,
|
|
1740
|
-
loadSize,
|
|
1741
|
-
omitPayload,
|
|
1742
|
-
ensureIsLoaded,
|
|
1743
|
-
selectorUsesExternalDeps
|
|
1744
|
-
} = {}) {
|
|
1745
|
-
const query = (0, import_react9.useMemo)(
|
|
1746
|
-
() => payload === false || payload === null || payload === void 0 ? [] : [
|
|
1747
|
-
{
|
|
1748
|
-
payload,
|
|
1749
|
-
disableRefetchOnMount,
|
|
1750
|
-
returnIdleStatus,
|
|
1751
|
-
returnRefetchingStatus,
|
|
1752
|
-
omitPayload,
|
|
1753
|
-
isOffScreen,
|
|
1754
|
-
loadSize
|
|
1755
|
-
}
|
|
1756
|
-
],
|
|
1757
|
-
[
|
|
1758
|
-
disableRefetchOnMount,
|
|
1759
|
-
isOffScreen,
|
|
1760
|
-
loadSize,
|
|
1761
|
-
omitPayload,
|
|
1762
|
-
payload,
|
|
1763
|
-
returnIdleStatus,
|
|
1764
|
-
returnRefetchingStatus
|
|
1765
|
-
]
|
|
1766
|
-
);
|
|
1767
|
-
const queryResult = useMultipleListQueries(query, {
|
|
1768
|
-
itemSelector,
|
|
1769
|
-
selectorUsesExternalDeps
|
|
1770
|
-
});
|
|
1771
|
-
const result = (0, import_react9.useMemo)(
|
|
1772
|
-
() => queryResult[0] ?? {
|
|
1773
|
-
payload: void 0,
|
|
1774
|
-
error: null,
|
|
1775
|
-
hasMore: false,
|
|
1776
|
-
isLoading: false,
|
|
1777
|
-
status: "idle",
|
|
1778
|
-
queryKey: "",
|
|
1779
|
-
items: [],
|
|
1780
|
-
isLoadingMore: false,
|
|
1781
|
-
queryMetadata: void 0
|
|
1782
|
-
},
|
|
1783
|
-
[queryResult]
|
|
1784
|
-
);
|
|
1785
|
-
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
1786
|
-
ensureIsLoaded,
|
|
1787
|
-
!!payload,
|
|
1788
|
-
() => {
|
|
1789
|
-
if (payload) {
|
|
1790
|
-
scheduleListQueryFetch("highPriority", payload);
|
|
1791
|
-
}
|
|
1792
|
-
}
|
|
1793
|
-
);
|
|
1794
|
-
(0, import_t_state5.useSubscribeToStore)(store, ({ observe }) => {
|
|
1795
|
-
if (!ensureIsLoaded)
|
|
1796
|
-
return;
|
|
1797
|
-
observe.ifSelector((state) => state.queries[result.queryKey]?.status).change.then(({ current }) => {
|
|
1798
|
-
if (current === "success" || current === "error") {
|
|
1799
|
-
emitIsLoadedEvt("isLoaded", true);
|
|
1800
|
-
}
|
|
1801
|
-
});
|
|
1802
|
-
});
|
|
1803
|
-
return useModifyResult(result);
|
|
1804
|
-
}
|
|
1805
|
-
async function fetchItem(fetchCtx, itemPayload) {
|
|
1806
|
-
if (!fetchItemOrquestrator) {
|
|
1807
|
-
throw new Error(noFetchFnError);
|
|
1808
|
-
}
|
|
1809
|
-
const itemKey = getCacheId(itemPayload);
|
|
1810
|
-
const isLoaded = !store.state.itemQueries[itemKey]?.wasLoaded;
|
|
1811
|
-
store.produceState(
|
|
1812
|
-
(draft) => {
|
|
1813
|
-
const itemQuery = draft.itemQueries[itemKey];
|
|
1814
|
-
if (!itemQuery) {
|
|
1815
|
-
draft.itemQueries[itemKey] = {
|
|
1816
|
-
status: "loading",
|
|
1817
|
-
error: null,
|
|
1818
|
-
wasLoaded: false,
|
|
1819
|
-
refetchOnMount: false,
|
|
1820
|
-
payload: (0, import_json2.klona)(itemPayload)
|
|
1821
|
-
};
|
|
1822
|
-
return;
|
|
1823
|
-
}
|
|
1824
|
-
itemQuery.status = itemQuery.wasLoaded ? "refetching" : "loading";
|
|
1825
|
-
itemQuery.error = null;
|
|
1826
|
-
itemQuery.refetchOnMount = false;
|
|
1827
|
-
},
|
|
1828
|
-
{
|
|
1829
|
-
action: {
|
|
1830
|
-
type: isLoaded ? "fetch-item-start" : "fetch-item-refetch-start",
|
|
1831
|
-
itemPayload
|
|
1832
|
-
}
|
|
1833
|
-
}
|
|
1834
|
-
);
|
|
1835
|
-
try {
|
|
1836
|
-
const item = await fetchItemFn(itemPayload);
|
|
1837
|
-
if (fetchCtx.shouldAbort())
|
|
1838
|
-
return false;
|
|
1839
|
-
store.produceState(
|
|
1840
|
-
(draft) => {
|
|
1841
|
-
const itemQuery = draft.itemQueries[itemKey];
|
|
1842
|
-
if (!itemQuery)
|
|
1843
|
-
return;
|
|
1844
|
-
itemQuery.status = "success";
|
|
1845
|
-
itemQuery.wasLoaded = true;
|
|
1846
|
-
draft.items[itemKey] = reusePrevIfEqual({
|
|
1847
|
-
current: item,
|
|
1848
|
-
prev: draft.items[itemKey] ?? void 0
|
|
1849
|
-
});
|
|
1850
|
-
},
|
|
1851
|
-
{
|
|
1852
|
-
action: { type: "fetch-item-success", itemPayload }
|
|
1853
|
-
}
|
|
1854
|
-
);
|
|
1855
|
-
return true;
|
|
1856
|
-
} catch (exception) {
|
|
1857
|
-
if (fetchCtx.shouldAbort())
|
|
1858
|
-
return false;
|
|
1859
|
-
const error = errorNormalizer(exception);
|
|
1860
|
-
store.produceState(
|
|
1861
|
-
(draft) => {
|
|
1862
|
-
const itemQuery = draft.itemQueries[itemKey];
|
|
1863
|
-
if (!itemQuery)
|
|
1864
|
-
return;
|
|
1865
|
-
itemQuery.status = "error";
|
|
1866
|
-
itemQuery.error = error;
|
|
1867
|
-
},
|
|
1868
|
-
{
|
|
1869
|
-
action: { type: "fetch-item-error", itemPayload }
|
|
1870
|
-
}
|
|
1871
|
-
);
|
|
1872
|
-
return false;
|
|
1873
|
-
}
|
|
1874
|
-
}
|
|
1875
|
-
const fetchItemOrquestrator = fetchItemFn && createCollectionFetchOrquestrator({
|
|
1876
|
-
fetchFn: fetchItem,
|
|
1877
|
-
lowPriorityThrottleMs,
|
|
1878
|
-
dynamicRealtimeThrottleMs,
|
|
1879
|
-
mediumPriorityThrottleMs
|
|
1880
|
-
});
|
|
1881
|
-
function scheduleItemFetch(fetchType, itemPayload) {
|
|
1882
|
-
if (!fetchItemOrquestrator) {
|
|
1883
|
-
throw new Error(noFetchFnError);
|
|
1884
|
-
}
|
|
1885
|
-
const fetchMultiple = Array.isArray(itemPayload);
|
|
1886
|
-
const itemsId = fetchMultiple ? itemPayload : [itemPayload];
|
|
1887
|
-
const results = itemsId.map((payload) => {
|
|
1888
|
-
const itemKey = getCacheId(payload);
|
|
1889
|
-
return fetchItemOrquestrator.get(itemKey).scheduleFetch(fetchType, payload);
|
|
1890
|
-
});
|
|
1891
|
-
return fetchMultiple ? results : results[0];
|
|
1892
|
-
}
|
|
1893
|
-
function getItemsKeyArray(itemsPayload) {
|
|
1894
|
-
if (Array.isArray(itemsPayload)) {
|
|
1895
|
-
return itemsPayload.map((payload) => ({
|
|
1896
|
-
itemKey: getItemKey(payload),
|
|
1897
|
-
payload
|
|
1898
|
-
}));
|
|
1899
|
-
}
|
|
1900
|
-
if (typeof itemsPayload === "function") {
|
|
1901
|
-
return filterAndMap(
|
|
1902
|
-
Object.entries(store.state.items),
|
|
1903
|
-
([itemKey, item], ignore) => {
|
|
1904
|
-
const payload = store.state.itemQueries[itemKey]?.payload;
|
|
1905
|
-
if (item === null || !payload)
|
|
1906
|
-
return ignore;
|
|
1907
|
-
return itemsPayload(payload, item) ? { itemKey, payload } : ignore;
|
|
1908
|
-
}
|
|
1909
|
-
);
|
|
1910
|
-
}
|
|
1911
|
-
return [{ payload: itemsPayload, itemKey: getItemKey(itemsPayload) }];
|
|
1912
|
-
}
|
|
1913
|
-
function startItemMutation(itemId) {
|
|
1914
|
-
const itemsKey = getItemsKeyArray(itemId);
|
|
1915
|
-
const endMutations = [];
|
|
1916
|
-
for (const { itemKey } of itemsKey) {
|
|
1917
|
-
endMutations.push(
|
|
1918
|
-
fetchItemOrquestrator?.get(itemKey).startMutation() || (() => {
|
|
1919
|
-
})
|
|
1920
|
-
);
|
|
1921
|
-
for (const [queryKey, query] of Object.entries(store.state.queries)) {
|
|
1922
|
-
if (query.items.includes(itemKey)) {
|
|
1923
|
-
endMutations.push(
|
|
1924
|
-
fetchQueryOrquestrator.get(queryKey).startMutation()
|
|
1925
|
-
);
|
|
1926
|
-
}
|
|
1927
|
-
}
|
|
1928
|
-
}
|
|
1929
|
-
return () => {
|
|
1930
|
-
for (const endMutation of endMutations) {
|
|
1931
|
-
endMutation();
|
|
1932
|
-
}
|
|
1933
|
-
};
|
|
1934
|
-
}
|
|
1935
|
-
function defaultItemDataSelector(data) {
|
|
1936
|
-
return data;
|
|
1937
|
-
}
|
|
1938
|
-
function useMultipleItems(items, {
|
|
1939
|
-
selector = defaultItemDataSelector,
|
|
1940
|
-
loadFromStateOnly,
|
|
1941
|
-
selectorUsesExternalDeps,
|
|
1942
|
-
returnIdleStatus: allItemsReturnIdleStatus,
|
|
1943
|
-
returnRefetchingStatus: allItemsReturnRefetchingStatus,
|
|
1944
|
-
disableRefetchOnMount: allItemsDisableRefetchOnMount,
|
|
1945
|
-
isOffScreen: allItemsIsOffScreen
|
|
1946
|
-
} = {}) {
|
|
1947
|
-
const dataSelector = (0, import_react9.useCallback)(selector, [
|
|
1948
|
-
selectorUsesExternalDeps ? selector : 0
|
|
1949
|
-
]);
|
|
1950
|
-
const memoizedItemKeys = useDeepMemo(
|
|
1951
|
-
() => items.map(
|
|
1952
|
-
(itemPayload) => ({
|
|
1953
|
-
itemKey: getItemKey(itemPayload.payload),
|
|
1954
|
-
payload: itemPayload.payload,
|
|
1955
|
-
disableRefetchOnMount: itemPayload.disableRefetchOnMount ?? allItemsDisableRefetchOnMount,
|
|
1956
|
-
returnIdleStatus: itemPayload.returnIdleStatus ?? allItemsReturnIdleStatus,
|
|
1957
|
-
returnRefetchingStatus: itemPayload.returnRefetchingStatus ?? allItemsReturnRefetchingStatus,
|
|
1958
|
-
isOffScreen: itemPayload.isOffScreen ?? allItemsIsOffScreen,
|
|
1959
|
-
queryMetadata: itemPayload.queryMetadata
|
|
1960
|
-
})
|
|
1961
|
-
),
|
|
1962
|
-
[
|
|
1963
|
-
allItemsDisableRefetchOnMount,
|
|
1964
|
-
allItemsIsOffScreen,
|
|
1965
|
-
allItemsReturnIdleStatus,
|
|
1966
|
-
allItemsReturnRefetchingStatus,
|
|
1967
|
-
items
|
|
1968
|
-
]
|
|
1969
|
-
);
|
|
1970
|
-
const resultSelector = (0, import_react9.useCallback)(
|
|
1971
|
-
(state) => {
|
|
1972
|
-
return memoizedItemKeys.map(
|
|
1973
|
-
({
|
|
1974
|
-
itemKey,
|
|
1975
|
-
payload,
|
|
1976
|
-
queryMetadata,
|
|
1977
|
-
returnRefetchingStatus,
|
|
1978
|
-
returnIdleStatus
|
|
1979
|
-
}) => {
|
|
1980
|
-
const itemQuery = state.itemQueries[itemKey];
|
|
1981
|
-
const itemState = state.items[itemKey];
|
|
1982
|
-
if (itemQuery === null) {
|
|
1983
|
-
return {
|
|
1984
|
-
status: "deleted",
|
|
1985
|
-
error: null,
|
|
1986
|
-
isLoading: false,
|
|
1987
|
-
payload,
|
|
1988
|
-
data: dataSelector(null, null),
|
|
1989
|
-
queryMetadata
|
|
1990
|
-
};
|
|
1991
|
-
}
|
|
1992
|
-
if (!itemQuery) {
|
|
1993
|
-
return {
|
|
1994
|
-
status: returnIdleStatus ? "idle" : "loading",
|
|
1995
|
-
error: null,
|
|
1996
|
-
isLoading: returnIdleStatus ? false : true,
|
|
1997
|
-
payload,
|
|
1998
|
-
data: dataSelector(null, null),
|
|
1999
|
-
queryMetadata
|
|
2000
|
-
};
|
|
2001
|
-
}
|
|
2002
|
-
let status = itemQuery.status;
|
|
2003
|
-
if (!returnRefetchingStatus && itemQuery.status === "refetching") {
|
|
2004
|
-
status = "success";
|
|
2005
|
-
}
|
|
2006
|
-
return {
|
|
2007
|
-
status,
|
|
2008
|
-
error: itemQuery.error,
|
|
2009
|
-
isLoading: status === "loading",
|
|
2010
|
-
data: dataSelector(itemState ?? null, itemQuery.payload),
|
|
2011
|
-
payload,
|
|
2012
|
-
queryMetadata
|
|
2013
|
-
};
|
|
2014
|
-
}
|
|
2015
|
-
);
|
|
2016
|
-
},
|
|
2017
|
-
[dataSelector, memoizedItemKeys]
|
|
2018
|
-
);
|
|
2019
|
-
const storeState = store.useSelector(resultSelector, {
|
|
2020
|
-
equalityFn: import_t_state5.deepEqual,
|
|
2021
|
-
useExternalDeps: true
|
|
2022
|
-
});
|
|
2023
|
-
(0, import_react8.useOnEvtmitterEvent)(storeEvents, "invalidateItem", (event) => {
|
|
2024
|
-
if (loadFromStateOnly)
|
|
2025
|
-
return;
|
|
2026
|
-
for (const { payload, itemKey, isOffScreen } of memoizedItemKeys) {
|
|
2027
|
-
if (isOffScreen)
|
|
2028
|
-
continue;
|
|
2029
|
-
if (itemKey !== event.itemKey)
|
|
2030
|
-
continue;
|
|
2031
|
-
if (!itemInvalidationWasTriggered.has(itemKey)) {
|
|
2032
|
-
store.produceState((draft) => {
|
|
2033
|
-
const query = draft.itemQueries[itemKey];
|
|
2034
|
-
if (!query?.refetchOnMount)
|
|
2035
|
-
return;
|
|
2036
|
-
query.refetchOnMount = false;
|
|
2037
|
-
});
|
|
2038
|
-
scheduleItemFetch(event.priority, payload);
|
|
2039
|
-
itemInvalidationWasTriggered.add(itemKey);
|
|
2040
|
-
}
|
|
2041
|
-
}
|
|
2042
|
-
});
|
|
2043
|
-
const ignoreItemsInRefetchOnMount = useConst(() => /* @__PURE__ */ new Set());
|
|
2044
|
-
(0, import_react9.useEffect)(() => {
|
|
2045
|
-
if (loadFromStateOnly)
|
|
2046
|
-
return;
|
|
2047
|
-
const removedItems = new Set(ignoreItemsInRefetchOnMount);
|
|
2048
|
-
for (const {
|
|
2049
|
-
payload,
|
|
2050
|
-
itemKey,
|
|
2051
|
-
isOffScreen,
|
|
2052
|
-
disableRefetchOnMount
|
|
2053
|
-
} of memoizedItemKeys) {
|
|
2054
|
-
removedItems.delete(itemKey);
|
|
2055
|
-
if (isOffScreen)
|
|
2056
|
-
continue;
|
|
2057
|
-
if (itemKey) {
|
|
2058
|
-
const itemState = store.state.itemQueries[itemKey];
|
|
2059
|
-
const fetchType = itemState?.refetchOnMount || "lowPriority";
|
|
2060
|
-
const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
|
|
2061
|
-
if (!shouldFetch && ignoreItemsInRefetchOnMount.has(itemKey)) {
|
|
2062
|
-
continue;
|
|
2063
|
-
}
|
|
2064
|
-
ignoreItemsInRefetchOnMount.add(itemKey);
|
|
2065
|
-
if (disableRefetchOnMount) {
|
|
2066
|
-
if (shouldFetch) {
|
|
2067
|
-
scheduleItemFetch(fetchType, payload);
|
|
2068
|
-
return;
|
|
2069
|
-
}
|
|
2070
|
-
} else {
|
|
2071
|
-
scheduleItemFetch(fetchType, payload);
|
|
2072
|
-
}
|
|
2073
|
-
}
|
|
2074
|
-
}
|
|
2075
|
-
for (const itemKey of removedItems) {
|
|
2076
|
-
ignoreItemsInRefetchOnMount.delete(itemKey);
|
|
2077
|
-
}
|
|
2078
|
-
}, [ignoreItemsInRefetchOnMount, loadFromStateOnly, memoizedItemKeys]);
|
|
2079
|
-
return storeState;
|
|
2080
|
-
}
|
|
2081
|
-
function useItem(itemPayload, {
|
|
2082
|
-
selector,
|
|
2083
|
-
selectorUsesExternalDeps,
|
|
2084
|
-
disableRefetchOnMount,
|
|
2085
|
-
returnIdleStatus,
|
|
2086
|
-
returnRefetchingStatus,
|
|
2087
|
-
ensureIsLoaded,
|
|
2088
|
-
loadFromStateOnly,
|
|
2089
|
-
isOffScreen
|
|
2090
|
-
} = {}) {
|
|
2091
|
-
const query = (0, import_react9.useMemo)(
|
|
2092
|
-
() => itemPayload === false || itemPayload === null || itemPayload === void 0 ? [] : [
|
|
2093
|
-
{
|
|
2094
|
-
payload: itemPayload,
|
|
2095
|
-
disableRefetchOnMount,
|
|
2096
|
-
isOffScreen,
|
|
2097
|
-
returnIdleStatus,
|
|
2098
|
-
returnRefetchingStatus
|
|
2099
|
-
}
|
|
2100
|
-
],
|
|
2101
|
-
[
|
|
2102
|
-
itemPayload,
|
|
2103
|
-
disableRefetchOnMount,
|
|
2104
|
-
isOffScreen,
|
|
2105
|
-
returnIdleStatus,
|
|
2106
|
-
returnRefetchingStatus
|
|
2107
|
-
]
|
|
2108
|
-
);
|
|
2109
|
-
const queryResult = useMultipleItems(query, {
|
|
2110
|
-
selector,
|
|
2111
|
-
selectorUsesExternalDeps,
|
|
2112
|
-
loadFromStateOnly
|
|
2113
|
-
});
|
|
2114
|
-
const memoizedSelector = (0, import_react9.useCallback)(
|
|
2115
|
-
selector ?? defaultItemDataSelector,
|
|
2116
|
-
[]
|
|
2117
|
-
);
|
|
2118
|
-
const result = (0, import_react9.useMemo)(
|
|
2119
|
-
() => queryResult[0] ?? {
|
|
2120
|
-
error: null,
|
|
2121
|
-
isLoading: false,
|
|
2122
|
-
status: "idle",
|
|
2123
|
-
data: memoizedSelector(null, null),
|
|
2124
|
-
payload: itemPayload || null,
|
|
2125
|
-
queryMetadata: void 0
|
|
2126
|
-
},
|
|
2127
|
-
[itemPayload, memoizedSelector, queryResult]
|
|
2128
|
-
);
|
|
2129
|
-
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
2130
|
-
ensureIsLoaded,
|
|
2131
|
-
!!itemPayload,
|
|
2132
|
-
() => {
|
|
2133
|
-
if (itemPayload) {
|
|
2134
|
-
scheduleItemFetch("highPriority", itemPayload);
|
|
2135
|
-
}
|
|
2136
|
-
}
|
|
2137
|
-
);
|
|
2138
|
-
(0, import_t_state5.useSubscribeToStore)(store, ({ observe }) => {
|
|
2139
|
-
if (!ensureIsLoaded || !itemPayload)
|
|
2140
|
-
return;
|
|
2141
|
-
observe.ifSelector((state) => state.itemQueries[itemPayload]?.status).change.then(({ current }) => {
|
|
2142
|
-
if (current === "success" || current === "error") {
|
|
2143
|
-
emitIsLoadedEvt("isLoaded", true);
|
|
2144
|
-
}
|
|
2145
|
-
});
|
|
2146
|
-
});
|
|
2147
|
-
return useModifyResult(result);
|
|
2148
|
-
}
|
|
2149
|
-
function useFindItem(findItem, {
|
|
2150
|
-
selector = defaultItemDataSelector
|
|
2151
|
-
} = {}) {
|
|
2152
|
-
return store.useSelector((state) => {
|
|
2153
|
-
const selectedItem = findAndMap(
|
|
2154
|
-
Object.entries(state.items),
|
|
2155
|
-
([itemKey, item]) => {
|
|
2156
|
-
if (!item)
|
|
2157
|
-
return false;
|
|
2158
|
-
const itemQuery = state.itemQueries[itemKey];
|
|
2159
|
-
if (!itemQuery)
|
|
2160
|
-
return false;
|
|
2161
|
-
if (findItem(item, itemQuery.payload)) {
|
|
2162
|
-
return { item, itemQuery };
|
|
2163
|
-
}
|
|
2164
|
-
return false;
|
|
2165
|
-
}
|
|
2166
|
-
);
|
|
2167
|
-
if (!selectedItem)
|
|
2168
|
-
return null;
|
|
2169
|
-
return selector(selectedItem.item, selectedItem.itemQuery.payload);
|
|
2170
|
-
});
|
|
2171
|
-
}
|
|
2172
|
-
function updateItemState(itemIds, produceNewData, {
|
|
2173
|
-
ifNothingWasUpdated
|
|
2174
|
-
} = {}) {
|
|
2175
|
-
const itemKeys = getItemsKeyArray(itemIds);
|
|
2176
|
-
let someItemWasUpdated = false;
|
|
2177
|
-
store.batch(
|
|
2178
|
-
() => {
|
|
2179
|
-
store.produceState((draftState) => {
|
|
2180
|
-
for (const { itemKey, payload } of itemKeys) {
|
|
2181
|
-
const item = draftState.items[itemKey];
|
|
2182
|
-
if (!item)
|
|
2183
|
-
continue;
|
|
2184
|
-
someItemWasUpdated = true;
|
|
2185
|
-
const newData = produceNewData(item, payload);
|
|
2186
|
-
if (newData) {
|
|
2187
|
-
draftState.items[itemKey] = newData;
|
|
2188
|
-
}
|
|
2189
|
-
}
|
|
2190
|
-
});
|
|
2191
|
-
if (someItemWasUpdated) {
|
|
2192
|
-
applyOptimisticListUpdates(itemKeys.map((i) => i.itemKey));
|
|
2193
|
-
}
|
|
2194
|
-
if (ifNothingWasUpdated && !someItemWasUpdated) {
|
|
2195
|
-
ifNothingWasUpdated();
|
|
2196
|
-
}
|
|
2197
|
-
},
|
|
2198
|
-
{ type: "update-item-state", item: itemKeys }
|
|
2199
|
-
);
|
|
2200
|
-
return someItemWasUpdated;
|
|
2201
|
-
}
|
|
2202
|
-
function applyOptimisticListUpdates(itemKeys) {
|
|
2203
|
-
if (!optimisticListUpdates)
|
|
2204
|
-
return;
|
|
2205
|
-
const queriesToInvalidate = [];
|
|
2206
|
-
store.produceState((draftState) => {
|
|
2207
|
-
for (const itemKey of itemKeys) {
|
|
2208
|
-
const item = draftState.items[itemKey];
|
|
2209
|
-
if (!item)
|
|
2210
|
-
continue;
|
|
2211
|
-
for (const {
|
|
2212
|
-
queries,
|
|
2213
|
-
filterItem,
|
|
2214
|
-
appendNewTo = "end",
|
|
2215
|
-
invalidateQueries,
|
|
2216
|
-
sort
|
|
2217
|
-
} of optimisticListUpdates) {
|
|
2218
|
-
const relatedFilterQueries = getQueriesKeyArray(queries);
|
|
2219
|
-
for (const { key: queryKey, payload } of relatedFilterQueries) {
|
|
2220
|
-
const queryState = draftState.queries[queryKey];
|
|
2221
|
-
if (filterItem) {
|
|
2222
|
-
const itemShouldBeIncluded = filterItem(item);
|
|
2223
|
-
if (itemShouldBeIncluded === null)
|
|
2224
|
-
continue;
|
|
2225
|
-
if (itemShouldBeIncluded) {
|
|
2226
|
-
if (!queryState) {
|
|
2227
|
-
draftState.queries[queryKey] = {
|
|
2228
|
-
status: "success",
|
|
2229
|
-
items: [itemKey],
|
|
2230
|
-
error: null,
|
|
2231
|
-
hasMore: false,
|
|
2232
|
-
payload,
|
|
2233
|
-
refetchOnMount: "lowPriority",
|
|
2234
|
-
wasLoaded: true
|
|
2235
|
-
};
|
|
2236
|
-
continue;
|
|
2237
|
-
}
|
|
2238
|
-
if (queryState.items.includes(itemKey))
|
|
2239
|
-
continue;
|
|
2240
|
-
if (invalidateQueries)
|
|
2241
|
-
queriesToInvalidate.push(payload);
|
|
2242
|
-
if (appendNewTo === "end") {
|
|
2243
|
-
queryState.items.push(itemKey);
|
|
2244
|
-
} else {
|
|
2245
|
-
queryState.items.unshift(itemKey);
|
|
2246
|
-
}
|
|
2247
|
-
} else {
|
|
2248
|
-
if (!queryState)
|
|
2249
|
-
continue;
|
|
2250
|
-
const itemIndex = queryState.items.indexOf(itemKey);
|
|
2251
|
-
if (itemIndex !== -1) {
|
|
2252
|
-
if (invalidateQueries)
|
|
2253
|
-
queriesToInvalidate.push(queryState.payload);
|
|
2254
|
-
queryState.items.splice(itemIndex, 1);
|
|
2255
|
-
}
|
|
2256
|
-
}
|
|
2257
|
-
}
|
|
2258
|
-
if (sort) {
|
|
2259
|
-
if (!queryState)
|
|
2260
|
-
continue;
|
|
2261
|
-
const queryHasItem = queryState.items.includes(itemKey);
|
|
2262
|
-
if (!queryHasItem)
|
|
2263
|
-
continue;
|
|
2264
|
-
queryState.items = sortBy(
|
|
2265
|
-
queryState.items,
|
|
2266
|
-
(itemId) => {
|
|
2267
|
-
const itemState = store.state.items[itemId];
|
|
2268
|
-
const itemPayload = store.state.itemQueries[itemId]?.payload;
|
|
2269
|
-
if (!itemState || !itemPayload)
|
|
2270
|
-
return Infinity;
|
|
2271
|
-
return sort.sortBy(itemState, itemPayload);
|
|
2272
|
-
},
|
|
2273
|
-
{
|
|
2274
|
-
order: sort.order
|
|
2275
|
-
}
|
|
2276
|
-
);
|
|
2277
|
-
}
|
|
2278
|
-
}
|
|
2279
|
-
}
|
|
2280
|
-
}
|
|
2281
|
-
});
|
|
2282
|
-
if (queriesToInvalidate.length)
|
|
2283
|
-
invalidateQueryAndItems({
|
|
2284
|
-
queryPayload: queriesToInvalidate,
|
|
2285
|
-
itemPayload: false
|
|
2286
|
-
});
|
|
2287
|
-
}
|
|
2288
|
-
function addItemToState(itemPayload, data, {
|
|
2289
|
-
addItemToQueries
|
|
2290
|
-
} = {}) {
|
|
2291
|
-
const itemKey = getItemKey(itemPayload);
|
|
2292
|
-
store.batch(() => {
|
|
2293
|
-
store.produceState(
|
|
2294
|
-
(draftState) => {
|
|
2295
|
-
draftState.items[itemKey] = data;
|
|
2296
|
-
draftState.itemQueries[itemKey] = {
|
|
2297
|
-
status: "success",
|
|
2298
|
-
wasLoaded: true,
|
|
2299
|
-
refetchOnMount: false,
|
|
2300
|
-
error: null,
|
|
2301
|
-
payload: (0, import_json2.klona)(itemPayload)
|
|
2302
|
-
};
|
|
2303
|
-
if (addItemToQueries) {
|
|
2304
|
-
const queries = getQueriesKeyArray(addItemToQueries.queries);
|
|
2305
|
-
for (const { key } of queries) {
|
|
2306
|
-
if (draftState.queries[key]) {
|
|
2307
|
-
const queryState = draftState.queries[key];
|
|
2308
|
-
if (!queryState)
|
|
2309
|
-
continue;
|
|
2310
|
-
if (queryState.items.includes(itemKey))
|
|
2311
|
-
continue;
|
|
2312
|
-
if (addItemToQueries.appendTo === "start") {
|
|
2313
|
-
queryState.items.unshift(itemKey);
|
|
2314
|
-
} else if (addItemToQueries.appendTo === "end") {
|
|
2315
|
-
queryState.items.push(itemKey);
|
|
2316
|
-
} else {
|
|
2317
|
-
const index = addItemToQueries.appendTo(
|
|
2318
|
-
filterAndMap(
|
|
2319
|
-
queryState.items,
|
|
2320
|
-
(itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
|
|
2321
|
-
)
|
|
2322
|
-
);
|
|
2323
|
-
queryState.items.splice(index, 0, itemKey);
|
|
2324
|
-
}
|
|
2325
|
-
}
|
|
2326
|
-
}
|
|
2327
|
-
}
|
|
2328
|
-
},
|
|
2329
|
-
{ action: { type: "create-item-state", itemPayload } }
|
|
2330
|
-
);
|
|
2331
|
-
applyOptimisticListUpdates([itemKey]);
|
|
2332
|
-
});
|
|
2333
|
-
}
|
|
2334
|
-
function deleteItemState(itemId) {
|
|
2335
|
-
const itemsId = getItemsKeyArray(itemId);
|
|
2336
|
-
store.produceState(
|
|
2337
|
-
(draftState) => {
|
|
2338
|
-
for (const { itemKey } of itemsId) {
|
|
2339
|
-
draftState.items[itemKey] = null;
|
|
2340
|
-
draftState.itemQueries[itemKey] = null;
|
|
2341
|
-
for (const query of Object.values(draftState.queries)) {
|
|
2342
|
-
if (query.items.includes(itemKey)) {
|
|
2343
|
-
query.items = query.items.filter((i) => i !== itemKey);
|
|
2344
|
-
}
|
|
2345
|
-
}
|
|
2346
|
-
}
|
|
2347
|
-
},
|
|
2348
|
-
{ action: { type: "delete-item-state", itemId } }
|
|
2349
|
-
);
|
|
2350
|
-
}
|
|
2351
|
-
function reset() {
|
|
2352
|
-
fetchItemOrquestrator?.reset();
|
|
2353
|
-
fetchQueryOrquestrator.reset();
|
|
2354
|
-
store.setState({
|
|
2355
|
-
items: {},
|
|
2356
|
-
queries: {},
|
|
2357
|
-
itemQueries: {}
|
|
2358
|
-
});
|
|
2359
|
-
}
|
|
2360
|
-
return {
|
|
2361
|
-
store,
|
|
2362
|
-
reset,
|
|
2363
|
-
scheduleListQueryFetch,
|
|
2364
|
-
getQueryState,
|
|
2365
|
-
getQueryKey,
|
|
2366
|
-
deleteItemState,
|
|
2367
|
-
getItemState,
|
|
2368
|
-
loadMore,
|
|
2369
|
-
invalidateQueryAndItems,
|
|
2370
|
-
scheduleItemFetch,
|
|
2371
|
-
awaitItemFetch,
|
|
2372
|
-
awaitListQueryFetch,
|
|
2373
|
-
useMultipleListQueries,
|
|
2374
|
-
useListQuery,
|
|
2375
|
-
getItemKey,
|
|
2376
|
-
useItem,
|
|
2377
|
-
useMultipleItems,
|
|
2378
|
-
addItemToState,
|
|
2379
|
-
updateItemState,
|
|
2380
|
-
startItemMutation,
|
|
2381
|
-
getQueriesState,
|
|
2382
|
-
getQueriesRelatedToItem,
|
|
2383
|
-
useFindItem
|
|
2384
|
-
};
|
|
2385
|
-
}
|
|
2386
|
-
|
|
2387
|
-
// src/useListItemIsLoading.ts
|
|
2388
|
-
var import_react11 = require("react");
|
|
2389
|
-
|
|
2390
|
-
// src/utils/useTimeout.ts
|
|
2391
|
-
var import_react10 = require("react");
|
|
2392
|
-
function useTimeout(ms) {
|
|
2393
|
-
const timeoutsRef = useConst(() => /* @__PURE__ */ new Set());
|
|
2394
|
-
const callback = (0, import_react10.useCallback)(
|
|
2395
|
-
(cb, overrideMs = ms) => {
|
|
2396
|
-
timeoutsRef.add(window.setTimeout(cb, overrideMs));
|
|
2397
|
-
},
|
|
2398
|
-
[ms, timeoutsRef]
|
|
2399
|
-
);
|
|
2400
|
-
const clear = (0, import_react10.useCallback)(() => {
|
|
2401
|
-
for (const timeout of timeoutsRef) {
|
|
2402
|
-
clearTimeout(timeout);
|
|
2403
|
-
}
|
|
2404
|
-
}, [timeoutsRef]);
|
|
2405
|
-
(0, import_react10.useEffect)(() => clear, [clear]);
|
|
2406
|
-
return (0, import_react10.useMemo)(() => {
|
|
2407
|
-
return {
|
|
2408
|
-
call: callback,
|
|
2409
|
-
clear
|
|
2410
|
-
};
|
|
2411
|
-
}, [callback, clear]);
|
|
2412
|
-
}
|
|
2413
|
-
|
|
2414
|
-
// src/useListItemIsLoading.ts
|
|
2415
|
-
function useListItemIsLoading({
|
|
2416
|
-
listIsLoading,
|
|
2417
|
-
isRefetching,
|
|
2418
|
-
itemExists,
|
|
2419
|
-
loadItemFallback,
|
|
2420
|
-
itemId
|
|
2421
|
-
}) {
|
|
2422
|
-
const isNotFound = !itemExists;
|
|
2423
|
-
const [willBeRefetched, setWillBeRefetched] = (0, import_react11.useState)(isNotFound);
|
|
2424
|
-
const resetWillBeRefechedTimeout = useTimeout(1e3);
|
|
2425
|
-
const callFallbackLoadItemTimeout = useTimeout(100);
|
|
2426
|
-
const ignoreSetWillBeRefetched = (0, import_react11.useRef)(false);
|
|
2427
|
-
const itemWasRefetched = useConst(() => /* @__PURE__ */ new Set());
|
|
2428
|
-
if (isNotFound && !isRefetching && !willBeRefetched && !ignoreSetWillBeRefetched.current && itemId && !itemWasRefetched.has(itemId)) {
|
|
2429
|
-
setWillBeRefetched(true);
|
|
2430
|
-
}
|
|
2431
|
-
const latestIsLoadingOrRefetching = useLatestValue(
|
|
2432
|
-
listIsLoading || isRefetching
|
|
2433
|
-
);
|
|
2434
|
-
const latestLoadItemFallback = useLatestValue(loadItemFallback);
|
|
2435
|
-
(0, import_react11.useEffect)(() => {
|
|
2436
|
-
if (isRefetching) {
|
|
2437
|
-
if (itemId)
|
|
2438
|
-
itemWasRefetched.add(itemId);
|
|
2439
|
-
resetWillBeRefechedTimeout.clear();
|
|
2440
|
-
callFallbackLoadItemTimeout.clear();
|
|
2441
|
-
setWillBeRefetched(false);
|
|
2442
|
-
}
|
|
2443
|
-
}, [
|
|
2444
|
-
callFallbackLoadItemTimeout,
|
|
2445
|
-
isRefetching,
|
|
2446
|
-
itemId,
|
|
2447
|
-
itemWasRefetched,
|
|
2448
|
-
resetWillBeRefechedTimeout
|
|
2449
|
-
]);
|
|
2450
|
-
(0, import_react11.useEffect)(() => {
|
|
2451
|
-
if (willBeRefetched) {
|
|
2452
|
-
callFallbackLoadItemTimeout.call(() => {
|
|
2453
|
-
if (!latestIsLoadingOrRefetching.insideEffect) {
|
|
2454
|
-
latestLoadItemFallback.insideEffect();
|
|
2455
|
-
return;
|
|
2456
|
-
}
|
|
2457
|
-
});
|
|
2458
|
-
resetWillBeRefechedTimeout.call(() => {
|
|
2459
|
-
ignoreSetWillBeRefetched.current = true;
|
|
2460
|
-
setWillBeRefetched(false);
|
|
2461
|
-
setTimeout(() => {
|
|
2462
|
-
ignoreSetWillBeRefetched.current = false;
|
|
2463
|
-
}, 20);
|
|
2464
|
-
});
|
|
2465
|
-
}
|
|
2466
|
-
return () => {
|
|
2467
|
-
resetWillBeRefechedTimeout.clear();
|
|
2468
|
-
callFallbackLoadItemTimeout.clear();
|
|
2469
|
-
};
|
|
2470
|
-
}, [
|
|
2471
|
-
resetWillBeRefechedTimeout,
|
|
2472
|
-
willBeRefetched,
|
|
2473
|
-
latestIsLoadingOrRefetching,
|
|
2474
|
-
latestLoadItemFallback,
|
|
2475
|
-
callFallbackLoadItemTimeout
|
|
2476
|
-
]);
|
|
2477
|
-
return listIsLoading || isNotFound && (isRefetching || !!willBeRefetched);
|
|
2478
|
-
}
|
|
2479
|
-
|
|
2480
|
-
// src/useListItemIsDeleted.ts
|
|
2481
|
-
var import_react12 = require("react");
|
|
2482
|
-
function useListItemIsDeleted({
|
|
2483
|
-
itemExists,
|
|
2484
|
-
listIsLoading,
|
|
2485
|
-
onDelete,
|
|
2486
|
-
itemId
|
|
2487
|
-
}) {
|
|
2488
|
-
const [isDeleted, setIsDeleted] = (0, import_react12.useState)();
|
|
2489
|
-
useOnChange({ itemExists, itemId }, ({ current, prev }) => {
|
|
2490
|
-
if (current.itemId !== itemId)
|
|
2491
|
-
return;
|
|
2492
|
-
if (listIsLoading || !prev)
|
|
2493
|
-
return;
|
|
2494
|
-
const wasItemDeleted = !current.itemExists && !!prev.itemExists;
|
|
2495
|
-
if (wasItemDeleted) {
|
|
2496
|
-
onDelete?.();
|
|
2497
|
-
setIsDeleted(itemId);
|
|
2498
|
-
}
|
|
2499
|
-
});
|
|
2500
|
-
return isDeleted === itemId;
|
|
2501
|
-
}
|
|
2502
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
2503
|
-
0 && (module.exports = {
|
|
2504
|
-
getCacheId,
|
|
2505
|
-
newTSDFCollectionStore,
|
|
2506
|
-
newTSDFDocumentStore,
|
|
2507
|
-
newTSDFListQueryStore,
|
|
2508
|
-
useListItemIsDeleted,
|
|
2509
|
-
useListItemIsLoading
|
|
2510
|
-
});
|