@raf-query/core 0.1.1 → 0.1.2

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/CHANGELOG.md CHANGED
@@ -1,10 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.1.2](https://github.com/KATT/raf-query/compare/0.1.1...0.1.2) (2026-10-12)
4
+
5
+ ### Features
6
+
7
+ * add isFetching(filter?) and useIsFetching(filter?) ([#25](https://github.com/KATT/raf-query/issues/25)) ([70c3b98](https://github.com/KATT/raf-query/commit/70c3b981027f717b1d619d5a2b23822f685c032a))
8
+
3
9
  ## [0.1.1](https://github.com/KATT/raf-query/compare/0.1.0...0.1.1) (2026-10-12)
4
10
 
5
11
  ### Features
6
12
 
7
- * **core:** add predicate to QueryFilter ([#24](https://github.com/KATT/raf-query/issues/24)) ([c0b9c18](https://github.com/KATT/raf-query/commit/c0b9c1829cb3b6446f765b151e520f0195af5276))
13
+ - **core:** add predicate to QueryFilter ([#24](https://github.com/KATT/raf-query/issues/24)) ([c0b9c18](https://github.com/KATT/raf-query/commit/c0b9c1829cb3b6446f765b151e520f0195af5276))
8
14
 
9
15
  ## [0.1.0](https://github.com/KATT/raf-query/compare/0.0.19...0.1.0) (2026-10-12)
10
16
 
package/README.md CHANGED
@@ -196,6 +196,16 @@ When the cache updates — refetch, invalidation, `setData` — the hook returns
196
196
 
197
197
  Queries do not refetch on mount. They refetch when a loader starts them again, when invalidated, on window focus or reconnect while stale, on `refetch()`, or on `refetchInterval`.
198
198
 
199
+ For an app-wide indicator, `useIsFetching()` returns how many queries have a request in flight, background refetches included, and re-renders when that changes. Pass a filter to count only some: `useIsFetching({ queryKey: ["comments"] })`. It's `0` on the server and during hydration, so the indicator never appears in server HTML. Outside React, `qc.isFetching(filter?)` returns the same number.
200
+
201
+ ```tsx
202
+ // Rendered once, in the root layout.
203
+ function GlobalProgress() {
204
+ const fetching = useIsFetching();
205
+ return fetching > 0 ? <ProgressBar /> : null;
206
+ }
207
+ ```
208
+
199
209
  ### Paged queries
200
210
 
201
211
  With an `infiniteQueryOptions` factory, `useQuery` returns the same tuple. The promise resolves to `{ pages, pageParams }`, and the controls add paging:
@@ -475,7 +485,7 @@ raf-query's cache is written in [Effect](https://effect.website): `QueryClient`
475
485
  - A `queryFn` can return an `Effect`. Cancelling the request interrupts it.
476
486
  - An Effect `queryFn`'s error type is the query's: `qc.effect.fetch(issueQuery, input)` fails with `IssueNotFound | Query.CancelledError`. `ErrorOf<TFactory>` names it; it's `unknown` for promises. A `queryFn` that throws instead of returning is a defect, and isn't retried.
477
487
  - `retry` accepts a `Schedule` as well as a count or predicate.
478
- - `qc.effect` has every verb as an Effect: `prefetch`, `ensure`, `fetch`, `invalidate`, `reset`, `remove`, `setData`, `getData` (an `Option`), `dehydrate`, `hydrate`. `watch` is a `Stream` of an entry's state, and `events` a `Stream` of cache events.
488
+ - `qc.effect` has every verb as an Effect: `prefetch`, `ensure`, `fetch`, `invalidate`, `reset`, `remove`, `setData`, `getData` (an `Option`), `isFetching` (a count), `dehydrate`, `hydrate`. `watch` is a `Stream` of an entry's state, and `events` a `Stream` of cache events.
479
489
  - SSR streaming is Effect too, and the integrations are thin wrappers over it. `dehydrateStream(options)` (scoped) returns the cache's `initial` state and a `Stream` of `updates`: entries that start or settle later, each sent once, batched per microtask. The stream ends when the scope closes. `hydrateStream(stream)` hydrates each state as it arrives. `Stream.toReadableStream` / `Stream.fromReadableStream` carry it over a web stream. The `sent` option (a `Set` of hashes) makes separate `dehydrate` calls of one request skip entries already sent.
480
490
  - Entries are a data type with module functions, as in Effect's own modules: `Query.fetch(query)`, `Query.refetch(query)`, `Query.invalidate(query)`, `Query.changes(query)`, and `Query.getUnsafe(query)` / `Query.subscribeUnsafe(query, listener)` for synchronous reads.
481
491
  - `QueryObserver` is a data type too: one reader's view of a query, as `useQuery` sees it (the controls and the promise, with `whilePending`, `refetchInterval` and the focus / reconnect options). `QueryObserver.make(query, options)` creates one, and `QueryObserver.changes(observer)` streams its results; while it runs, the query counts as read and polls on `refetchInterval`.
@@ -612,6 +622,7 @@ Both render nothing on the server, since the server's cache isn't the browser's.
612
622
  | `remove(factory, input)` / `remove(filter)` | Remove entries and abort their requests. [Filters](#filters) |
613
623
  | `effect` | The [Effect](#effect) `QueryCache` behind the client |
614
624
  | `subscribe(listener)` | Cache events: `added`, `updated`, `observers` (a reader subscribed or left), `removed`, and `failed` (with the `error`) |
625
+ | `isFetching(filter?)` | How many entries have a request in flight, optionally only those matching `{ queryKey, exact? }` |
615
626
  | `getAll()`, `getQuery(factory, input?)` | Cache entries, for devtools and integrations |
616
627
  | `clear()` | Drop every entry and abort in-flight requests |
617
628
  | `mount()` | Start window-focus and reconnect refetching. The providers call it |
@@ -663,6 +674,7 @@ In the browser, retries pause while `navigator.onLine` is `false`: after the ret
663
674
  | `useQueryClient()` | Read the client |
664
675
  | `useQuery(factory, handle, options?)` | `[Thenable<Data>, controls]`; for paged factories `Data` is `{ pages, pageParams }` and controls include paging |
665
676
  | `useWaterfall(factory, input)` | Start a query during render and return its handle |
677
+ | `useIsFetching(filter?)` | How many queries have a request in flight, re-rendering when it changes; for global loading indicators |
666
678
 
667
679
  ## Development
668
680
 
package/dist/cache.d.mts CHANGED
@@ -109,6 +109,13 @@ export interface QueryCache {
109
109
  * screen.
110
110
  */
111
111
  invalidate<TFactory extends QueryFactory>(...target: Target<TFactory>): Effect.Effect<void>;
112
+ /**
113
+ * How many entries have a request in flight: all of them, or those
114
+ * matching `filter`. For global loading indicators.
115
+ */
116
+ isFetching(filter?: Filter): Effect.Effect<number>;
117
+ /** `isFetching`, synchronously, for renders. */
118
+ isFetchingUnsafe(filter?: Filter): number;
112
119
  readonly isServer: boolean;
113
120
  /**
114
121
  * Refetch read, stale queries on window focus and reconnect while the
package/dist/cache.mjs CHANGED
@@ -11,8 +11,9 @@ import { Clock, Context, Effect, Layer, Option, Predicate, RcRef, Stream } from
11
11
  /**
12
12
  * The query cache, as an Effect service. `make` builds one in the current
13
13
  * scope; closing the scope interrupts every request and timer it started.
14
- * Everything that waits, or changes the cache, is an Effect; `getQueryUnsafe`
15
- * and `subscribeUnsafe` are for renders and external stores.
14
+ * Everything that waits, or changes the cache, is an Effect; `getQueryUnsafe`,
15
+ * `isFetchingUnsafe` and `subscribeUnsafe` are for renders and external
16
+ * stores.
16
17
  *
17
18
  * Requests run with the services the cache was built with, under those of
18
19
  * the latest verb that called for the entry. So the verbs that can start a
@@ -135,6 +136,7 @@ const make = Effect.fnUntraced(function* (config = {}) {
135
136
  };
136
137
  };
137
138
  const getQueryUnsafe = (factory, ...args) => typed(build(factory, args[0]));
139
+ const isFetchingUnsafe = (filter) => (filter ? match([filter]) : [...state.entries.values()]).filter((entry) => entry.state.isFetching).length;
138
140
  return {
139
141
  all: Effect.sync(() => [...state.entries.values()]),
140
142
  clear: Effect.sync(() => {
@@ -158,6 +160,8 @@ const make = Effect.fnUntraced(function* (config = {}) {
158
160
  hydrate: (dehydrated, options = {}) => Effect.sync(() => hydrateEntries(entryFor, dehydrated, options)),
159
161
  hydrateStream: (states, options = {}) => Stream.runForEach(states, (dehydrated) => Effect.sync(() => hydrateEntries(entryFor, dehydrated, options))),
160
162
  invalidate: (...target) => Effect.suspend(() => settleAll(match(target).map(invalidate))),
163
+ isFetching: (filter) => Effect.sync(() => isFetchingUnsafe(filter)),
164
+ isFetchingUnsafe,
161
165
  isServer: state.isServer,
162
166
  mount: state.isServer ? Effect.void : Effect.asVoid(RcRef.get(subscription)),
163
167
  prefetch: (factory, ...args) => Effect.map(buildFor(factory, args[0]), (entry) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raf-query/core",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Framework-agnostic core of raf-query, a render-as-you-fetch query library",
5
5
  "keywords": [
6
6
  "query",