tsdf 8.0.0-beta.8 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.cjs +263 -78
- package/dist/main.d.ts +47 -13
- package/dist/main.js +255 -70
- package/package.json +2 -2
package/dist/main.cjs
CHANGED
|
@@ -36,7 +36,7 @@ module.exports = __toCommonJS(main_exports);
|
|
|
36
36
|
var import_immer = require("immer");
|
|
37
37
|
var import_mitt2 = __toESM(require("mitt"), 1);
|
|
38
38
|
var import_react3 = require("react");
|
|
39
|
-
var
|
|
39
|
+
var import_t_state3 = require("t-state");
|
|
40
40
|
|
|
41
41
|
// src/fetchOrquestrator.ts
|
|
42
42
|
function createFetchOrquestrator({
|
|
@@ -269,6 +269,13 @@ function getAutoIncrementId() {
|
|
|
269
269
|
return ++autoIncrementId;
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
+
// src/storeShared.ts
|
|
273
|
+
var fetchTypePriority = {
|
|
274
|
+
lowPriority: 0,
|
|
275
|
+
realtimeUpdate: 1,
|
|
276
|
+
highPriority: 2
|
|
277
|
+
};
|
|
278
|
+
|
|
272
279
|
// src/useEnsureIsLoaded.ts
|
|
273
280
|
var import_mitt = __toESM(require("mitt"), 1);
|
|
274
281
|
var import_react2 = require("react");
|
|
@@ -370,6 +377,21 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
370
377
|
return [useModifyResult, isLoadedEvtEmitter.emit];
|
|
371
378
|
}
|
|
372
379
|
|
|
380
|
+
// src/utils/reuseRefIfEqual.ts
|
|
381
|
+
var import_t_state2 = require("t-state");
|
|
382
|
+
function reusePrevIfEqual({
|
|
383
|
+
prev,
|
|
384
|
+
current,
|
|
385
|
+
d: equalityFn_ = import_t_state2.deepEqual
|
|
386
|
+
}) {
|
|
387
|
+
if (prev === void 0)
|
|
388
|
+
return current;
|
|
389
|
+
if (equalityFn_(prev, current)) {
|
|
390
|
+
return prev;
|
|
391
|
+
}
|
|
392
|
+
return current;
|
|
393
|
+
}
|
|
394
|
+
|
|
373
395
|
// src/documentStore.ts
|
|
374
396
|
function newTSDFDocumentStore({
|
|
375
397
|
debugName,
|
|
@@ -380,15 +402,16 @@ function newTSDFDocumentStore({
|
|
|
380
402
|
lowPriorityThrottleMs,
|
|
381
403
|
mediumPriorityThrottleMs,
|
|
382
404
|
dynamicRealtimeThrottleMs,
|
|
405
|
+
disableInitialDataInvalidation,
|
|
383
406
|
errorNormalizer
|
|
384
407
|
}) {
|
|
385
|
-
const store = new
|
|
408
|
+
const store = new import_t_state3.Store({
|
|
386
409
|
debugName,
|
|
387
410
|
state: {
|
|
388
411
|
data: initialData ?? null,
|
|
389
412
|
error: null,
|
|
390
413
|
status: initialData ? "success" : "idle",
|
|
391
|
-
refetchOnMount: false
|
|
414
|
+
refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
|
|
392
415
|
}
|
|
393
416
|
});
|
|
394
417
|
async function fetch(fetchCtx, _) {
|
|
@@ -408,7 +431,7 @@ function newTSDFDocumentStore({
|
|
|
408
431
|
return false;
|
|
409
432
|
store.setPartialState(
|
|
410
433
|
{
|
|
411
|
-
data,
|
|
434
|
+
data: reusePrevIfEqual({ prev: store.state.data, current: data }),
|
|
412
435
|
status: "success"
|
|
413
436
|
},
|
|
414
437
|
{ action: "fetch-success" }
|
|
@@ -449,6 +472,10 @@ function newTSDFDocumentStore({
|
|
|
449
472
|
const storeEvents = (0, import_mitt2.default)();
|
|
450
473
|
let invalidationWasTriggered = false;
|
|
451
474
|
function invalidateData(priority = "highPriority") {
|
|
475
|
+
const currentInvalidationPriority = store.state.refetchOnMount ? fetchTypePriority[store.state.refetchOnMount] : -1;
|
|
476
|
+
const newInvalidationPriority = fetchTypePriority[priority];
|
|
477
|
+
if (currentInvalidationPriority >= newInvalidationPriority)
|
|
478
|
+
return;
|
|
452
479
|
store.setKey("refetchOnMount", priority, { action: "invalidate-data" });
|
|
453
480
|
invalidationWasTriggered = false;
|
|
454
481
|
storeEvents.emit("invalidateData", priority);
|
|
@@ -493,13 +520,14 @@ function newTSDFDocumentStore({
|
|
|
493
520
|
(0, import_react3.useEffect)(() => {
|
|
494
521
|
if (disabled)
|
|
495
522
|
return;
|
|
523
|
+
const fetchType = store.state.refetchOnMount || "lowPriority";
|
|
496
524
|
if (disableRefetchOnMount) {
|
|
497
525
|
const shouldFetch = store.state.refetchOnMount || store.state.status === "idle";
|
|
498
526
|
if (shouldFetch) {
|
|
499
|
-
scheduleFetch(
|
|
527
|
+
scheduleFetch(fetchType);
|
|
500
528
|
}
|
|
501
529
|
} else {
|
|
502
|
-
scheduleFetch(
|
|
530
|
+
scheduleFetch(fetchType);
|
|
503
531
|
}
|
|
504
532
|
}, [disableRefetchOnMount, disabled]);
|
|
505
533
|
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
@@ -509,7 +537,7 @@ function newTSDFDocumentStore({
|
|
|
509
537
|
scheduleFetch("highPriority");
|
|
510
538
|
}
|
|
511
539
|
);
|
|
512
|
-
(0,
|
|
540
|
+
(0, import_t_state3.useSubscribeToStore)(store, ({ observe }) => {
|
|
513
541
|
observe.ifSelector((state) => state.status).change.then(({ current }) => {
|
|
514
542
|
if (current === "success" || current === "error") {
|
|
515
543
|
emitIsLoadedEvt("isLoaded", true);
|
|
@@ -551,7 +579,7 @@ function newTSDFDocumentStore({
|
|
|
551
579
|
// src/collectionStore.ts
|
|
552
580
|
var import_mitt3 = __toESM(require("mitt"), 1);
|
|
553
581
|
var import_react4 = require("react");
|
|
554
|
-
var
|
|
582
|
+
var import_t_state4 = require("t-state");
|
|
555
583
|
|
|
556
584
|
// src/collectionFetchOrquestrator.ts
|
|
557
585
|
function createCollectionFetchOrquestrator(props) {
|
|
@@ -568,13 +596,12 @@ function createCollectionFetchOrquestrator(props) {
|
|
|
568
596
|
}
|
|
569
597
|
|
|
570
598
|
// src/utils/filterAndMap.ts
|
|
571
|
-
var ignore = Symbol();
|
|
572
599
|
function filterAndMap(arr, fn) {
|
|
573
600
|
const result = [];
|
|
574
601
|
let i = 0;
|
|
575
602
|
for (const item of arr) {
|
|
576
|
-
const mappedItem = fn(item,
|
|
577
|
-
if (mappedItem !==
|
|
603
|
+
const mappedItem = fn(item, false, i);
|
|
604
|
+
if (mappedItem !== false) {
|
|
578
605
|
result.push(mappedItem);
|
|
579
606
|
}
|
|
580
607
|
i++;
|
|
@@ -592,10 +619,10 @@ function getCacheId(input) {
|
|
|
592
619
|
return typeof input === "string" || typeof input === "number" ? String(input) : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
|
|
593
620
|
}
|
|
594
621
|
function orderedProps(obj) {
|
|
595
|
-
return filterAndMap(Object.keys(obj).sort(), (k,
|
|
622
|
+
return filterAndMap(Object.keys(obj).sort(), (k, ignore) => {
|
|
596
623
|
const value = obj[k];
|
|
597
624
|
if (value === void 0)
|
|
598
|
-
return
|
|
625
|
+
return ignore;
|
|
599
626
|
return { [k]: obj[k] };
|
|
600
627
|
});
|
|
601
628
|
}
|
|
@@ -608,14 +635,30 @@ function newTSDFCollectionStore({
|
|
|
608
635
|
disableRefetchOnWindowFocus,
|
|
609
636
|
lowPriorityThrottleMs,
|
|
610
637
|
errorNormalizer,
|
|
638
|
+
disableInitialDataInvalidation,
|
|
611
639
|
mediumPriorityThrottleMs,
|
|
640
|
+
initialStateItems,
|
|
612
641
|
disableRefetchOnMount: globalDisableRefetchOnMount,
|
|
613
642
|
dynamicRealtimeThrottleMs,
|
|
614
643
|
getCollectionItemKey: filterCollectionItemObjKey
|
|
615
644
|
}) {
|
|
616
|
-
const
|
|
645
|
+
const initialState = {};
|
|
646
|
+
if (initialStateItems) {
|
|
647
|
+
for (const item of initialStateItems) {
|
|
648
|
+
const itemKey = getItemKey(item.payload);
|
|
649
|
+
initialState[itemKey] = {
|
|
650
|
+
data: item.data,
|
|
651
|
+
error: null,
|
|
652
|
+
status: "success",
|
|
653
|
+
payload: item.payload,
|
|
654
|
+
refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
|
|
655
|
+
wasLoaded: true
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
const store = new import_t_state4.Store({
|
|
617
660
|
debugName,
|
|
618
|
-
state:
|
|
661
|
+
state: initialState
|
|
619
662
|
});
|
|
620
663
|
function getItemKey(params) {
|
|
621
664
|
return getCacheId(
|
|
@@ -646,7 +689,7 @@ function newTSDFCollectionStore({
|
|
|
646
689
|
item.refetchOnMount = false;
|
|
647
690
|
},
|
|
648
691
|
{
|
|
649
|
-
equalityCheck:
|
|
692
|
+
equalityCheck: import_t_state4.deepEqual,
|
|
650
693
|
action: {
|
|
651
694
|
type: !itemState?.data ? "fetch-start-loading" : "fetch-start-refetching",
|
|
652
695
|
payload
|
|
@@ -662,7 +705,7 @@ function newTSDFCollectionStore({
|
|
|
662
705
|
const item = draft[itemId];
|
|
663
706
|
if (!item)
|
|
664
707
|
return;
|
|
665
|
-
item.data = data;
|
|
708
|
+
item.data = reusePrevIfEqual({ current: data, prev: item.data });
|
|
666
709
|
item.status = "success";
|
|
667
710
|
item.wasLoaded = true;
|
|
668
711
|
},
|
|
@@ -721,8 +764,8 @@ function newTSDFCollectionStore({
|
|
|
721
764
|
if (Array.isArray(params)) {
|
|
722
765
|
return params.map(getItemKey);
|
|
723
766
|
} else if (typeof params === "function") {
|
|
724
|
-
return filterAndMap(Object.entries(items), ([itemKey, item],
|
|
725
|
-
return item && params(item.payload, item.data) ? itemKey :
|
|
767
|
+
return filterAndMap(Object.entries(items), ([itemKey, item], ignore) => {
|
|
768
|
+
return item && params(item.payload, item.data) ? itemKey : ignore;
|
|
726
769
|
});
|
|
727
770
|
} else {
|
|
728
771
|
return [getItemKey(params)];
|
|
@@ -733,12 +776,19 @@ function newTSDFCollectionStore({
|
|
|
733
776
|
function invalidateItem(itemPayload, priority = "highPriority") {
|
|
734
777
|
const itemsKey = getItemsKeyArray(itemPayload);
|
|
735
778
|
for (const itemKey of itemsKey) {
|
|
779
|
+
const item = store.state[itemKey];
|
|
780
|
+
if (!item)
|
|
781
|
+
continue;
|
|
782
|
+
const currentInvalidationPriority = item.refetchOnMount ? fetchTypePriority[item.refetchOnMount] : -1;
|
|
783
|
+
const newInvalidationPriority = fetchTypePriority[priority];
|
|
784
|
+
if (currentInvalidationPriority >= newInvalidationPriority)
|
|
785
|
+
return;
|
|
736
786
|
store.produceState(
|
|
737
787
|
(draft) => {
|
|
738
|
-
const
|
|
739
|
-
if (!
|
|
788
|
+
const draftItem = draft[itemKey];
|
|
789
|
+
if (!draftItem)
|
|
740
790
|
return;
|
|
741
|
-
|
|
791
|
+
draftItem.refetchOnMount = priority;
|
|
742
792
|
},
|
|
743
793
|
{ action: { type: "invalidate-data", itemKey } }
|
|
744
794
|
);
|
|
@@ -749,8 +799,8 @@ function newTSDFCollectionStore({
|
|
|
749
799
|
function getItemState(params) {
|
|
750
800
|
if (typeof params === "function" || Array.isArray(params)) {
|
|
751
801
|
const itemsId = getItemsKeyArray(params);
|
|
752
|
-
return filterAndMap(itemsId, (itemId,
|
|
753
|
-
return store.state[itemId] ||
|
|
802
|
+
return filterAndMap(itemsId, (itemId, ignore) => {
|
|
803
|
+
return store.state[itemId] || ignore;
|
|
754
804
|
});
|
|
755
805
|
}
|
|
756
806
|
return store.state[getItemKey(params)];
|
|
@@ -824,7 +874,7 @@ function newTSDFCollectionStore({
|
|
|
824
874
|
]
|
|
825
875
|
);
|
|
826
876
|
const storeState = store.useSelector(resultSelector, {
|
|
827
|
-
equalityFn:
|
|
877
|
+
equalityFn: import_t_state4.deepEqual,
|
|
828
878
|
useExternalDeps: true
|
|
829
879
|
});
|
|
830
880
|
useOnMittEvent(storeEvents, "invalidateData", (event) => {
|
|
@@ -840,18 +890,16 @@ function newTSDFCollectionStore({
|
|
|
840
890
|
(0, import_react4.useEffect)(() => {
|
|
841
891
|
for (const { itemKey: itemId, payload } of queriesWithId) {
|
|
842
892
|
if (itemId) {
|
|
893
|
+
const itemState = getItemState(payload);
|
|
894
|
+
const fetchType = itemState?.refetchOnMount || "lowPriority";
|
|
843
895
|
if (disableRefetchOnMount) {
|
|
844
|
-
const itemState = getItemState(payload);
|
|
845
896
|
const shouldFetch = !itemState?.wasLoaded || itemState.refetchOnMount;
|
|
846
897
|
if (shouldFetch) {
|
|
847
|
-
scheduleFetch(
|
|
848
|
-
itemState?.refetchOnMount || "lowPriority",
|
|
849
|
-
payload
|
|
850
|
-
);
|
|
898
|
+
scheduleFetch(fetchType, payload);
|
|
851
899
|
return;
|
|
852
900
|
}
|
|
853
901
|
} else {
|
|
854
|
-
scheduleFetch(
|
|
902
|
+
scheduleFetch(fetchType, payload);
|
|
855
903
|
}
|
|
856
904
|
}
|
|
857
905
|
}
|
|
@@ -885,7 +933,7 @@ function newTSDFCollectionStore({
|
|
|
885
933
|
}
|
|
886
934
|
}
|
|
887
935
|
);
|
|
888
|
-
(0,
|
|
936
|
+
(0, import_t_state4.useSubscribeToStore)(store, ({ observe }) => {
|
|
889
937
|
if (!ensureIsLoaded)
|
|
890
938
|
return;
|
|
891
939
|
observe.ifSelector((state) => state[result.itemStateKey]?.status).change.then(({ current }) => {
|
|
@@ -989,8 +1037,23 @@ function newTSDFCollectionStore({
|
|
|
989
1037
|
// src/listQueryStore.ts
|
|
990
1038
|
var import_mitt4 = __toESM(require("mitt"), 1);
|
|
991
1039
|
var import_react5 = require("react");
|
|
992
|
-
var
|
|
1040
|
+
var import_t_state5 = require("t-state");
|
|
993
1041
|
var import_json2 = require("klona/json");
|
|
1042
|
+
|
|
1043
|
+
// src/utils/findAndMap.ts
|
|
1044
|
+
function findAndMap(arr, findFn) {
|
|
1045
|
+
let i = 0;
|
|
1046
|
+
for (const item of arr) {
|
|
1047
|
+
const mappedItem = findFn(item, i);
|
|
1048
|
+
if (mappedItem !== false) {
|
|
1049
|
+
return mappedItem;
|
|
1050
|
+
}
|
|
1051
|
+
i++;
|
|
1052
|
+
}
|
|
1053
|
+
return void 0;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// src/listQueryStore.ts
|
|
994
1057
|
var noFetchFnError = "No fetchItemFn was provided";
|
|
995
1058
|
function newTSDFListQueryStore({
|
|
996
1059
|
debugName,
|
|
@@ -998,16 +1061,44 @@ function newTSDFListQueryStore({
|
|
|
998
1061
|
fetchItemFn,
|
|
999
1062
|
errorNormalizer,
|
|
1000
1063
|
defaultQuerySize = 50,
|
|
1064
|
+
initialData,
|
|
1001
1065
|
disableRefetchOnWindowFocus,
|
|
1002
1066
|
disableRefetchOnMount: globalDisableRefetchOnMount,
|
|
1003
1067
|
lowPriorityThrottleMs,
|
|
1068
|
+
disableInitialDataInvalidation,
|
|
1004
1069
|
mediumPriorityThrottleMs,
|
|
1005
1070
|
dynamicRealtimeThrottleMs,
|
|
1006
1071
|
syncMutationsAndInvalidations
|
|
1007
1072
|
}) {
|
|
1008
|
-
const
|
|
1073
|
+
const initialState = { items: {}, queries: {}, itemQueries: {} };
|
|
1074
|
+
if (initialData) {
|
|
1075
|
+
for (const { payload, data } of initialData.items) {
|
|
1076
|
+
const itemKey = getItemKey(payload);
|
|
1077
|
+
initialState.items[itemKey] = data;
|
|
1078
|
+
initialState.itemQueries[itemKey] = {
|
|
1079
|
+
error: null,
|
|
1080
|
+
status: "success",
|
|
1081
|
+
refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
|
|
1082
|
+
wasLoaded: true,
|
|
1083
|
+
payload
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
for (const { payload, items, hasMore } of initialData.queries) {
|
|
1087
|
+
const queryKey = getQueryKey(payload);
|
|
1088
|
+
initialState.queries[queryKey] = {
|
|
1089
|
+
error: null,
|
|
1090
|
+
status: "success",
|
|
1091
|
+
refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
|
|
1092
|
+
wasLoaded: true,
|
|
1093
|
+
payload,
|
|
1094
|
+
items,
|
|
1095
|
+
hasMore
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
const store = new import_t_state5.Store({
|
|
1009
1100
|
debugName,
|
|
1010
|
-
state:
|
|
1101
|
+
state: initialState
|
|
1011
1102
|
});
|
|
1012
1103
|
function getQueryKey(params) {
|
|
1013
1104
|
return getCacheId(params);
|
|
@@ -1040,7 +1131,7 @@ function newTSDFListQueryStore({
|
|
|
1040
1131
|
query.refetchOnMount = false;
|
|
1041
1132
|
},
|
|
1042
1133
|
{
|
|
1043
|
-
equalityCheck:
|
|
1134
|
+
equalityCheck: import_t_state5.deepEqual,
|
|
1044
1135
|
action: {
|
|
1045
1136
|
type: isLoading ? "fetch-query-start-loading" : fetchType === "loadMore" ? "fetch-query-loading-more" : "refetching-query-start",
|
|
1046
1137
|
payload
|
|
@@ -1071,7 +1162,10 @@ function newTSDFListQueryStore({
|
|
|
1071
1162
|
query.items = [];
|
|
1072
1163
|
for (const { data, itemPayload } of items) {
|
|
1073
1164
|
const itemKey = getItemKey(itemPayload);
|
|
1074
|
-
draft.items[itemKey] =
|
|
1165
|
+
draft.items[itemKey] = reusePrevIfEqual({
|
|
1166
|
+
current: data,
|
|
1167
|
+
prev: draft.items[itemKey] ?? void 0
|
|
1168
|
+
});
|
|
1075
1169
|
query.items.push(String(itemKey));
|
|
1076
1170
|
const itemQuery = draft.itemQueries[itemKey];
|
|
1077
1171
|
if (!itemQuery || itemQuery.status !== "loading" && itemQuery.status !== "refetching") {
|
|
@@ -1118,13 +1212,27 @@ function newTSDFListQueryStore({
|
|
|
1118
1212
|
function getQueryState(params) {
|
|
1119
1213
|
return store.state.queries[getQueryKey(params)];
|
|
1120
1214
|
}
|
|
1215
|
+
function getQueriesState(params) {
|
|
1216
|
+
const queryKeys = getQueriesKeyArray(params);
|
|
1217
|
+
return filterAndMap(queryKeys, ({ key }) => {
|
|
1218
|
+
const query = store.state.queries[key];
|
|
1219
|
+
return query || false;
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
function getQueriesRelatedToItem(itemPayload) {
|
|
1223
|
+
const itemKey = getItemKey(itemPayload);
|
|
1224
|
+
return getQueriesState((queryPayload) => {
|
|
1225
|
+
const queryState = store.state.queries[getQueryKey(queryPayload)];
|
|
1226
|
+
return !!queryState?.items.includes(itemKey);
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1121
1229
|
function getItemState(itemPayload) {
|
|
1122
1230
|
if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
|
|
1123
1231
|
const itemsId = getItemsKeyArray(itemPayload);
|
|
1124
|
-
return filterAndMap(itemsId, ({ itemKey },
|
|
1232
|
+
return filterAndMap(itemsId, ({ itemKey }, ignore) => {
|
|
1125
1233
|
const item = store.state.items[itemKey];
|
|
1126
1234
|
const payload = store.state.itemQueries[itemKey]?.payload;
|
|
1127
|
-
return !item || !payload ?
|
|
1235
|
+
return !item || !payload ? ignore : { payload, data: item };
|
|
1128
1236
|
});
|
|
1129
1237
|
}
|
|
1130
1238
|
return store.state.items[getItemKey(itemPayload)];
|
|
@@ -1150,9 +1258,10 @@ function newTSDFListQueryStore({
|
|
|
1150
1258
|
return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
|
|
1151
1259
|
}
|
|
1152
1260
|
function getQueryItems(query, itemDataSelector) {
|
|
1153
|
-
return filterAndMap(query.items, (itemKey,
|
|
1261
|
+
return filterAndMap(query.items, (itemKey, ignore) => {
|
|
1154
1262
|
const item = store.state.items[itemKey];
|
|
1155
|
-
|
|
1263
|
+
const itemPayload = store.state.itemQueries[itemKey]?.payload;
|
|
1264
|
+
return item && itemPayload ? itemDataSelector(item, itemPayload) : ignore;
|
|
1156
1265
|
});
|
|
1157
1266
|
}
|
|
1158
1267
|
async function awaitListQueryFetch(params, size = defaultQuerySize) {
|
|
@@ -1174,7 +1283,10 @@ function newTSDFListQueryStore({
|
|
|
1174
1283
|
};
|
|
1175
1284
|
}
|
|
1176
1285
|
return query.error ? { items: [], error: query.error, hasMore: query.hasMore } : {
|
|
1177
|
-
items: getQueryItems(query, (data,
|
|
1286
|
+
items: getQueryItems(query, (data, itemPayload) => ({
|
|
1287
|
+
data,
|
|
1288
|
+
itemPayload
|
|
1289
|
+
})),
|
|
1178
1290
|
error: null,
|
|
1179
1291
|
hasMore: query.hasMore
|
|
1180
1292
|
};
|
|
@@ -1203,11 +1315,11 @@ function newTSDFListQueryStore({
|
|
|
1203
1315
|
} else if (typeof payloads === "function") {
|
|
1204
1316
|
return filterAndMap(
|
|
1205
1317
|
Object.entries(store.state.queries),
|
|
1206
|
-
([queryKey, query],
|
|
1318
|
+
([queryKey, query], ignore) => {
|
|
1207
1319
|
return payloads(query.payload, query, queryKey) ? {
|
|
1208
1320
|
key: queryKey,
|
|
1209
1321
|
payload: query.payload
|
|
1210
|
-
} :
|
|
1322
|
+
} : ignore;
|
|
1211
1323
|
}
|
|
1212
1324
|
);
|
|
1213
1325
|
} else {
|
|
@@ -1219,6 +1331,13 @@ function newTSDFListQueryStore({
|
|
|
1219
1331
|
function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
|
|
1220
1332
|
const queriesKey = getQueriesKeyArray(queryPayload);
|
|
1221
1333
|
for (const { key } of queriesKey) {
|
|
1334
|
+
const queryState = store.state.queries[key];
|
|
1335
|
+
if (!queryState)
|
|
1336
|
+
continue;
|
|
1337
|
+
const currentInvalidationPriority = queryState.refetchOnMount ? fetchTypePriority[queryState.refetchOnMount] : -1;
|
|
1338
|
+
const newInvalidationPriority = fetchTypePriority[priority];
|
|
1339
|
+
if (currentInvalidationPriority >= newInvalidationPriority)
|
|
1340
|
+
return;
|
|
1222
1341
|
store.produceState(
|
|
1223
1342
|
(draft) => {
|
|
1224
1343
|
const query = draft.queries[key];
|
|
@@ -1318,7 +1437,7 @@ function newTSDFListQueryStore({
|
|
|
1318
1437
|
]
|
|
1319
1438
|
);
|
|
1320
1439
|
const storeState = store.useSelector(resultSelector, {
|
|
1321
|
-
equalityFn:
|
|
1440
|
+
equalityFn: import_t_state5.deepEqual,
|
|
1322
1441
|
useExternalDeps: true
|
|
1323
1442
|
});
|
|
1324
1443
|
useOnMittEvent(storeEvents, "invalidateQuery", (event) => {
|
|
@@ -1335,19 +1454,16 @@ function newTSDFListQueryStore({
|
|
|
1335
1454
|
(0, import_react5.useEffect)(() => {
|
|
1336
1455
|
for (const { key: itemId, payload: fetchParams } of queriesWithId) {
|
|
1337
1456
|
if (itemId) {
|
|
1457
|
+
const itemState = getQueryState(fetchParams);
|
|
1458
|
+
const fetchType = itemState?.refetchOnMount || "lowPriority";
|
|
1338
1459
|
if (disableRefetchOnMount) {
|
|
1339
|
-
const itemState = getQueryState(fetchParams);
|
|
1340
1460
|
const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
|
|
1341
1461
|
if (shouldFetch) {
|
|
1342
|
-
scheduleListQueryFetch(
|
|
1343
|
-
itemState?.refetchOnMount || "lowPriority",
|
|
1344
|
-
fetchParams,
|
|
1345
|
-
loadSizeConst
|
|
1346
|
-
);
|
|
1462
|
+
scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
|
|
1347
1463
|
return;
|
|
1348
1464
|
}
|
|
1349
1465
|
} else {
|
|
1350
|
-
scheduleListQueryFetch(
|
|
1466
|
+
scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
|
|
1351
1467
|
}
|
|
1352
1468
|
}
|
|
1353
1469
|
}
|
|
@@ -1382,7 +1498,7 @@ function newTSDFListQueryStore({
|
|
|
1382
1498
|
}
|
|
1383
1499
|
}
|
|
1384
1500
|
);
|
|
1385
|
-
(0,
|
|
1501
|
+
(0, import_t_state5.useSubscribeToStore)(store, ({ observe }) => {
|
|
1386
1502
|
if (!ensureIsLoaded)
|
|
1387
1503
|
return;
|
|
1388
1504
|
observe.ifSelector((state) => state.queries[result.queryKey]?.status).change.then(({ current }) => {
|
|
@@ -1434,7 +1550,10 @@ function newTSDFListQueryStore({
|
|
|
1434
1550
|
return;
|
|
1435
1551
|
itemQuery.status = "success";
|
|
1436
1552
|
itemQuery.wasLoaded = true;
|
|
1437
|
-
draft.items[itemKey] =
|
|
1553
|
+
draft.items[itemKey] = reusePrevIfEqual({
|
|
1554
|
+
current: item,
|
|
1555
|
+
prev: draft.items[itemKey] ?? void 0
|
|
1556
|
+
});
|
|
1438
1557
|
},
|
|
1439
1558
|
{
|
|
1440
1559
|
action: { type: "fetch-item-success", itemPayload }
|
|
@@ -1488,11 +1607,11 @@ function newTSDFListQueryStore({
|
|
|
1488
1607
|
if (typeof itemsPayload === "function") {
|
|
1489
1608
|
return filterAndMap(
|
|
1490
1609
|
Object.entries(store.state.items),
|
|
1491
|
-
([itemKey, item],
|
|
1610
|
+
([itemKey, item], ignore) => {
|
|
1492
1611
|
const payload = store.state.itemQueries[itemKey]?.payload;
|
|
1493
1612
|
if (item === null || !payload)
|
|
1494
|
-
return
|
|
1495
|
-
return itemsPayload(payload, item) ? { itemKey, payload } :
|
|
1613
|
+
return ignore;
|
|
1614
|
+
return itemsPayload(payload, item) ? { itemKey, payload } : ignore;
|
|
1496
1615
|
}
|
|
1497
1616
|
);
|
|
1498
1617
|
}
|
|
@@ -1505,6 +1624,13 @@ function newTSDFListQueryStore({
|
|
|
1505
1624
|
}
|
|
1506
1625
|
const itemsKey = getItemsKeyArray(itemId);
|
|
1507
1626
|
for (const { itemKey } of itemsKey) {
|
|
1627
|
+
const item = store.state.itemQueries[itemKey];
|
|
1628
|
+
if (!item)
|
|
1629
|
+
continue;
|
|
1630
|
+
const currentInvalidationPriority = item.refetchOnMount ? fetchTypePriority[item.refetchOnMount] : -1;
|
|
1631
|
+
const newInvalidationPriority = fetchTypePriority[priority];
|
|
1632
|
+
if (currentInvalidationPriority >= newInvalidationPriority)
|
|
1633
|
+
return;
|
|
1508
1634
|
store.produceState(
|
|
1509
1635
|
(draft) => {
|
|
1510
1636
|
const query = draft.itemQueries[itemKey];
|
|
@@ -1528,12 +1654,13 @@ function newTSDFListQueryStore({
|
|
|
1528
1654
|
}
|
|
1529
1655
|
}
|
|
1530
1656
|
function startItemMutation(itemId) {
|
|
1531
|
-
if (!fetchItemOrquestrator)
|
|
1532
|
-
throw new Error(noFetchFnError);
|
|
1533
1657
|
const itemsKey = getItemsKeyArray(itemId);
|
|
1534
1658
|
const endMutations = [];
|
|
1535
1659
|
for (const { itemKey, payload } of itemsKey) {
|
|
1536
|
-
endMutations.push(
|
|
1660
|
+
endMutations.push(
|
|
1661
|
+
fetchItemOrquestrator?.get(itemKey).startMutation() || (() => {
|
|
1662
|
+
})
|
|
1663
|
+
);
|
|
1537
1664
|
if (syncMutationsAndInvalidations) {
|
|
1538
1665
|
for (const [queryKey, query] of Object.entries(store.state.queries)) {
|
|
1539
1666
|
if (syncMutationsAndInvalidations.syncItemAndQuery(
|
|
@@ -1560,7 +1687,8 @@ function newTSDFListQueryStore({
|
|
|
1560
1687
|
selector = defaultItemDataSelector,
|
|
1561
1688
|
returnIdleStatus,
|
|
1562
1689
|
returnRefetchingStatus,
|
|
1563
|
-
disableRefetchOnMount
|
|
1690
|
+
disableRefetchOnMount,
|
|
1691
|
+
loadFromStateOnly
|
|
1564
1692
|
} = {}) {
|
|
1565
1693
|
const dataSelector = (0, import_react5.useCallback)(selector, []);
|
|
1566
1694
|
const memoizedItemKeys = useDeepMemo(
|
|
@@ -1587,7 +1715,7 @@ function newTSDFListQueryStore({
|
|
|
1587
1715
|
error: null,
|
|
1588
1716
|
isLoading: false,
|
|
1589
1717
|
payload,
|
|
1590
|
-
data: dataSelector(null,
|
|
1718
|
+
data: dataSelector(null, null)
|
|
1591
1719
|
};
|
|
1592
1720
|
}
|
|
1593
1721
|
if (!itemQuery) {
|
|
@@ -1596,7 +1724,7 @@ function newTSDFListQueryStore({
|
|
|
1596
1724
|
error: null,
|
|
1597
1725
|
isLoading: returnIdleStatus ? false : true,
|
|
1598
1726
|
payload,
|
|
1599
|
-
data: dataSelector(null,
|
|
1727
|
+
data: dataSelector(null, null)
|
|
1600
1728
|
};
|
|
1601
1729
|
}
|
|
1602
1730
|
let status = itemQuery.status;
|
|
@@ -1607,7 +1735,7 @@ function newTSDFListQueryStore({
|
|
|
1607
1735
|
status,
|
|
1608
1736
|
error: itemQuery.error,
|
|
1609
1737
|
isLoading: status === "loading",
|
|
1610
|
-
data: dataSelector(itemState ?? null,
|
|
1738
|
+
data: dataSelector(itemState ?? null, itemQuery.payload),
|
|
1611
1739
|
payload
|
|
1612
1740
|
};
|
|
1613
1741
|
}
|
|
@@ -1621,10 +1749,12 @@ function newTSDFListQueryStore({
|
|
|
1621
1749
|
]
|
|
1622
1750
|
);
|
|
1623
1751
|
const storeState = store.useSelector(resultSelector, {
|
|
1624
|
-
equalityFn:
|
|
1752
|
+
equalityFn: import_t_state5.deepEqual,
|
|
1625
1753
|
useExternalDeps: true
|
|
1626
1754
|
});
|
|
1627
1755
|
useOnMittEvent(storeEvents, "invalidateItem", (event) => {
|
|
1756
|
+
if (loadFromStateOnly)
|
|
1757
|
+
return;
|
|
1628
1758
|
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
1629
1759
|
if (itemKey !== event.itemKey)
|
|
1630
1760
|
continue;
|
|
@@ -1635,24 +1765,24 @@ function newTSDFListQueryStore({
|
|
|
1635
1765
|
}
|
|
1636
1766
|
});
|
|
1637
1767
|
(0, import_react5.useEffect)(() => {
|
|
1768
|
+
if (loadFromStateOnly)
|
|
1769
|
+
return;
|
|
1638
1770
|
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
1639
1771
|
if (itemKey) {
|
|
1772
|
+
const itemState = store.state.itemQueries[itemKey];
|
|
1773
|
+
const fetchType = itemState?.refetchOnMount || "lowPriority";
|
|
1640
1774
|
if (disableRefetchOnMount) {
|
|
1641
|
-
const itemState = store.state.itemQueries[itemKey];
|
|
1642
1775
|
const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
|
|
1643
1776
|
if (shouldFetch) {
|
|
1644
|
-
scheduleItemFetch(
|
|
1645
|
-
itemState?.refetchOnMount || "lowPriority",
|
|
1646
|
-
payload
|
|
1647
|
-
);
|
|
1777
|
+
scheduleItemFetch(fetchType, payload);
|
|
1648
1778
|
return;
|
|
1649
1779
|
}
|
|
1650
1780
|
} else {
|
|
1651
|
-
scheduleItemFetch(
|
|
1781
|
+
scheduleItemFetch(fetchType, payload);
|
|
1652
1782
|
}
|
|
1653
1783
|
}
|
|
1654
1784
|
}
|
|
1655
|
-
}, [disableRefetchOnMount, memoizedItemKeys]);
|
|
1785
|
+
}, [disableRefetchOnMount, loadFromStateOnly, memoizedItemKeys]);
|
|
1656
1786
|
return storeState;
|
|
1657
1787
|
}
|
|
1658
1788
|
function useItem(itemPayload, options = {}) {
|
|
@@ -1671,7 +1801,7 @@ function newTSDFListQueryStore({
|
|
|
1671
1801
|
error: null,
|
|
1672
1802
|
isLoading: false,
|
|
1673
1803
|
status: "idle",
|
|
1674
|
-
data: memoizedSelector(null,
|
|
1804
|
+
data: memoizedSelector(null, null),
|
|
1675
1805
|
payload: itemPayload || null
|
|
1676
1806
|
},
|
|
1677
1807
|
[itemPayload, memoizedSelector, queryResult]
|
|
@@ -1685,7 +1815,7 @@ function newTSDFListQueryStore({
|
|
|
1685
1815
|
}
|
|
1686
1816
|
}
|
|
1687
1817
|
);
|
|
1688
|
-
(0,
|
|
1818
|
+
(0, import_t_state5.useSubscribeToStore)(store, ({ observe }) => {
|
|
1689
1819
|
if (!ensureIsLoaded || !itemPayload)
|
|
1690
1820
|
return;
|
|
1691
1821
|
observe.ifSelector((state) => state.itemQueries[itemPayload]?.status).change.then(({ current }) => {
|
|
@@ -1696,7 +1826,32 @@ function newTSDFListQueryStore({
|
|
|
1696
1826
|
});
|
|
1697
1827
|
return useModifyResult(result);
|
|
1698
1828
|
}
|
|
1699
|
-
function
|
|
1829
|
+
function useFindItem(findItem, {
|
|
1830
|
+
selector = defaultItemDataSelector
|
|
1831
|
+
} = {}) {
|
|
1832
|
+
return store.useSelector((state) => {
|
|
1833
|
+
const selectedItem = findAndMap(
|
|
1834
|
+
Object.entries(state.items),
|
|
1835
|
+
([itemKey, item]) => {
|
|
1836
|
+
if (!item)
|
|
1837
|
+
return false;
|
|
1838
|
+
const itemQuery = state.itemQueries[itemKey];
|
|
1839
|
+
if (!itemQuery)
|
|
1840
|
+
return false;
|
|
1841
|
+
if (findItem(item, itemQuery.payload)) {
|
|
1842
|
+
return { item, itemQuery };
|
|
1843
|
+
}
|
|
1844
|
+
return false;
|
|
1845
|
+
}
|
|
1846
|
+
);
|
|
1847
|
+
if (!selectedItem)
|
|
1848
|
+
return null;
|
|
1849
|
+
return selector(selectedItem.item, selectedItem.itemQuery.payload);
|
|
1850
|
+
});
|
|
1851
|
+
}
|
|
1852
|
+
function updateItemState(itemIds, produceNewData, {
|
|
1853
|
+
ifNothingWasUpdated
|
|
1854
|
+
} = {}) {
|
|
1700
1855
|
const itemKeys = getItemsKeyArray(itemIds);
|
|
1701
1856
|
let someItemWasUpdated = false;
|
|
1702
1857
|
store.batch(
|
|
@@ -1721,7 +1876,9 @@ function newTSDFListQueryStore({
|
|
|
1721
1876
|
);
|
|
1722
1877
|
return someItemWasUpdated;
|
|
1723
1878
|
}
|
|
1724
|
-
function addItemToState(itemPayload, data
|
|
1879
|
+
function addItemToState(itemPayload, data, {
|
|
1880
|
+
addItemToQueries
|
|
1881
|
+
} = {}) {
|
|
1725
1882
|
const itemKey = getItemKey(itemPayload);
|
|
1726
1883
|
store.produceState(
|
|
1727
1884
|
(draftState) => {
|
|
@@ -1733,6 +1890,29 @@ function newTSDFListQueryStore({
|
|
|
1733
1890
|
error: null,
|
|
1734
1891
|
payload: (0, import_json2.klona)(itemPayload)
|
|
1735
1892
|
};
|
|
1893
|
+
if (addItemToQueries) {
|
|
1894
|
+
const queries = getQueriesKeyArray(addItemToQueries.queries);
|
|
1895
|
+
for (const { key } of queries) {
|
|
1896
|
+
if (draftState.queries[key]) {
|
|
1897
|
+
const queryState = draftState.queries[key];
|
|
1898
|
+
if (!queryState)
|
|
1899
|
+
continue;
|
|
1900
|
+
if (addItemToQueries.appendTo === "start") {
|
|
1901
|
+
queryState.items.unshift(itemKey);
|
|
1902
|
+
} else if (addItemToQueries.appendTo === "end") {
|
|
1903
|
+
queryState.items.push(itemKey);
|
|
1904
|
+
} else {
|
|
1905
|
+
const index = addItemToQueries.appendTo(
|
|
1906
|
+
filterAndMap(
|
|
1907
|
+
queryState.items,
|
|
1908
|
+
(itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
|
|
1909
|
+
)
|
|
1910
|
+
);
|
|
1911
|
+
queryState.items.splice(index, 0, itemKey);
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1736
1916
|
},
|
|
1737
1917
|
{ action: { type: "create-item-state", itemPayload } }
|
|
1738
1918
|
);
|
|
@@ -1757,7 +1937,9 @@ function newTSDFListQueryStore({
|
|
|
1757
1937
|
if (!disableRefetchOnWindowFocus) {
|
|
1758
1938
|
let handleFocus2 = function() {
|
|
1759
1939
|
invalidateQuery(() => true, "lowPriority");
|
|
1760
|
-
|
|
1940
|
+
if (fetchItemFn) {
|
|
1941
|
+
invalidateItem(() => true, "lowPriority");
|
|
1942
|
+
}
|
|
1761
1943
|
};
|
|
1762
1944
|
var handleFocus = handleFocus2;
|
|
1763
1945
|
window.addEventListener("focus", handleFocus2);
|
|
@@ -1783,7 +1965,10 @@ function newTSDFListQueryStore({
|
|
|
1783
1965
|
useMultipleItems,
|
|
1784
1966
|
addItemToState,
|
|
1785
1967
|
updateItemState,
|
|
1786
|
-
startItemMutation
|
|
1968
|
+
startItemMutation,
|
|
1969
|
+
getQueriesState,
|
|
1970
|
+
getQueriesRelatedToItem,
|
|
1971
|
+
useFindItem
|
|
1787
1972
|
};
|
|
1788
1973
|
}
|
|
1789
1974
|
// Annotate the CommonJS export names for ESM import in node:
|