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.js CHANGED
@@ -235,6 +235,13 @@ function getAutoIncrementId() {
235
235
  return ++autoIncrementId;
236
236
  }
237
237
 
238
+ // src/storeShared.ts
239
+ var fetchTypePriority = {
240
+ lowPriority: 0,
241
+ realtimeUpdate: 1,
242
+ highPriority: 2
243
+ };
244
+
238
245
  // src/useEnsureIsLoaded.ts
239
246
  import mitt from "mitt";
240
247
  import { useMemo as useMemo2, useState } from "react";
@@ -336,6 +343,21 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
336
343
  return [useModifyResult, isLoadedEvtEmitter.emit];
337
344
  }
338
345
 
346
+ // src/utils/reuseRefIfEqual.ts
347
+ import { deepEqual as deepEqual2 } from "t-state";
348
+ function reusePrevIfEqual({
349
+ prev,
350
+ current,
351
+ d: equalityFn_ = deepEqual2
352
+ }) {
353
+ if (prev === void 0)
354
+ return current;
355
+ if (equalityFn_(prev, current)) {
356
+ return prev;
357
+ }
358
+ return current;
359
+ }
360
+
339
361
  // src/documentStore.ts
340
362
  function newTSDFDocumentStore({
341
363
  debugName,
@@ -346,6 +368,7 @@ function newTSDFDocumentStore({
346
368
  lowPriorityThrottleMs,
347
369
  mediumPriorityThrottleMs,
348
370
  dynamicRealtimeThrottleMs,
371
+ disableInitialDataInvalidation,
349
372
  errorNormalizer
350
373
  }) {
351
374
  const store = new Store({
@@ -354,7 +377,7 @@ function newTSDFDocumentStore({
354
377
  data: initialData ?? null,
355
378
  error: null,
356
379
  status: initialData ? "success" : "idle",
357
- refetchOnMount: false
380
+ refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
358
381
  }
359
382
  });
360
383
  async function fetch(fetchCtx, _) {
@@ -374,7 +397,7 @@ function newTSDFDocumentStore({
374
397
  return false;
375
398
  store.setPartialState(
376
399
  {
377
- data,
400
+ data: reusePrevIfEqual({ prev: store.state.data, current: data }),
378
401
  status: "success"
379
402
  },
380
403
  { action: "fetch-success" }
@@ -415,6 +438,10 @@ function newTSDFDocumentStore({
415
438
  const storeEvents = mitt2();
416
439
  let invalidationWasTriggered = false;
417
440
  function invalidateData(priority = "highPriority") {
441
+ const currentInvalidationPriority = store.state.refetchOnMount ? fetchTypePriority[store.state.refetchOnMount] : -1;
442
+ const newInvalidationPriority = fetchTypePriority[priority];
443
+ if (currentInvalidationPriority >= newInvalidationPriority)
444
+ return;
418
445
  store.setKey("refetchOnMount", priority, { action: "invalidate-data" });
419
446
  invalidationWasTriggered = false;
420
447
  storeEvents.emit("invalidateData", priority);
@@ -459,13 +486,14 @@ function newTSDFDocumentStore({
459
486
  useEffect2(() => {
460
487
  if (disabled)
461
488
  return;
489
+ const fetchType = store.state.refetchOnMount || "lowPriority";
462
490
  if (disableRefetchOnMount) {
463
491
  const shouldFetch = store.state.refetchOnMount || store.state.status === "idle";
464
492
  if (shouldFetch) {
465
- scheduleFetch(store.state.refetchOnMount || "lowPriority");
493
+ scheduleFetch(fetchType);
466
494
  }
467
495
  } else {
468
- scheduleFetch("lowPriority");
496
+ scheduleFetch(fetchType);
469
497
  }
470
498
  }, [disableRefetchOnMount, disabled]);
471
499
  const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
@@ -517,7 +545,7 @@ function newTSDFDocumentStore({
517
545
  // src/collectionStore.ts
518
546
  import mitt3 from "mitt";
519
547
  import { useCallback as useCallback2, useEffect as useEffect3, useMemo as useMemo3 } from "react";
520
- import { deepEqual as deepEqual2, Store as Store2, useSubscribeToStore as useSubscribeToStore2 } from "t-state";
548
+ import { deepEqual as deepEqual3, Store as Store2, useSubscribeToStore as useSubscribeToStore2 } from "t-state";
521
549
 
522
550
  // src/collectionFetchOrquestrator.ts
523
551
  function createCollectionFetchOrquestrator(props) {
@@ -534,13 +562,12 @@ function createCollectionFetchOrquestrator(props) {
534
562
  }
535
563
 
536
564
  // src/utils/filterAndMap.ts
537
- var ignore = Symbol();
538
565
  function filterAndMap(arr, fn) {
539
566
  const result = [];
540
567
  let i = 0;
541
568
  for (const item of arr) {
542
- const mappedItem = fn(item, ignore, i);
543
- if (mappedItem !== ignore) {
569
+ const mappedItem = fn(item, false, i);
570
+ if (mappedItem !== false) {
544
571
  result.push(mappedItem);
545
572
  }
546
573
  i++;
@@ -558,10 +585,10 @@ function getCacheId(input) {
558
585
  return typeof input === "string" || typeof input === "number" ? String(input) : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
559
586
  }
560
587
  function orderedProps(obj) {
561
- return filterAndMap(Object.keys(obj).sort(), (k, ignore2) => {
588
+ return filterAndMap(Object.keys(obj).sort(), (k, ignore) => {
562
589
  const value = obj[k];
563
590
  if (value === void 0)
564
- return ignore2;
591
+ return ignore;
565
592
  return { [k]: obj[k] };
566
593
  });
567
594
  }
@@ -574,14 +601,30 @@ function newTSDFCollectionStore({
574
601
  disableRefetchOnWindowFocus,
575
602
  lowPriorityThrottleMs,
576
603
  errorNormalizer,
604
+ disableInitialDataInvalidation,
577
605
  mediumPriorityThrottleMs,
606
+ initialStateItems,
578
607
  disableRefetchOnMount: globalDisableRefetchOnMount,
579
608
  dynamicRealtimeThrottleMs,
580
609
  getCollectionItemKey: filterCollectionItemObjKey
581
610
  }) {
611
+ const initialState = {};
612
+ if (initialStateItems) {
613
+ for (const item of initialStateItems) {
614
+ const itemKey = getItemKey(item.payload);
615
+ initialState[itemKey] = {
616
+ data: item.data,
617
+ error: null,
618
+ status: "success",
619
+ payload: item.payload,
620
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
621
+ wasLoaded: true
622
+ };
623
+ }
624
+ }
582
625
  const store = new Store2({
583
626
  debugName,
584
- state: {}
627
+ state: initialState
585
628
  });
586
629
  function getItemKey(params) {
587
630
  return getCacheId(
@@ -612,7 +655,7 @@ function newTSDFCollectionStore({
612
655
  item.refetchOnMount = false;
613
656
  },
614
657
  {
615
- equalityCheck: deepEqual2,
658
+ equalityCheck: deepEqual3,
616
659
  action: {
617
660
  type: !itemState?.data ? "fetch-start-loading" : "fetch-start-refetching",
618
661
  payload
@@ -628,7 +671,7 @@ function newTSDFCollectionStore({
628
671
  const item = draft[itemId];
629
672
  if (!item)
630
673
  return;
631
- item.data = data;
674
+ item.data = reusePrevIfEqual({ current: data, prev: item.data });
632
675
  item.status = "success";
633
676
  item.wasLoaded = true;
634
677
  },
@@ -687,8 +730,8 @@ function newTSDFCollectionStore({
687
730
  if (Array.isArray(params)) {
688
731
  return params.map(getItemKey);
689
732
  } else if (typeof params === "function") {
690
- return filterAndMap(Object.entries(items), ([itemKey, item], ignore2) => {
691
- return item && params(item.payload, item.data) ? itemKey : ignore2;
733
+ return filterAndMap(Object.entries(items), ([itemKey, item], ignore) => {
734
+ return item && params(item.payload, item.data) ? itemKey : ignore;
692
735
  });
693
736
  } else {
694
737
  return [getItemKey(params)];
@@ -699,12 +742,19 @@ function newTSDFCollectionStore({
699
742
  function invalidateItem(itemPayload, priority = "highPriority") {
700
743
  const itemsKey = getItemsKeyArray(itemPayload);
701
744
  for (const itemKey of itemsKey) {
745
+ const item = store.state[itemKey];
746
+ if (!item)
747
+ continue;
748
+ const currentInvalidationPriority = item.refetchOnMount ? fetchTypePriority[item.refetchOnMount] : -1;
749
+ const newInvalidationPriority = fetchTypePriority[priority];
750
+ if (currentInvalidationPriority >= newInvalidationPriority)
751
+ return;
702
752
  store.produceState(
703
753
  (draft) => {
704
- const item = draft[itemKey];
705
- if (!item)
754
+ const draftItem = draft[itemKey];
755
+ if (!draftItem)
706
756
  return;
707
- item.refetchOnMount = priority;
757
+ draftItem.refetchOnMount = priority;
708
758
  },
709
759
  { action: { type: "invalidate-data", itemKey } }
710
760
  );
@@ -715,8 +765,8 @@ function newTSDFCollectionStore({
715
765
  function getItemState(params) {
716
766
  if (typeof params === "function" || Array.isArray(params)) {
717
767
  const itemsId = getItemsKeyArray(params);
718
- return filterAndMap(itemsId, (itemId, ignore2) => {
719
- return store.state[itemId] || ignore2;
768
+ return filterAndMap(itemsId, (itemId, ignore) => {
769
+ return store.state[itemId] || ignore;
720
770
  });
721
771
  }
722
772
  return store.state[getItemKey(params)];
@@ -790,7 +840,7 @@ function newTSDFCollectionStore({
790
840
  ]
791
841
  );
792
842
  const storeState = store.useSelector(resultSelector, {
793
- equalityFn: deepEqual2,
843
+ equalityFn: deepEqual3,
794
844
  useExternalDeps: true
795
845
  });
796
846
  useOnMittEvent(storeEvents, "invalidateData", (event) => {
@@ -806,18 +856,16 @@ function newTSDFCollectionStore({
806
856
  useEffect3(() => {
807
857
  for (const { itemKey: itemId, payload } of queriesWithId) {
808
858
  if (itemId) {
859
+ const itemState = getItemState(payload);
860
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
809
861
  if (disableRefetchOnMount) {
810
- const itemState = getItemState(payload);
811
862
  const shouldFetch = !itemState?.wasLoaded || itemState.refetchOnMount;
812
863
  if (shouldFetch) {
813
- scheduleFetch(
814
- itemState?.refetchOnMount || "lowPriority",
815
- payload
816
- );
864
+ scheduleFetch(fetchType, payload);
817
865
  return;
818
866
  }
819
867
  } else {
820
- scheduleFetch("lowPriority", payload);
868
+ scheduleFetch(fetchType, payload);
821
869
  }
822
870
  }
823
871
  }
@@ -955,8 +1003,23 @@ function newTSDFCollectionStore({
955
1003
  // src/listQueryStore.ts
956
1004
  import mitt4 from "mitt";
957
1005
  import { useCallback as useCallback3, useEffect as useEffect4, useMemo as useMemo4 } from "react";
958
- import { deepEqual as deepEqual3, Store as Store3, useSubscribeToStore as useSubscribeToStore3 } from "t-state";
1006
+ import { deepEqual as deepEqual4, Store as Store3, useSubscribeToStore as useSubscribeToStore3 } from "t-state";
959
1007
  import { klona as klona2 } from "klona/json";
1008
+
1009
+ // src/utils/findAndMap.ts
1010
+ function findAndMap(arr, findFn) {
1011
+ let i = 0;
1012
+ for (const item of arr) {
1013
+ const mappedItem = findFn(item, i);
1014
+ if (mappedItem !== false) {
1015
+ return mappedItem;
1016
+ }
1017
+ i++;
1018
+ }
1019
+ return void 0;
1020
+ }
1021
+
1022
+ // src/listQueryStore.ts
960
1023
  var noFetchFnError = "No fetchItemFn was provided";
961
1024
  function newTSDFListQueryStore({
962
1025
  debugName,
@@ -964,16 +1027,44 @@ function newTSDFListQueryStore({
964
1027
  fetchItemFn,
965
1028
  errorNormalizer,
966
1029
  defaultQuerySize = 50,
1030
+ initialData,
967
1031
  disableRefetchOnWindowFocus,
968
1032
  disableRefetchOnMount: globalDisableRefetchOnMount,
969
1033
  lowPriorityThrottleMs,
1034
+ disableInitialDataInvalidation,
970
1035
  mediumPriorityThrottleMs,
971
1036
  dynamicRealtimeThrottleMs,
972
1037
  syncMutationsAndInvalidations
973
1038
  }) {
1039
+ const initialState = { items: {}, queries: {}, itemQueries: {} };
1040
+ if (initialData) {
1041
+ for (const { payload, data } of initialData.items) {
1042
+ const itemKey = getItemKey(payload);
1043
+ initialState.items[itemKey] = data;
1044
+ initialState.itemQueries[itemKey] = {
1045
+ error: null,
1046
+ status: "success",
1047
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1048
+ wasLoaded: true,
1049
+ payload
1050
+ };
1051
+ }
1052
+ for (const { payload, items, hasMore } of initialData.queries) {
1053
+ const queryKey = getQueryKey(payload);
1054
+ initialState.queries[queryKey] = {
1055
+ error: null,
1056
+ status: "success",
1057
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1058
+ wasLoaded: true,
1059
+ payload,
1060
+ items,
1061
+ hasMore
1062
+ };
1063
+ }
1064
+ }
974
1065
  const store = new Store3({
975
1066
  debugName,
976
- state: { items: {}, queries: {}, itemQueries: {} }
1067
+ state: initialState
977
1068
  });
978
1069
  function getQueryKey(params) {
979
1070
  return getCacheId(params);
@@ -1006,7 +1097,7 @@ function newTSDFListQueryStore({
1006
1097
  query.refetchOnMount = false;
1007
1098
  },
1008
1099
  {
1009
- equalityCheck: deepEqual3,
1100
+ equalityCheck: deepEqual4,
1010
1101
  action: {
1011
1102
  type: isLoading ? "fetch-query-start-loading" : fetchType === "loadMore" ? "fetch-query-loading-more" : "refetching-query-start",
1012
1103
  payload
@@ -1037,7 +1128,10 @@ function newTSDFListQueryStore({
1037
1128
  query.items = [];
1038
1129
  for (const { data, itemPayload } of items) {
1039
1130
  const itemKey = getItemKey(itemPayload);
1040
- draft.items[itemKey] = data;
1131
+ draft.items[itemKey] = reusePrevIfEqual({
1132
+ current: data,
1133
+ prev: draft.items[itemKey] ?? void 0
1134
+ });
1041
1135
  query.items.push(String(itemKey));
1042
1136
  const itemQuery = draft.itemQueries[itemKey];
1043
1137
  if (!itemQuery || itemQuery.status !== "loading" && itemQuery.status !== "refetching") {
@@ -1084,13 +1178,27 @@ function newTSDFListQueryStore({
1084
1178
  function getQueryState(params) {
1085
1179
  return store.state.queries[getQueryKey(params)];
1086
1180
  }
1181
+ function getQueriesState(params) {
1182
+ const queryKeys = getQueriesKeyArray(params);
1183
+ return filterAndMap(queryKeys, ({ key }) => {
1184
+ const query = store.state.queries[key];
1185
+ return query || false;
1186
+ });
1187
+ }
1188
+ function getQueriesRelatedToItem(itemPayload) {
1189
+ const itemKey = getItemKey(itemPayload);
1190
+ return getQueriesState((queryPayload) => {
1191
+ const queryState = store.state.queries[getQueryKey(queryPayload)];
1192
+ return !!queryState?.items.includes(itemKey);
1193
+ });
1194
+ }
1087
1195
  function getItemState(itemPayload) {
1088
1196
  if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
1089
1197
  const itemsId = getItemsKeyArray(itemPayload);
1090
- return filterAndMap(itemsId, ({ itemKey }, ignore2) => {
1198
+ return filterAndMap(itemsId, ({ itemKey }, ignore) => {
1091
1199
  const item = store.state.items[itemKey];
1092
1200
  const payload = store.state.itemQueries[itemKey]?.payload;
1093
- return !item || !payload ? ignore2 : { payload, data: item };
1201
+ return !item || !payload ? ignore : { payload, data: item };
1094
1202
  });
1095
1203
  }
1096
1204
  return store.state.items[getItemKey(itemPayload)];
@@ -1116,9 +1224,10 @@ function newTSDFListQueryStore({
1116
1224
  return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
1117
1225
  }
1118
1226
  function getQueryItems(query, itemDataSelector) {
1119
- return filterAndMap(query.items, (itemKey, ignore2) => {
1227
+ return filterAndMap(query.items, (itemKey, ignore) => {
1120
1228
  const item = store.state.items[itemKey];
1121
- return item ? itemDataSelector(item, itemKey) : ignore2;
1229
+ const itemPayload = store.state.itemQueries[itemKey]?.payload;
1230
+ return item && itemPayload ? itemDataSelector(item, itemPayload) : ignore;
1122
1231
  });
1123
1232
  }
1124
1233
  async function awaitListQueryFetch(params, size = defaultQuerySize) {
@@ -1140,7 +1249,10 @@ function newTSDFListQueryStore({
1140
1249
  };
1141
1250
  }
1142
1251
  return query.error ? { items: [], error: query.error, hasMore: query.hasMore } : {
1143
- items: getQueryItems(query, (data, itemKey) => ({ data, itemKey })),
1252
+ items: getQueryItems(query, (data, itemPayload) => ({
1253
+ data,
1254
+ itemPayload
1255
+ })),
1144
1256
  error: null,
1145
1257
  hasMore: query.hasMore
1146
1258
  };
@@ -1169,11 +1281,11 @@ function newTSDFListQueryStore({
1169
1281
  } else if (typeof payloads === "function") {
1170
1282
  return filterAndMap(
1171
1283
  Object.entries(store.state.queries),
1172
- ([queryKey, query], ignore2) => {
1284
+ ([queryKey, query], ignore) => {
1173
1285
  return payloads(query.payload, query, queryKey) ? {
1174
1286
  key: queryKey,
1175
1287
  payload: query.payload
1176
- } : ignore2;
1288
+ } : ignore;
1177
1289
  }
1178
1290
  );
1179
1291
  } else {
@@ -1185,6 +1297,13 @@ function newTSDFListQueryStore({
1185
1297
  function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
1186
1298
  const queriesKey = getQueriesKeyArray(queryPayload);
1187
1299
  for (const { key } of queriesKey) {
1300
+ const queryState = store.state.queries[key];
1301
+ if (!queryState)
1302
+ continue;
1303
+ const currentInvalidationPriority = queryState.refetchOnMount ? fetchTypePriority[queryState.refetchOnMount] : -1;
1304
+ const newInvalidationPriority = fetchTypePriority[priority];
1305
+ if (currentInvalidationPriority >= newInvalidationPriority)
1306
+ return;
1188
1307
  store.produceState(
1189
1308
  (draft) => {
1190
1309
  const query = draft.queries[key];
@@ -1284,7 +1403,7 @@ function newTSDFListQueryStore({
1284
1403
  ]
1285
1404
  );
1286
1405
  const storeState = store.useSelector(resultSelector, {
1287
- equalityFn: deepEqual3,
1406
+ equalityFn: deepEqual4,
1288
1407
  useExternalDeps: true
1289
1408
  });
1290
1409
  useOnMittEvent(storeEvents, "invalidateQuery", (event) => {
@@ -1301,19 +1420,16 @@ function newTSDFListQueryStore({
1301
1420
  useEffect4(() => {
1302
1421
  for (const { key: itemId, payload: fetchParams } of queriesWithId) {
1303
1422
  if (itemId) {
1423
+ const itemState = getQueryState(fetchParams);
1424
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
1304
1425
  if (disableRefetchOnMount) {
1305
- const itemState = getQueryState(fetchParams);
1306
1426
  const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
1307
1427
  if (shouldFetch) {
1308
- scheduleListQueryFetch(
1309
- itemState?.refetchOnMount || "lowPriority",
1310
- fetchParams,
1311
- loadSizeConst
1312
- );
1428
+ scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
1313
1429
  return;
1314
1430
  }
1315
1431
  } else {
1316
- scheduleListQueryFetch("lowPriority", fetchParams, loadSizeConst);
1432
+ scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
1317
1433
  }
1318
1434
  }
1319
1435
  }
@@ -1400,7 +1516,10 @@ function newTSDFListQueryStore({
1400
1516
  return;
1401
1517
  itemQuery.status = "success";
1402
1518
  itemQuery.wasLoaded = true;
1403
- draft.items[itemKey] = item;
1519
+ draft.items[itemKey] = reusePrevIfEqual({
1520
+ current: item,
1521
+ prev: draft.items[itemKey] ?? void 0
1522
+ });
1404
1523
  },
1405
1524
  {
1406
1525
  action: { type: "fetch-item-success", itemPayload }
@@ -1454,11 +1573,11 @@ function newTSDFListQueryStore({
1454
1573
  if (typeof itemsPayload === "function") {
1455
1574
  return filterAndMap(
1456
1575
  Object.entries(store.state.items),
1457
- ([itemKey, item], ignore2) => {
1576
+ ([itemKey, item], ignore) => {
1458
1577
  const payload = store.state.itemQueries[itemKey]?.payload;
1459
1578
  if (item === null || !payload)
1460
- return ignore2;
1461
- return itemsPayload(payload, item) ? { itemKey, payload } : ignore2;
1579
+ return ignore;
1580
+ return itemsPayload(payload, item) ? { itemKey, payload } : ignore;
1462
1581
  }
1463
1582
  );
1464
1583
  }
@@ -1471,6 +1590,13 @@ function newTSDFListQueryStore({
1471
1590
  }
1472
1591
  const itemsKey = getItemsKeyArray(itemId);
1473
1592
  for (const { itemKey } of itemsKey) {
1593
+ const item = store.state.itemQueries[itemKey];
1594
+ if (!item)
1595
+ continue;
1596
+ const currentInvalidationPriority = item.refetchOnMount ? fetchTypePriority[item.refetchOnMount] : -1;
1597
+ const newInvalidationPriority = fetchTypePriority[priority];
1598
+ if (currentInvalidationPriority >= newInvalidationPriority)
1599
+ return;
1474
1600
  store.produceState(
1475
1601
  (draft) => {
1476
1602
  const query = draft.itemQueries[itemKey];
@@ -1494,12 +1620,13 @@ function newTSDFListQueryStore({
1494
1620
  }
1495
1621
  }
1496
1622
  function startItemMutation(itemId) {
1497
- if (!fetchItemOrquestrator)
1498
- throw new Error(noFetchFnError);
1499
1623
  const itemsKey = getItemsKeyArray(itemId);
1500
1624
  const endMutations = [];
1501
1625
  for (const { itemKey, payload } of itemsKey) {
1502
- endMutations.push(fetchItemOrquestrator.get(itemKey).startMutation());
1626
+ endMutations.push(
1627
+ fetchItemOrquestrator?.get(itemKey).startMutation() || (() => {
1628
+ })
1629
+ );
1503
1630
  if (syncMutationsAndInvalidations) {
1504
1631
  for (const [queryKey, query] of Object.entries(store.state.queries)) {
1505
1632
  if (syncMutationsAndInvalidations.syncItemAndQuery(
@@ -1526,7 +1653,8 @@ function newTSDFListQueryStore({
1526
1653
  selector = defaultItemDataSelector,
1527
1654
  returnIdleStatus,
1528
1655
  returnRefetchingStatus,
1529
- disableRefetchOnMount
1656
+ disableRefetchOnMount,
1657
+ loadFromStateOnly
1530
1658
  } = {}) {
1531
1659
  const dataSelector = useCallback3(selector, []);
1532
1660
  const memoizedItemKeys = useDeepMemo(
@@ -1553,7 +1681,7 @@ function newTSDFListQueryStore({
1553
1681
  error: null,
1554
1682
  isLoading: false,
1555
1683
  payload,
1556
- data: dataSelector(null, itemKey)
1684
+ data: dataSelector(null, null)
1557
1685
  };
1558
1686
  }
1559
1687
  if (!itemQuery) {
@@ -1562,7 +1690,7 @@ function newTSDFListQueryStore({
1562
1690
  error: null,
1563
1691
  isLoading: returnIdleStatus ? false : true,
1564
1692
  payload,
1565
- data: dataSelector(null, itemKey)
1693
+ data: dataSelector(null, null)
1566
1694
  };
1567
1695
  }
1568
1696
  let status = itemQuery.status;
@@ -1573,7 +1701,7 @@ function newTSDFListQueryStore({
1573
1701
  status,
1574
1702
  error: itemQuery.error,
1575
1703
  isLoading: status === "loading",
1576
- data: dataSelector(itemState ?? null, itemKey),
1704
+ data: dataSelector(itemState ?? null, itemQuery.payload),
1577
1705
  payload
1578
1706
  };
1579
1707
  }
@@ -1587,10 +1715,12 @@ function newTSDFListQueryStore({
1587
1715
  ]
1588
1716
  );
1589
1717
  const storeState = store.useSelector(resultSelector, {
1590
- equalityFn: deepEqual3,
1718
+ equalityFn: deepEqual4,
1591
1719
  useExternalDeps: true
1592
1720
  });
1593
1721
  useOnMittEvent(storeEvents, "invalidateItem", (event) => {
1722
+ if (loadFromStateOnly)
1723
+ return;
1594
1724
  for (const { payload, itemKey } of memoizedItemKeys) {
1595
1725
  if (itemKey !== event.itemKey)
1596
1726
  continue;
@@ -1601,24 +1731,24 @@ function newTSDFListQueryStore({
1601
1731
  }
1602
1732
  });
1603
1733
  useEffect4(() => {
1734
+ if (loadFromStateOnly)
1735
+ return;
1604
1736
  for (const { payload, itemKey } of memoizedItemKeys) {
1605
1737
  if (itemKey) {
1738
+ const itemState = store.state.itemQueries[itemKey];
1739
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
1606
1740
  if (disableRefetchOnMount) {
1607
- const itemState = store.state.itemQueries[itemKey];
1608
1741
  const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
1609
1742
  if (shouldFetch) {
1610
- scheduleItemFetch(
1611
- itemState?.refetchOnMount || "lowPriority",
1612
- payload
1613
- );
1743
+ scheduleItemFetch(fetchType, payload);
1614
1744
  return;
1615
1745
  }
1616
1746
  } else {
1617
- scheduleItemFetch("lowPriority", payload);
1747
+ scheduleItemFetch(fetchType, payload);
1618
1748
  }
1619
1749
  }
1620
1750
  }
1621
- }, [disableRefetchOnMount, memoizedItemKeys]);
1751
+ }, [disableRefetchOnMount, loadFromStateOnly, memoizedItemKeys]);
1622
1752
  return storeState;
1623
1753
  }
1624
1754
  function useItem(itemPayload, options = {}) {
@@ -1637,7 +1767,7 @@ function newTSDFListQueryStore({
1637
1767
  error: null,
1638
1768
  isLoading: false,
1639
1769
  status: "idle",
1640
- data: memoizedSelector(null, ""),
1770
+ data: memoizedSelector(null, null),
1641
1771
  payload: itemPayload || null
1642
1772
  },
1643
1773
  [itemPayload, memoizedSelector, queryResult]
@@ -1662,7 +1792,32 @@ function newTSDFListQueryStore({
1662
1792
  });
1663
1793
  return useModifyResult(result);
1664
1794
  }
1665
- function updateItemState(itemIds, produceNewData, ifNothingWasUpdated) {
1795
+ function useFindItem(findItem, {
1796
+ selector = defaultItemDataSelector
1797
+ } = {}) {
1798
+ return store.useSelector((state) => {
1799
+ const selectedItem = findAndMap(
1800
+ Object.entries(state.items),
1801
+ ([itemKey, item]) => {
1802
+ if (!item)
1803
+ return false;
1804
+ const itemQuery = state.itemQueries[itemKey];
1805
+ if (!itemQuery)
1806
+ return false;
1807
+ if (findItem(item, itemQuery.payload)) {
1808
+ return { item, itemQuery };
1809
+ }
1810
+ return false;
1811
+ }
1812
+ );
1813
+ if (!selectedItem)
1814
+ return null;
1815
+ return selector(selectedItem.item, selectedItem.itemQuery.payload);
1816
+ });
1817
+ }
1818
+ function updateItemState(itemIds, produceNewData, {
1819
+ ifNothingWasUpdated
1820
+ } = {}) {
1666
1821
  const itemKeys = getItemsKeyArray(itemIds);
1667
1822
  let someItemWasUpdated = false;
1668
1823
  store.batch(
@@ -1687,7 +1842,9 @@ function newTSDFListQueryStore({
1687
1842
  );
1688
1843
  return someItemWasUpdated;
1689
1844
  }
1690
- function addItemToState(itemPayload, data) {
1845
+ function addItemToState(itemPayload, data, {
1846
+ addItemToQueries
1847
+ } = {}) {
1691
1848
  const itemKey = getItemKey(itemPayload);
1692
1849
  store.produceState(
1693
1850
  (draftState) => {
@@ -1699,6 +1856,29 @@ function newTSDFListQueryStore({
1699
1856
  error: null,
1700
1857
  payload: klona2(itemPayload)
1701
1858
  };
1859
+ if (addItemToQueries) {
1860
+ const queries = getQueriesKeyArray(addItemToQueries.queries);
1861
+ for (const { key } of queries) {
1862
+ if (draftState.queries[key]) {
1863
+ const queryState = draftState.queries[key];
1864
+ if (!queryState)
1865
+ continue;
1866
+ if (addItemToQueries.appendTo === "start") {
1867
+ queryState.items.unshift(itemKey);
1868
+ } else if (addItemToQueries.appendTo === "end") {
1869
+ queryState.items.push(itemKey);
1870
+ } else {
1871
+ const index = addItemToQueries.appendTo(
1872
+ filterAndMap(
1873
+ queryState.items,
1874
+ (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
1875
+ )
1876
+ );
1877
+ queryState.items.splice(index, 0, itemKey);
1878
+ }
1879
+ }
1880
+ }
1881
+ }
1702
1882
  },
1703
1883
  { action: { type: "create-item-state", itemPayload } }
1704
1884
  );
@@ -1723,7 +1903,9 @@ function newTSDFListQueryStore({
1723
1903
  if (!disableRefetchOnWindowFocus) {
1724
1904
  let handleFocus2 = function() {
1725
1905
  invalidateQuery(() => true, "lowPriority");
1726
- invalidateItem(() => true, "lowPriority");
1906
+ if (fetchItemFn) {
1907
+ invalidateItem(() => true, "lowPriority");
1908
+ }
1727
1909
  };
1728
1910
  var handleFocus = handleFocus2;
1729
1911
  window.addEventListener("focus", handleFocus2);
@@ -1749,7 +1931,10 @@ function newTSDFListQueryStore({
1749
1931
  useMultipleItems,
1750
1932
  addItemToState,
1751
1933
  updateItemState,
1752
- startItemMutation
1934
+ startItemMutation,
1935
+ getQueriesState,
1936
+ getQueriesRelatedToItem,
1937
+ useFindItem
1753
1938
  };
1754
1939
  }
1755
1940
  export {