@nkzw/fate-indexeddb 1.10.2 → 1.11.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.
Files changed (2) hide show
  1. package/README.md +26 -36
  2. package/package.json +2 -5
package/README.md CHANGED
@@ -609,6 +609,7 @@ Lists with different filters or sorting arguments stay separate. Loading another
609
609
  - `cache-first` (_default_): Returns data from the cache if available, otherwise fetches from the network.
610
610
  - `stale-while-revalidate`: Returns data from the cache and simultaneously fetches fresh data from the network.
611
611
  - `network-only`: Always fetches data from the network, bypassing the cache.
612
+ - `cache-only` (React): Observes cached data without fetching or suspending. Returns `undefined` when the selection is missing.
612
613
 
613
614
  You can pass the request mode as an option to `useRequest`:
614
615
 
@@ -623,6 +624,18 @@ const { posts } = useRequest(
623
624
  );
624
625
  ```
625
626
 
627
+ For screens that also work offline, switch the same hook to `cache-only`:
628
+
629
+ ```tsx
630
+ const result = useRequest(
631
+ { viewer: { view: ViewerView } },
632
+ { mode: isOnline ? 'network-only' : 'cache-only' },
633
+ );
634
+ const viewer = useView(ViewerView, result?.viewer);
635
+ ```
636
+
637
+ Cache-only results stay subscribed to cache changes. Their references never fetch missing view fields. Switching offline bypasses a pending Suspense gate immediately, so the component can keep its local state and render its offline UI. The existing network modes keep their required result type; modes that can be `cache-only` return an optional result.
638
+
626
639
  ### Refreshing Requests
627
640
 
628
641
  To refresh posts already displayed by `useRequest`, call `useRefetch` with the same request:
@@ -667,7 +680,7 @@ try {
667
680
 
668
681
  Cancellation rejects with `signal.reason`, usually an `AbortError`. If callers share a request, cancelling one does not cancel the others.
669
682
 
670
- You can also use `timeout` and `signal` with `useRefetch`, `useRequestState`, and Vue requests. Keep the signal stable when passing it to a hook. These options apply to the request, including its background refresh, rather than later pagination or mutation calls.
683
+ You can also use `timeout` and `signal` with `useRefetch` and Vue requests. Keep the signal stable when passing it to a hook. These options apply to the request, including its background refresh, rather than later pagination or mutation calls.
671
684
 
672
685
  ### Cache Lifetime
673
686
 
@@ -748,18 +761,21 @@ Snapshots from different hydration scopes cannot be combined. Keep each user's s
748
761
 
749
762
  ### Observing Request State
750
763
 
751
- For a search box or a screen that handles loading and errors itself, use `useRequestState` instead of Suspense:
764
+ Outside React or Vue components, `fate.observeRequest(request, options)` provides request state through `getSnapshot()` and `subscribe(listener)`:
752
765
 
753
- ```tsx
754
- const state = useRequestState(request, { mode: 'stale-while-revalidate' });
766
+ ```ts
767
+ const observer = fate.observeRequest(request, { mode: 'stale-while-revalidate' });
768
+ const unsubscribe = observer.subscribe(() => {
769
+ console.log(observer.getSnapshot());
770
+ });
755
771
 
756
- // In a button's event handler:
757
- const refresh = () => state.refetch().catch(reportError);
772
+ // Stop observing when the consumer is disposed.
773
+ unsubscribe();
758
774
  ```
759
775
 
760
- The result contains `data`, `error`, `isFetching`, `status`, and `refetch`. Data contains the same refs as `useRequest`, so child components can read them with `useView`. During a refresh, cached data stays visible and `isFetching` is true. A failed refresh sets `error` and keeps the previous data.
776
+ The snapshot contains `data`, `error`, `isFetching`, `status`, and `refetch`. Data contains the same refs as `useRequest`. During a refresh, cached data stays available and `isFetching` is true. A failed refresh sets `error` and keeps the previous data.
761
777
 
762
- Use `enabled: false` to wait for input before fetching, or `mode: 'cache-only'` to read only cached data. The possible statuses are:
778
+ Reading a snapshot does not start a request. The first subscription starts an enabled request and retains its data; the last unsubscribe releases it. Use `enabled: false` to disable fetching, or `mode: 'cache-only'` to observe cached data without fetching missing fields. The possible statuses are:
763
779
 
764
780
  | Status | Meaning |
765
781
  | ---------- | -------------------------------------------------- |
@@ -769,7 +785,7 @@ Use `enabled: false` to wait for input before fetching, or `mode: 'cache-only'`
769
785
  | `ready` | Data is available. |
770
786
  | `error` | The initial request failed. |
771
787
 
772
- `refetch()` returns a promise and requires an enabled request that isn't cache-only. For Vue, see [Request State](/docs/guide/vue.md#request-state). Outside a component, `fate.observeRequest(request, options)` provides the same state through `getSnapshot()` and `subscribe(listener)`. Subscribing starts the request; unsubscribing releases it.
788
+ `observer.getSnapshot().refetch()` returns a promise and requires an enabled request that isn't cache-only. Handle its rejected promise in the calling code.
773
789
 
774
790
  ### Aliases
775
791
 
@@ -1861,33 +1877,7 @@ const [postItems, loadNext] = useListView(PostCardView, () => request.data.value
1861
1877
  </template>
1862
1878
  ```
1863
1879
 
1864
- Awaiting `ready()` in `<script setup>` participates in Vue Suspense. The getter passed to `useListView` keeps the list in sync when the request result changes. To render loading and error states yourself, use `useRequestState` instead.
1865
-
1866
- ### Request State
1867
-
1868
- Use `useRequestState` to render loading and error states without Suspense:
1869
-
1870
- ```vue
1871
- <script setup lang="ts">
1872
- import { useRequestState } from 'vue-fate';
1873
- import { UserView } from '../fateViews';
1874
- import UserCard from '../ui/UserCard.vue';
1875
-
1876
- const state = useRequestState({ viewer: { view: UserView } }, { mode: 'stale-while-revalidate' });
1877
- </script>
1878
-
1879
- <template>
1880
- <p v-if="state.isFetching">Loading…</p>
1881
- <p v-if="state.error">Could not load your profile.</p>
1882
- <UserCard v-if="state.data?.viewer" :user="state.data.viewer" />
1883
- </template>
1884
- ```
1885
-
1886
- The result is a ref: use `state.value` in script, while templates unwrap it automatically. Its data contains view refs, which components such as `UserCard` resolve with `useView`. Cached data stays visible during a refresh, and `isFetching` and `error` report its progress and failures.
1887
-
1888
- The request and options accept plain values, refs, or getters. Pass `{ enabled: false }` to disable fetching, or `{ mode: 'cache-only' }` to observe cached data without fetching missing fields. The `status` is `disabled`, `missing`, `pending`, `ready`, or `error`, as described in [Observing Request State](/docs/guide/requests.md#observing-request-state).
1889
-
1890
- Call `state.value.refetch()` to fetch fresh data. Handle its rejected promise in the calling code. Disabled and cache-only requests reject refetches. Vue cleans up the observer when the component unmounts; call `state.dispose()` to stop it earlier.
1880
+ Awaiting `ready()` in `<script setup>` participates in Vue Suspense. The getter passed to `useListView` keeps the list in sync when the request result changes. To render loading and error states yourself, read the resource’s `pending` and `error` refs without awaiting `ready()`.
1891
1881
 
1892
1882
  ### Refreshing Requests
1893
1883
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nkzw/fate-indexeddb",
3
- "version": "1.10.2",
3
+ "version": "1.11.0",
4
4
  "description": "Optional IndexedDB persistence adapter for fate.",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -19,15 +19,12 @@
19
19
  "idb": "^8.0.4"
20
20
  },
21
21
  "devDependencies": {
22
- "@nkzw/fate": "1.10.2",
22
+ "@nkzw/fate": "1.11.0",
23
23
  "fake-indexeddb": "^6.2.6",
24
24
  "vite": "npm:@voidzero-dev/vite-plus-core@1.3.0",
25
25
  "vite-plus": "1.3.0"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@nkzw/fate": "^1.5.1"
29
- },
30
- "scripts": {
31
- "build": "vp pack --tsconfig tsconfig.json -d lib src/index.ts"
32
29
  }
33
30
  }