tsdf 8.0.0-beta.9 → 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,
@@ -375,7 +397,7 @@ function newTSDFDocumentStore({
375
397
  return false;
376
398
  store.setPartialState(
377
399
  {
378
- data,
400
+ data: reusePrevIfEqual({ prev: store.state.data, current: data }),
379
401
  status: "success"
380
402
  },
381
403
  { action: "fetch-success" }
@@ -416,6 +438,10 @@ function newTSDFDocumentStore({
416
438
  const storeEvents = mitt2();
417
439
  let invalidationWasTriggered = false;
418
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;
419
445
  store.setKey("refetchOnMount", priority, { action: "invalidate-data" });
420
446
  invalidationWasTriggered = false;
421
447
  storeEvents.emit("invalidateData", priority);
@@ -460,13 +486,14 @@ function newTSDFDocumentStore({
460
486
  useEffect2(() => {
461
487
  if (disabled)
462
488
  return;
489
+ const fetchType = store.state.refetchOnMount || "lowPriority";
463
490
  if (disableRefetchOnMount) {
464
491
  const shouldFetch = store.state.refetchOnMount || store.state.status === "idle";
465
492
  if (shouldFetch) {
466
- scheduleFetch(store.state.refetchOnMount || "lowPriority");
493
+ scheduleFetch(fetchType);
467
494
  }
468
495
  } else {
469
- scheduleFetch("lowPriority");
496
+ scheduleFetch(fetchType);
470
497
  }
471
498
  }, [disableRefetchOnMount, disabled]);
472
499
  const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
@@ -518,7 +545,7 @@ function newTSDFDocumentStore({
518
545
  // src/collectionStore.ts
519
546
  import mitt3 from "mitt";
520
547
  import { useCallback as useCallback2, useEffect as useEffect3, useMemo as useMemo3 } from "react";
521
- 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";
522
549
 
523
550
  // src/collectionFetchOrquestrator.ts
524
551
  function createCollectionFetchOrquestrator(props) {
@@ -535,13 +562,12 @@ function createCollectionFetchOrquestrator(props) {
535
562
  }
536
563
 
537
564
  // src/utils/filterAndMap.ts
538
- var ignore = Symbol();
539
565
  function filterAndMap(arr, fn) {
540
566
  const result = [];
541
567
  let i = 0;
542
568
  for (const item of arr) {
543
- const mappedItem = fn(item, ignore, i);
544
- if (mappedItem !== ignore) {
569
+ const mappedItem = fn(item, false, i);
570
+ if (mappedItem !== false) {
545
571
  result.push(mappedItem);
546
572
  }
547
573
  i++;
@@ -559,10 +585,10 @@ function getCacheId(input) {
559
585
  return typeof input === "string" || typeof input === "number" ? String(input) : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
560
586
  }
561
587
  function orderedProps(obj) {
562
- return filterAndMap(Object.keys(obj).sort(), (k, ignore2) => {
588
+ return filterAndMap(Object.keys(obj).sort(), (k, ignore) => {
563
589
  const value = obj[k];
564
590
  if (value === void 0)
565
- return ignore2;
591
+ return ignore;
566
592
  return { [k]: obj[k] };
567
593
  });
568
594
  }
@@ -629,7 +655,7 @@ function newTSDFCollectionStore({
629
655
  item.refetchOnMount = false;
630
656
  },
631
657
  {
632
- equalityCheck: deepEqual2,
658
+ equalityCheck: deepEqual3,
633
659
  action: {
634
660
  type: !itemState?.data ? "fetch-start-loading" : "fetch-start-refetching",
635
661
  payload
@@ -645,7 +671,7 @@ function newTSDFCollectionStore({
645
671
  const item = draft[itemId];
646
672
  if (!item)
647
673
  return;
648
- item.data = data;
674
+ item.data = reusePrevIfEqual({ current: data, prev: item.data });
649
675
  item.status = "success";
650
676
  item.wasLoaded = true;
651
677
  },
@@ -704,8 +730,8 @@ function newTSDFCollectionStore({
704
730
  if (Array.isArray(params)) {
705
731
  return params.map(getItemKey);
706
732
  } else if (typeof params === "function") {
707
- return filterAndMap(Object.entries(items), ([itemKey, item], ignore2) => {
708
- 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;
709
735
  });
710
736
  } else {
711
737
  return [getItemKey(params)];
@@ -716,12 +742,19 @@ function newTSDFCollectionStore({
716
742
  function invalidateItem(itemPayload, priority = "highPriority") {
717
743
  const itemsKey = getItemsKeyArray(itemPayload);
718
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;
719
752
  store.produceState(
720
753
  (draft) => {
721
- const item = draft[itemKey];
722
- if (!item)
754
+ const draftItem = draft[itemKey];
755
+ if (!draftItem)
723
756
  return;
724
- item.refetchOnMount = priority;
757
+ draftItem.refetchOnMount = priority;
725
758
  },
726
759
  { action: { type: "invalidate-data", itemKey } }
727
760
  );
@@ -732,8 +765,8 @@ function newTSDFCollectionStore({
732
765
  function getItemState(params) {
733
766
  if (typeof params === "function" || Array.isArray(params)) {
734
767
  const itemsId = getItemsKeyArray(params);
735
- return filterAndMap(itemsId, (itemId, ignore2) => {
736
- return store.state[itemId] || ignore2;
768
+ return filterAndMap(itemsId, (itemId, ignore) => {
769
+ return store.state[itemId] || ignore;
737
770
  });
738
771
  }
739
772
  return store.state[getItemKey(params)];
@@ -807,7 +840,7 @@ function newTSDFCollectionStore({
807
840
  ]
808
841
  );
809
842
  const storeState = store.useSelector(resultSelector, {
810
- equalityFn: deepEqual2,
843
+ equalityFn: deepEqual3,
811
844
  useExternalDeps: true
812
845
  });
813
846
  useOnMittEvent(storeEvents, "invalidateData", (event) => {
@@ -823,18 +856,16 @@ function newTSDFCollectionStore({
823
856
  useEffect3(() => {
824
857
  for (const { itemKey: itemId, payload } of queriesWithId) {
825
858
  if (itemId) {
859
+ const itemState = getItemState(payload);
860
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
826
861
  if (disableRefetchOnMount) {
827
- const itemState = getItemState(payload);
828
862
  const shouldFetch = !itemState?.wasLoaded || itemState.refetchOnMount;
829
863
  if (shouldFetch) {
830
- scheduleFetch(
831
- itemState?.refetchOnMount || "lowPriority",
832
- payload
833
- );
864
+ scheduleFetch(fetchType, payload);
834
865
  return;
835
866
  }
836
867
  } else {
837
- scheduleFetch("lowPriority", payload);
868
+ scheduleFetch(fetchType, payload);
838
869
  }
839
870
  }
840
871
  }
@@ -972,8 +1003,23 @@ function newTSDFCollectionStore({
972
1003
  // src/listQueryStore.ts
973
1004
  import mitt4 from "mitt";
974
1005
  import { useCallback as useCallback3, useEffect as useEffect4, useMemo as useMemo4 } from "react";
975
- 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";
976
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
977
1023
  var noFetchFnError = "No fetchItemFn was provided";
978
1024
  function newTSDFListQueryStore({
979
1025
  debugName,
@@ -1011,7 +1057,7 @@ function newTSDFListQueryStore({
1011
1057
  refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1012
1058
  wasLoaded: true,
1013
1059
  payload,
1014
- items: items.map(getItemKey),
1060
+ items,
1015
1061
  hasMore
1016
1062
  };
1017
1063
  }
@@ -1051,7 +1097,7 @@ function newTSDFListQueryStore({
1051
1097
  query.refetchOnMount = false;
1052
1098
  },
1053
1099
  {
1054
- equalityCheck: deepEqual3,
1100
+ equalityCheck: deepEqual4,
1055
1101
  action: {
1056
1102
  type: isLoading ? "fetch-query-start-loading" : fetchType === "loadMore" ? "fetch-query-loading-more" : "refetching-query-start",
1057
1103
  payload
@@ -1082,7 +1128,10 @@ function newTSDFListQueryStore({
1082
1128
  query.items = [];
1083
1129
  for (const { data, itemPayload } of items) {
1084
1130
  const itemKey = getItemKey(itemPayload);
1085
- draft.items[itemKey] = data;
1131
+ draft.items[itemKey] = reusePrevIfEqual({
1132
+ current: data,
1133
+ prev: draft.items[itemKey] ?? void 0
1134
+ });
1086
1135
  query.items.push(String(itemKey));
1087
1136
  const itemQuery = draft.itemQueries[itemKey];
1088
1137
  if (!itemQuery || itemQuery.status !== "loading" && itemQuery.status !== "refetching") {
@@ -1129,13 +1178,27 @@ function newTSDFListQueryStore({
1129
1178
  function getQueryState(params) {
1130
1179
  return store.state.queries[getQueryKey(params)];
1131
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
+ }
1132
1195
  function getItemState(itemPayload) {
1133
1196
  if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
1134
1197
  const itemsId = getItemsKeyArray(itemPayload);
1135
- return filterAndMap(itemsId, ({ itemKey }, ignore2) => {
1198
+ return filterAndMap(itemsId, ({ itemKey }, ignore) => {
1136
1199
  const item = store.state.items[itemKey];
1137
1200
  const payload = store.state.itemQueries[itemKey]?.payload;
1138
- return !item || !payload ? ignore2 : { payload, data: item };
1201
+ return !item || !payload ? ignore : { payload, data: item };
1139
1202
  });
1140
1203
  }
1141
1204
  return store.state.items[getItemKey(itemPayload)];
@@ -1161,9 +1224,10 @@ function newTSDFListQueryStore({
1161
1224
  return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
1162
1225
  }
1163
1226
  function getQueryItems(query, itemDataSelector) {
1164
- return filterAndMap(query.items, (itemKey, ignore2) => {
1227
+ return filterAndMap(query.items, (itemKey, ignore) => {
1165
1228
  const item = store.state.items[itemKey];
1166
- return item ? itemDataSelector(item, itemKey) : ignore2;
1229
+ const itemPayload = store.state.itemQueries[itemKey]?.payload;
1230
+ return item && itemPayload ? itemDataSelector(item, itemPayload) : ignore;
1167
1231
  });
1168
1232
  }
1169
1233
  async function awaitListQueryFetch(params, size = defaultQuerySize) {
@@ -1185,7 +1249,10 @@ function newTSDFListQueryStore({
1185
1249
  };
1186
1250
  }
1187
1251
  return query.error ? { items: [], error: query.error, hasMore: query.hasMore } : {
1188
- items: getQueryItems(query, (data, itemKey) => ({ data, itemKey })),
1252
+ items: getQueryItems(query, (data, itemPayload) => ({
1253
+ data,
1254
+ itemPayload
1255
+ })),
1189
1256
  error: null,
1190
1257
  hasMore: query.hasMore
1191
1258
  };
@@ -1214,11 +1281,11 @@ function newTSDFListQueryStore({
1214
1281
  } else if (typeof payloads === "function") {
1215
1282
  return filterAndMap(
1216
1283
  Object.entries(store.state.queries),
1217
- ([queryKey, query], ignore2) => {
1284
+ ([queryKey, query], ignore) => {
1218
1285
  return payloads(query.payload, query, queryKey) ? {
1219
1286
  key: queryKey,
1220
1287
  payload: query.payload
1221
- } : ignore2;
1288
+ } : ignore;
1222
1289
  }
1223
1290
  );
1224
1291
  } else {
@@ -1230,6 +1297,13 @@ function newTSDFListQueryStore({
1230
1297
  function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
1231
1298
  const queriesKey = getQueriesKeyArray(queryPayload);
1232
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;
1233
1307
  store.produceState(
1234
1308
  (draft) => {
1235
1309
  const query = draft.queries[key];
@@ -1329,7 +1403,7 @@ function newTSDFListQueryStore({
1329
1403
  ]
1330
1404
  );
1331
1405
  const storeState = store.useSelector(resultSelector, {
1332
- equalityFn: deepEqual3,
1406
+ equalityFn: deepEqual4,
1333
1407
  useExternalDeps: true
1334
1408
  });
1335
1409
  useOnMittEvent(storeEvents, "invalidateQuery", (event) => {
@@ -1346,19 +1420,16 @@ function newTSDFListQueryStore({
1346
1420
  useEffect4(() => {
1347
1421
  for (const { key: itemId, payload: fetchParams } of queriesWithId) {
1348
1422
  if (itemId) {
1423
+ const itemState = getQueryState(fetchParams);
1424
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
1349
1425
  if (disableRefetchOnMount) {
1350
- const itemState = getQueryState(fetchParams);
1351
1426
  const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
1352
1427
  if (shouldFetch) {
1353
- scheduleListQueryFetch(
1354
- itemState?.refetchOnMount || "lowPriority",
1355
- fetchParams,
1356
- loadSizeConst
1357
- );
1428
+ scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
1358
1429
  return;
1359
1430
  }
1360
1431
  } else {
1361
- scheduleListQueryFetch("lowPriority", fetchParams, loadSizeConst);
1432
+ scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
1362
1433
  }
1363
1434
  }
1364
1435
  }
@@ -1445,7 +1516,10 @@ function newTSDFListQueryStore({
1445
1516
  return;
1446
1517
  itemQuery.status = "success";
1447
1518
  itemQuery.wasLoaded = true;
1448
- draft.items[itemKey] = item;
1519
+ draft.items[itemKey] = reusePrevIfEqual({
1520
+ current: item,
1521
+ prev: draft.items[itemKey] ?? void 0
1522
+ });
1449
1523
  },
1450
1524
  {
1451
1525
  action: { type: "fetch-item-success", itemPayload }
@@ -1499,11 +1573,11 @@ function newTSDFListQueryStore({
1499
1573
  if (typeof itemsPayload === "function") {
1500
1574
  return filterAndMap(
1501
1575
  Object.entries(store.state.items),
1502
- ([itemKey, item], ignore2) => {
1576
+ ([itemKey, item], ignore) => {
1503
1577
  const payload = store.state.itemQueries[itemKey]?.payload;
1504
1578
  if (item === null || !payload)
1505
- return ignore2;
1506
- return itemsPayload(payload, item) ? { itemKey, payload } : ignore2;
1579
+ return ignore;
1580
+ return itemsPayload(payload, item) ? { itemKey, payload } : ignore;
1507
1581
  }
1508
1582
  );
1509
1583
  }
@@ -1516,6 +1590,13 @@ function newTSDFListQueryStore({
1516
1590
  }
1517
1591
  const itemsKey = getItemsKeyArray(itemId);
1518
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;
1519
1600
  store.produceState(
1520
1601
  (draft) => {
1521
1602
  const query = draft.itemQueries[itemKey];
@@ -1539,12 +1620,13 @@ function newTSDFListQueryStore({
1539
1620
  }
1540
1621
  }
1541
1622
  function startItemMutation(itemId) {
1542
- if (!fetchItemOrquestrator)
1543
- throw new Error(noFetchFnError);
1544
1623
  const itemsKey = getItemsKeyArray(itemId);
1545
1624
  const endMutations = [];
1546
1625
  for (const { itemKey, payload } of itemsKey) {
1547
- endMutations.push(fetchItemOrquestrator.get(itemKey).startMutation());
1626
+ endMutations.push(
1627
+ fetchItemOrquestrator?.get(itemKey).startMutation() || (() => {
1628
+ })
1629
+ );
1548
1630
  if (syncMutationsAndInvalidations) {
1549
1631
  for (const [queryKey, query] of Object.entries(store.state.queries)) {
1550
1632
  if (syncMutationsAndInvalidations.syncItemAndQuery(
@@ -1571,7 +1653,8 @@ function newTSDFListQueryStore({
1571
1653
  selector = defaultItemDataSelector,
1572
1654
  returnIdleStatus,
1573
1655
  returnRefetchingStatus,
1574
- disableRefetchOnMount
1656
+ disableRefetchOnMount,
1657
+ loadFromStateOnly
1575
1658
  } = {}) {
1576
1659
  const dataSelector = useCallback3(selector, []);
1577
1660
  const memoizedItemKeys = useDeepMemo(
@@ -1598,7 +1681,7 @@ function newTSDFListQueryStore({
1598
1681
  error: null,
1599
1682
  isLoading: false,
1600
1683
  payload,
1601
- data: dataSelector(null, itemKey)
1684
+ data: dataSelector(null, null)
1602
1685
  };
1603
1686
  }
1604
1687
  if (!itemQuery) {
@@ -1607,7 +1690,7 @@ function newTSDFListQueryStore({
1607
1690
  error: null,
1608
1691
  isLoading: returnIdleStatus ? false : true,
1609
1692
  payload,
1610
- data: dataSelector(null, itemKey)
1693
+ data: dataSelector(null, null)
1611
1694
  };
1612
1695
  }
1613
1696
  let status = itemQuery.status;
@@ -1618,7 +1701,7 @@ function newTSDFListQueryStore({
1618
1701
  status,
1619
1702
  error: itemQuery.error,
1620
1703
  isLoading: status === "loading",
1621
- data: dataSelector(itemState ?? null, itemKey),
1704
+ data: dataSelector(itemState ?? null, itemQuery.payload),
1622
1705
  payload
1623
1706
  };
1624
1707
  }
@@ -1632,10 +1715,12 @@ function newTSDFListQueryStore({
1632
1715
  ]
1633
1716
  );
1634
1717
  const storeState = store.useSelector(resultSelector, {
1635
- equalityFn: deepEqual3,
1718
+ equalityFn: deepEqual4,
1636
1719
  useExternalDeps: true
1637
1720
  });
1638
1721
  useOnMittEvent(storeEvents, "invalidateItem", (event) => {
1722
+ if (loadFromStateOnly)
1723
+ return;
1639
1724
  for (const { payload, itemKey } of memoizedItemKeys) {
1640
1725
  if (itemKey !== event.itemKey)
1641
1726
  continue;
@@ -1646,24 +1731,24 @@ function newTSDFListQueryStore({
1646
1731
  }
1647
1732
  });
1648
1733
  useEffect4(() => {
1734
+ if (loadFromStateOnly)
1735
+ return;
1649
1736
  for (const { payload, itemKey } of memoizedItemKeys) {
1650
1737
  if (itemKey) {
1738
+ const itemState = store.state.itemQueries[itemKey];
1739
+ const fetchType = itemState?.refetchOnMount || "lowPriority";
1651
1740
  if (disableRefetchOnMount) {
1652
- const itemState = store.state.itemQueries[itemKey];
1653
1741
  const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
1654
1742
  if (shouldFetch) {
1655
- scheduleItemFetch(
1656
- itemState?.refetchOnMount || "lowPriority",
1657
- payload
1658
- );
1743
+ scheduleItemFetch(fetchType, payload);
1659
1744
  return;
1660
1745
  }
1661
1746
  } else {
1662
- scheduleItemFetch("lowPriority", payload);
1747
+ scheduleItemFetch(fetchType, payload);
1663
1748
  }
1664
1749
  }
1665
1750
  }
1666
- }, [disableRefetchOnMount, memoizedItemKeys]);
1751
+ }, [disableRefetchOnMount, loadFromStateOnly, memoizedItemKeys]);
1667
1752
  return storeState;
1668
1753
  }
1669
1754
  function useItem(itemPayload, options = {}) {
@@ -1682,7 +1767,7 @@ function newTSDFListQueryStore({
1682
1767
  error: null,
1683
1768
  isLoading: false,
1684
1769
  status: "idle",
1685
- data: memoizedSelector(null, ""),
1770
+ data: memoizedSelector(null, null),
1686
1771
  payload: itemPayload || null
1687
1772
  },
1688
1773
  [itemPayload, memoizedSelector, queryResult]
@@ -1707,7 +1792,32 @@ function newTSDFListQueryStore({
1707
1792
  });
1708
1793
  return useModifyResult(result);
1709
1794
  }
1710
- 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
+ } = {}) {
1711
1821
  const itemKeys = getItemsKeyArray(itemIds);
1712
1822
  let someItemWasUpdated = false;
1713
1823
  store.batch(
@@ -1732,7 +1842,9 @@ function newTSDFListQueryStore({
1732
1842
  );
1733
1843
  return someItemWasUpdated;
1734
1844
  }
1735
- function addItemToState(itemPayload, data) {
1845
+ function addItemToState(itemPayload, data, {
1846
+ addItemToQueries
1847
+ } = {}) {
1736
1848
  const itemKey = getItemKey(itemPayload);
1737
1849
  store.produceState(
1738
1850
  (draftState) => {
@@ -1744,6 +1856,29 @@ function newTSDFListQueryStore({
1744
1856
  error: null,
1745
1857
  payload: klona2(itemPayload)
1746
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
+ }
1747
1882
  },
1748
1883
  { action: { type: "create-item-state", itemPayload } }
1749
1884
  );
@@ -1768,7 +1903,9 @@ function newTSDFListQueryStore({
1768
1903
  if (!disableRefetchOnWindowFocus) {
1769
1904
  let handleFocus2 = function() {
1770
1905
  invalidateQuery(() => true, "lowPriority");
1771
- invalidateItem(() => true, "lowPriority");
1906
+ if (fetchItemFn) {
1907
+ invalidateItem(() => true, "lowPriority");
1908
+ }
1772
1909
  };
1773
1910
  var handleFocus = handleFocus2;
1774
1911
  window.addEventListener("focus", handleFocus2);
@@ -1794,7 +1931,10 @@ function newTSDFListQueryStore({
1794
1931
  useMultipleItems,
1795
1932
  addItemToState,
1796
1933
  updateItemState,
1797
- startItemMutation
1934
+ startItemMutation,
1935
+ getQueriesState,
1936
+ getQueriesRelatedToItem,
1937
+ useFindItem
1798
1938
  };
1799
1939
  }
1800
1940
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsdf",
3
- "version": "8.0.0-beta.9",
3
+ "version": "9.0.0",
4
4
  "description": "A data fetching solution based on t-state",
5
5
  "license": "MIT",
6
6
  "author": "Lucas Santos",
@@ -66,7 +66,7 @@
66
66
  "immer": "^9.0.17",
67
67
  "klona": "^2.0.6",
68
68
  "mitt": "^3.0.0",
69
- "t-state": "^7.5.1"
69
+ "t-state": "^8.0.0"
70
70
  },
71
71
  "peerDependencies": {
72
72
  "react": "^18.0.0",