tsdf 8.0.0-beta.15 → 8.0.0-beta.17

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
  }
@@ -1788,7 +1801,9 @@ function newTSDFListQueryStore({
1788
1801
  });
1789
1802
  return useModifyResult(result);
1790
1803
  }
1791
- function updateItemState(itemIds, produceNewData, ifNothingWasUpdated) {
1804
+ function updateItemState(itemIds, produceNewData, {
1805
+ ifNothingWasUpdated
1806
+ } = {}) {
1792
1807
  const itemKeys = getItemsKeyArray(itemIds);
1793
1808
  let someItemWasUpdated = false;
1794
1809
  store.batch(
@@ -1813,7 +1828,9 @@ function newTSDFListQueryStore({
1813
1828
  );
1814
1829
  return someItemWasUpdated;
1815
1830
  }
1816
- function addItemToState(itemPayload, data) {
1831
+ function addItemToState(itemPayload, data, {
1832
+ addItemToQueries
1833
+ } = {}) {
1817
1834
  const itemKey = getItemKey(itemPayload);
1818
1835
  store.produceState(
1819
1836
  (draftState) => {
@@ -1825,6 +1842,29 @@ function newTSDFListQueryStore({
1825
1842
  error: null,
1826
1843
  payload: (0, import_json2.klona)(itemPayload)
1827
1844
  };
1845
+ if (addItemToQueries) {
1846
+ const queries = getQueriesKeyArray(addItemToQueries.queries);
1847
+ for (const { key } of queries) {
1848
+ if (draftState.queries[key]) {
1849
+ const queryState = draftState.queries[key];
1850
+ if (!queryState)
1851
+ continue;
1852
+ if (addItemToQueries.appendTo === "start") {
1853
+ queryState.items.unshift(itemKey);
1854
+ } else if (addItemToQueries.appendTo === "end") {
1855
+ queryState.items.push(itemKey);
1856
+ } else {
1857
+ const index = addItemToQueries.appendTo(
1858
+ filterAndMap(
1859
+ queryState.items,
1860
+ (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
1861
+ )
1862
+ );
1863
+ queryState.items.splice(index, 0, itemKey);
1864
+ }
1865
+ }
1866
+ }
1867
+ }
1828
1868
  },
1829
1869
  { action: { type: "create-item-state", itemPayload } }
1830
1870
  );
@@ -1875,7 +1915,9 @@ function newTSDFListQueryStore({
1875
1915
  useMultipleItems,
1876
1916
  addItemToState,
1877
1917
  updateItemState,
1878
- startItemMutation
1918
+ startItemMutation,
1919
+ getQueriesState,
1920
+ getQueriesRelatedToItem
1879
1921
  };
1880
1922
  }
1881
1923
  // 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;
@@ -282,9 +282,18 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
282
282
  returnIdleStatus?: boolean | undefined;
283
283
  returnRefetchingStatus?: boolean | undefined;
284
284
  }) => readonly TSFDUseListItemReturn<SelectedItem_3, NError, ItemPayload>[];
285
- addItemToState: (itemPayload: ItemPayload, data: ItemState) => void;
286
- updateItemState: (itemIds: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[], produceNewData: (draftData: ItemState, itemPayload: ItemPayload) => void | ItemState, ifNothingWasUpdated?: () => void) => boolean;
285
+ addItemToState: (itemPayload: ItemPayload, data: ItemState, { addItemToQueries, }?: {
286
+ addItemToQueries?: {
287
+ queries: QueryPayload | ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean) | QueryPayload[];
288
+ appendTo: "start" | "end" | ((itemsPayload: ItemPayload[]) => number);
289
+ } | undefined;
290
+ }) => void;
291
+ updateItemState: (itemIds: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[], produceNewData: (draftData: ItemState, itemPayload: ItemPayload) => void | ItemState, { ifNothingWasUpdated, }?: {
292
+ ifNothingWasUpdated?: (() => void) | undefined;
293
+ }) => boolean;
287
294
  startItemMutation: (itemId: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[]) => () => void;
295
+ getQueriesState: (params: ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean) | QueryPayload[]) => TSFDListQuery<NError, QueryPayload>[];
296
+ getQueriesRelatedToItem: (itemPayload: ItemPayload) => TSFDListQuery<NError, QueryPayload>[];
288
297
  };
289
298
  type TSFDListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload, ItemPayload>>;
290
299
 
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
  }
@@ -1754,7 +1767,9 @@ function newTSDFListQueryStore({
1754
1767
  });
1755
1768
  return useModifyResult(result);
1756
1769
  }
1757
- function updateItemState(itemIds, produceNewData, ifNothingWasUpdated) {
1770
+ function updateItemState(itemIds, produceNewData, {
1771
+ ifNothingWasUpdated
1772
+ } = {}) {
1758
1773
  const itemKeys = getItemsKeyArray(itemIds);
1759
1774
  let someItemWasUpdated = false;
1760
1775
  store.batch(
@@ -1779,7 +1794,9 @@ function newTSDFListQueryStore({
1779
1794
  );
1780
1795
  return someItemWasUpdated;
1781
1796
  }
1782
- function addItemToState(itemPayload, data) {
1797
+ function addItemToState(itemPayload, data, {
1798
+ addItemToQueries
1799
+ } = {}) {
1783
1800
  const itemKey = getItemKey(itemPayload);
1784
1801
  store.produceState(
1785
1802
  (draftState) => {
@@ -1791,6 +1808,29 @@ function newTSDFListQueryStore({
1791
1808
  error: null,
1792
1809
  payload: klona2(itemPayload)
1793
1810
  };
1811
+ if (addItemToQueries) {
1812
+ const queries = getQueriesKeyArray(addItemToQueries.queries);
1813
+ for (const { key } of queries) {
1814
+ if (draftState.queries[key]) {
1815
+ const queryState = draftState.queries[key];
1816
+ if (!queryState)
1817
+ continue;
1818
+ if (addItemToQueries.appendTo === "start") {
1819
+ queryState.items.unshift(itemKey);
1820
+ } else if (addItemToQueries.appendTo === "end") {
1821
+ queryState.items.push(itemKey);
1822
+ } else {
1823
+ const index = addItemToQueries.appendTo(
1824
+ filterAndMap(
1825
+ queryState.items,
1826
+ (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
1827
+ )
1828
+ );
1829
+ queryState.items.splice(index, 0, itemKey);
1830
+ }
1831
+ }
1832
+ }
1833
+ }
1794
1834
  },
1795
1835
  { action: { type: "create-item-state", itemPayload } }
1796
1836
  );
@@ -1841,7 +1881,9 @@ function newTSDFListQueryStore({
1841
1881
  useMultipleItems,
1842
1882
  addItemToState,
1843
1883
  updateItemState,
1844
- startItemMutation
1884
+ startItemMutation,
1885
+ getQueriesState,
1886
+ getQueriesRelatedToItem
1845
1887
  };
1846
1888
  }
1847
1889
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsdf",
3
- "version": "8.0.0-beta.15",
3
+ "version": "8.0.0-beta.17",
4
4
  "description": "A data fetching solution based on t-state",
5
5
  "license": "MIT",
6
6
  "author": "Lucas Santos",