tsdf 8.0.0-beta.16 → 8.0.0-beta.18

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
@@ -596,13 +596,12 @@ function createCollectionFetchOrquestrator(props) {
596
596
  }
597
597
 
598
598
  // src/utils/filterAndMap.ts
599
- var ignore = Symbol();
600
599
  function filterAndMap(arr, fn) {
601
600
  const result = [];
602
601
  let i = 0;
603
602
  for (const item of arr) {
604
- const mappedItem = fn(item, ignore, i);
605
- if (mappedItem !== ignore) {
603
+ const mappedItem = fn(item, false, i);
604
+ if (mappedItem !== false) {
606
605
  result.push(mappedItem);
607
606
  }
608
607
  i++;
@@ -620,10 +619,10 @@ function getCacheId(input) {
620
619
  return typeof input === "string" || typeof input === "number" ? String(input) : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
621
620
  }
622
621
  function orderedProps(obj) {
623
- return filterAndMap(Object.keys(obj).sort(), (k, ignore2) => {
622
+ return filterAndMap(Object.keys(obj).sort(), (k, ignore) => {
624
623
  const value = obj[k];
625
624
  if (value === void 0)
626
- return ignore2;
625
+ return ignore;
627
626
  return { [k]: obj[k] };
628
627
  });
629
628
  }
@@ -765,8 +764,8 @@ function newTSDFCollectionStore({
765
764
  if (Array.isArray(params)) {
766
765
  return params.map(getItemKey);
767
766
  } else if (typeof params === "function") {
768
- return filterAndMap(Object.entries(items), ([itemKey, item], ignore2) => {
769
- 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;
770
769
  });
771
770
  } else {
772
771
  return [getItemKey(params)];
@@ -800,8 +799,8 @@ function newTSDFCollectionStore({
800
799
  function getItemState(params) {
801
800
  if (typeof params === "function" || Array.isArray(params)) {
802
801
  const itemsId = getItemsKeyArray(params);
803
- return filterAndMap(itemsId, (itemId, ignore2) => {
804
- return store.state[itemId] || ignore2;
802
+ return filterAndMap(itemsId, (itemId, ignore) => {
803
+ return store.state[itemId] || ignore;
805
804
  });
806
805
  }
807
806
  return store.state[getItemKey(params)];
@@ -1198,13 +1197,27 @@ function newTSDFListQueryStore({
1198
1197
  function getQueryState(params) {
1199
1198
  return store.state.queries[getQueryKey(params)];
1200
1199
  }
1200
+ function getQueriesState(params) {
1201
+ const queryKeys = getQueriesKeyArray(params);
1202
+ return filterAndMap(queryKeys, ({ key }) => {
1203
+ const query = store.state.queries[key];
1204
+ return query || false;
1205
+ });
1206
+ }
1207
+ function getQueriesRelatedToItem(itemPayload) {
1208
+ const itemKey = getItemKey(itemPayload);
1209
+ return getQueriesState((queryPayload) => {
1210
+ const queryState = store.state.queries[getQueryKey(queryPayload)];
1211
+ return !!queryState?.items.includes(itemKey);
1212
+ });
1213
+ }
1201
1214
  function getItemState(itemPayload) {
1202
1215
  if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
1203
1216
  const itemsId = getItemsKeyArray(itemPayload);
1204
- return filterAndMap(itemsId, ({ itemKey }, ignore2) => {
1217
+ return filterAndMap(itemsId, ({ itemKey }, ignore) => {
1205
1218
  const item = store.state.items[itemKey];
1206
1219
  const payload = store.state.itemQueries[itemKey]?.payload;
1207
- return !item || !payload ? ignore2 : { payload, data: item };
1220
+ return !item || !payload ? ignore : { payload, data: item };
1208
1221
  });
1209
1222
  }
1210
1223
  return store.state.items[getItemKey(itemPayload)];
@@ -1230,9 +1243,9 @@ function newTSDFListQueryStore({
1230
1243
  return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
1231
1244
  }
1232
1245
  function getQueryItems(query, itemDataSelector) {
1233
- return filterAndMap(query.items, (itemKey, ignore2) => {
1246
+ return filterAndMap(query.items, (itemKey, ignore) => {
1234
1247
  const item = store.state.items[itemKey];
1235
- return item ? itemDataSelector(item, itemKey) : ignore2;
1248
+ return item ? itemDataSelector(item, itemKey) : ignore;
1236
1249
  });
1237
1250
  }
1238
1251
  async function awaitListQueryFetch(params, size = defaultQuerySize) {
@@ -1283,11 +1296,11 @@ function newTSDFListQueryStore({
1283
1296
  } else if (typeof payloads === "function") {
1284
1297
  return filterAndMap(
1285
1298
  Object.entries(store.state.queries),
1286
- ([queryKey, query], ignore2) => {
1299
+ ([queryKey, query], ignore) => {
1287
1300
  return payloads(query.payload, query, queryKey) ? {
1288
1301
  key: queryKey,
1289
1302
  payload: query.payload
1290
- } : ignore2;
1303
+ } : ignore;
1291
1304
  }
1292
1305
  );
1293
1306
  } else {
@@ -1575,11 +1588,11 @@ function newTSDFListQueryStore({
1575
1588
  if (typeof itemsPayload === "function") {
1576
1589
  return filterAndMap(
1577
1590
  Object.entries(store.state.items),
1578
- ([itemKey, item], ignore2) => {
1591
+ ([itemKey, item], ignore) => {
1579
1592
  const payload = store.state.itemQueries[itemKey]?.payload;
1580
1593
  if (item === null || !payload)
1581
- return ignore2;
1582
- return itemsPayload(payload, item) ? { itemKey, payload } : ignore2;
1594
+ return ignore;
1595
+ return itemsPayload(payload, item) ? { itemKey, payload } : ignore;
1583
1596
  }
1584
1597
  );
1585
1598
  }
@@ -1654,7 +1667,8 @@ function newTSDFListQueryStore({
1654
1667
  selector = defaultItemDataSelector,
1655
1668
  returnIdleStatus,
1656
1669
  returnRefetchingStatus,
1657
- disableRefetchOnMount
1670
+ disableRefetchOnMount,
1671
+ loadFromStateOnly
1658
1672
  } = {}) {
1659
1673
  const dataSelector = (0, import_react5.useCallback)(selector, []);
1660
1674
  const memoizedItemKeys = useDeepMemo(
@@ -1719,6 +1733,8 @@ function newTSDFListQueryStore({
1719
1733
  useExternalDeps: true
1720
1734
  });
1721
1735
  useOnMittEvent(storeEvents, "invalidateItem", (event) => {
1736
+ if (loadFromStateOnly)
1737
+ return;
1722
1738
  for (const { payload, itemKey } of memoizedItemKeys) {
1723
1739
  if (itemKey !== event.itemKey)
1724
1740
  continue;
@@ -1729,6 +1745,8 @@ function newTSDFListQueryStore({
1729
1745
  }
1730
1746
  });
1731
1747
  (0, import_react5.useEffect)(() => {
1748
+ if (loadFromStateOnly)
1749
+ return;
1732
1750
  for (const { payload, itemKey } of memoizedItemKeys) {
1733
1751
  if (itemKey) {
1734
1752
  const itemState = store.state.itemQueries[itemKey];
@@ -1744,7 +1762,7 @@ function newTSDFListQueryStore({
1744
1762
  }
1745
1763
  }
1746
1764
  }
1747
- }, [disableRefetchOnMount, memoizedItemKeys]);
1765
+ }, [disableRefetchOnMount, loadFromStateOnly, memoizedItemKeys]);
1748
1766
  return storeState;
1749
1767
  }
1750
1768
  function useItem(itemPayload, options = {}) {
@@ -1844,7 +1862,7 @@ function newTSDFListQueryStore({
1844
1862
  const index = addItemToQueries.appendTo(
1845
1863
  filterAndMap(
1846
1864
  queryState.items,
1847
- (itemKey2, ignore2) => draftState.itemQueries[itemKey2]?.payload || ignore2
1865
+ (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
1848
1866
  )
1849
1867
  );
1850
1868
  queryState.items.splice(index, 0, itemKey);
@@ -1876,7 +1894,9 @@ function newTSDFListQueryStore({
1876
1894
  if (!disableRefetchOnWindowFocus) {
1877
1895
  let handleFocus2 = function() {
1878
1896
  invalidateQuery(() => true, "lowPriority");
1879
- invalidateItem(() => true, "lowPriority");
1897
+ if (fetchItemFn) {
1898
+ invalidateItem(() => true, "lowPriority");
1899
+ }
1880
1900
  };
1881
1901
  var handleFocus = handleFocus2;
1882
1902
  window.addEventListener("focus", handleFocus2);
@@ -1902,7 +1922,9 @@ function newTSDFListQueryStore({
1902
1922
  useMultipleItems,
1903
1923
  addItemToState,
1904
1924
  updateItemState,
1905
- startItemMutation
1925
+ startItemMutation,
1926
+ getQueriesState,
1927
+ getQueriesRelatedToItem
1906
1928
  };
1907
1929
  }
1908
1930
  // Annotate the CommonJS export names for ESM import in node:
package/dist/main.d.ts CHANGED
@@ -219,7 +219,7 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
219
219
  (itemPayload: ItemPayload): ItemState | null | undefined;
220
220
  };
221
221
  loadMore: (params: QueryPayload, size: number) => ScheduleFetchResults;
222
- invalidateQuery: (queryPayload: QueryPayload | QueryPayload[] | ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean), priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
222
+ invalidateQuery: (queryPayload: QueryPayload | ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean) | QueryPayload[], priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
223
223
  invalidateItem: (itemId: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[], priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
224
224
  scheduleItemFetch: {
225
225
  (fetchType: FetchType, itemPayload: ItemPayload): ScheduleFetchResults;
@@ -275,16 +275,18 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
275
275
  returnIdleStatus?: boolean | undefined;
276
276
  returnRefetchingStatus?: boolean | undefined;
277
277
  ensureIsLoaded?: boolean | undefined;
278
+ loadFromStateOnly?: boolean | undefined;
278
279
  }) => TSFDUseListItemReturn<SelectedItem_2, NError, ItemPayload>;
279
- useMultipleItems: <SelectedItem_3 = ItemState | null>(itemsPayload_: ItemPayload[], { selector, returnIdleStatus, returnRefetchingStatus, disableRefetchOnMount, }?: {
280
+ useMultipleItems: <SelectedItem_3 = ItemState | null>(itemsPayload_: ItemPayload[], { selector, returnIdleStatus, returnRefetchingStatus, disableRefetchOnMount, loadFromStateOnly, }?: {
280
281
  selector?: ((data: ItemState | null, id: string) => SelectedItem_3) | undefined;
281
282
  disableRefetchOnMount?: boolean | undefined;
282
283
  returnIdleStatus?: boolean | undefined;
283
284
  returnRefetchingStatus?: boolean | undefined;
285
+ loadFromStateOnly?: boolean | undefined;
284
286
  }) => readonly TSFDUseListItemReturn<SelectedItem_3, NError, ItemPayload>[];
285
287
  addItemToState: (itemPayload: ItemPayload, data: ItemState, { addItemToQueries, }?: {
286
288
  addItemToQueries?: {
287
- queries: QueryPayload | QueryPayload[] | ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean);
289
+ queries: QueryPayload | ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean) | QueryPayload[];
288
290
  appendTo: "start" | "end" | ((itemsPayload: ItemPayload[]) => number);
289
291
  } | undefined;
290
292
  }) => void;
@@ -292,6 +294,8 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
292
294
  ifNothingWasUpdated?: (() => void) | undefined;
293
295
  }) => boolean;
294
296
  startItemMutation: (itemId: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[]) => () => void;
297
+ getQueriesState: (params: ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean) | QueryPayload[]) => TSFDListQuery<NError, QueryPayload>[];
298
+ getQueriesRelatedToItem: (itemPayload: ItemPayload) => TSFDListQuery<NError, QueryPayload>[];
295
299
  };
296
300
  type TSFDListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload, ItemPayload>>;
297
301
 
package/dist/main.js CHANGED
@@ -562,13 +562,12 @@ function createCollectionFetchOrquestrator(props) {
562
562
  }
563
563
 
564
564
  // src/utils/filterAndMap.ts
565
- var ignore = Symbol();
566
565
  function filterAndMap(arr, fn) {
567
566
  const result = [];
568
567
  let i = 0;
569
568
  for (const item of arr) {
570
- const mappedItem = fn(item, ignore, i);
571
- if (mappedItem !== ignore) {
569
+ const mappedItem = fn(item, false, i);
570
+ if (mappedItem !== false) {
572
571
  result.push(mappedItem);
573
572
  }
574
573
  i++;
@@ -586,10 +585,10 @@ function getCacheId(input) {
586
585
  return typeof input === "string" || typeof input === "number" ? String(input) : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
587
586
  }
588
587
  function orderedProps(obj) {
589
- return filterAndMap(Object.keys(obj).sort(), (k, ignore2) => {
588
+ return filterAndMap(Object.keys(obj).sort(), (k, ignore) => {
590
589
  const value = obj[k];
591
590
  if (value === void 0)
592
- return ignore2;
591
+ return ignore;
593
592
  return { [k]: obj[k] };
594
593
  });
595
594
  }
@@ -731,8 +730,8 @@ function newTSDFCollectionStore({
731
730
  if (Array.isArray(params)) {
732
731
  return params.map(getItemKey);
733
732
  } else if (typeof params === "function") {
734
- return filterAndMap(Object.entries(items), ([itemKey, item], ignore2) => {
735
- 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;
736
735
  });
737
736
  } else {
738
737
  return [getItemKey(params)];
@@ -766,8 +765,8 @@ function newTSDFCollectionStore({
766
765
  function getItemState(params) {
767
766
  if (typeof params === "function" || Array.isArray(params)) {
768
767
  const itemsId = getItemsKeyArray(params);
769
- return filterAndMap(itemsId, (itemId, ignore2) => {
770
- return store.state[itemId] || ignore2;
768
+ return filterAndMap(itemsId, (itemId, ignore) => {
769
+ return store.state[itemId] || ignore;
771
770
  });
772
771
  }
773
772
  return store.state[getItemKey(params)];
@@ -1164,13 +1163,27 @@ function newTSDFListQueryStore({
1164
1163
  function getQueryState(params) {
1165
1164
  return store.state.queries[getQueryKey(params)];
1166
1165
  }
1166
+ function getQueriesState(params) {
1167
+ const queryKeys = getQueriesKeyArray(params);
1168
+ return filterAndMap(queryKeys, ({ key }) => {
1169
+ const query = store.state.queries[key];
1170
+ return query || false;
1171
+ });
1172
+ }
1173
+ function getQueriesRelatedToItem(itemPayload) {
1174
+ const itemKey = getItemKey(itemPayload);
1175
+ return getQueriesState((queryPayload) => {
1176
+ const queryState = store.state.queries[getQueryKey(queryPayload)];
1177
+ return !!queryState?.items.includes(itemKey);
1178
+ });
1179
+ }
1167
1180
  function getItemState(itemPayload) {
1168
1181
  if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
1169
1182
  const itemsId = getItemsKeyArray(itemPayload);
1170
- return filterAndMap(itemsId, ({ itemKey }, ignore2) => {
1183
+ return filterAndMap(itemsId, ({ itemKey }, ignore) => {
1171
1184
  const item = store.state.items[itemKey];
1172
1185
  const payload = store.state.itemQueries[itemKey]?.payload;
1173
- return !item || !payload ? ignore2 : { payload, data: item };
1186
+ return !item || !payload ? ignore : { payload, data: item };
1174
1187
  });
1175
1188
  }
1176
1189
  return store.state.items[getItemKey(itemPayload)];
@@ -1196,9 +1209,9 @@ function newTSDFListQueryStore({
1196
1209
  return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
1197
1210
  }
1198
1211
  function getQueryItems(query, itemDataSelector) {
1199
- return filterAndMap(query.items, (itemKey, ignore2) => {
1212
+ return filterAndMap(query.items, (itemKey, ignore) => {
1200
1213
  const item = store.state.items[itemKey];
1201
- return item ? itemDataSelector(item, itemKey) : ignore2;
1214
+ return item ? itemDataSelector(item, itemKey) : ignore;
1202
1215
  });
1203
1216
  }
1204
1217
  async function awaitListQueryFetch(params, size = defaultQuerySize) {
@@ -1249,11 +1262,11 @@ function newTSDFListQueryStore({
1249
1262
  } else if (typeof payloads === "function") {
1250
1263
  return filterAndMap(
1251
1264
  Object.entries(store.state.queries),
1252
- ([queryKey, query], ignore2) => {
1265
+ ([queryKey, query], ignore) => {
1253
1266
  return payloads(query.payload, query, queryKey) ? {
1254
1267
  key: queryKey,
1255
1268
  payload: query.payload
1256
- } : ignore2;
1269
+ } : ignore;
1257
1270
  }
1258
1271
  );
1259
1272
  } else {
@@ -1541,11 +1554,11 @@ function newTSDFListQueryStore({
1541
1554
  if (typeof itemsPayload === "function") {
1542
1555
  return filterAndMap(
1543
1556
  Object.entries(store.state.items),
1544
- ([itemKey, item], ignore2) => {
1557
+ ([itemKey, item], ignore) => {
1545
1558
  const payload = store.state.itemQueries[itemKey]?.payload;
1546
1559
  if (item === null || !payload)
1547
- return ignore2;
1548
- return itemsPayload(payload, item) ? { itemKey, payload } : ignore2;
1560
+ return ignore;
1561
+ return itemsPayload(payload, item) ? { itemKey, payload } : ignore;
1549
1562
  }
1550
1563
  );
1551
1564
  }
@@ -1620,7 +1633,8 @@ function newTSDFListQueryStore({
1620
1633
  selector = defaultItemDataSelector,
1621
1634
  returnIdleStatus,
1622
1635
  returnRefetchingStatus,
1623
- disableRefetchOnMount
1636
+ disableRefetchOnMount,
1637
+ loadFromStateOnly
1624
1638
  } = {}) {
1625
1639
  const dataSelector = useCallback3(selector, []);
1626
1640
  const memoizedItemKeys = useDeepMemo(
@@ -1685,6 +1699,8 @@ function newTSDFListQueryStore({
1685
1699
  useExternalDeps: true
1686
1700
  });
1687
1701
  useOnMittEvent(storeEvents, "invalidateItem", (event) => {
1702
+ if (loadFromStateOnly)
1703
+ return;
1688
1704
  for (const { payload, itemKey } of memoizedItemKeys) {
1689
1705
  if (itemKey !== event.itemKey)
1690
1706
  continue;
@@ -1695,6 +1711,8 @@ function newTSDFListQueryStore({
1695
1711
  }
1696
1712
  });
1697
1713
  useEffect4(() => {
1714
+ if (loadFromStateOnly)
1715
+ return;
1698
1716
  for (const { payload, itemKey } of memoizedItemKeys) {
1699
1717
  if (itemKey) {
1700
1718
  const itemState = store.state.itemQueries[itemKey];
@@ -1710,7 +1728,7 @@ function newTSDFListQueryStore({
1710
1728
  }
1711
1729
  }
1712
1730
  }
1713
- }, [disableRefetchOnMount, memoizedItemKeys]);
1731
+ }, [disableRefetchOnMount, loadFromStateOnly, memoizedItemKeys]);
1714
1732
  return storeState;
1715
1733
  }
1716
1734
  function useItem(itemPayload, options = {}) {
@@ -1810,7 +1828,7 @@ function newTSDFListQueryStore({
1810
1828
  const index = addItemToQueries.appendTo(
1811
1829
  filterAndMap(
1812
1830
  queryState.items,
1813
- (itemKey2, ignore2) => draftState.itemQueries[itemKey2]?.payload || ignore2
1831
+ (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
1814
1832
  )
1815
1833
  );
1816
1834
  queryState.items.splice(index, 0, itemKey);
@@ -1842,7 +1860,9 @@ function newTSDFListQueryStore({
1842
1860
  if (!disableRefetchOnWindowFocus) {
1843
1861
  let handleFocus2 = function() {
1844
1862
  invalidateQuery(() => true, "lowPriority");
1845
- invalidateItem(() => true, "lowPriority");
1863
+ if (fetchItemFn) {
1864
+ invalidateItem(() => true, "lowPriority");
1865
+ }
1846
1866
  };
1847
1867
  var handleFocus = handleFocus2;
1848
1868
  window.addEventListener("focus", handleFocus2);
@@ -1868,7 +1888,9 @@ function newTSDFListQueryStore({
1868
1888
  useMultipleItems,
1869
1889
  addItemToState,
1870
1890
  updateItemState,
1871
- startItemMutation
1891
+ startItemMutation,
1892
+ getQueriesState,
1893
+ getQueriesRelatedToItem
1872
1894
  };
1873
1895
  }
1874
1896
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsdf",
3
- "version": "8.0.0-beta.16",
3
+ "version": "8.0.0-beta.18",
4
4
  "description": "A data fetching solution based on t-state",
5
5
  "license": "MIT",
6
6
  "author": "Lucas Santos",