tsdf 11.2.0 → 12.2.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
@@ -395,21 +395,25 @@ var import_react5 = require("evtmitter/react");
395
395
  function newTSDFDocumentStore({
396
396
  debugName,
397
397
  fetchFn,
398
- initialData,
398
+ getInitialData,
399
399
  disableRefetchOnMount: globalDisableRefetchOnMount,
400
400
  lowPriorityThrottleMs,
401
401
  mediumPriorityThrottleMs,
402
402
  dynamicRealtimeThrottleMs,
403
403
  disableInitialDataInvalidation,
404
- errorNormalizer
404
+ errorNormalizer,
405
+ onInvalidate
405
406
  }) {
406
407
  const store = new import_t_state3.Store({
407
408
  debugName,
408
- state: {
409
- data: initialData ?? null,
410
- error: null,
411
- status: initialData ? "success" : "idle",
412
- refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
409
+ state: () => {
410
+ const initialData = getInitialData?.();
411
+ return {
412
+ data: initialData ?? null,
413
+ error: null,
414
+ status: initialData ? "success" : "idle",
415
+ refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
416
+ };
413
417
  }
414
418
  });
415
419
  async function fetch(fetchCtx, _) {
@@ -480,6 +484,7 @@ function newTSDFDocumentStore({
480
484
  store.setKey("refetchOnMount", priority, { action: "invalidate-data" });
481
485
  invalidationWasTriggered = false;
482
486
  storeEvents.emit("invalidateData", priority);
487
+ onInvalidate?.(priority);
483
488
  }
484
489
  function useDocument({
485
490
  selector,
@@ -648,28 +653,32 @@ function newTSDFCollectionStore({
648
653
  errorNormalizer,
649
654
  disableInitialDataInvalidation,
650
655
  mediumPriorityThrottleMs,
651
- initialStateItems,
656
+ getInitialData,
652
657
  disableRefetchOnMount: globalDisableRefetchOnMount,
653
658
  dynamicRealtimeThrottleMs,
654
- getCollectionItemKey: filterCollectionItemObjKey
659
+ getCollectionItemKey: filterCollectionItemObjKey,
660
+ onInvalidate
655
661
  }) {
656
- const initialState = {};
657
- if (initialStateItems) {
658
- for (const item of initialStateItems) {
659
- const itemKey = getItemKey(item.payload);
660
- initialState[itemKey] = {
661
- data: item.data,
662
- error: null,
663
- status: "success",
664
- payload: item.payload,
665
- refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
666
- wasLoaded: true
667
- };
668
- }
669
- }
670
662
  const store = new import_t_state4.Store({
671
663
  debugName,
672
- state: initialState
664
+ state: () => {
665
+ const initialState = {};
666
+ const initialStateItems = getInitialData?.();
667
+ if (initialStateItems) {
668
+ for (const item of initialStateItems) {
669
+ const itemKey = getItemKey(item.payload);
670
+ initialState[itemKey] = {
671
+ data: item.data,
672
+ error: null,
673
+ status: "success",
674
+ payload: item.payload,
675
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
676
+ wasLoaded: true
677
+ };
678
+ }
679
+ }
680
+ return initialState;
681
+ }
673
682
  });
674
683
  function getItemKey(params) {
675
684
  return getCacheId(
@@ -808,6 +817,13 @@ function newTSDFCollectionStore({
808
817
  );
809
818
  invalidationWasTriggered.delete(itemKey);
810
819
  storeEvents.emit("invalidateData", { priority, itemKey });
820
+ if (item.data) {
821
+ onInvalidate?.({
822
+ priority,
823
+ payload: item.payload,
824
+ itemState: item.data
825
+ });
826
+ }
811
827
  }
812
828
  }
813
829
  function getItemState(params) {
@@ -916,7 +932,7 @@ function newTSDFCollectionStore({
916
932
  const shouldFetch = !itemState?.wasLoaded || itemState.refetchOnMount;
917
933
  if (shouldFetch) {
918
934
  scheduleFetch(fetchType, payload);
919
- return;
935
+ continue;
920
936
  }
921
937
  } else {
922
938
  scheduleFetch(fetchType, payload);
@@ -1072,6 +1088,31 @@ function findAndMap(arr, findFn) {
1072
1088
  return void 0;
1073
1089
  }
1074
1090
 
1091
+ // src/utils/sortBy.ts
1092
+ function sortBy(arr, getPriority, { order = "asc" } = {}) {
1093
+ return [...arr].sort((a, b) => {
1094
+ const _aPriority = getPriority(a);
1095
+ const _bPriority = getPriority(b);
1096
+ const aPriority = Array.isArray(_aPriority) ? _aPriority : [_aPriority];
1097
+ const bPriority = Array.isArray(_bPriority) ? _bPriority : [_bPriority];
1098
+ for (let i = 0; i < aPriority.length; i++) {
1099
+ const levelOrder = typeof order === "string" ? order : order[i] ?? "asc";
1100
+ const aP = aPriority[i] ?? 0;
1101
+ const bP = bPriority[i] ?? 0;
1102
+ if (aP === bP) {
1103
+ continue;
1104
+ }
1105
+ if (bP === Infinity || aP === -Infinity || aP < bP) {
1106
+ return levelOrder === "asc" ? -1 : 1;
1107
+ }
1108
+ if (aP === Infinity || bP === -Infinity || aP > bP) {
1109
+ return levelOrder === "asc" ? 1 : -1;
1110
+ }
1111
+ }
1112
+ return 0;
1113
+ });
1114
+ }
1115
+
1075
1116
  // src/listQueryStore.ts
1076
1117
  var noFetchFnError = "No fetchItemFn was provided";
1077
1118
  function newTSDFListQueryStore({
@@ -1080,43 +1121,49 @@ function newTSDFListQueryStore({
1080
1121
  fetchItemFn,
1081
1122
  errorNormalizer,
1082
1123
  defaultQuerySize = 50,
1083
- initialData,
1124
+ getInitialData,
1084
1125
  disableRefetchOnMount: globalDisableRefetchOnMount,
1085
1126
  lowPriorityThrottleMs,
1086
1127
  disableInitialDataInvalidation,
1087
1128
  mediumPriorityThrottleMs,
1088
1129
  dynamicRealtimeThrottleMs,
1089
- syncMutationsAndInvalidations
1130
+ syncMutationsAndInvalidations,
1131
+ optimisticListUpdates,
1132
+ onInvalidateItem,
1133
+ onInvalidateQuery
1090
1134
  }) {
1091
- const initialState = { items: {}, queries: {}, itemQueries: {} };
1092
- if (initialData) {
1093
- for (const { payload, data } of initialData.items) {
1094
- const itemKey = getItemKey(payload);
1095
- initialState.items[itemKey] = data;
1096
- initialState.itemQueries[itemKey] = {
1097
- error: null,
1098
- status: "success",
1099
- refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1100
- wasLoaded: true,
1101
- payload
1102
- };
1103
- }
1104
- for (const { payload, items, hasMore } of initialData.queries) {
1105
- const queryKey = getQueryKey(payload);
1106
- initialState.queries[queryKey] = {
1107
- error: null,
1108
- status: "success",
1109
- refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1110
- wasLoaded: true,
1111
- payload,
1112
- items,
1113
- hasMore
1114
- };
1115
- }
1116
- }
1117
1135
  const store = new import_t_state5.Store({
1118
1136
  debugName,
1119
- state: initialState
1137
+ state: () => {
1138
+ const initialState = { items: {}, queries: {}, itemQueries: {} };
1139
+ const initialData = getInitialData?.();
1140
+ if (initialData) {
1141
+ for (const { payload, data } of initialData.items) {
1142
+ const itemKey = getItemKey(payload);
1143
+ initialState.items[itemKey] = data;
1144
+ initialState.itemQueries[itemKey] = {
1145
+ error: null,
1146
+ status: "success",
1147
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1148
+ wasLoaded: true,
1149
+ payload
1150
+ };
1151
+ }
1152
+ for (const { payload, items, hasMore } of initialData.queries) {
1153
+ const queryKey = getQueryKey(payload);
1154
+ initialState.queries[queryKey] = {
1155
+ error: null,
1156
+ status: "success",
1157
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1158
+ wasLoaded: true,
1159
+ payload,
1160
+ items,
1161
+ hasMore
1162
+ };
1163
+ }
1164
+ }
1165
+ return initialState;
1166
+ }
1120
1167
  });
1121
1168
  function getQueryKey(params) {
1122
1169
  return getCacheId(params);
@@ -1360,7 +1407,7 @@ function newTSDFListQueryStore({
1360
1407
  const queryInvalidationWasTriggered = /* @__PURE__ */ new Set();
1361
1408
  function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
1362
1409
  const queriesKey = getQueriesKeyArray(queryPayload);
1363
- for (const { key } of queriesKey) {
1410
+ for (const { key, payload } of queriesKey) {
1364
1411
  const queryState = store.state.queries[key];
1365
1412
  if (!queryState)
1366
1413
  continue;
@@ -1379,6 +1426,7 @@ function newTSDFListQueryStore({
1379
1426
  );
1380
1427
  queryInvalidationWasTriggered.delete(key);
1381
1428
  storeEvents.emit("invalidateQuery", { priority, queryKey: key });
1429
+ onInvalidateQuery?.(payload, priority);
1382
1430
  }
1383
1431
  if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
1384
1432
  invalidateItem(
@@ -1498,7 +1546,7 @@ function newTSDFListQueryStore({
1498
1546
  const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
1499
1547
  if (shouldFetch) {
1500
1548
  scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
1501
- return;
1549
+ continue;
1502
1550
  }
1503
1551
  } else {
1504
1552
  scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
@@ -1662,7 +1710,7 @@ function newTSDFListQueryStore({
1662
1710
  throw new Error(noFetchFnError);
1663
1711
  }
1664
1712
  const itemsKey = getItemsKeyArray(itemId);
1665
- for (const { itemKey } of itemsKey) {
1713
+ for (const { itemKey, payload } of itemsKey) {
1666
1714
  const item = store.state.itemQueries[itemKey];
1667
1715
  if (!item)
1668
1716
  continue;
@@ -1681,6 +1729,16 @@ function newTSDFListQueryStore({
1681
1729
  );
1682
1730
  itemInvalidationWasTriggered.delete(itemKey);
1683
1731
  storeEvents.emit("invalidateItem", { priority, itemKey });
1732
+ if (onInvalidateItem) {
1733
+ const itemState = store.state.items[itemKey];
1734
+ if (itemState) {
1735
+ onInvalidateItem({
1736
+ priority,
1737
+ itemState,
1738
+ payload
1739
+ });
1740
+ }
1741
+ }
1684
1742
  }
1685
1743
  if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
1686
1744
  invalidateQuery(
@@ -1913,6 +1971,9 @@ function newTSDFListQueryStore({
1913
1971
  }
1914
1972
  }
1915
1973
  });
1974
+ if (someItemWasUpdated) {
1975
+ applyOptimisticListUpdates(itemKeys.map((i) => i.itemKey));
1976
+ }
1916
1977
  if (ifNothingWasUpdated && !someItemWasUpdated) {
1917
1978
  ifNothingWasUpdated();
1918
1979
  }
@@ -1921,48 +1982,134 @@ function newTSDFListQueryStore({
1921
1982
  );
1922
1983
  return someItemWasUpdated;
1923
1984
  }
1985
+ function applyOptimisticListUpdates(itemKeys) {
1986
+ if (!optimisticListUpdates)
1987
+ return;
1988
+ const queriesToInvalidate = [];
1989
+ store.produceState((draftState) => {
1990
+ for (const itemKey of itemKeys) {
1991
+ const item = draftState.items[itemKey];
1992
+ if (!item)
1993
+ continue;
1994
+ for (const {
1995
+ queries,
1996
+ filterItem,
1997
+ appendNewTo = "end",
1998
+ invalidateQueries,
1999
+ sort
2000
+ } of optimisticListUpdates) {
2001
+ const relatedFilterQueries = getQueriesKeyArray(queries);
2002
+ for (const { key: queryKey, payload } of relatedFilterQueries) {
2003
+ const queryState = draftState.queries[queryKey];
2004
+ if (filterItem) {
2005
+ const itemShouldBeIncluded = filterItem(item);
2006
+ if (itemShouldBeIncluded === null)
2007
+ continue;
2008
+ if (itemShouldBeIncluded) {
2009
+ if (!queryState) {
2010
+ draftState.queries[queryKey] = {
2011
+ status: "success",
2012
+ items: [itemKey],
2013
+ error: null,
2014
+ hasMore: false,
2015
+ payload,
2016
+ refetchOnMount: "lowPriority",
2017
+ wasLoaded: true
2018
+ };
2019
+ continue;
2020
+ }
2021
+ if (queryState.items.includes(itemKey))
2022
+ continue;
2023
+ if (invalidateQueries)
2024
+ queriesToInvalidate.push(payload);
2025
+ if (appendNewTo === "end") {
2026
+ queryState.items.push(itemKey);
2027
+ } else {
2028
+ queryState.items.unshift(itemKey);
2029
+ }
2030
+ } else {
2031
+ if (!queryState)
2032
+ continue;
2033
+ const itemIndex = queryState.items.indexOf(itemKey);
2034
+ if (itemIndex !== -1) {
2035
+ if (invalidateQueries)
2036
+ queriesToInvalidate.push(queryState.payload);
2037
+ queryState.items.splice(itemIndex, 1);
2038
+ }
2039
+ }
2040
+ }
2041
+ if (sort) {
2042
+ if (!queryState)
2043
+ continue;
2044
+ const queryHasItem = queryState.items.includes(itemKey);
2045
+ if (!queryHasItem)
2046
+ continue;
2047
+ queryState.items = sortBy(
2048
+ queryState.items,
2049
+ (itemId) => {
2050
+ const itemState = store.state.items[itemId];
2051
+ const itemPayload = store.state.itemQueries[itemId]?.payload;
2052
+ if (!itemState || !itemPayload)
2053
+ return Infinity;
2054
+ return sort.sortBy(itemState, itemPayload);
2055
+ },
2056
+ {
2057
+ order: sort.order
2058
+ }
2059
+ );
2060
+ }
2061
+ }
2062
+ }
2063
+ }
2064
+ });
2065
+ if (queriesToInvalidate.length)
2066
+ invalidateQuery(queriesToInvalidate);
2067
+ }
1924
2068
  function addItemToState(itemPayload, data, {
1925
2069
  addItemToQueries
1926
2070
  } = {}) {
1927
2071
  const itemKey = getItemKey(itemPayload);
1928
- store.produceState(
1929
- (draftState) => {
1930
- draftState.items[itemKey] = data;
1931
- draftState.itemQueries[itemKey] = {
1932
- status: "success",
1933
- wasLoaded: true,
1934
- refetchOnMount: false,
1935
- error: null,
1936
- payload: (0, import_json2.klona)(itemPayload)
1937
- };
1938
- if (addItemToQueries) {
1939
- const queries = getQueriesKeyArray(addItemToQueries.queries);
1940
- for (const { key } of queries) {
1941
- if (draftState.queries[key]) {
1942
- const queryState = draftState.queries[key];
1943
- if (!queryState)
1944
- continue;
1945
- if (queryState.items.includes(itemKey))
1946
- continue;
1947
- if (addItemToQueries.appendTo === "start") {
1948
- queryState.items.unshift(itemKey);
1949
- } else if (addItemToQueries.appendTo === "end") {
1950
- queryState.items.push(itemKey);
1951
- } else {
1952
- const index = addItemToQueries.appendTo(
1953
- filterAndMap(
1954
- queryState.items,
1955
- (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
1956
- )
1957
- );
1958
- queryState.items.splice(index, 0, itemKey);
2072
+ store.batch(() => {
2073
+ store.produceState(
2074
+ (draftState) => {
2075
+ draftState.items[itemKey] = data;
2076
+ draftState.itemQueries[itemKey] = {
2077
+ status: "success",
2078
+ wasLoaded: true,
2079
+ refetchOnMount: false,
2080
+ error: null,
2081
+ payload: (0, import_json2.klona)(itemPayload)
2082
+ };
2083
+ if (addItemToQueries) {
2084
+ const queries = getQueriesKeyArray(addItemToQueries.queries);
2085
+ for (const { key } of queries) {
2086
+ if (draftState.queries[key]) {
2087
+ const queryState = draftState.queries[key];
2088
+ if (!queryState)
2089
+ continue;
2090
+ if (queryState.items.includes(itemKey))
2091
+ continue;
2092
+ if (addItemToQueries.appendTo === "start") {
2093
+ queryState.items.unshift(itemKey);
2094
+ } else if (addItemToQueries.appendTo === "end") {
2095
+ queryState.items.push(itemKey);
2096
+ } else {
2097
+ const index = addItemToQueries.appendTo(
2098
+ filterAndMap(
2099
+ queryState.items,
2100
+ (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
2101
+ )
2102
+ );
2103
+ queryState.items.splice(index, 0, itemKey);
2104
+ }
1959
2105
  }
1960
2106
  }
1961
2107
  }
1962
- }
1963
- },
1964
- { action: { type: "create-item-state", itemPayload } }
1965
- );
2108
+ },
2109
+ { action: { type: "create-item-state", itemPayload } }
2110
+ );
2111
+ applyOptimisticListUpdates([itemKey]);
2112
+ });
1966
2113
  }
1967
2114
  function deleteItemState(itemId) {
1968
2115
  const itemsId = getItemsKeyArray(itemId);