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.js CHANGED
@@ -367,21 +367,25 @@ import { useOnEvtmitterEvent as useOnEvtmitterEvent2 } from "evtmitter/react";
367
367
  function newTSDFDocumentStore({
368
368
  debugName,
369
369
  fetchFn,
370
- initialData,
370
+ getInitialData,
371
371
  disableRefetchOnMount: globalDisableRefetchOnMount,
372
372
  lowPriorityThrottleMs,
373
373
  mediumPriorityThrottleMs,
374
374
  dynamicRealtimeThrottleMs,
375
375
  disableInitialDataInvalidation,
376
- errorNormalizer
376
+ errorNormalizer,
377
+ onInvalidate
377
378
  }) {
378
379
  const store = new Store({
379
380
  debugName,
380
- state: {
381
- data: initialData ?? null,
382
- error: null,
383
- status: initialData ? "success" : "idle",
384
- refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
381
+ state: () => {
382
+ const initialData = getInitialData?.();
383
+ return {
384
+ data: initialData ?? null,
385
+ error: null,
386
+ status: initialData ? "success" : "idle",
387
+ refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
388
+ };
385
389
  }
386
390
  });
387
391
  async function fetch(fetchCtx, _) {
@@ -452,6 +456,7 @@ function newTSDFDocumentStore({
452
456
  store.setKey("refetchOnMount", priority, { action: "invalidate-data" });
453
457
  invalidationWasTriggered = false;
454
458
  storeEvents.emit("invalidateData", priority);
459
+ onInvalidate?.(priority);
455
460
  }
456
461
  function useDocument({
457
462
  selector,
@@ -620,28 +625,32 @@ function newTSDFCollectionStore({
620
625
  errorNormalizer,
621
626
  disableInitialDataInvalidation,
622
627
  mediumPriorityThrottleMs,
623
- initialStateItems,
628
+ getInitialData,
624
629
  disableRefetchOnMount: globalDisableRefetchOnMount,
625
630
  dynamicRealtimeThrottleMs,
626
- getCollectionItemKey: filterCollectionItemObjKey
631
+ getCollectionItemKey: filterCollectionItemObjKey,
632
+ onInvalidate
627
633
  }) {
628
- const initialState = {};
629
- if (initialStateItems) {
630
- for (const item of initialStateItems) {
631
- const itemKey = getItemKey(item.payload);
632
- initialState[itemKey] = {
633
- data: item.data,
634
- error: null,
635
- status: "success",
636
- payload: item.payload,
637
- refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
638
- wasLoaded: true
639
- };
640
- }
641
- }
642
634
  const store = new Store2({
643
635
  debugName,
644
- state: initialState
636
+ state: () => {
637
+ const initialState = {};
638
+ const initialStateItems = getInitialData?.();
639
+ if (initialStateItems) {
640
+ for (const item of initialStateItems) {
641
+ const itemKey = getItemKey(item.payload);
642
+ initialState[itemKey] = {
643
+ data: item.data,
644
+ error: null,
645
+ status: "success",
646
+ payload: item.payload,
647
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
648
+ wasLoaded: true
649
+ };
650
+ }
651
+ }
652
+ return initialState;
653
+ }
645
654
  });
646
655
  function getItemKey(params) {
647
656
  return getCacheId(
@@ -780,6 +789,13 @@ function newTSDFCollectionStore({
780
789
  );
781
790
  invalidationWasTriggered.delete(itemKey);
782
791
  storeEvents.emit("invalidateData", { priority, itemKey });
792
+ if (item.data) {
793
+ onInvalidate?.({
794
+ priority,
795
+ payload: item.payload,
796
+ itemState: item.data
797
+ });
798
+ }
783
799
  }
784
800
  }
785
801
  function getItemState(params) {
@@ -888,7 +904,7 @@ function newTSDFCollectionStore({
888
904
  const shouldFetch = !itemState?.wasLoaded || itemState.refetchOnMount;
889
905
  if (shouldFetch) {
890
906
  scheduleFetch(fetchType, payload);
891
- return;
907
+ continue;
892
908
  }
893
909
  } else {
894
910
  scheduleFetch(fetchType, payload);
@@ -1044,6 +1060,31 @@ function findAndMap(arr, findFn) {
1044
1060
  return void 0;
1045
1061
  }
1046
1062
 
1063
+ // src/utils/sortBy.ts
1064
+ function sortBy(arr, getPriority, { order = "asc" } = {}) {
1065
+ return [...arr].sort((a, b) => {
1066
+ const _aPriority = getPriority(a);
1067
+ const _bPriority = getPriority(b);
1068
+ const aPriority = Array.isArray(_aPriority) ? _aPriority : [_aPriority];
1069
+ const bPriority = Array.isArray(_bPriority) ? _bPriority : [_bPriority];
1070
+ for (let i = 0; i < aPriority.length; i++) {
1071
+ const levelOrder = typeof order === "string" ? order : order[i] ?? "asc";
1072
+ const aP = aPriority[i] ?? 0;
1073
+ const bP = bPriority[i] ?? 0;
1074
+ if (aP === bP) {
1075
+ continue;
1076
+ }
1077
+ if (bP === Infinity || aP === -Infinity || aP < bP) {
1078
+ return levelOrder === "asc" ? -1 : 1;
1079
+ }
1080
+ if (aP === Infinity || bP === -Infinity || aP > bP) {
1081
+ return levelOrder === "asc" ? 1 : -1;
1082
+ }
1083
+ }
1084
+ return 0;
1085
+ });
1086
+ }
1087
+
1047
1088
  // src/listQueryStore.ts
1048
1089
  var noFetchFnError = "No fetchItemFn was provided";
1049
1090
  function newTSDFListQueryStore({
@@ -1052,43 +1093,49 @@ function newTSDFListQueryStore({
1052
1093
  fetchItemFn,
1053
1094
  errorNormalizer,
1054
1095
  defaultQuerySize = 50,
1055
- initialData,
1096
+ getInitialData,
1056
1097
  disableRefetchOnMount: globalDisableRefetchOnMount,
1057
1098
  lowPriorityThrottleMs,
1058
1099
  disableInitialDataInvalidation,
1059
1100
  mediumPriorityThrottleMs,
1060
1101
  dynamicRealtimeThrottleMs,
1061
- syncMutationsAndInvalidations
1102
+ syncMutationsAndInvalidations,
1103
+ optimisticListUpdates,
1104
+ onInvalidateItem,
1105
+ onInvalidateQuery
1062
1106
  }) {
1063
- const initialState = { items: {}, queries: {}, itemQueries: {} };
1064
- if (initialData) {
1065
- for (const { payload, data } of initialData.items) {
1066
- const itemKey = getItemKey(payload);
1067
- initialState.items[itemKey] = data;
1068
- initialState.itemQueries[itemKey] = {
1069
- error: null,
1070
- status: "success",
1071
- refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1072
- wasLoaded: true,
1073
- payload
1074
- };
1075
- }
1076
- for (const { payload, items, hasMore } of initialData.queries) {
1077
- const queryKey = getQueryKey(payload);
1078
- initialState.queries[queryKey] = {
1079
- error: null,
1080
- status: "success",
1081
- refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1082
- wasLoaded: true,
1083
- payload,
1084
- items,
1085
- hasMore
1086
- };
1087
- }
1088
- }
1089
1107
  const store = new Store3({
1090
1108
  debugName,
1091
- state: initialState
1109
+ state: () => {
1110
+ const initialState = { items: {}, queries: {}, itemQueries: {} };
1111
+ const initialData = getInitialData?.();
1112
+ if (initialData) {
1113
+ for (const { payload, data } of initialData.items) {
1114
+ const itemKey = getItemKey(payload);
1115
+ initialState.items[itemKey] = data;
1116
+ initialState.itemQueries[itemKey] = {
1117
+ error: null,
1118
+ status: "success",
1119
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1120
+ wasLoaded: true,
1121
+ payload
1122
+ };
1123
+ }
1124
+ for (const { payload, items, hasMore } of initialData.queries) {
1125
+ const queryKey = getQueryKey(payload);
1126
+ initialState.queries[queryKey] = {
1127
+ error: null,
1128
+ status: "success",
1129
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1130
+ wasLoaded: true,
1131
+ payload,
1132
+ items,
1133
+ hasMore
1134
+ };
1135
+ }
1136
+ }
1137
+ return initialState;
1138
+ }
1092
1139
  });
1093
1140
  function getQueryKey(params) {
1094
1141
  return getCacheId(params);
@@ -1332,7 +1379,7 @@ function newTSDFListQueryStore({
1332
1379
  const queryInvalidationWasTriggered = /* @__PURE__ */ new Set();
1333
1380
  function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
1334
1381
  const queriesKey = getQueriesKeyArray(queryPayload);
1335
- for (const { key } of queriesKey) {
1382
+ for (const { key, payload } of queriesKey) {
1336
1383
  const queryState = store.state.queries[key];
1337
1384
  if (!queryState)
1338
1385
  continue;
@@ -1351,6 +1398,7 @@ function newTSDFListQueryStore({
1351
1398
  );
1352
1399
  queryInvalidationWasTriggered.delete(key);
1353
1400
  storeEvents.emit("invalidateQuery", { priority, queryKey: key });
1401
+ onInvalidateQuery?.(payload, priority);
1354
1402
  }
1355
1403
  if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
1356
1404
  invalidateItem(
@@ -1470,7 +1518,7 @@ function newTSDFListQueryStore({
1470
1518
  const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
1471
1519
  if (shouldFetch) {
1472
1520
  scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
1473
- return;
1521
+ continue;
1474
1522
  }
1475
1523
  } else {
1476
1524
  scheduleListQueryFetch(fetchType, fetchParams, loadSizeConst);
@@ -1634,7 +1682,7 @@ function newTSDFListQueryStore({
1634
1682
  throw new Error(noFetchFnError);
1635
1683
  }
1636
1684
  const itemsKey = getItemsKeyArray(itemId);
1637
- for (const { itemKey } of itemsKey) {
1685
+ for (const { itemKey, payload } of itemsKey) {
1638
1686
  const item = store.state.itemQueries[itemKey];
1639
1687
  if (!item)
1640
1688
  continue;
@@ -1653,6 +1701,16 @@ function newTSDFListQueryStore({
1653
1701
  );
1654
1702
  itemInvalidationWasTriggered.delete(itemKey);
1655
1703
  storeEvents.emit("invalidateItem", { priority, itemKey });
1704
+ if (onInvalidateItem) {
1705
+ const itemState = store.state.items[itemKey];
1706
+ if (itemState) {
1707
+ onInvalidateItem({
1708
+ priority,
1709
+ itemState,
1710
+ payload
1711
+ });
1712
+ }
1713
+ }
1656
1714
  }
1657
1715
  if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
1658
1716
  invalidateQuery(
@@ -1885,6 +1943,9 @@ function newTSDFListQueryStore({
1885
1943
  }
1886
1944
  }
1887
1945
  });
1946
+ if (someItemWasUpdated) {
1947
+ applyOptimisticListUpdates(itemKeys.map((i) => i.itemKey));
1948
+ }
1888
1949
  if (ifNothingWasUpdated && !someItemWasUpdated) {
1889
1950
  ifNothingWasUpdated();
1890
1951
  }
@@ -1893,48 +1954,134 @@ function newTSDFListQueryStore({
1893
1954
  );
1894
1955
  return someItemWasUpdated;
1895
1956
  }
1957
+ function applyOptimisticListUpdates(itemKeys) {
1958
+ if (!optimisticListUpdates)
1959
+ return;
1960
+ const queriesToInvalidate = [];
1961
+ store.produceState((draftState) => {
1962
+ for (const itemKey of itemKeys) {
1963
+ const item = draftState.items[itemKey];
1964
+ if (!item)
1965
+ continue;
1966
+ for (const {
1967
+ queries,
1968
+ filterItem,
1969
+ appendNewTo = "end",
1970
+ invalidateQueries,
1971
+ sort
1972
+ } of optimisticListUpdates) {
1973
+ const relatedFilterQueries = getQueriesKeyArray(queries);
1974
+ for (const { key: queryKey, payload } of relatedFilterQueries) {
1975
+ const queryState = draftState.queries[queryKey];
1976
+ if (filterItem) {
1977
+ const itemShouldBeIncluded = filterItem(item);
1978
+ if (itemShouldBeIncluded === null)
1979
+ continue;
1980
+ if (itemShouldBeIncluded) {
1981
+ if (!queryState) {
1982
+ draftState.queries[queryKey] = {
1983
+ status: "success",
1984
+ items: [itemKey],
1985
+ error: null,
1986
+ hasMore: false,
1987
+ payload,
1988
+ refetchOnMount: "lowPriority",
1989
+ wasLoaded: true
1990
+ };
1991
+ continue;
1992
+ }
1993
+ if (queryState.items.includes(itemKey))
1994
+ continue;
1995
+ if (invalidateQueries)
1996
+ queriesToInvalidate.push(payload);
1997
+ if (appendNewTo === "end") {
1998
+ queryState.items.push(itemKey);
1999
+ } else {
2000
+ queryState.items.unshift(itemKey);
2001
+ }
2002
+ } else {
2003
+ if (!queryState)
2004
+ continue;
2005
+ const itemIndex = queryState.items.indexOf(itemKey);
2006
+ if (itemIndex !== -1) {
2007
+ if (invalidateQueries)
2008
+ queriesToInvalidate.push(queryState.payload);
2009
+ queryState.items.splice(itemIndex, 1);
2010
+ }
2011
+ }
2012
+ }
2013
+ if (sort) {
2014
+ if (!queryState)
2015
+ continue;
2016
+ const queryHasItem = queryState.items.includes(itemKey);
2017
+ if (!queryHasItem)
2018
+ continue;
2019
+ queryState.items = sortBy(
2020
+ queryState.items,
2021
+ (itemId) => {
2022
+ const itemState = store.state.items[itemId];
2023
+ const itemPayload = store.state.itemQueries[itemId]?.payload;
2024
+ if (!itemState || !itemPayload)
2025
+ return Infinity;
2026
+ return sort.sortBy(itemState, itemPayload);
2027
+ },
2028
+ {
2029
+ order: sort.order
2030
+ }
2031
+ );
2032
+ }
2033
+ }
2034
+ }
2035
+ }
2036
+ });
2037
+ if (queriesToInvalidate.length)
2038
+ invalidateQuery(queriesToInvalidate);
2039
+ }
1896
2040
  function addItemToState(itemPayload, data, {
1897
2041
  addItemToQueries
1898
2042
  } = {}) {
1899
2043
  const itemKey = getItemKey(itemPayload);
1900
- store.produceState(
1901
- (draftState) => {
1902
- draftState.items[itemKey] = data;
1903
- draftState.itemQueries[itemKey] = {
1904
- status: "success",
1905
- wasLoaded: true,
1906
- refetchOnMount: false,
1907
- error: null,
1908
- payload: klona2(itemPayload)
1909
- };
1910
- if (addItemToQueries) {
1911
- const queries = getQueriesKeyArray(addItemToQueries.queries);
1912
- for (const { key } of queries) {
1913
- if (draftState.queries[key]) {
1914
- const queryState = draftState.queries[key];
1915
- if (!queryState)
1916
- continue;
1917
- if (queryState.items.includes(itemKey))
1918
- continue;
1919
- if (addItemToQueries.appendTo === "start") {
1920
- queryState.items.unshift(itemKey);
1921
- } else if (addItemToQueries.appendTo === "end") {
1922
- queryState.items.push(itemKey);
1923
- } else {
1924
- const index = addItemToQueries.appendTo(
1925
- filterAndMap(
1926
- queryState.items,
1927
- (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
1928
- )
1929
- );
1930
- queryState.items.splice(index, 0, itemKey);
2044
+ store.batch(() => {
2045
+ store.produceState(
2046
+ (draftState) => {
2047
+ draftState.items[itemKey] = data;
2048
+ draftState.itemQueries[itemKey] = {
2049
+ status: "success",
2050
+ wasLoaded: true,
2051
+ refetchOnMount: false,
2052
+ error: null,
2053
+ payload: klona2(itemPayload)
2054
+ };
2055
+ if (addItemToQueries) {
2056
+ const queries = getQueriesKeyArray(addItemToQueries.queries);
2057
+ for (const { key } of queries) {
2058
+ if (draftState.queries[key]) {
2059
+ const queryState = draftState.queries[key];
2060
+ if (!queryState)
2061
+ continue;
2062
+ if (queryState.items.includes(itemKey))
2063
+ continue;
2064
+ if (addItemToQueries.appendTo === "start") {
2065
+ queryState.items.unshift(itemKey);
2066
+ } else if (addItemToQueries.appendTo === "end") {
2067
+ queryState.items.push(itemKey);
2068
+ } else {
2069
+ const index = addItemToQueries.appendTo(
2070
+ filterAndMap(
2071
+ queryState.items,
2072
+ (itemKey2, ignore) => draftState.itemQueries[itemKey2]?.payload || ignore
2073
+ )
2074
+ );
2075
+ queryState.items.splice(index, 0, itemKey);
2076
+ }
1931
2077
  }
1932
2078
  }
1933
2079
  }
1934
- }
1935
- },
1936
- { action: { type: "create-item-state", itemPayload } }
1937
- );
2080
+ },
2081
+ { action: { type: "create-item-state", itemPayload } }
2082
+ );
2083
+ applyOptimisticListUpdates([itemKey]);
2084
+ });
1938
2085
  }
1939
2086
  function deleteItemState(itemId) {
1940
2087
  const itemsId = getItemsKeyArray(itemId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsdf",
3
- "version": "11.2.0",
3
+ "version": "12.2.0",
4
4
  "description": "A data fetching solution based on t-state",
5
5
  "license": "MIT",
6
6
  "author": "Lucas Santos",
@@ -22,55 +22,55 @@
22
22
  "engines": {
23
23
  "node": ">=16.0.0"
24
24
  },
25
- "scripts": {
26
- "test": "vitest --ui --mode development",
27
- "test:changed": "vitest --changed --ui --mode development",
28
- "lint": "pnpm tsc && pnpm eslint && pnpm format",
29
- "eslint": "CI=true eslint --color --ext .jsx,.js,.ts,.tsx src/ test/",
30
- "format": "prettier --write src/**/*.ts",
31
- "tsc": "tsc -p tsconfig.prod.json",
32
- "tsc:watch": "tsc -p tsconfig.prod.json --watch",
33
- "test:run": "vitest run",
34
- "build": "pnpm test:run && pnpm lint && pnpm build:no-test",
35
- "build:no-test": "tsup --dts",
36
- "npm-publish": "./scripts/check-if-is-sync.sh && pnpm build && npm publish"
37
- },
38
25
  "devDependencies": {
39
- "@lucasols/eslint-plugin-extended-lint": "^0.1.1",
40
- "@testing-library/jest-dom": "^5.16.5",
41
- "@testing-library/react": "^13.4.0",
42
- "@types/node": "^18.11.18",
26
+ "@lucasols/eslint-plugin-extended-lint": "^0.10.0",
27
+ "@testing-library/jest-dom": "^5.17.0",
28
+ "@testing-library/react": "^14.0.0",
29
+ "@types/node": "^20.4.5",
43
30
  "@types/react": "^18.0.26",
44
31
  "@types/react-dom": "^18.0.10",
45
- "@types/testing-library__jest-dom": "^5.14.5",
46
- "@typescript-eslint/eslint-plugin": "^5.48.1",
47
- "@typescript-eslint/parser": "^5.48.1",
48
- "@vitest/ui": "0.27.1",
49
- "eslint": "^8.31.0",
32
+ "@types/testing-library__jest-dom": "^5.14.9",
33
+ "@typescript-eslint/eslint-plugin": "^6.2.0",
34
+ "@typescript-eslint/parser": "^6.2.0",
35
+ "@vitest/ui": "0.33.0",
36
+ "eslint": "^8.46.0",
50
37
  "eslint-plugin-local-rules": "^1.3.2",
51
38
  "eslint-plugin-react-hooks": "^4.6.0",
52
- "eslint-plugin-vitest": "^0.0.29",
53
- "happy-dom": "^8.1.4",
54
- "jsdom": "^21.0.0",
55
- "prettier": "^2.8.2",
56
- "pretty-format": "^29.3.1",
39
+ "eslint-plugin-vitest": "^0.2.8",
40
+ "happy-dom": "^10.5.2",
41
+ "jsdom": "^22.1.0",
42
+ "prettier": "^3.0.0",
43
+ "pretty-format": "^29.6.2",
57
44
  "react": "^18.2.0",
58
45
  "react-dom": "^18.2.0",
59
46
  "tiny-glob": "^0.2.9",
60
47
  "tsm": "^2.3.0",
61
- "tsup": "^6.7.0",
62
- "typescript": "^5.0.4",
63
- "vite": "^4.2.1",
64
- "vitest": "^0.30.1"
48
+ "tsup": "^7.1.0",
49
+ "typescript": "^5.1.6",
50
+ "vite": "^4.4.7",
51
+ "vitest": "^0.33.0"
65
52
  },
66
53
  "dependencies": {
67
- "evtmitter": "^0.3.1",
68
- "immer": "^9.0.17",
54
+ "evtmitter": "^0.3.3",
55
+ "immer": "^10.0.2",
69
56
  "klona": "^2.0.6",
70
- "t-state": "^8.3.0"
57
+ "t-state": "^8.8.0"
71
58
  },
72
59
  "peerDependencies": {
73
60
  "react": "^18.0.0",
74
61
  "react-dom": "^18.0.0"
62
+ },
63
+ "scripts": {
64
+ "test": "vitest --ui --mode development",
65
+ "test:changed": "vitest --changed --ui --mode development",
66
+ "lint": "pnpm tsc && pnpm eslint && pnpm format",
67
+ "eslint": "CI=true eslint --color --ext .jsx,.js,.ts,.tsx src/ test/",
68
+ "format": "prettier --write src/**/*.ts",
69
+ "tsc": "tsc -p tsconfig.prod.json",
70
+ "tsc:watch": "tsc -p tsconfig.prod.json --watch",
71
+ "test:run": "vitest run",
72
+ "build": "pnpm test:run && pnpm lint && pnpm build:no-test",
73
+ "build:no-test": "tsup --dts",
74
+ "pre-publish": "./scripts/check-if-is-sync.sh && pnpm build"
75
75
  }
76
- }
76
+ }