tsdf 8.0.0-beta.9 → 9.1.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 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 import_t_state2 = require("t-state");
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,
@@ -383,7 +405,7 @@ function newTSDFDocumentStore({
383
405
  disableInitialDataInvalidation,
384
406
  errorNormalizer
385
407
  }) {
386
- const store = new import_t_state2.Store({
408
+ const store = new import_t_state3.Store({
387
409
  debugName,
388
410
  state: {
389
411
  data: initialData ?? null,
@@ -409,7 +431,7 @@ function newTSDFDocumentStore({
409
431
  return false;
410
432
  store.setPartialState(
411
433
  {
412
- data,
434
+ data: reusePrevIfEqual({ prev: store.state.data, current: data }),
413
435
  status: "success"
414
436
  },
415
437
  { action: "fetch-success" }
@@ -450,6 +472,10 @@ function newTSDFDocumentStore({
450
472
  const storeEvents = (0, import_mitt2.default)();
451
473
  let invalidationWasTriggered = false;
452
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;
453
479
  store.setKey("refetchOnMount", priority, { action: "invalidate-data" });
454
480
  invalidationWasTriggered = false;
455
481
  storeEvents.emit("invalidateData", priority);
@@ -494,13 +520,14 @@ function newTSDFDocumentStore({
494
520
  (0, import_react3.useEffect)(() => {
495
521
  if (disabled)
496
522
  return;
523
+ const fetchType = store.state.refetchOnMount || "lowPriority";
497
524
  if (disableRefetchOnMount) {
498
525
  const shouldFetch = store.state.refetchOnMount || store.state.status === "idle";
499
526
  if (shouldFetch) {
500
- scheduleFetch(store.state.refetchOnMount || "lowPriority");
527
+ scheduleFetch(fetchType);
501
528
  }
502
529
  } else {
503
- scheduleFetch("lowPriority");
530
+ scheduleFetch(fetchType);
504
531
  }
505
532
  }, [disableRefetchOnMount, disabled]);
506
533
  const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
@@ -510,7 +537,7 @@ function newTSDFDocumentStore({
510
537
  scheduleFetch("highPriority");
511
538
  }
512
539
  );
513
- (0, import_t_state2.useSubscribeToStore)(store, ({ observe }) => {
540
+ (0, import_t_state3.useSubscribeToStore)(store, ({ observe }) => {
514
541
  observe.ifSelector((state) => state.status).change.then(({ current }) => {
515
542
  if (current === "success" || current === "error") {
516
543
  emitIsLoadedEvt("isLoaded", true);
@@ -552,7 +579,7 @@ function newTSDFDocumentStore({
552
579
  // src/collectionStore.ts
553
580
  var import_mitt3 = __toESM(require("mitt"), 1);
554
581
  var import_react4 = require("react");
555
- var import_t_state3 = require("t-state");
582
+ var import_t_state4 = require("t-state");
556
583
 
557
584
  // src/collectionFetchOrquestrator.ts
558
585
  function createCollectionFetchOrquestrator(props) {
@@ -569,13 +596,12 @@ function createCollectionFetchOrquestrator(props) {
569
596
  }
570
597
 
571
598
  // src/utils/filterAndMap.ts
572
- var ignore = Symbol();
573
599
  function filterAndMap(arr, fn) {
574
600
  const result = [];
575
601
  let i = 0;
576
602
  for (const item of arr) {
577
- const mappedItem = fn(item, ignore, i);
578
- if (mappedItem !== ignore) {
603
+ const mappedItem = fn(item, false, i);
604
+ if (mappedItem !== false) {
579
605
  result.push(mappedItem);
580
606
  }
581
607
  i++;
@@ -593,10 +619,10 @@ function getCacheId(input) {
593
619
  return typeof input === "string" || typeof input === "number" ? String(input) : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
594
620
  }
595
621
  function orderedProps(obj) {
596
- return filterAndMap(Object.keys(obj).sort(), (k, ignore2) => {
622
+ return filterAndMap(Object.keys(obj).sort(), (k, ignore) => {
597
623
  const value = obj[k];
598
624
  if (value === void 0)
599
- return ignore2;
625
+ return ignore;
600
626
  return { [k]: obj[k] };
601
627
  });
602
628
  }
@@ -630,7 +656,7 @@ function newTSDFCollectionStore({
630
656
  };
631
657
  }
632
658
  }
633
- const store = new import_t_state3.Store({
659
+ const store = new import_t_state4.Store({
634
660
  debugName,
635
661
  state: initialState
636
662
  });
@@ -663,7 +689,7 @@ function newTSDFCollectionStore({
663
689
  item.refetchOnMount = false;
664
690
  },
665
691
  {
666
- equalityCheck: import_t_state3.deepEqual,
692
+ equalityCheck: import_t_state4.deepEqual,
667
693
  action: {
668
694
  type: !itemState?.data ? "fetch-start-loading" : "fetch-start-refetching",
669
695
  payload
@@ -679,7 +705,7 @@ function newTSDFCollectionStore({
679
705
  const item = draft[itemId];
680
706
  if (!item)
681
707
  return;
682
- item.data = data;
708
+ item.data = reusePrevIfEqual({ current: data, prev: item.data });
683
709
  item.status = "success";
684
710
  item.wasLoaded = true;
685
711
  },
@@ -738,8 +764,8 @@ function newTSDFCollectionStore({
738
764
  if (Array.isArray(params)) {
739
765
  return params.map(getItemKey);
740
766
  } else if (typeof params === "function") {
741
- return filterAndMap(Object.entries(items), ([itemKey, item], ignore2) => {
742
- return item && params(item.payload, item.data) ? itemKey : ignore2;
767
+ return filterAndMap(Object.entries(items), ([itemKey, item], ignore) => {
768
+ return item && params(item.payload, item.data) ? itemKey : ignore;
743
769
  });
744
770
  } else {
745
771
  return [getItemKey(params)];
@@ -750,12 +776,19 @@ function newTSDFCollectionStore({
750
776
  function invalidateItem(itemPayload, priority = "highPriority") {
751
777
  const itemsKey = getItemsKeyArray(itemPayload);
752
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;
753
786
  store.produceState(
754
787
  (draft) => {
755
- const item = draft[itemKey];
756
- if (!item)
788
+ const draftItem = draft[itemKey];
789
+ if (!draftItem)
757
790
  return;
758
- item.refetchOnMount = priority;
791
+ draftItem.refetchOnMount = priority;
759
792
  },
760
793
  { action: { type: "invalidate-data", itemKey } }
761
794
  );
@@ -766,8 +799,8 @@ function newTSDFCollectionStore({
766
799
  function getItemState(params) {
767
800
  if (typeof params === "function" || Array.isArray(params)) {
768
801
  const itemsId = getItemsKeyArray(params);
769
- return filterAndMap(itemsId, (itemId, ignore2) => {
770
- return store.state[itemId] || ignore2;
802
+ return filterAndMap(itemsId, (itemId, ignore) => {
803
+ return store.state[itemId] || ignore;
771
804
  });
772
805
  }
773
806
  return store.state[getItemKey(params)];
@@ -841,7 +874,7 @@ function newTSDFCollectionStore({
841
874
  ]
842
875
  );
843
876
  const storeState = store.useSelector(resultSelector, {
844
- equalityFn: import_t_state3.deepEqual,
877
+ equalityFn: import_t_state4.deepEqual,
845
878
  useExternalDeps: true
846
879
  });
847
880
  useOnMittEvent(storeEvents, "invalidateData", (event) => {
@@ -857,18 +890,16 @@ function newTSDFCollectionStore({
857
890
  (0, import_react4.useEffect)(() => {
858
891
  for (const { itemKey: itemId, payload } of queriesWithId) {
859
892
  if (itemId) {
893
+ const itemState = getItemState(payload);
894
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
860
895
  if (disableRefetchOnMount) {
861
- const itemState = getItemState(payload);
862
896
  const shouldFetch = !itemState?.wasLoaded || itemState.refetchOnMount;
863
897
  if (shouldFetch) {
864
- scheduleFetch(
865
- itemState?.refetchOnMount || "lowPriority",
866
- payload
867
- );
898
+ scheduleFetch(fetchType, payload);
868
899
  return;
869
900
  }
870
901
  } else {
871
- scheduleFetch("lowPriority", payload);
902
+ scheduleFetch(fetchType, payload);
872
903
  }
873
904
  }
874
905
  }
@@ -902,7 +933,7 @@ function newTSDFCollectionStore({
902
933
  }
903
934
  }
904
935
  );
905
- (0, import_t_state3.useSubscribeToStore)(store, ({ observe }) => {
936
+ (0, import_t_state4.useSubscribeToStore)(store, ({ observe }) => {
906
937
  if (!ensureIsLoaded)
907
938
  return;
908
939
  observe.ifSelector((state) => state[result.itemStateKey]?.status).change.then(({ current }) => {
@@ -1006,8 +1037,23 @@ function newTSDFCollectionStore({
1006
1037
  // src/listQueryStore.ts
1007
1038
  var import_mitt4 = __toESM(require("mitt"), 1);
1008
1039
  var import_react5 = require("react");
1009
- var import_t_state4 = require("t-state");
1040
+ var import_t_state5 = require("t-state");
1010
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
1011
1057
  var noFetchFnError = "No fetchItemFn was provided";
1012
1058
  function newTSDFListQueryStore({
1013
1059
  debugName,
@@ -1045,12 +1091,12 @@ function newTSDFListQueryStore({
1045
1091
  refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1046
1092
  wasLoaded: true,
1047
1093
  payload,
1048
- items: items.map(getItemKey),
1094
+ items,
1049
1095
  hasMore
1050
1096
  };
1051
1097
  }
1052
1098
  }
1053
- const store = new import_t_state4.Store({
1099
+ const store = new import_t_state5.Store({
1054
1100
  debugName,
1055
1101
  state: initialState
1056
1102
  });
@@ -1085,7 +1131,7 @@ function newTSDFListQueryStore({
1085
1131
  query.refetchOnMount = false;
1086
1132
  },
1087
1133
  {
1088
- equalityCheck: import_t_state4.deepEqual,
1134
+ equalityCheck: import_t_state5.deepEqual,
1089
1135
  action: {
1090
1136
  type: isLoading ? "fetch-query-start-loading" : fetchType === "loadMore" ? "fetch-query-loading-more" : "refetching-query-start",
1091
1137
  payload
@@ -1116,7 +1162,10 @@ function newTSDFListQueryStore({
1116
1162
  query.items = [];
1117
1163
  for (const { data, itemPayload } of items) {
1118
1164
  const itemKey = getItemKey(itemPayload);
1119
- draft.items[itemKey] = data;
1165
+ draft.items[itemKey] = reusePrevIfEqual({
1166
+ current: data,
1167
+ prev: draft.items[itemKey] ?? void 0
1168
+ });
1120
1169
  query.items.push(String(itemKey));
1121
1170
  const itemQuery = draft.itemQueries[itemKey];
1122
1171
  if (!itemQuery || itemQuery.status !== "loading" && itemQuery.status !== "refetching") {
@@ -1163,13 +1212,27 @@ function newTSDFListQueryStore({
1163
1212
  function getQueryState(params) {
1164
1213
  return store.state.queries[getQueryKey(params)];
1165
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
+ }
1166
1229
  function getItemState(itemPayload) {
1167
1230
  if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
1168
1231
  const itemsId = getItemsKeyArray(itemPayload);
1169
- return filterAndMap(itemsId, ({ itemKey }, ignore2) => {
1232
+ return filterAndMap(itemsId, ({ itemKey }, ignore) => {
1170
1233
  const item = store.state.items[itemKey];
1171
1234
  const payload = store.state.itemQueries[itemKey]?.payload;
1172
- return !item || !payload ? ignore2 : { payload, data: item };
1235
+ return !item || !payload ? ignore : { payload, data: item };
1173
1236
  });
1174
1237
  }
1175
1238
  return store.state.items[getItemKey(itemPayload)];
@@ -1195,9 +1258,10 @@ function newTSDFListQueryStore({
1195
1258
  return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
1196
1259
  }
1197
1260
  function getQueryItems(query, itemDataSelector) {
1198
- return filterAndMap(query.items, (itemKey, ignore2) => {
1261
+ return filterAndMap(query.items, (itemKey, ignore) => {
1199
1262
  const item = store.state.items[itemKey];
1200
- return item ? itemDataSelector(item, itemKey) : ignore2;
1263
+ const itemPayload = store.state.itemQueries[itemKey]?.payload;
1264
+ return item && itemPayload ? itemDataSelector(item, itemPayload) : ignore;
1201
1265
  });
1202
1266
  }
1203
1267
  async function awaitListQueryFetch(params, size = defaultQuerySize) {
@@ -1219,7 +1283,10 @@ function newTSDFListQueryStore({
1219
1283
  };
1220
1284
  }
1221
1285
  return query.error ? { items: [], error: query.error, hasMore: query.hasMore } : {
1222
- items: getQueryItems(query, (data, itemKey) => ({ data, itemKey })),
1286
+ items: getQueryItems(query, (data, itemPayload) => ({
1287
+ data,
1288
+ itemPayload
1289
+ })),
1223
1290
  error: null,
1224
1291
  hasMore: query.hasMore
1225
1292
  };
@@ -1248,11 +1315,11 @@ function newTSDFListQueryStore({
1248
1315
  } else if (typeof payloads === "function") {
1249
1316
  return filterAndMap(
1250
1317
  Object.entries(store.state.queries),
1251
- ([queryKey, query], ignore2) => {
1318
+ ([queryKey, query], ignore) => {
1252
1319
  return payloads(query.payload, query, queryKey) ? {
1253
1320
  key: queryKey,
1254
1321
  payload: query.payload
1255
- } : ignore2;
1322
+ } : ignore;
1256
1323
  }
1257
1324
  );
1258
1325
  } else {
@@ -1264,6 +1331,13 @@ function newTSDFListQueryStore({
1264
1331
  function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
1265
1332
  const queriesKey = getQueriesKeyArray(queryPayload);
1266
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;
1267
1341
  store.produceState(
1268
1342
  (draft) => {
1269
1343
  const query = draft.queries[key];
@@ -1363,7 +1437,7 @@ function newTSDFListQueryStore({
1363
1437
  ]
1364
1438
  );
1365
1439
  const storeState = store.useSelector(resultSelector, {
1366
- equalityFn: import_t_state4.deepEqual,
1440
+ equalityFn: import_t_state5.deepEqual,
1367
1441
  useExternalDeps: true
1368
1442
  });
1369
1443
  useOnMittEvent(storeEvents, "invalidateQuery", (event) => {
@@ -1380,19 +1454,16 @@ function newTSDFListQueryStore({
1380
1454
  (0, import_react5.useEffect)(() => {
1381
1455
  for (const { key: itemId, payload: fetchParams } of queriesWithId) {
1382
1456
  if (itemId) {
1457
+ const itemState = getQueryState(fetchParams);
1458
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
1383
1459
  if (disableRefetchOnMount) {
1384
- const itemState = getQueryState(fetchParams);
1385
1460
  const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
1386
1461
  if (shouldFetch) {
1387
- scheduleListQueryFetch(
1388
- itemState?.refetchOnMount || "lowPriority",
1389
- fetchParams,
1390
- loadSizeConst
1391
- );
1462
+ scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
1392
1463
  return;
1393
1464
  }
1394
1465
  } else {
1395
- scheduleListQueryFetch("lowPriority", fetchParams, loadSizeConst);
1466
+ scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
1396
1467
  }
1397
1468
  }
1398
1469
  }
@@ -1427,7 +1498,7 @@ function newTSDFListQueryStore({
1427
1498
  }
1428
1499
  }
1429
1500
  );
1430
- (0, import_t_state4.useSubscribeToStore)(store, ({ observe }) => {
1501
+ (0, import_t_state5.useSubscribeToStore)(store, ({ observe }) => {
1431
1502
  if (!ensureIsLoaded)
1432
1503
  return;
1433
1504
  observe.ifSelector((state) => state.queries[result.queryKey]?.status).change.then(({ current }) => {
@@ -1479,7 +1550,10 @@ function newTSDFListQueryStore({
1479
1550
  return;
1480
1551
  itemQuery.status = "success";
1481
1552
  itemQuery.wasLoaded = true;
1482
- draft.items[itemKey] = item;
1553
+ draft.items[itemKey] = reusePrevIfEqual({
1554
+ current: item,
1555
+ prev: draft.items[itemKey] ?? void 0
1556
+ });
1483
1557
  },
1484
1558
  {
1485
1559
  action: { type: "fetch-item-success", itemPayload }
@@ -1533,11 +1607,11 @@ function newTSDFListQueryStore({
1533
1607
  if (typeof itemsPayload === "function") {
1534
1608
  return filterAndMap(
1535
1609
  Object.entries(store.state.items),
1536
- ([itemKey, item], ignore2) => {
1610
+ ([itemKey, item], ignore) => {
1537
1611
  const payload = store.state.itemQueries[itemKey]?.payload;
1538
1612
  if (item === null || !payload)
1539
- return ignore2;
1540
- return itemsPayload(payload, item) ? { itemKey, payload } : ignore2;
1613
+ return ignore;
1614
+ return itemsPayload(payload, item) ? { itemKey, payload } : ignore;
1541
1615
  }
1542
1616
  );
1543
1617
  }
@@ -1550,6 +1624,13 @@ function newTSDFListQueryStore({
1550
1624
  }
1551
1625
  const itemsKey = getItemsKeyArray(itemId);
1552
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;
1553
1634
  store.produceState(
1554
1635
  (draft) => {
1555
1636
  const query = draft.itemQueries[itemKey];
@@ -1573,12 +1654,13 @@ function newTSDFListQueryStore({
1573
1654
  }
1574
1655
  }
1575
1656
  function startItemMutation(itemId) {
1576
- if (!fetchItemOrquestrator)
1577
- throw new Error(noFetchFnError);
1578
1657
  const itemsKey = getItemsKeyArray(itemId);
1579
1658
  const endMutations = [];
1580
1659
  for (const { itemKey, payload } of itemsKey) {
1581
- endMutations.push(fetchItemOrquestrator.get(itemKey).startMutation());
1660
+ endMutations.push(
1661
+ fetchItemOrquestrator?.get(itemKey).startMutation() || (() => {
1662
+ })
1663
+ );
1582
1664
  if (syncMutationsAndInvalidations) {
1583
1665
  for (const [queryKey, query] of Object.entries(store.state.queries)) {
1584
1666
  if (syncMutationsAndInvalidations.syncItemAndQuery(
@@ -1605,7 +1687,8 @@ function newTSDFListQueryStore({
1605
1687
  selector = defaultItemDataSelector,
1606
1688
  returnIdleStatus,
1607
1689
  returnRefetchingStatus,
1608
- disableRefetchOnMount
1690
+ disableRefetchOnMount,
1691
+ loadFromStateOnly
1609
1692
  } = {}) {
1610
1693
  const dataSelector = (0, import_react5.useCallback)(selector, []);
1611
1694
  const memoizedItemKeys = useDeepMemo(
@@ -1632,7 +1715,7 @@ function newTSDFListQueryStore({
1632
1715
  error: null,
1633
1716
  isLoading: false,
1634
1717
  payload,
1635
- data: dataSelector(null, itemKey)
1718
+ data: dataSelector(null, null)
1636
1719
  };
1637
1720
  }
1638
1721
  if (!itemQuery) {
@@ -1641,7 +1724,7 @@ function newTSDFListQueryStore({
1641
1724
  error: null,
1642
1725
  isLoading: returnIdleStatus ? false : true,
1643
1726
  payload,
1644
- data: dataSelector(null, itemKey)
1727
+ data: dataSelector(null, null)
1645
1728
  };
1646
1729
  }
1647
1730
  let status = itemQuery.status;
@@ -1652,7 +1735,7 @@ function newTSDFListQueryStore({
1652
1735
  status,
1653
1736
  error: itemQuery.error,
1654
1737
  isLoading: status === "loading",
1655
- data: dataSelector(itemState ?? null, itemKey),
1738
+ data: dataSelector(itemState ?? null, itemQuery.payload),
1656
1739
  payload
1657
1740
  };
1658
1741
  }
@@ -1666,10 +1749,12 @@ function newTSDFListQueryStore({
1666
1749
  ]
1667
1750
  );
1668
1751
  const storeState = store.useSelector(resultSelector, {
1669
- equalityFn: import_t_state4.deepEqual,
1752
+ equalityFn: import_t_state5.deepEqual,
1670
1753
  useExternalDeps: true
1671
1754
  });
1672
1755
  useOnMittEvent(storeEvents, "invalidateItem", (event) => {
1756
+ if (loadFromStateOnly)
1757
+ return;
1673
1758
  for (const { payload, itemKey } of memoizedItemKeys) {
1674
1759
  if (itemKey !== event.itemKey)
1675
1760
  continue;
@@ -1680,24 +1765,24 @@ function newTSDFListQueryStore({
1680
1765
  }
1681
1766
  });
1682
1767
  (0, import_react5.useEffect)(() => {
1768
+ if (loadFromStateOnly)
1769
+ return;
1683
1770
  for (const { payload, itemKey } of memoizedItemKeys) {
1684
1771
  if (itemKey) {
1772
+ const itemState = store.state.itemQueries[itemKey];
1773
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
1685
1774
  if (disableRefetchOnMount) {
1686
- const itemState = store.state.itemQueries[itemKey];
1687
1775
  const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
1688
1776
  if (shouldFetch) {
1689
- scheduleItemFetch(
1690
- itemState?.refetchOnMount || "lowPriority",
1691
- payload
1692
- );
1777
+ scheduleItemFetch(fetchType, payload);
1693
1778
  return;
1694
1779
  }
1695
1780
  } else {
1696
- scheduleItemFetch("lowPriority", payload);
1781
+ scheduleItemFetch(fetchType, payload);
1697
1782
  }
1698
1783
  }
1699
1784
  }
1700
- }, [disableRefetchOnMount, memoizedItemKeys]);
1785
+ }, [disableRefetchOnMount, loadFromStateOnly, memoizedItemKeys]);
1701
1786
  return storeState;
1702
1787
  }
1703
1788
  function useItem(itemPayload, options = {}) {
@@ -1716,7 +1801,7 @@ function newTSDFListQueryStore({
1716
1801
  error: null,
1717
1802
  isLoading: false,
1718
1803
  status: "idle",
1719
- data: memoizedSelector(null, ""),
1804
+ data: memoizedSelector(null, null),
1720
1805
  payload: itemPayload || null
1721
1806
  },
1722
1807
  [itemPayload, memoizedSelector, queryResult]
@@ -1730,7 +1815,7 @@ function newTSDFListQueryStore({
1730
1815
  }
1731
1816
  }
1732
1817
  );
1733
- (0, import_t_state4.useSubscribeToStore)(store, ({ observe }) => {
1818
+ (0, import_t_state5.useSubscribeToStore)(store, ({ observe }) => {
1734
1819
  if (!ensureIsLoaded || !itemPayload)
1735
1820
  return;
1736
1821
  observe.ifSelector((state) => state.itemQueries[itemPayload]?.status).change.then(({ current }) => {
@@ -1741,7 +1826,32 @@ function newTSDFListQueryStore({
1741
1826
  });
1742
1827
  return useModifyResult(result);
1743
1828
  }
1744
- function updateItemState(itemIds, produceNewData, ifNothingWasUpdated) {
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
+ } = {}) {
1745
1855
  const itemKeys = getItemsKeyArray(itemIds);
1746
1856
  let someItemWasUpdated = false;
1747
1857
  store.batch(
@@ -1766,7 +1876,9 @@ function newTSDFListQueryStore({
1766
1876
  );
1767
1877
  return someItemWasUpdated;
1768
1878
  }
1769
- function addItemToState(itemPayload, data) {
1879
+ function addItemToState(itemPayload, data, {
1880
+ addItemToQueries
1881
+ } = {}) {
1770
1882
  const itemKey = getItemKey(itemPayload);
1771
1883
  store.produceState(
1772
1884
  (draftState) => {
@@ -1778,6 +1890,29 @@ function newTSDFListQueryStore({
1778
1890
  error: null,
1779
1891
  payload: (0, import_json2.klona)(itemPayload)
1780
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
+ }
1781
1916
  },
1782
1917
  { action: { type: "create-item-state", itemPayload } }
1783
1918
  );
@@ -1802,7 +1937,9 @@ function newTSDFListQueryStore({
1802
1937
  if (!disableRefetchOnWindowFocus) {
1803
1938
  let handleFocus2 = function() {
1804
1939
  invalidateQuery(() => true, "lowPriority");
1805
- invalidateItem(() => true, "lowPriority");
1940
+ if (fetchItemFn) {
1941
+ invalidateItem(() => true, "lowPriority");
1942
+ }
1806
1943
  };
1807
1944
  var handleFocus = handleFocus2;
1808
1945
  window.addEventListener("focus", handleFocus2);
@@ -1828,7 +1965,10 @@ function newTSDFListQueryStore({
1828
1965
  useMultipleItems,
1829
1966
  addItemToState,
1830
1967
  updateItemState,
1831
- startItemMutation
1968
+ startItemMutation,
1969
+ getQueriesState,
1970
+ getQueriesRelatedToItem,
1971
+ useFindItem
1832
1972
  };
1833
1973
  }
1834
1974
  // Annotate the CommonJS export names for ESM import in node: