tsdf 8.0.0-beta.1 → 8.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.cjs +1 -0
- package/dist/main.d.ts +269 -0
- package/dist/main.js +1 -0
- package/package.json +4 -1
- package/.eslintignore +0 -3
- package/.eslintrc.cjs +0 -119
- package/.gitattributes +0 -22
- package/.prettierrc +0 -6
- package/.vscode/settings.json +0 -3
- package/.vscode/tasks.json +0 -35
- package/fetch-orquestrator-scenarios.excalidraw +0 -10115
- package/scripts/check-if-is-sync.sh +0 -6
- package/src/collectionFetchOrquestrator.ts +0 -23
- package/src/collectionStore.ts +0 -608
- package/src/documentStore.ts +0 -272
- package/src/fetchOrquestrator.ts +0 -335
- package/src/listQueryStore.ts +0 -1190
- package/src/main.ts +0 -22
- package/src/storeShared.ts +0 -5
- package/src/useEnsureIsLoaded.ts +0 -50
- package/src/utils/filterAndMap.ts +0 -20
- package/src/utils/getCacheId.ts +0 -19
- package/src/utils/hooks.ts +0 -110
- package/src/utils/isObject.ts +0 -3
- package/src/utils/math.ts +0 -7
- package/src/utils/serializableClone.ts +0 -3
- package/src/utils/useLatestValue.ts +0 -16
- package/test/__snapshots__/listQueryStore.test.ts.snap +0 -146
- package/test/__snapshots__/listQueryStoreHooks.test.ts.snap +0 -9
- package/test/collectionStore.test.ts +0 -523
- package/test/collectionStoreHooks.test.tsx +0 -651
- package/test/documentStore.test.ts +0 -183
- package/test/documentStoreHooks.test.tsx +0 -857
- package/test/fetchOrquestrator.test.ts +0 -913
- package/test/listQueryStore.test.ts +0 -1158
- package/test/listQueryStoreHooks.test.tsx +0 -662
- package/test/listQueryStoreHooks2.test.tsx +0 -483
- package/test/listQueryStoreHooksMutations.test.tsx +0 -764
- package/test/mocks/fetchMock.ts +0 -296
- package/test/mocks/fetchOrquestratorEnv.ts +0 -370
- package/test/setup/setup.ts +0 -109
- package/test/testHelpers.test.ts +0 -88
- package/test/utils/RenderObjValues.tsx +0 -23
- package/test/utils/arrayUtils.ts +0 -13
- package/test/utils/createDefaultListQueryStore.ts +0 -275
- package/test/utils/createOrVariations.ts +0 -48
- package/test/utils/dedent.ts +0 -50
- package/test/utils/delayCall.ts +0 -25
- package/test/utils/math.ts +0 -3
- package/test/utils/objectUtils.ts +0 -18
- package/test/utils/range.ts +0 -19
- package/test/utils/sleep.ts +0 -3
- package/test/utils/storeUtils.ts +0 -460
- package/tsconfig.json +0 -24
- package/tsconfig.prod.json +0 -9
- package/tsup.config.ts +0 -12
- package/vitest.config.ts +0 -10
package/src/listQueryStore.ts
DELETED
|
@@ -1,1190 +0,0 @@
|
|
|
1
|
-
import mitt from 'mitt';
|
|
2
|
-
import { useCallback, useEffect, useMemo } from 'react';
|
|
3
|
-
import { deepEqual, Store, useSubscribeToStore } from 't-state';
|
|
4
|
-
import { createCollectionFetchOrquestrator } from './collectionFetchOrquestrator';
|
|
5
|
-
import {
|
|
6
|
-
FetchType,
|
|
7
|
-
ScheduleFetchResults,
|
|
8
|
-
FetchContext as FetchCtx,
|
|
9
|
-
} from './fetchOrquestrator';
|
|
10
|
-
import { Status, ValidPayload, ValidStoreState } from './storeShared';
|
|
11
|
-
import { useEnsureIsLoaded } from './useEnsureIsLoaded';
|
|
12
|
-
import { filterAndMap } from './utils/filterAndMap';
|
|
13
|
-
import { getCacheId } from './utils/getCacheId';
|
|
14
|
-
import { useConst, useDeepMemo, useOnMittEvent } from './utils/hooks';
|
|
15
|
-
import { serializableClone } from './utils/serializableClone';
|
|
16
|
-
|
|
17
|
-
type QueryStatus = Status | 'loadingMore';
|
|
18
|
-
|
|
19
|
-
export type TSFDListQuery<NError, QueryPayload extends ValidPayload> = {
|
|
20
|
-
error: NError | null;
|
|
21
|
-
status: QueryStatus;
|
|
22
|
-
payload: QueryPayload;
|
|
23
|
-
hasMore: boolean;
|
|
24
|
-
wasLoaded: boolean;
|
|
25
|
-
refetchOnMount: false | FetchType;
|
|
26
|
-
items: string[];
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export type TSDFItemQuery<NError> = {
|
|
30
|
-
error: NError | null;
|
|
31
|
-
status: Exclude<QueryStatus, 'loadingMore'>;
|
|
32
|
-
wasLoaded: boolean;
|
|
33
|
-
refetchOnMount: false | FetchType;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export type TSFDListQueryState<
|
|
37
|
-
ItemState extends ValidStoreState,
|
|
38
|
-
NError,
|
|
39
|
-
QueryPayload extends ValidPayload,
|
|
40
|
-
> = {
|
|
41
|
-
items: Record<string, ItemState | null>;
|
|
42
|
-
queries: Record<string, TSFDListQuery<NError, QueryPayload>>;
|
|
43
|
-
itemQueries: Record<string, TSDFItemQuery<NError> | null>;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export type TSFDUseListQueryReturn<Selected, ItemPayload, NError> = {
|
|
47
|
-
items: Selected[];
|
|
48
|
-
status: QueryStatus | 'idle';
|
|
49
|
-
payload: ItemPayload | undefined;
|
|
50
|
-
error: NError | null;
|
|
51
|
-
queryKey: string;
|
|
52
|
-
hasMore: boolean;
|
|
53
|
-
isLoading: boolean;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export type TSFDUseItemReturn<Selected, NError> = {
|
|
57
|
-
data: Selected;
|
|
58
|
-
status: QueryStatus | 'idle' | 'deleted';
|
|
59
|
-
itemId: string;
|
|
60
|
-
error: NError | null;
|
|
61
|
-
isLoading: boolean;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
type FetchListFnReturn<ItemState extends ValidStoreState> = {
|
|
65
|
-
items: { id: string | number; data: ItemState }[];
|
|
66
|
-
hasMore: boolean;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const noFetchFnError = 'No fetchItemFn was provided';
|
|
70
|
-
|
|
71
|
-
export function newTSDFListQueryStore<
|
|
72
|
-
ItemState extends ValidStoreState,
|
|
73
|
-
NError,
|
|
74
|
-
QueryPayload extends ValidPayload,
|
|
75
|
-
>({
|
|
76
|
-
debugName,
|
|
77
|
-
fetchListFn,
|
|
78
|
-
fetchItemFn,
|
|
79
|
-
errorNormalizer,
|
|
80
|
-
defaultQuerySize = 50,
|
|
81
|
-
disableRefetchOnWindowFocus,
|
|
82
|
-
disableRefetchOnMount: globalDisableRefetchOnMount,
|
|
83
|
-
lowPriorityThrottleMs,
|
|
84
|
-
mediumPriorityThrottleMs,
|
|
85
|
-
dynamicRealtimeThrottleMs,
|
|
86
|
-
syncMutationsAndInvalidations,
|
|
87
|
-
}: {
|
|
88
|
-
debugName?: string;
|
|
89
|
-
fetchListFn: (
|
|
90
|
-
payload: QueryPayload,
|
|
91
|
-
size: number,
|
|
92
|
-
) => Promise<FetchListFnReturn<ItemState>>;
|
|
93
|
-
fetchItemFn?: (itemId: string) => Promise<ItemState>;
|
|
94
|
-
errorNormalizer: (exception: unknown) => NError;
|
|
95
|
-
defaultQuerySize?: number;
|
|
96
|
-
disableRefetchOnWindowFocus?: boolean;
|
|
97
|
-
syncMutationsAndInvalidations?: {
|
|
98
|
-
syncQueries: (query1: QueryPayload, query2: QueryPayload) => boolean;
|
|
99
|
-
syncItemAndQuery: (itemId: string, query: QueryPayload) => boolean;
|
|
100
|
-
};
|
|
101
|
-
disableRefetchOnMount?: boolean;
|
|
102
|
-
lowPriorityThrottleMs?: number;
|
|
103
|
-
mediumPriorityThrottleMs?: number;
|
|
104
|
-
dynamicRealtimeThrottleMs?: (lastFetchDuration: number) => number;
|
|
105
|
-
}) {
|
|
106
|
-
type State = TSFDListQueryState<ItemState, NError, QueryPayload>;
|
|
107
|
-
type Query = TSFDListQuery<NError, QueryPayload>;
|
|
108
|
-
|
|
109
|
-
const store = new Store<State>({
|
|
110
|
-
debugName,
|
|
111
|
-
state: { items: {}, queries: {}, itemQueries: {} },
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
function getQueryKey(params: QueryPayload): string {
|
|
115
|
-
return getCacheId(params);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async function fetchQuery(
|
|
119
|
-
fetchCtx: FetchCtx,
|
|
120
|
-
[fetchType, queryPayload, size = defaultQuerySize]: [
|
|
121
|
-
'load' | 'loadMore',
|
|
122
|
-
QueryPayload,
|
|
123
|
-
number | undefined,
|
|
124
|
-
],
|
|
125
|
-
): Promise<boolean> {
|
|
126
|
-
const payload = serializableClone(queryPayload);
|
|
127
|
-
const queryKey = getQueryKey(payload);
|
|
128
|
-
|
|
129
|
-
const queryState = store.state.queries[queryKey];
|
|
130
|
-
|
|
131
|
-
const isLoading = !queryState?.wasLoaded;
|
|
132
|
-
|
|
133
|
-
store.produceState(
|
|
134
|
-
(draft) => {
|
|
135
|
-
const query = draft.queries[queryKey];
|
|
136
|
-
|
|
137
|
-
if (!query) {
|
|
138
|
-
draft.queries[queryKey] = {
|
|
139
|
-
error: null,
|
|
140
|
-
status: 'loading',
|
|
141
|
-
wasLoaded: false,
|
|
142
|
-
payload,
|
|
143
|
-
refetchOnMount: false,
|
|
144
|
-
hasMore: false,
|
|
145
|
-
items: [],
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
query.status = query.wasLoaded
|
|
152
|
-
? fetchType === 'loadMore'
|
|
153
|
-
? 'loadingMore'
|
|
154
|
-
: 'refetching'
|
|
155
|
-
: 'loading';
|
|
156
|
-
query.error = null;
|
|
157
|
-
query.refetchOnMount = false;
|
|
158
|
-
},
|
|
159
|
-
{
|
|
160
|
-
equalityCheck: deepEqual,
|
|
161
|
-
action: {
|
|
162
|
-
type: isLoading
|
|
163
|
-
? 'fetch-query-start-loading'
|
|
164
|
-
: fetchType === 'loadMore'
|
|
165
|
-
? 'fetch-query-loading-more'
|
|
166
|
-
: 'refetching-query-start',
|
|
167
|
-
payload,
|
|
168
|
-
},
|
|
169
|
-
},
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
const currentQuerySize = queryState?.items.length ?? 0;
|
|
173
|
-
|
|
174
|
-
const querySize = ((): number => {
|
|
175
|
-
if (fetchType === 'loadMore') {
|
|
176
|
-
return currentQuerySize + size;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (isLoading) return size;
|
|
180
|
-
|
|
181
|
-
return Math.max(currentQuerySize, size);
|
|
182
|
-
})();
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
const { items, hasMore } = await fetchListFn(payload, querySize);
|
|
186
|
-
|
|
187
|
-
if (fetchCtx.shouldAbort()) return false;
|
|
188
|
-
|
|
189
|
-
store.produceState(
|
|
190
|
-
(draft) => {
|
|
191
|
-
const query = draft.queries[queryKey];
|
|
192
|
-
|
|
193
|
-
if (!query) return;
|
|
194
|
-
|
|
195
|
-
query.status = 'success';
|
|
196
|
-
query.wasLoaded = true;
|
|
197
|
-
query.hasMore = hasMore;
|
|
198
|
-
|
|
199
|
-
query.items = [];
|
|
200
|
-
|
|
201
|
-
for (const { data, id } of items) {
|
|
202
|
-
draft.items[id] = data;
|
|
203
|
-
query.items.push(String(id));
|
|
204
|
-
|
|
205
|
-
const itemQuery = draft.itemQueries[id];
|
|
206
|
-
|
|
207
|
-
if (
|
|
208
|
-
!itemQuery ||
|
|
209
|
-
(itemQuery.status !== 'loading' &&
|
|
210
|
-
itemQuery.status !== 'refetching')
|
|
211
|
-
) {
|
|
212
|
-
draft.itemQueries[id] = {
|
|
213
|
-
error: null,
|
|
214
|
-
refetchOnMount: false,
|
|
215
|
-
status: 'success',
|
|
216
|
-
wasLoaded: true,
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
},
|
|
221
|
-
{
|
|
222
|
-
action: { type: 'fetch-query-success', payload },
|
|
223
|
-
},
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
for (const { id } of items) {
|
|
227
|
-
const itemFetchOrquestrator = fetchItemOrquestrator?.get(String(id));
|
|
228
|
-
|
|
229
|
-
if (itemFetchOrquestrator) {
|
|
230
|
-
itemFetchOrquestrator.setLastFetchStartTime(fetchCtx.getStartTime());
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return true;
|
|
235
|
-
} catch (exception) {
|
|
236
|
-
if (fetchCtx.shouldAbort()) return false;
|
|
237
|
-
|
|
238
|
-
const error = errorNormalizer(exception);
|
|
239
|
-
|
|
240
|
-
store.produceState(
|
|
241
|
-
(draft) => {
|
|
242
|
-
const query = draft.queries[queryKey];
|
|
243
|
-
|
|
244
|
-
if (!query) return;
|
|
245
|
-
|
|
246
|
-
query.status = 'error';
|
|
247
|
-
query.error = error;
|
|
248
|
-
},
|
|
249
|
-
{
|
|
250
|
-
action: { type: 'fetch-query-error', payload, error },
|
|
251
|
-
},
|
|
252
|
-
);
|
|
253
|
-
|
|
254
|
-
return false;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function getQueryState(params: QueryPayload): Query | undefined {
|
|
259
|
-
return store.state.queries[getQueryKey(params)];
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function getItemState(
|
|
263
|
-
id: string[] | FilterItemFn,
|
|
264
|
-
): { id: string; data: ItemState }[];
|
|
265
|
-
function getItemState(id: string): ItemState | null | undefined;
|
|
266
|
-
function getItemState(
|
|
267
|
-
id: string | string[] | FilterItemFn,
|
|
268
|
-
): ItemState | null | undefined | { id: string; data: ItemState }[] {
|
|
269
|
-
if (typeof id === 'string') {
|
|
270
|
-
return store.state.items[id];
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const itemsId = getItemsIdArray(id);
|
|
274
|
-
|
|
275
|
-
return filterAndMap(itemsId, (itemId, ignore) => {
|
|
276
|
-
const item = store.state.items[itemId];
|
|
277
|
-
return !item ? ignore : { id: itemId, data: item };
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const fetchQueryOrquestrator = createCollectionFetchOrquestrator({
|
|
282
|
-
fetchFn: fetchQuery,
|
|
283
|
-
lowPriorityThrottleMs,
|
|
284
|
-
dynamicRealtimeThrottleMs,
|
|
285
|
-
mediumPriorityThrottleMs,
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
function scheduleListQueryFetch(
|
|
289
|
-
fetchType: FetchType,
|
|
290
|
-
payload: QueryPayload,
|
|
291
|
-
size?: number,
|
|
292
|
-
): ScheduleFetchResults;
|
|
293
|
-
function scheduleListQueryFetch(
|
|
294
|
-
fetchType: FetchType,
|
|
295
|
-
payload: QueryPayload[],
|
|
296
|
-
size?: number,
|
|
297
|
-
): ScheduleFetchResults[];
|
|
298
|
-
function scheduleListQueryFetch(
|
|
299
|
-
fetchType: FetchType,
|
|
300
|
-
payload: QueryPayload | QueryPayload[],
|
|
301
|
-
size?: number,
|
|
302
|
-
): ScheduleFetchResults | ScheduleFetchResults[] {
|
|
303
|
-
const multiplePayloads = Array.isArray(payload);
|
|
304
|
-
|
|
305
|
-
const payloads = multiplePayloads ? payload : [payload];
|
|
306
|
-
|
|
307
|
-
const results = payloads.map((param) => {
|
|
308
|
-
return fetchQueryOrquestrator
|
|
309
|
-
.get(getQueryKey(param))
|
|
310
|
-
.scheduleFetch(fetchType, ['load', param, size]);
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
return multiplePayloads ? results : results[0]!;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
function loadMore(params: QueryPayload, size: number): ScheduleFetchResults {
|
|
317
|
-
const queryState = getQueryState(params);
|
|
318
|
-
|
|
319
|
-
if (!queryState || !queryState.hasMore) return 'skipped';
|
|
320
|
-
|
|
321
|
-
return fetchQueryOrquestrator
|
|
322
|
-
.get(getQueryKey(params))
|
|
323
|
-
.scheduleFetch('highPriority', ['loadMore', params, size]);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
function getQueryItems<T>(
|
|
327
|
-
query: Query,
|
|
328
|
-
itemDataSelector: (data: ItemState, id: string) => T,
|
|
329
|
-
): T[] {
|
|
330
|
-
return filterAndMap(query.items, (itemId, ignore) => {
|
|
331
|
-
const data = store.state.items[itemId];
|
|
332
|
-
return data ? itemDataSelector(data, itemId) : ignore;
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
async function awaitListQueryFetch(
|
|
337
|
-
params: QueryPayload,
|
|
338
|
-
size = defaultQuerySize,
|
|
339
|
-
): Promise<
|
|
340
|
-
| { items: []; error: NError; hasMore: boolean }
|
|
341
|
-
| {
|
|
342
|
-
items: { data: ItemState; id: string }[];
|
|
343
|
-
error: null;
|
|
344
|
-
hasMore: boolean;
|
|
345
|
-
}
|
|
346
|
-
> {
|
|
347
|
-
const queryKey = getQueryKey(params);
|
|
348
|
-
|
|
349
|
-
const wasAborted = await fetchQueryOrquestrator
|
|
350
|
-
.get(queryKey)
|
|
351
|
-
.awaitFetch(['load', params, size]);
|
|
352
|
-
|
|
353
|
-
if (wasAborted) {
|
|
354
|
-
return {
|
|
355
|
-
items: [],
|
|
356
|
-
error: errorNormalizer(new Error('Aborted')),
|
|
357
|
-
hasMore: false,
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
const query = store.state.queries[queryKey];
|
|
362
|
-
|
|
363
|
-
if (!query) {
|
|
364
|
-
return {
|
|
365
|
-
items: [],
|
|
366
|
-
error: errorNormalizer(new Error('Not found')),
|
|
367
|
-
hasMore: false,
|
|
368
|
-
};
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
return query.error
|
|
372
|
-
? { items: [], error: query.error, hasMore: query.hasMore }
|
|
373
|
-
: {
|
|
374
|
-
items: getQueryItems(query, (data, id) => ({ data, id })),
|
|
375
|
-
error: null,
|
|
376
|
-
hasMore: query.hasMore,
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
async function awaitItemFetch(itemId: string): Promise<
|
|
381
|
-
| { data: null; error: NError }
|
|
382
|
-
| {
|
|
383
|
-
data: ItemState;
|
|
384
|
-
error: null;
|
|
385
|
-
}
|
|
386
|
-
> {
|
|
387
|
-
if (!fetchItemOrquestrator) throw new Error(noFetchFnError);
|
|
388
|
-
|
|
389
|
-
const wasAborted = await fetchItemOrquestrator
|
|
390
|
-
.get(itemId)
|
|
391
|
-
.awaitFetch(itemId);
|
|
392
|
-
|
|
393
|
-
if (wasAborted) {
|
|
394
|
-
return { data: null, error: errorNormalizer(new Error('Aborted')) };
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
const itemData = store.state.items[itemId];
|
|
398
|
-
const itemQuery = store.state.itemQueries[itemId];
|
|
399
|
-
|
|
400
|
-
if (!itemQuery || !itemData) {
|
|
401
|
-
return { data: null, error: errorNormalizer(new Error('Not found')) };
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
return itemQuery.error
|
|
405
|
-
? { data: null, error: itemQuery.error }
|
|
406
|
-
: { data: itemData, error: null };
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
type FilterQueryFn = (
|
|
410
|
-
params: QueryPayload,
|
|
411
|
-
data: Query,
|
|
412
|
-
queryKey: string,
|
|
413
|
-
) => boolean;
|
|
414
|
-
|
|
415
|
-
function getQueriesKeyArray(
|
|
416
|
-
payloads: QueryPayload | QueryPayload[] | FilterQueryFn,
|
|
417
|
-
): { key: string; payload: QueryPayload }[] {
|
|
418
|
-
if (Array.isArray(payloads)) {
|
|
419
|
-
return payloads.map((payload) => ({
|
|
420
|
-
key: getQueryKey(payload),
|
|
421
|
-
payload,
|
|
422
|
-
}));
|
|
423
|
-
} else if (typeof payloads === 'function') {
|
|
424
|
-
return filterAndMap(
|
|
425
|
-
Object.entries(store.state.queries),
|
|
426
|
-
([queryKey, query], ignore) => {
|
|
427
|
-
return payloads(query.payload, query, queryKey)
|
|
428
|
-
? {
|
|
429
|
-
key: queryKey,
|
|
430
|
-
payload: query.payload,
|
|
431
|
-
}
|
|
432
|
-
: ignore;
|
|
433
|
-
},
|
|
434
|
-
);
|
|
435
|
-
} else {
|
|
436
|
-
return [{ key: getQueryKey(payloads), payload: payloads }];
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
const storeEvents = mitt<{
|
|
441
|
-
invalidateQuery: { priority: FetchType; queryKey: string };
|
|
442
|
-
invalidateItem: { priority: FetchType; itemId: string };
|
|
443
|
-
}>();
|
|
444
|
-
|
|
445
|
-
const queryInvalidationWasTriggered = new Set<string>();
|
|
446
|
-
|
|
447
|
-
function invalidateQuery(
|
|
448
|
-
queryPayload: QueryPayload | QueryPayload[] | FilterQueryFn,
|
|
449
|
-
priority: FetchType = 'highPriority',
|
|
450
|
-
ignoreInvalidationSync = false,
|
|
451
|
-
) {
|
|
452
|
-
const queriesKey = getQueriesKeyArray(queryPayload);
|
|
453
|
-
|
|
454
|
-
for (const { key } of queriesKey) {
|
|
455
|
-
store.produceState(
|
|
456
|
-
(draft) => {
|
|
457
|
-
const query = draft.queries[key];
|
|
458
|
-
|
|
459
|
-
if (!query) return;
|
|
460
|
-
|
|
461
|
-
query.refetchOnMount = priority;
|
|
462
|
-
},
|
|
463
|
-
{ action: { type: 'invalidate-data', key } },
|
|
464
|
-
);
|
|
465
|
-
|
|
466
|
-
queryInvalidationWasTriggered.delete(key);
|
|
467
|
-
storeEvents.emit('invalidateQuery', { priority, queryKey: key });
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
|
|
471
|
-
invalidateItem(
|
|
472
|
-
(_, itemId) =>
|
|
473
|
-
queriesKey.some(({ payload }) => {
|
|
474
|
-
return syncMutationsAndInvalidations.syncItemAndQuery(
|
|
475
|
-
itemId,
|
|
476
|
-
payload,
|
|
477
|
-
);
|
|
478
|
-
}),
|
|
479
|
-
priority,
|
|
480
|
-
true,
|
|
481
|
-
);
|
|
482
|
-
|
|
483
|
-
invalidateQuery(
|
|
484
|
-
(queryToInvalidatePayload, _, queryToInvalidateKey) =>
|
|
485
|
-
queriesKey.some((alreadyInvalidatedQueryPayload) => {
|
|
486
|
-
return (
|
|
487
|
-
queryToInvalidateKey !== alreadyInvalidatedQueryPayload.key &&
|
|
488
|
-
syncMutationsAndInvalidations.syncQueries(
|
|
489
|
-
alreadyInvalidatedQueryPayload.payload,
|
|
490
|
-
queryToInvalidatePayload,
|
|
491
|
-
)
|
|
492
|
-
);
|
|
493
|
-
}),
|
|
494
|
-
priority,
|
|
495
|
-
true,
|
|
496
|
-
);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
function defaultItemSelector<T>(data: ItemState, id: string): T {
|
|
501
|
-
return { id, data } as T;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
function useMultipleListQueries<
|
|
505
|
-
SelectedItem = { id: string; data: ItemState },
|
|
506
|
-
>(
|
|
507
|
-
queries: readonly QueryPayload[],
|
|
508
|
-
{
|
|
509
|
-
itemSelector = defaultItemSelector,
|
|
510
|
-
omitPayload,
|
|
511
|
-
returnIdleStatus,
|
|
512
|
-
returnRefetchingStatus,
|
|
513
|
-
loadSize,
|
|
514
|
-
disableRefetchOnMount = globalDisableRefetchOnMount,
|
|
515
|
-
}: {
|
|
516
|
-
itemSelector?: (data: ItemState, id: string) => SelectedItem;
|
|
517
|
-
omitPayload?: boolean;
|
|
518
|
-
disableRefetchOnMount?: boolean;
|
|
519
|
-
returnIdleStatus?: boolean;
|
|
520
|
-
returnRefetchingStatus?: boolean;
|
|
521
|
-
loadSize?: number;
|
|
522
|
-
} = {},
|
|
523
|
-
) {
|
|
524
|
-
type QueryWithId = {
|
|
525
|
-
key: string;
|
|
526
|
-
payload: QueryPayload;
|
|
527
|
-
};
|
|
528
|
-
|
|
529
|
-
const queriesWithId = useDeepMemo(() => {
|
|
530
|
-
return queries.map((payload): QueryWithId => {
|
|
531
|
-
return {
|
|
532
|
-
key: getQueryKey(payload),
|
|
533
|
-
payload,
|
|
534
|
-
};
|
|
535
|
-
});
|
|
536
|
-
}, [queries]);
|
|
537
|
-
|
|
538
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
539
|
-
const dataSelector = useCallback(itemSelector, []);
|
|
540
|
-
|
|
541
|
-
const resultSelector = useCallback(
|
|
542
|
-
(state: State) => {
|
|
543
|
-
return queriesWithId.map(
|
|
544
|
-
({
|
|
545
|
-
key: queryKey,
|
|
546
|
-
payload,
|
|
547
|
-
}): TSFDUseListQueryReturn<SelectedItem, QueryPayload, NError> => {
|
|
548
|
-
const query = state.queries[queryKey];
|
|
549
|
-
|
|
550
|
-
if (!query) {
|
|
551
|
-
return {
|
|
552
|
-
queryKey,
|
|
553
|
-
status: returnIdleStatus ? 'idle' : 'loading',
|
|
554
|
-
items: [],
|
|
555
|
-
error: null,
|
|
556
|
-
hasMore: false,
|
|
557
|
-
payload: omitPayload ? undefined : payload,
|
|
558
|
-
isLoading: returnIdleStatus ? false : true,
|
|
559
|
-
};
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
let status = query.status;
|
|
563
|
-
|
|
564
|
-
if (!returnRefetchingStatus && query.status === 'refetching') {
|
|
565
|
-
status = 'success';
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
return {
|
|
569
|
-
queryKey,
|
|
570
|
-
status,
|
|
571
|
-
items: getQueryItems(query, dataSelector),
|
|
572
|
-
error: query.error,
|
|
573
|
-
hasMore: query.hasMore,
|
|
574
|
-
isLoading: status === 'loading',
|
|
575
|
-
payload: omitPayload ? undefined : query.payload,
|
|
576
|
-
};
|
|
577
|
-
},
|
|
578
|
-
);
|
|
579
|
-
},
|
|
580
|
-
[
|
|
581
|
-
dataSelector,
|
|
582
|
-
omitPayload,
|
|
583
|
-
queriesWithId,
|
|
584
|
-
returnIdleStatus,
|
|
585
|
-
returnRefetchingStatus,
|
|
586
|
-
],
|
|
587
|
-
);
|
|
588
|
-
|
|
589
|
-
const storeState = store.useSelector(resultSelector, {
|
|
590
|
-
equalityFn: deepEqual,
|
|
591
|
-
useExternalDeps: true,
|
|
592
|
-
});
|
|
593
|
-
|
|
594
|
-
useOnMittEvent(storeEvents, 'invalidateQuery', (event) => {
|
|
595
|
-
for (const { key, payload } of queriesWithId) {
|
|
596
|
-
if (key !== event.queryKey) continue;
|
|
597
|
-
|
|
598
|
-
if (!queryInvalidationWasTriggered.has(key)) {
|
|
599
|
-
scheduleListQueryFetch(event.priority, payload);
|
|
600
|
-
queryInvalidationWasTriggered.add(key);
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
});
|
|
604
|
-
|
|
605
|
-
const loadSizeConst = useConst(() => loadSize);
|
|
606
|
-
|
|
607
|
-
useEffect(() => {
|
|
608
|
-
for (const { key: itemId, payload: fetchParams } of queriesWithId) {
|
|
609
|
-
if (itemId) {
|
|
610
|
-
if (disableRefetchOnMount) {
|
|
611
|
-
const itemState = getQueryState(fetchParams);
|
|
612
|
-
|
|
613
|
-
const shouldFetch =
|
|
614
|
-
!itemState || !itemState.wasLoaded || itemState.refetchOnMount;
|
|
615
|
-
|
|
616
|
-
if (shouldFetch) {
|
|
617
|
-
scheduleListQueryFetch(
|
|
618
|
-
itemState?.refetchOnMount || 'lowPriority',
|
|
619
|
-
fetchParams,
|
|
620
|
-
loadSizeConst,
|
|
621
|
-
);
|
|
622
|
-
return;
|
|
623
|
-
}
|
|
624
|
-
} else {
|
|
625
|
-
scheduleListQueryFetch('lowPriority', fetchParams, loadSizeConst);
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
}, [disableRefetchOnMount, loadSizeConst, queriesWithId]);
|
|
630
|
-
|
|
631
|
-
return storeState;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
function useListQuery<SelectedItem = { id: string; data: ItemState }>(
|
|
635
|
-
payload: QueryPayload | false | null | undefined,
|
|
636
|
-
options: {
|
|
637
|
-
itemSelector?: (data: ItemState, id: string) => SelectedItem;
|
|
638
|
-
omitPayload?: boolean;
|
|
639
|
-
disableRefetchOnMount?: boolean;
|
|
640
|
-
returnIdleStatus?: boolean;
|
|
641
|
-
returnRefetchingStatus?: boolean;
|
|
642
|
-
ensureIsLoaded?: boolean;
|
|
643
|
-
loadSize?: number;
|
|
644
|
-
} = {},
|
|
645
|
-
) {
|
|
646
|
-
const { ensureIsLoaded } = options;
|
|
647
|
-
|
|
648
|
-
const query = useMemo(
|
|
649
|
-
() =>
|
|
650
|
-
payload === false || payload === null || payload === undefined
|
|
651
|
-
? []
|
|
652
|
-
: [payload],
|
|
653
|
-
[payload],
|
|
654
|
-
);
|
|
655
|
-
|
|
656
|
-
const queryResult = useMultipleListQueries(query, options);
|
|
657
|
-
|
|
658
|
-
const result = useMemo(
|
|
659
|
-
(): TSFDUseListQueryReturn<SelectedItem, QueryPayload, NError> =>
|
|
660
|
-
queryResult[0] ?? {
|
|
661
|
-
payload: undefined,
|
|
662
|
-
error: null,
|
|
663
|
-
hasMore: false,
|
|
664
|
-
isLoading: false,
|
|
665
|
-
status: 'idle',
|
|
666
|
-
queryKey: '',
|
|
667
|
-
items: [],
|
|
668
|
-
},
|
|
669
|
-
[queryResult],
|
|
670
|
-
);
|
|
671
|
-
|
|
672
|
-
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
673
|
-
ensureIsLoaded,
|
|
674
|
-
!!payload,
|
|
675
|
-
() => {
|
|
676
|
-
if (payload) {
|
|
677
|
-
scheduleListQueryFetch('highPriority', payload);
|
|
678
|
-
}
|
|
679
|
-
},
|
|
680
|
-
);
|
|
681
|
-
|
|
682
|
-
useSubscribeToStore(store, ({ observe }) => {
|
|
683
|
-
if (!ensureIsLoaded) return;
|
|
684
|
-
|
|
685
|
-
observe
|
|
686
|
-
.ifSelector((state) => state.queries[result.queryKey]?.status)
|
|
687
|
-
.change.then(({ current }) => {
|
|
688
|
-
if (current === 'success' || current === 'error') {
|
|
689
|
-
emitIsLoadedEvt('isLoaded', true);
|
|
690
|
-
}
|
|
691
|
-
});
|
|
692
|
-
});
|
|
693
|
-
|
|
694
|
-
return useModifyResult(result);
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
async function fetchItem(
|
|
698
|
-
fetchCtx: FetchCtx,
|
|
699
|
-
itemId: string,
|
|
700
|
-
): Promise<boolean> {
|
|
701
|
-
if (!fetchItemOrquestrator) {
|
|
702
|
-
throw new Error(noFetchFnError);
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
const isLoaded = !store.state.itemQueries[itemId]?.wasLoaded;
|
|
706
|
-
|
|
707
|
-
store.produceState(
|
|
708
|
-
(draft) => {
|
|
709
|
-
const itemQuery = draft.itemQueries[itemId];
|
|
710
|
-
|
|
711
|
-
if (!itemQuery) {
|
|
712
|
-
draft.itemQueries[itemId] = {
|
|
713
|
-
status: 'loading',
|
|
714
|
-
error: null,
|
|
715
|
-
wasLoaded: false,
|
|
716
|
-
refetchOnMount: false,
|
|
717
|
-
};
|
|
718
|
-
|
|
719
|
-
return;
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
itemQuery.status = itemQuery.wasLoaded ? 'refetching' : 'loading';
|
|
723
|
-
itemQuery.error = null;
|
|
724
|
-
itemQuery.refetchOnMount = false;
|
|
725
|
-
},
|
|
726
|
-
{
|
|
727
|
-
action: {
|
|
728
|
-
type: isLoaded ? 'fetch-item-start' : 'fetch-item-refetch-start',
|
|
729
|
-
itemId,
|
|
730
|
-
},
|
|
731
|
-
},
|
|
732
|
-
);
|
|
733
|
-
|
|
734
|
-
try {
|
|
735
|
-
const item = await fetchItemFn(itemId);
|
|
736
|
-
|
|
737
|
-
if (fetchCtx.shouldAbort()) return false;
|
|
738
|
-
|
|
739
|
-
store.produceState(
|
|
740
|
-
(draft) => {
|
|
741
|
-
const itemQuery = draft.itemQueries[itemId];
|
|
742
|
-
|
|
743
|
-
if (!itemQuery) return;
|
|
744
|
-
|
|
745
|
-
itemQuery.status = 'success';
|
|
746
|
-
itemQuery.wasLoaded = true;
|
|
747
|
-
|
|
748
|
-
draft.items[itemId] = item;
|
|
749
|
-
},
|
|
750
|
-
{
|
|
751
|
-
action: { type: 'fetch-item-success', itemId },
|
|
752
|
-
},
|
|
753
|
-
);
|
|
754
|
-
return true;
|
|
755
|
-
} catch (exception) {
|
|
756
|
-
if (fetchCtx.shouldAbort()) return false;
|
|
757
|
-
|
|
758
|
-
const error = errorNormalizer(exception);
|
|
759
|
-
|
|
760
|
-
store.produceState(
|
|
761
|
-
(draft) => {
|
|
762
|
-
const itemQuery = draft.itemQueries[itemId];
|
|
763
|
-
|
|
764
|
-
if (!itemQuery) return;
|
|
765
|
-
|
|
766
|
-
itemQuery.status = 'error';
|
|
767
|
-
itemQuery.error = error;
|
|
768
|
-
},
|
|
769
|
-
{
|
|
770
|
-
action: { type: 'fetch-item-error', itemId },
|
|
771
|
-
},
|
|
772
|
-
);
|
|
773
|
-
|
|
774
|
-
return false;
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
const fetchItemOrquestrator =
|
|
779
|
-
fetchItemFn &&
|
|
780
|
-
createCollectionFetchOrquestrator({
|
|
781
|
-
fetchFn: fetchItem,
|
|
782
|
-
lowPriorityThrottleMs,
|
|
783
|
-
dynamicRealtimeThrottleMs,
|
|
784
|
-
mediumPriorityThrottleMs,
|
|
785
|
-
});
|
|
786
|
-
|
|
787
|
-
function scheduleItemFetch(
|
|
788
|
-
fetchType: FetchType,
|
|
789
|
-
itemId: string,
|
|
790
|
-
): ScheduleFetchResults;
|
|
791
|
-
function scheduleItemFetch(
|
|
792
|
-
fetchType: FetchType,
|
|
793
|
-
itemId: string[],
|
|
794
|
-
): ScheduleFetchResults[];
|
|
795
|
-
function scheduleItemFetch(
|
|
796
|
-
fetchType: FetchType,
|
|
797
|
-
itemId: string | string[],
|
|
798
|
-
): ScheduleFetchResults | ScheduleFetchResults[] {
|
|
799
|
-
if (!fetchItemOrquestrator) {
|
|
800
|
-
throw new Error(noFetchFnError);
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
const fetchMultiple = Array.isArray(itemId);
|
|
804
|
-
|
|
805
|
-
const itemsId = fetchMultiple ? itemId : [itemId];
|
|
806
|
-
|
|
807
|
-
const results = itemsId.map((id) => {
|
|
808
|
-
return fetchItemOrquestrator.get(id).scheduleFetch(fetchType, id);
|
|
809
|
-
});
|
|
810
|
-
|
|
811
|
-
return fetchMultiple ? results : results[0]!;
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
type FilterItemFn = (item: ItemState, itemId: string) => boolean;
|
|
815
|
-
|
|
816
|
-
function getItemsIdArray(itemId: string | string[] | FilterItemFn): string[] {
|
|
817
|
-
if (Array.isArray(itemId)) {
|
|
818
|
-
return itemId;
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
if (typeof itemId === 'function') {
|
|
822
|
-
return filterAndMap(
|
|
823
|
-
Object.entries(store.state.items),
|
|
824
|
-
([id, item], ignore) => {
|
|
825
|
-
if (item === null) return ignore;
|
|
826
|
-
|
|
827
|
-
return itemId(item, id) ? id : ignore;
|
|
828
|
-
},
|
|
829
|
-
);
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
return [itemId];
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
const itemInvalidationWasTriggered = new Set<string>();
|
|
836
|
-
|
|
837
|
-
function invalidateItem(
|
|
838
|
-
itemId: string | string[] | FilterItemFn,
|
|
839
|
-
priority: FetchType = 'highPriority',
|
|
840
|
-
ignoreInvalidationSync = false,
|
|
841
|
-
) {
|
|
842
|
-
if (!fetchItemOrquestrator) {
|
|
843
|
-
throw new Error(noFetchFnError);
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
const itemsId = getItemsIdArray(itemId);
|
|
847
|
-
|
|
848
|
-
for (const id of itemsId) {
|
|
849
|
-
store.produceState(
|
|
850
|
-
(draft) => {
|
|
851
|
-
const query = draft.itemQueries[id];
|
|
852
|
-
|
|
853
|
-
if (!query) return;
|
|
854
|
-
|
|
855
|
-
query.refetchOnMount = priority;
|
|
856
|
-
},
|
|
857
|
-
{ action: { type: 'invalidate-item', queryKey: id } },
|
|
858
|
-
);
|
|
859
|
-
|
|
860
|
-
itemInvalidationWasTriggered.delete(id);
|
|
861
|
-
storeEvents.emit('invalidateItem', { priority, itemId: id });
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
|
|
865
|
-
invalidateQuery(
|
|
866
|
-
(query) =>
|
|
867
|
-
itemsId.some((id) =>
|
|
868
|
-
syncMutationsAndInvalidations.syncItemAndQuery(id, query),
|
|
869
|
-
),
|
|
870
|
-
priority,
|
|
871
|
-
true,
|
|
872
|
-
);
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
function startItemMutation(itemId: string | string[] | FilterItemFn) {
|
|
877
|
-
if (!fetchItemOrquestrator) throw new Error(noFetchFnError);
|
|
878
|
-
|
|
879
|
-
const itemsId = getItemsIdArray(itemId);
|
|
880
|
-
|
|
881
|
-
const endMutations: (() => void)[] = [];
|
|
882
|
-
|
|
883
|
-
for (const id of itemsId) {
|
|
884
|
-
endMutations.push(fetchItemOrquestrator.get(id).startMutation());
|
|
885
|
-
|
|
886
|
-
if (syncMutationsAndInvalidations) {
|
|
887
|
-
for (const [queryKey, query] of Object.entries(store.state.queries)) {
|
|
888
|
-
if (
|
|
889
|
-
syncMutationsAndInvalidations.syncItemAndQuery(id, query.payload)
|
|
890
|
-
) {
|
|
891
|
-
endMutations.push(
|
|
892
|
-
fetchQueryOrquestrator.get(queryKey).startMutation(),
|
|
893
|
-
);
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
return () => {
|
|
900
|
-
for (const endMutation of endMutations) {
|
|
901
|
-
endMutation();
|
|
902
|
-
}
|
|
903
|
-
};
|
|
904
|
-
}
|
|
905
|
-
|
|
906
|
-
function defaultItemDataSelector<T>(data: ItemState | null): T {
|
|
907
|
-
return data as unknown as T;
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
function useMultipleItems<SelectedItem = ItemState>(
|
|
911
|
-
itemIds: string[],
|
|
912
|
-
{
|
|
913
|
-
selector = defaultItemDataSelector,
|
|
914
|
-
returnIdleStatus,
|
|
915
|
-
returnRefetchingStatus,
|
|
916
|
-
disableRefetchOnMount,
|
|
917
|
-
}: {
|
|
918
|
-
selector?: (data: ItemState | null, id: string) => SelectedItem;
|
|
919
|
-
disableRefetchOnMount?: boolean;
|
|
920
|
-
returnIdleStatus?: boolean;
|
|
921
|
-
returnRefetchingStatus?: boolean;
|
|
922
|
-
} = {},
|
|
923
|
-
) {
|
|
924
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
925
|
-
const dataSelector = useCallback(selector, []);
|
|
926
|
-
|
|
927
|
-
const memoizedItemIds = useDeepMemo(() => itemIds, [itemIds]);
|
|
928
|
-
|
|
929
|
-
const resultSelector = useCallback(
|
|
930
|
-
(state: State) => {
|
|
931
|
-
return memoizedItemIds.map(
|
|
932
|
-
(itemId): TSFDUseItemReturn<SelectedItem, NError> => {
|
|
933
|
-
const itemQuery = state.itemQueries[itemId];
|
|
934
|
-
const itemState = state.items[itemId];
|
|
935
|
-
|
|
936
|
-
if (itemQuery === null) {
|
|
937
|
-
return {
|
|
938
|
-
status: 'deleted',
|
|
939
|
-
error: null,
|
|
940
|
-
isLoading: false,
|
|
941
|
-
itemId,
|
|
942
|
-
data: dataSelector(null, itemId),
|
|
943
|
-
};
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
if (!itemQuery) {
|
|
947
|
-
return {
|
|
948
|
-
status: returnIdleStatus ? 'idle' : 'loading',
|
|
949
|
-
error: null,
|
|
950
|
-
isLoading: returnIdleStatus ? false : true,
|
|
951
|
-
itemId,
|
|
952
|
-
data: dataSelector(null, itemId),
|
|
953
|
-
};
|
|
954
|
-
}
|
|
955
|
-
|
|
956
|
-
let status = itemQuery.status;
|
|
957
|
-
|
|
958
|
-
if (!returnRefetchingStatus && itemQuery.status === 'refetching') {
|
|
959
|
-
status = 'success';
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
return {
|
|
963
|
-
status,
|
|
964
|
-
error: itemQuery.error,
|
|
965
|
-
isLoading: status === 'loading',
|
|
966
|
-
data: dataSelector(itemState ?? null, itemId),
|
|
967
|
-
itemId,
|
|
968
|
-
};
|
|
969
|
-
},
|
|
970
|
-
);
|
|
971
|
-
},
|
|
972
|
-
[dataSelector, memoizedItemIds, returnIdleStatus, returnRefetchingStatus],
|
|
973
|
-
);
|
|
974
|
-
|
|
975
|
-
const storeState = store.useSelector(resultSelector, {
|
|
976
|
-
equalityFn: deepEqual,
|
|
977
|
-
useExternalDeps: true,
|
|
978
|
-
});
|
|
979
|
-
|
|
980
|
-
useOnMittEvent(storeEvents, 'invalidateItem', (event) => {
|
|
981
|
-
for (const itemId of memoizedItemIds) {
|
|
982
|
-
if (itemId !== event.itemId) continue;
|
|
983
|
-
|
|
984
|
-
if (!itemInvalidationWasTriggered.has(itemId)) {
|
|
985
|
-
scheduleItemFetch(event.priority, itemId);
|
|
986
|
-
itemInvalidationWasTriggered.add(itemId);
|
|
987
|
-
}
|
|
988
|
-
}
|
|
989
|
-
});
|
|
990
|
-
|
|
991
|
-
useEffect(() => {
|
|
992
|
-
for (const itemId of memoizedItemIds) {
|
|
993
|
-
if (itemId) {
|
|
994
|
-
if (disableRefetchOnMount) {
|
|
995
|
-
const itemState = store.state.itemQueries[itemId];
|
|
996
|
-
|
|
997
|
-
const shouldFetch =
|
|
998
|
-
!itemState || !itemState.wasLoaded || itemState.refetchOnMount;
|
|
999
|
-
|
|
1000
|
-
if (shouldFetch) {
|
|
1001
|
-
scheduleItemFetch(
|
|
1002
|
-
itemState?.refetchOnMount || 'lowPriority',
|
|
1003
|
-
itemId,
|
|
1004
|
-
);
|
|
1005
|
-
return;
|
|
1006
|
-
}
|
|
1007
|
-
} else {
|
|
1008
|
-
scheduleItemFetch('lowPriority', itemId);
|
|
1009
|
-
}
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
}, [disableRefetchOnMount, memoizedItemIds]);
|
|
1013
|
-
|
|
1014
|
-
return storeState;
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
function useItem<SelectedItem = ItemState>(
|
|
1018
|
-
itemId: string | false | null | undefined,
|
|
1019
|
-
options: {
|
|
1020
|
-
selector?: (data: ItemState | null, id: string) => SelectedItem;
|
|
1021
|
-
disableRefetchOnMount?: boolean;
|
|
1022
|
-
returnIdleStatus?: boolean;
|
|
1023
|
-
returnRefetchingStatus?: boolean;
|
|
1024
|
-
ensureIsLoaded?: boolean;
|
|
1025
|
-
} = {},
|
|
1026
|
-
) {
|
|
1027
|
-
const { ensureIsLoaded } = options;
|
|
1028
|
-
|
|
1029
|
-
const query = useMemo(
|
|
1030
|
-
() =>
|
|
1031
|
-
itemId === false || itemId === null || itemId === undefined
|
|
1032
|
-
? []
|
|
1033
|
-
: [itemId],
|
|
1034
|
-
[itemId],
|
|
1035
|
-
);
|
|
1036
|
-
|
|
1037
|
-
const queryResult = useMultipleItems<SelectedItem>(query, options);
|
|
1038
|
-
|
|
1039
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1040
|
-
const memoizedSelector = useCallback(
|
|
1041
|
-
options.selector ?? defaultItemDataSelector,
|
|
1042
|
-
[],
|
|
1043
|
-
);
|
|
1044
|
-
|
|
1045
|
-
const result = useMemo(
|
|
1046
|
-
(): TSFDUseItemReturn<SelectedItem, NError> =>
|
|
1047
|
-
queryResult[0] ?? {
|
|
1048
|
-
error: null,
|
|
1049
|
-
isLoading: false,
|
|
1050
|
-
status: 'idle',
|
|
1051
|
-
data: memoizedSelector(null, itemId || ''),
|
|
1052
|
-
itemId: itemId || '',
|
|
1053
|
-
},
|
|
1054
|
-
[itemId, memoizedSelector, queryResult],
|
|
1055
|
-
);
|
|
1056
|
-
|
|
1057
|
-
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
1058
|
-
ensureIsLoaded,
|
|
1059
|
-
!!itemId,
|
|
1060
|
-
() => {
|
|
1061
|
-
if (itemId) {
|
|
1062
|
-
scheduleItemFetch('highPriority', itemId);
|
|
1063
|
-
}
|
|
1064
|
-
},
|
|
1065
|
-
);
|
|
1066
|
-
|
|
1067
|
-
useSubscribeToStore(store, ({ observe }) => {
|
|
1068
|
-
if (!ensureIsLoaded || !itemId) return;
|
|
1069
|
-
|
|
1070
|
-
observe
|
|
1071
|
-
.ifSelector((state) => state.itemQueries[itemId]?.status)
|
|
1072
|
-
.change.then(({ current }) => {
|
|
1073
|
-
if (current === 'success' || current === 'error') {
|
|
1074
|
-
emitIsLoadedEvt('isLoaded', true);
|
|
1075
|
-
}
|
|
1076
|
-
});
|
|
1077
|
-
});
|
|
1078
|
-
|
|
1079
|
-
return useModifyResult(result);
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
function updateItemState(
|
|
1083
|
-
itemIds: string | string[] | FilterItemFn,
|
|
1084
|
-
produceNewData: (draftData: ItemState, itemId: string) => void | ItemState,
|
|
1085
|
-
ifNothingWasUpdated?: () => void,
|
|
1086
|
-
): boolean {
|
|
1087
|
-
const itemKeys = getItemsIdArray(itemIds);
|
|
1088
|
-
|
|
1089
|
-
let someItemWasUpdated = false as boolean;
|
|
1090
|
-
|
|
1091
|
-
store.batch(
|
|
1092
|
-
() => {
|
|
1093
|
-
store.produceState((draftState) => {
|
|
1094
|
-
for (const itemId of itemKeys) {
|
|
1095
|
-
const item = draftState.items[itemId];
|
|
1096
|
-
|
|
1097
|
-
if (!item) continue;
|
|
1098
|
-
|
|
1099
|
-
someItemWasUpdated = true;
|
|
1100
|
-
const newData = produceNewData(item, itemId);
|
|
1101
|
-
|
|
1102
|
-
if (newData) {
|
|
1103
|
-
draftState.items[itemId] = newData;
|
|
1104
|
-
}
|
|
1105
|
-
}
|
|
1106
|
-
});
|
|
1107
|
-
|
|
1108
|
-
if (ifNothingWasUpdated && !someItemWasUpdated) {
|
|
1109
|
-
ifNothingWasUpdated();
|
|
1110
|
-
}
|
|
1111
|
-
},
|
|
1112
|
-
{ type: 'update-item-state', item: itemKeys },
|
|
1113
|
-
);
|
|
1114
|
-
|
|
1115
|
-
return someItemWasUpdated;
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
function addItemToState(itemId: string, data: ItemState) {
|
|
1119
|
-
store.produceState(
|
|
1120
|
-
(draftState) => {
|
|
1121
|
-
draftState.items[itemId] = data;
|
|
1122
|
-
draftState.itemQueries[itemId] = {
|
|
1123
|
-
status: 'success',
|
|
1124
|
-
wasLoaded: true,
|
|
1125
|
-
refetchOnMount: false,
|
|
1126
|
-
error: null,
|
|
1127
|
-
};
|
|
1128
|
-
},
|
|
1129
|
-
{ action: { type: 'create-item-state', itemId } },
|
|
1130
|
-
);
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
function deleteItemState(itemId: string | string[] | FilterItemFn) {
|
|
1134
|
-
const itemsId = getItemsIdArray(itemId);
|
|
1135
|
-
|
|
1136
|
-
store.produceState(
|
|
1137
|
-
(draftState) => {
|
|
1138
|
-
for (const itemKey of itemsId) {
|
|
1139
|
-
draftState.items[itemKey] = null;
|
|
1140
|
-
draftState.itemQueries[itemKey] = null;
|
|
1141
|
-
|
|
1142
|
-
for (const query of Object.values(draftState.queries)) {
|
|
1143
|
-
if (query.items.includes(itemKey)) {
|
|
1144
|
-
query.items = query.items.filter((i) => i !== itemKey);
|
|
1145
|
-
}
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1148
|
-
},
|
|
1149
|
-
{ action: { type: 'delete-item-state', itemId } },
|
|
1150
|
-
);
|
|
1151
|
-
}
|
|
1152
|
-
|
|
1153
|
-
if (!disableRefetchOnWindowFocus) {
|
|
1154
|
-
function handleFocus() {
|
|
1155
|
-
invalidateQuery(() => true, 'lowPriority');
|
|
1156
|
-
invalidateItem(() => true, 'lowPriority');
|
|
1157
|
-
}
|
|
1158
|
-
|
|
1159
|
-
window.addEventListener('focus', handleFocus);
|
|
1160
|
-
window.addEventListener('visibilitychange', handleFocus);
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
return {
|
|
1164
|
-
store,
|
|
1165
|
-
scheduleListQueryFetch,
|
|
1166
|
-
getQueryState,
|
|
1167
|
-
getQueryKey,
|
|
1168
|
-
deleteItemState,
|
|
1169
|
-
getItemState,
|
|
1170
|
-
loadMore,
|
|
1171
|
-
invalidateQuery,
|
|
1172
|
-
invalidateItem,
|
|
1173
|
-
scheduleItemFetch,
|
|
1174
|
-
awaitItemFetch,
|
|
1175
|
-
awaitListQueryFetch,
|
|
1176
|
-
useMultipleListQueries,
|
|
1177
|
-
useListQuery,
|
|
1178
|
-
useItem,
|
|
1179
|
-
useMultipleItems,
|
|
1180
|
-
addItemToState,
|
|
1181
|
-
updateItemState,
|
|
1182
|
-
startItemMutation,
|
|
1183
|
-
};
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
export type TSFDListQueryStore<
|
|
1187
|
-
ItemState extends ValidStoreState,
|
|
1188
|
-
NError,
|
|
1189
|
-
QueryPayload extends ValidPayload,
|
|
1190
|
-
> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload>>;
|