tsdf 12.4.0 → 12.6.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
@@ -22,7 +22,8 @@ var main_exports = {};
22
22
  __export(main_exports, {
23
23
  newTSDFCollectionStore: () => newTSDFCollectionStore,
24
24
  newTSDFDocumentStore: () => newTSDFDocumentStore,
25
- newTSDFListQueryStore: () => newTSDFListQueryStore
25
+ newTSDFListQueryStore: () => newTSDFListQueryStore,
26
+ useListItemIsLoading: () => useListItemIsLoading
26
27
  });
27
28
  module.exports = __toCommonJS(main_exports);
28
29
 
@@ -312,6 +313,14 @@ function useDeepCompareMemoize(value, equalityFn = import_t_state.deepEqual) {
312
313
  function useDeepMemo(callback, deeps) {
313
314
  return (0, import_react.useMemo)(callback, useDeepCompareMemoize(deeps));
314
315
  }
316
+ function useLatestValue(value) {
317
+ const ref = (0, import_react.useRef)({ insideEffect: value, insideMemo: value });
318
+ ref.current.insideMemo = value;
319
+ (0, import_react.useLayoutEffect)(() => {
320
+ ref.current.insideEffect = value;
321
+ });
322
+ return ref.current;
323
+ }
315
324
  function usePrevious(value, initial) {
316
325
  const ref = (0, import_react.useRef)(initial);
317
326
  (0, import_react.useEffect)(() => {
@@ -2164,9 +2173,99 @@ function newTSDFListQueryStore({
2164
2173
  useFindItem
2165
2174
  };
2166
2175
  }
2176
+
2177
+ // src/useListItemIsLoading.ts
2178
+ var import_react11 = require("react");
2179
+
2180
+ // src/utils/useTimeout.ts
2181
+ var import_react10 = require("react");
2182
+ function useTimeout(ms) {
2183
+ const timeoutsRef = useConst(() => /* @__PURE__ */ new Set());
2184
+ const callback = (0, import_react10.useCallback)(
2185
+ (cb, overrideMs = ms) => {
2186
+ timeoutsRef.add(window.setTimeout(cb, overrideMs));
2187
+ },
2188
+ [ms, timeoutsRef]
2189
+ );
2190
+ const clear = (0, import_react10.useCallback)(() => {
2191
+ for (const timeout of timeoutsRef) {
2192
+ clearTimeout(timeout);
2193
+ }
2194
+ }, [timeoutsRef]);
2195
+ (0, import_react10.useEffect)(() => clear, [clear]);
2196
+ return (0, import_react10.useMemo)(() => {
2197
+ return {
2198
+ call: callback,
2199
+ clear
2200
+ };
2201
+ }, [callback, clear]);
2202
+ }
2203
+
2204
+ // src/useListItemIsLoading.ts
2205
+ function useListItemIsLoading({
2206
+ isLoading,
2207
+ isRefetching,
2208
+ isNotFound,
2209
+ loadItemFallback,
2210
+ itemId
2211
+ }) {
2212
+ const [willBeRefetched, setWillBeRefetched] = (0, import_react11.useState)(isNotFound);
2213
+ const resetWillBeRefechedTimeout = useTimeout(1e3);
2214
+ const callFallbackLoadItemTimeout = useTimeout(100);
2215
+ const ignoreSetWillBeRefetched = (0, import_react11.useRef)(false);
2216
+ const itemWasRefetched = useConst(() => /* @__PURE__ */ new Set());
2217
+ if (isNotFound && !isRefetching && !willBeRefetched && !ignoreSetWillBeRefetched.current && !itemWasRefetched.has(itemId)) {
2218
+ setWillBeRefetched(true);
2219
+ }
2220
+ const latestIsLoadingOrRefetching = useLatestValue(isLoading || isRefetching);
2221
+ const latestLoadItemFallback = useLatestValue(loadItemFallback);
2222
+ (0, import_react11.useEffect)(() => {
2223
+ if (isRefetching) {
2224
+ itemWasRefetched.add(itemId);
2225
+ resetWillBeRefechedTimeout.clear();
2226
+ callFallbackLoadItemTimeout.clear();
2227
+ setWillBeRefetched(false);
2228
+ }
2229
+ }, [
2230
+ callFallbackLoadItemTimeout,
2231
+ isRefetching,
2232
+ itemId,
2233
+ itemWasRefetched,
2234
+ resetWillBeRefechedTimeout
2235
+ ]);
2236
+ (0, import_react11.useEffect)(() => {
2237
+ if (willBeRefetched) {
2238
+ callFallbackLoadItemTimeout.call(() => {
2239
+ if (!latestIsLoadingOrRefetching.insideEffect) {
2240
+ latestLoadItemFallback.insideEffect();
2241
+ return;
2242
+ }
2243
+ });
2244
+ resetWillBeRefechedTimeout.call(() => {
2245
+ ignoreSetWillBeRefetched.current = true;
2246
+ setWillBeRefetched(false);
2247
+ setTimeout(() => {
2248
+ ignoreSetWillBeRefetched.current = false;
2249
+ }, 20);
2250
+ });
2251
+ }
2252
+ return () => {
2253
+ resetWillBeRefechedTimeout.clear();
2254
+ callFallbackLoadItemTimeout.clear();
2255
+ };
2256
+ }, [
2257
+ resetWillBeRefechedTimeout,
2258
+ willBeRefetched,
2259
+ latestIsLoadingOrRefetching,
2260
+ latestLoadItemFallback,
2261
+ callFallbackLoadItemTimeout
2262
+ ]);
2263
+ return isLoading || isNotFound && (isRefetching || !!willBeRefetched);
2264
+ }
2167
2265
  // Annotate the CommonJS export names for ESM import in node:
2168
2266
  0 && (module.exports = {
2169
2267
  newTSDFCollectionStore,
2170
2268
  newTSDFDocumentStore,
2171
- newTSDFListQueryStore
2269
+ newTSDFListQueryStore,
2270
+ useListItemIsLoading
2172
2271
  });
package/dist/main.d.cts CHANGED
@@ -333,4 +333,14 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
333
333
  };
334
334
  type TSFDListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload, ItemPayload>>;
335
335
 
336
- export { CollectionInitialStateItem, FetchListFnReturn, FetchListFnReturnItem, ListQueryStoreInitialData, OnCollectionItemInvalidate, OnDocumentInvalidate, OnListQueryInvalidate, OnListQueryItemInvalidate, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn, TSFDUseListItemReturn, TSFDUseListQueryReturn, ValidPayload, ValidStoreState, newTSDFCollectionStore, newTSDFDocumentStore, newTSDFListQueryStore };
336
+ /** Use to detect if a item of a list may be loading in cases which there is no
337
+ * clear indication of the status of individual items, ex: an item in a document collection that is loaded on demand */
338
+ declare function useListItemIsLoading({ isLoading, isRefetching, isNotFound, loadItemFallback, itemId, }: {
339
+ itemId: string;
340
+ isRefetching: boolean;
341
+ isLoading: boolean;
342
+ isNotFound: boolean;
343
+ loadItemFallback: () => void;
344
+ }): boolean;
345
+
346
+ export { CollectionInitialStateItem, FetchListFnReturn, FetchListFnReturnItem, ListQueryStoreInitialData, OnCollectionItemInvalidate, OnDocumentInvalidate, OnListQueryInvalidate, OnListQueryItemInvalidate, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn, TSFDUseListItemReturn, TSFDUseListQueryReturn, ValidPayload, ValidStoreState, newTSDFCollectionStore, newTSDFDocumentStore, newTSDFListQueryStore, useListItemIsLoading };
package/dist/main.d.ts CHANGED
@@ -333,4 +333,14 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
333
333
  };
334
334
  type TSFDListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload, ItemPayload>>;
335
335
 
336
- export { CollectionInitialStateItem, FetchListFnReturn, FetchListFnReturnItem, ListQueryStoreInitialData, OnCollectionItemInvalidate, OnDocumentInvalidate, OnListQueryInvalidate, OnListQueryItemInvalidate, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn, TSFDUseListItemReturn, TSFDUseListQueryReturn, ValidPayload, ValidStoreState, newTSDFCollectionStore, newTSDFDocumentStore, newTSDFListQueryStore };
336
+ /** Use to detect if a item of a list may be loading in cases which there is no
337
+ * clear indication of the status of individual items, ex: an item in a document collection that is loaded on demand */
338
+ declare function useListItemIsLoading({ isLoading, isRefetching, isNotFound, loadItemFallback, itemId, }: {
339
+ itemId: string;
340
+ isRefetching: boolean;
341
+ isLoading: boolean;
342
+ isNotFound: boolean;
343
+ loadItemFallback: () => void;
344
+ }): boolean;
345
+
346
+ export { CollectionInitialStateItem, FetchListFnReturn, FetchListFnReturnItem, ListQueryStoreInitialData, OnCollectionItemInvalidate, OnDocumentInvalidate, OnListQueryInvalidate, OnListQueryItemInvalidate, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn, TSFDUseListItemReturn, TSFDUseListQueryReturn, ValidPayload, ValidStoreState, newTSDFCollectionStore, newTSDFDocumentStore, newTSDFListQueryStore, useListItemIsLoading };
package/dist/main.js CHANGED
@@ -284,6 +284,14 @@ function useDeepCompareMemoize(value, equalityFn = deepEqual) {
284
284
  function useDeepMemo(callback, deeps) {
285
285
  return useMemo(callback, useDeepCompareMemoize(deeps));
286
286
  }
287
+ function useLatestValue(value) {
288
+ const ref = useRef({ insideEffect: value, insideMemo: value });
289
+ ref.current.insideMemo = value;
290
+ useLayoutEffect(() => {
291
+ ref.current.insideEffect = value;
292
+ });
293
+ return ref.current;
294
+ }
287
295
  function usePrevious(value, initial) {
288
296
  const ref = useRef(initial);
289
297
  useEffect(() => {
@@ -2136,8 +2144,98 @@ function newTSDFListQueryStore({
2136
2144
  useFindItem
2137
2145
  };
2138
2146
  }
2147
+
2148
+ // src/useListItemIsLoading.ts
2149
+ import { useEffect as useEffect6, useRef as useRef2, useState as useState2 } from "react";
2150
+
2151
+ // src/utils/useTimeout.ts
2152
+ import { useCallback as useCallback4, useEffect as useEffect5, useMemo as useMemo5 } from "react";
2153
+ function useTimeout(ms) {
2154
+ const timeoutsRef = useConst(() => /* @__PURE__ */ new Set());
2155
+ const callback = useCallback4(
2156
+ (cb, overrideMs = ms) => {
2157
+ timeoutsRef.add(window.setTimeout(cb, overrideMs));
2158
+ },
2159
+ [ms, timeoutsRef]
2160
+ );
2161
+ const clear = useCallback4(() => {
2162
+ for (const timeout of timeoutsRef) {
2163
+ clearTimeout(timeout);
2164
+ }
2165
+ }, [timeoutsRef]);
2166
+ useEffect5(() => clear, [clear]);
2167
+ return useMemo5(() => {
2168
+ return {
2169
+ call: callback,
2170
+ clear
2171
+ };
2172
+ }, [callback, clear]);
2173
+ }
2174
+
2175
+ // src/useListItemIsLoading.ts
2176
+ function useListItemIsLoading({
2177
+ isLoading,
2178
+ isRefetching,
2179
+ isNotFound,
2180
+ loadItemFallback,
2181
+ itemId
2182
+ }) {
2183
+ const [willBeRefetched, setWillBeRefetched] = useState2(isNotFound);
2184
+ const resetWillBeRefechedTimeout = useTimeout(1e3);
2185
+ const callFallbackLoadItemTimeout = useTimeout(100);
2186
+ const ignoreSetWillBeRefetched = useRef2(false);
2187
+ const itemWasRefetched = useConst(() => /* @__PURE__ */ new Set());
2188
+ if (isNotFound && !isRefetching && !willBeRefetched && !ignoreSetWillBeRefetched.current && !itemWasRefetched.has(itemId)) {
2189
+ setWillBeRefetched(true);
2190
+ }
2191
+ const latestIsLoadingOrRefetching = useLatestValue(isLoading || isRefetching);
2192
+ const latestLoadItemFallback = useLatestValue(loadItemFallback);
2193
+ useEffect6(() => {
2194
+ if (isRefetching) {
2195
+ itemWasRefetched.add(itemId);
2196
+ resetWillBeRefechedTimeout.clear();
2197
+ callFallbackLoadItemTimeout.clear();
2198
+ setWillBeRefetched(false);
2199
+ }
2200
+ }, [
2201
+ callFallbackLoadItemTimeout,
2202
+ isRefetching,
2203
+ itemId,
2204
+ itemWasRefetched,
2205
+ resetWillBeRefechedTimeout
2206
+ ]);
2207
+ useEffect6(() => {
2208
+ if (willBeRefetched) {
2209
+ callFallbackLoadItemTimeout.call(() => {
2210
+ if (!latestIsLoadingOrRefetching.insideEffect) {
2211
+ latestLoadItemFallback.insideEffect();
2212
+ return;
2213
+ }
2214
+ });
2215
+ resetWillBeRefechedTimeout.call(() => {
2216
+ ignoreSetWillBeRefetched.current = true;
2217
+ setWillBeRefetched(false);
2218
+ setTimeout(() => {
2219
+ ignoreSetWillBeRefetched.current = false;
2220
+ }, 20);
2221
+ });
2222
+ }
2223
+ return () => {
2224
+ resetWillBeRefechedTimeout.clear();
2225
+ callFallbackLoadItemTimeout.clear();
2226
+ };
2227
+ }, [
2228
+ resetWillBeRefechedTimeout,
2229
+ willBeRefetched,
2230
+ latestIsLoadingOrRefetching,
2231
+ latestLoadItemFallback,
2232
+ callFallbackLoadItemTimeout
2233
+ ]);
2234
+ return isLoading || isNotFound && (isRefetching || !!willBeRefetched);
2235
+ }
2139
2236
  export {
2140
2237
  newTSDFCollectionStore,
2141
2238
  newTSDFDocumentStore,
2142
- newTSDFListQueryStore
2239
+ newTSDFListQueryStore,
2240
+ useListItemIsLoading
2143
2241
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsdf",
3
- "version": "12.4.0",
3
+ "version": "12.6.0",
4
4
  "description": "A data fetching solution based on t-state",
5
5
  "license": "MIT",
6
6
  "author": "Lucas Santos",
@@ -23,38 +23,38 @@
23
23
  "node": ">=16.0.0"
24
24
  },
25
25
  "devDependencies": {
26
- "@lucasols/eslint-plugin-extended-lint": "^0.10.0",
27
- "@testing-library/jest-dom": "^5.17.0",
26
+ "@lucasols/eslint-plugin-extended-lint": "^1.4.0",
27
+ "@testing-library/jest-dom": "^6.1.3",
28
28
  "@testing-library/react": "^14.0.0",
29
- "@types/node": "^20.4.5",
30
- "@types/react": "^18.0.26",
31
- "@types/react-dom": "^18.0.10",
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",
37
- "eslint-plugin-local-rules": "^1.3.2",
29
+ "@types/node": "^20.8.4",
30
+ "@types/react": "^18.2.28",
31
+ "@types/react-dom": "^18.2.13",
32
+ "@types/testing-library__jest-dom": "^6.0.0",
33
+ "@typescript-eslint/eslint-plugin": "^6.7.5",
34
+ "@typescript-eslint/parser": "^6.7.5",
35
+ "@vitest/ui": "1.0.0-beta.1",
36
+ "eslint": "^8.51.0",
37
+ "eslint-plugin-local-rules": "^2.0.0",
38
38
  "eslint-plugin-react-hooks": "^4.6.0",
39
- "eslint-plugin-vitest": "^0.2.8",
40
- "happy-dom": "^10.5.2",
39
+ "eslint-plugin-vitest": "^0.3.2",
40
+ "happy-dom": "^12.9.1",
41
41
  "jsdom": "^22.1.0",
42
- "prettier": "^3.0.0",
43
- "pretty-format": "^29.6.2",
42
+ "prettier": "^3.0.3",
43
+ "pretty-format": "^29.7.0",
44
44
  "react": "^18.2.0",
45
45
  "react-dom": "^18.2.0",
46
46
  "tiny-glob": "^0.2.9",
47
47
  "tsm": "^2.3.0",
48
- "tsup": "^7.1.0",
49
- "typescript": "^5.1.6",
50
- "vite": "^4.4.7",
51
- "vitest": "^0.33.0"
48
+ "tsup": "^7.2.0",
49
+ "typescript": "^5.2.2",
50
+ "vite": "^4.4.11",
51
+ "vitest": "1.0.0-beta.1"
52
52
  },
53
53
  "dependencies": {
54
54
  "evtmitter": "^0.3.3",
55
- "immer": "^10.0.2",
55
+ "immer": "^10.0.3",
56
56
  "klona": "^2.0.6",
57
- "t-state": "^8.9.0"
57
+ "t-state": "^8.10.0"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "react": "^18.0.0",