@softistx/riva-react 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +220 -2
- package/dist/chunks/index-gzs9smr9.js +42 -0
- package/dist/chunks/index-gzs9smr9.js.map +10 -0
- package/dist/chunks/index-zww0a7j9.js +20 -0
- package/dist/chunks/index-zww0a7j9.js.map +10 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/entities/index.d.ts +2 -0
- package/dist/entities/index.d.ts.map +1 -0
- package/dist/entities/index.js +21 -0
- package/dist/entities/index.js.map +10 -0
- package/dist/entities/use-fragment.d.ts +7 -0
- package/dist/entities/use-fragment.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +111 -0
- package/dist/index.js.map +13 -0
- package/dist/infinite/index.d.ts +2 -0
- package/dist/infinite/index.d.ts.map +1 -0
- package/dist/infinite/index.js +21 -0
- package/dist/infinite/index.js.map +10 -0
- package/dist/infinite/use-infinite-query.d.ts +19 -0
- package/dist/infinite/use-infinite-query.d.ts.map +1 -0
- package/dist/query-state.d.ts +18 -0
- package/dist/query-state.d.ts.map +1 -0
- package/dist/store/index.d.ts +2 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +75 -0
- package/dist/store/index.js.map +10 -0
- package/dist/store/use-store.d.ts +12 -0
- package/dist/store/use-store.d.ts.map +1 -0
- package/dist/suspend.d.ts +8 -0
- package/dist/suspend.d.ts.map +1 -0
- package/dist/use-mutation.d.ts +18 -0
- package/dist/use-mutation.d.ts.map +1 -0
- package/dist/use-query.d.ts +25 -0
- package/dist/use-query.d.ts.map +1 -0
- package/dist/use-signal.d.ts +6 -0
- package/dist/use-signal.d.ts.map +1 -0
- package/docs/README.md +20 -0
- package/docs/guide/entities.md +25 -0
- package/docs/guide/infinite.md +35 -0
- package/docs/guide/mutations.md +41 -0
- package/docs/guide/provider.md +34 -0
- package/docs/guide/queries.md +41 -0
- package/docs/guide/signals.md +27 -0
- package/docs/guide/ssr.md +27 -0
- package/docs/guide/store.md +27 -0
- package/docs/guide/suspense.md +38 -0
- package/docs/troubleshooting.md +102 -0
- package/package.json +82 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Steve Tsala
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,221 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @softistx/riva-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React bindings for [riva](https://github.com/softistx/riva/blob/develop/packages/riva/README.md): a provider, hooks over signals, queries, mutations, infinite lists and entities, and Suspense. Typed end to end: variables and data are inferred from the definition, never annotated at the call site.
|
|
4
|
+
|
|
5
|
+
Guides and fixes: [docs](./docs/README.md). Work in progress, see the [spec](https://github.com/softistx/riva/blob/develop/docs/spec.md).
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @softistx/riva @softistx/riva-react react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peers: `react` ^19 and `@softistx/riva`. Every hook binds through `useSyncExternalStore`, so it is concurrent-safe and renders on the server.
|
|
12
|
+
|
|
13
|
+
The examples are schematic: `Patient`, `fetchPatients`, `sendRename` and `fetchPatientPage` stand for your own types and transport functions.
|
|
14
|
+
|
|
15
|
+
## Provider
|
|
16
|
+
|
|
17
|
+
One `RivaClient` per tree: one in the browser, one per request on the server. A hook with no provider above it throws a `RivaScopeError` that names the fix.
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { createRivaClient } from '@softistx/riva';
|
|
21
|
+
import { RivaProvider } from '@softistx/riva-react';
|
|
22
|
+
|
|
23
|
+
const client = createRivaClient(); // per request on the server
|
|
24
|
+
|
|
25
|
+
export const App = () => (
|
|
26
|
+
<RivaProvider client={client}>
|
|
27
|
+
<Patients />
|
|
28
|
+
</RivaProvider>
|
|
29
|
+
);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`useClient()` returns the client, to call `def.invalidate(client, vars)` or `def.ensure(client, vars)` from an event handler. [Guide](./docs/guide/provider.md).
|
|
33
|
+
|
|
34
|
+
## Signals and computeds
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { signal, computed } from '@softistx/riva';
|
|
38
|
+
import { useSignal, useValue } from '@softistx/riva-react';
|
|
39
|
+
|
|
40
|
+
const filter = signal('');
|
|
41
|
+
const hasFilter = computed((get) => get(filter).length > 0);
|
|
42
|
+
|
|
43
|
+
function Search() {
|
|
44
|
+
const [q, setQ] = useSignal(filter); // setQ takes a value or an updater
|
|
45
|
+
const active = useValue(hasFilter); // a signal or a computed
|
|
46
|
+
return <input value={q} data-active={active} onChange={(e) => setQ(e.target.value)} />;
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
[Guide](./docs/guide/signals.md).
|
|
51
|
+
|
|
52
|
+
## Queries
|
|
53
|
+
|
|
54
|
+
`useQuery(def, vars?, opts?)` returns the query state (`status`, `data`, `error`, `isFetching`, `updatedAt`) plus `refetch`. It fetches while the component is mounted, when the data is missing or stale. Variables and data are inferred from `fetch`.
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { query } from '@softistx/riva';
|
|
58
|
+
import { useQuery } from '@softistx/riva-react';
|
|
59
|
+
|
|
60
|
+
const patients = query({
|
|
61
|
+
id: 'patients',
|
|
62
|
+
staleTime: '30s',
|
|
63
|
+
fetch: ({ school }: { school: string }, { signal }) => fetchPatients(school, signal), // Promise<Patient[]>
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
function PatientList({ school }: { school: string }) {
|
|
67
|
+
const { data, status, error, refetch } = useQuery(patients, { school });
|
|
68
|
+
if (status === 'error') return <button onClick={refetch}>Retry: {String(error)}</button>;
|
|
69
|
+
return <ul>{data?.map((p) => <li key={p.id}>{p.name}</li>)}</ul>;
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Options: `enabled` (`true`) and `suspense` (`false`). Variables are compared by value, so an inline `{ school }` never resubscribes; `refetch()` never rejects. [Guide](./docs/guide/queries.md).
|
|
74
|
+
|
|
75
|
+
## Suspense
|
|
76
|
+
|
|
77
|
+
With `suspense: true`, the component suspends while there is no data, and `data` is no longer optional in its type. A failure with no data is thrown to the nearest error boundary. With `enabled: false` it never suspends: the query reads `idle`.
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
import { Suspense } from 'react';
|
|
81
|
+
import { ErrorBoundary } from 'react-error-boundary';
|
|
82
|
+
|
|
83
|
+
function PatientList({ school }: { school: string }) {
|
|
84
|
+
const { data } = useQuery(patients, { school }, { suspense: true });
|
|
85
|
+
return <ul>{data.map((p) => <li key={p.id}>{p.name}</li>)}</ul>; // data: Patient[]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const Page = ({ school }: { school: string }) => (
|
|
89
|
+
<ErrorBoundary fallback={<p>Failed</p>}>
|
|
90
|
+
<Suspense fallback={<p>Loading…</p>}>
|
|
91
|
+
<PatientList school={school} />
|
|
92
|
+
</Suspense>
|
|
93
|
+
</ErrorBoundary>
|
|
94
|
+
);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
To retry after an error, start the fetch again before resetting the boundary (`onReset={() => patients.prefetch(client, { school })}`). [Guide](./docs/guide/suspense.md).
|
|
98
|
+
|
|
99
|
+
## Mutations
|
|
100
|
+
|
|
101
|
+
`useMutation(def)` holds its status per hook instance. `run` resolves with the server data and rejects with the error (also in `error`). [Guide](./docs/guide/mutations.md).
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
import { mutation } from '@softistx/riva';
|
|
105
|
+
import { useMutation } from '@softistx/riva-react';
|
|
106
|
+
|
|
107
|
+
const rename = mutation({
|
|
108
|
+
id: 'renamePatient',
|
|
109
|
+
run: (vars: { id: string; name: string }, { signal }) => sendRename(vars, signal), // Promise<Patient>
|
|
110
|
+
invalidates: [patients],
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
function Rename({ id }: { id: string }) {
|
|
114
|
+
const { run, isPending, error, reset } = useMutation(rename);
|
|
115
|
+
return (
|
|
116
|
+
<button disabled={isPending} onClick={() => run({ id, name: 'New name' }).catch(() => {})}>
|
|
117
|
+
Rename
|
|
118
|
+
</button>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Infinite lists
|
|
124
|
+
|
|
125
|
+
`@softistx/riva-react/infinite` binds a list built with `infinite()` from `@softistx/riva/infinite`. `data.nodes` is every loaded page's nodes, flattened.
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
import { infinite } from '@softistx/riva/infinite';
|
|
129
|
+
import { useInfiniteQuery } from '@softistx/riva-react/infinite';
|
|
130
|
+
|
|
131
|
+
const patientsFeed = infinite({
|
|
132
|
+
id: 'patientsFeed',
|
|
133
|
+
// `after` is the cursor the engine fills in; the caller passes `school` only.
|
|
134
|
+
fetch: ({ school, after }: { school: string; after?: string }) => fetchPatientPage(school, after),
|
|
135
|
+
// A page: { patients: { edges: [{ node: Patient, cursor }], pageInfo: { hasNextPage, endCursor, … } } }
|
|
136
|
+
connection: (page) => page.patients,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
function Feed({ school }: { school: string }) {
|
|
140
|
+
const { data, hasNext, isFetchingNext, fetchNext } = useInfiniteQuery(patientsFeed, { school });
|
|
141
|
+
return (
|
|
142
|
+
<>
|
|
143
|
+
<ul>{data?.nodes.map((p) => <li key={p.id}>{p.name}</li>)}</ul>
|
|
144
|
+
{hasNext && <button disabled={isFetchingNext} onClick={fetchNext}>More</button>}
|
|
145
|
+
</>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
It also returns `fetchPrevious`, `hasPrevious`, `isFetchingPrevious` and `refetch`. [Guide](./docs/guide/infinite.md).
|
|
151
|
+
|
|
152
|
+
## Stores
|
|
153
|
+
|
|
154
|
+
`@softistx/riva-react/store` binds a store built with `store()` from `@softistx/riva/store` (see the core README). `useStore(def)` returns `[s, a]`: `s` is the state and the computeds, `a` the actions bound to the provider's client (stable for that client).
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
import { useStore } from '@softistx/riva-react/store';
|
|
158
|
+
|
|
159
|
+
function Books() {
|
|
160
|
+
const [s, a] = useStore(BookStore);
|
|
161
|
+
return (
|
|
162
|
+
<>
|
|
163
|
+
<input value={s.filter} onChange={(e) => a.filterBy(e.target.value)} />
|
|
164
|
+
<p>{s.total} books</p>
|
|
165
|
+
<ul>{s.visible.map((b) => <li key={b.title}>{b.title}</li>)}</ul>
|
|
166
|
+
<button onClick={() => a.add({ title: 'New' })}>Add</button>
|
|
167
|
+
</>
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`s` is tracked per key and read-only: a write throws `RIVA_STORE`; write through `a`. [Guide](./docs/guide/store.md).
|
|
173
|
+
|
|
174
|
+
## Entities
|
|
175
|
+
|
|
176
|
+
`@softistx/riva-react/entities` reads one entity of the normalized cache by its ref (`Type:id`); the component re-renders when that entity changes, and only then. [Guide](./docs/guide/entities.md).
|
|
177
|
+
|
|
178
|
+
```tsx
|
|
179
|
+
import { useFragment } from '@softistx/riva-react/entities';
|
|
180
|
+
|
|
181
|
+
function PatientName({ id }: { id: string }) {
|
|
182
|
+
const patient = useFragment<{ name: string }>(`Patient:${id}`);
|
|
183
|
+
return <span>{patient?.name ?? '…'}</span>;
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Server rendering
|
|
188
|
+
|
|
189
|
+
Render each request under its own `RivaProvider` with its own client; no hook holds module-level state, so nothing leaks between requests.
|
|
190
|
+
|
|
191
|
+
```tsx
|
|
192
|
+
import { createRivaClient } from '@softistx/riva';
|
|
193
|
+
import { RivaProvider } from '@softistx/riva-react';
|
|
194
|
+
import { renderToReadableStream } from 'react-dom/server';
|
|
195
|
+
|
|
196
|
+
export async function handle(school: string): Promise<Response> {
|
|
197
|
+
const client = createRivaClient(); // one per request
|
|
198
|
+
await patients.ensure(client, { school }); // so the first render has data
|
|
199
|
+
const stream = await renderToReadableStream(
|
|
200
|
+
<RivaProvider client={client}>
|
|
201
|
+
<PatientList school={school} />
|
|
202
|
+
</RivaProvider>,
|
|
203
|
+
);
|
|
204
|
+
return new Response(stream, { headers: { 'content-type': 'text/html' } });
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
A hook never starts a fetch during a server render, except through `suspense: true`. To hand the cache to the browser, use `@softistx/riva-react-router` or `dehydrate` and `hydrate`. [Guide](./docs/guide/ssr.md).
|
|
209
|
+
|
|
210
|
+
## Entry points
|
|
211
|
+
|
|
212
|
+
| Import | Adds |
|
|
213
|
+
| --- | --- |
|
|
214
|
+
| `@softistx/riva-react` | `RivaProvider`, `useClient`, `useSignal`, `useValue`, `useQuery`, `useMutation` |
|
|
215
|
+
| `@softistx/riva-react/infinite` | `useInfiniteQuery` |
|
|
216
|
+
| `@softistx/riva-react/entities` | `useFragment` |
|
|
217
|
+
| `@softistx/riva-react/store` | `useStore` |
|
|
218
|
+
|
|
219
|
+
Types: `MutationResult` (what `useMutation` returns), `RivaProviderProps`, `SuspendedState` and `UseQueryOptions` (main entry), `InfiniteResult` and `UseInfiniteQueryOptions` (`/infinite`).
|
|
220
|
+
|
|
221
|
+
Core plus the main entry stays under 9 kB gzipped; the subpaths cost nothing until imported.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useClient2
|
|
3
|
+
} from "./index-zww0a7j9.js";
|
|
4
|
+
|
|
5
|
+
// src/query-state.ts
|
|
6
|
+
import {
|
|
7
|
+
stableKey
|
|
8
|
+
} from "@softistx/riva";
|
|
9
|
+
import { useCallback, useRef, useSyncExternalStore } from "react";
|
|
10
|
+
var NOOP = () => {};
|
|
11
|
+
var SUSPENSE_FRESH = 1000;
|
|
12
|
+
var DISABLED = Object.freeze({
|
|
13
|
+
status: "idle",
|
|
14
|
+
data: undefined,
|
|
15
|
+
error: undefined,
|
|
16
|
+
isFetching: false,
|
|
17
|
+
updatedAt: 0
|
|
18
|
+
});
|
|
19
|
+
function useStableVars(id, vars) {
|
|
20
|
+
const key = stableKey(id, vars);
|
|
21
|
+
const ref = useRef(undefined);
|
|
22
|
+
if (ref.current?.key !== key)
|
|
23
|
+
ref.current = { key, vars };
|
|
24
|
+
return ref.current;
|
|
25
|
+
}
|
|
26
|
+
function useQueryState(def, vars, enabled, suspense = false) {
|
|
27
|
+
const client = useClient2();
|
|
28
|
+
const stable = useStableVars(def.id, vars);
|
|
29
|
+
const subscribe = useCallback((notify) => enabled ? client.subscribe(def, stable.vars, notify, suspense ? { staleTime: SUSPENSE_FRESH } : undefined) : NOOP, [client, def, stable, enabled, suspense]);
|
|
30
|
+
const read = () => enabled ? client.get(def, stable.vars) : DISABLED;
|
|
31
|
+
const state = useSyncExternalStore(subscribe, read, read);
|
|
32
|
+
const refetch = useCallback(async () => {
|
|
33
|
+
def.invalidate(client, stable.vars);
|
|
34
|
+
await def.ensure(client, stable.vars).catch(NOOP);
|
|
35
|
+
}, [client, def, stable]);
|
|
36
|
+
return { client, state, key: stable.key, vars: stable.vars, refetch };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { useQueryState };
|
|
40
|
+
|
|
41
|
+
//# debugId=4D3DE7A90EA397DC64756E2164756E21
|
|
42
|
+
//# sourceMappingURL=index-gzs9smr9.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/query-state.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import {\n\ttype Query,\n\ttype QueryState,\n\ttype RivaClient,\n\tstableKey,\n} from '@softistx/riva';\nimport { useCallback, useRef, useSyncExternalStore } from 'react';\nimport { useClient } from './context';\n\n// biome-ignore lint/suspicious/noExplicitAny: any query definition\nexport type AnyQuery = Query<any, any, any>;\n\nconst NOOP = () => {};\n\n/** How long (ms) a mount trusts what a Suspense render fetched. */\nconst SUSPENSE_FRESH = 1000;\n\n/** What a disabled query reads: idle, with no entry created. */\nconst DISABLED: QueryState<never> = Object.freeze({\n\tstatus: 'idle',\n\tdata: undefined,\n\terror: undefined,\n\tisFetching: false,\n\tupdatedAt: 0,\n});\n\n/** The variables of the first render that produced this key: an inline `{ a }` never resubscribes. */\nexport function useStableVars<V>(\n\tid: string,\n\tvars: V,\n): { key: string; vars: V } {\n\tconst key = stableKey(id, vars);\n\tconst ref = useRef<{ key: string; vars: V } | undefined>(undefined);\n\tif (ref.current?.key !== key) ref.current = { key, vars };\n\treturn ref.current;\n}\n\nexport interface QueryBinding<S> {\n\tclient: RivaClient;\n\tstate: S;\n\tkey: string;\n\tvars: unknown;\n\t/** Marks the entry stale and fetches it; never rejects, the error is in the state. */\n\trefetch: () => Promise<void>;\n}\n\n/** Observes a query entry through `useSyncExternalStore`; nothing is observed while disabled. */\nexport function useQueryState<S extends QueryState<unknown>>(\n\tdef: AnyQuery,\n\tvars: unknown,\n\tenabled: boolean,\n\tsuspense = false,\n): QueryBinding<S> {\n\tconst client = useClient();\n\tconst stable = useStableVars(def.id, vars);\n\tconst subscribe = useCallback(\n\t\t(notify: () => void) =>\n\t\t\tenabled\n\t\t\t\t? client.subscribe(\n\t\t\t\t\t\tdef,\n\t\t\t\t\t\tstable.vars,\n\t\t\t\t\t\tnotify,\n\t\t\t\t\t\t// What a Suspense render just fetched is not fetched again on mount.\n\t\t\t\t\t\tsuspense ? { staleTime: SUSPENSE_FRESH } : undefined,\n\t\t\t\t\t)\n\t\t\t\t: NOOP,\n\t\t[client, def, stable, enabled, suspense],\n\t);\n\tconst read = () => (enabled ? client.get(def, stable.vars) : DISABLED) as S;\n\tconst state = useSyncExternalStore(subscribe, read, read);\n\tconst refetch = useCallback(async () => {\n\t\tdef.invalidate(client, stable.vars);\n\t\tawait def.ensure(client, stable.vars).catch(NOOP);\n\t}, [client, def, stable]);\n\treturn { client, state, key: stable.key, vars: stable.vars, refetch };\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;AAAA;AAAA;AAAA;AAMA;AAMA,IAAM,OAAO,MAAM;AAGnB,IAAM,iBAAiB;AAGvB,IAAM,WAA8B,OAAO,OAAO;AAAA,EACjD,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,WAAW;AACZ,CAAC;AAGM,SAAS,aAAgB,CAC/B,IACA,MAC2B;AAAA,EAC3B,MAAM,MAAM,UAAU,IAAI,IAAI;AAAA,EAC9B,MAAM,MAAM,OAA6C,SAAS;AAAA,EAClE,IAAI,IAAI,SAAS,QAAQ;AAAA,IAAK,IAAI,UAAU,EAAE,KAAK,KAAK;AAAA,EACxD,OAAO,IAAI;AAAA;AAaL,SAAS,aAA4C,CAC3D,KACA,MACA,SACA,WAAW,OACO;AAAA,EAClB,MAAM,SAAS,WAAU;AAAA,EACzB,MAAM,SAAS,cAAc,IAAI,IAAI,IAAI;AAAA,EACzC,MAAM,YAAY,YACjB,CAAC,WACA,UACG,OAAO,UACP,KACA,OAAO,MACP,QAEA,WAAW,EAAE,WAAW,eAAe,IAAI,SAC5C,IACC,MACJ,CAAC,QAAQ,KAAK,QAAQ,SAAS,QAAQ,CACxC;AAAA,EACA,MAAM,OAAO,MAAO,UAAU,OAAO,IAAI,KAAK,OAAO,IAAI,IAAI;AAAA,EAC7D,MAAM,QAAQ,qBAAqB,WAAW,MAAM,IAAI;AAAA,EACxD,MAAM,UAAU,YAAY,YAAY;AAAA,IACvC,IAAI,WAAW,QAAQ,OAAO,IAAI;AAAA,IAClC,MAAM,IAAI,OAAO,QAAQ,OAAO,IAAI,EAAE,MAAM,IAAI;AAAA,KAC9C,CAAC,QAAQ,KAAK,MAAM,CAAC;AAAA,EACxB,OAAO,EAAE,QAAQ,OAAO,KAAK,OAAO,KAAK,MAAM,OAAO,MAAM,QAAQ;AAAA;",
|
|
8
|
+
"debugId": "4D3DE7A90EA397DC64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/context.ts
|
|
2
|
+
import { RivaScopeError } from "@softistx/riva";
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
createElement,
|
|
6
|
+
useContext
|
|
7
|
+
} from "react";
|
|
8
|
+
var RivaContext = createContext(undefined);
|
|
9
|
+
var RivaProvider2 = ({ client, children }) => createElement(RivaContext, { value: client }, children);
|
|
10
|
+
function useClient2() {
|
|
11
|
+
const client = useContext(RivaContext);
|
|
12
|
+
if (!client)
|
|
13
|
+
throw new RivaScopeError("No RivaClient in scope: wrap the tree in <RivaProvider client={client}>. On the server, create one client per request.");
|
|
14
|
+
return client;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { RivaProvider2, useClient2 };
|
|
18
|
+
|
|
19
|
+
//# debugId=170BE6D7653AD8CA64756E2164756E21
|
|
20
|
+
//# sourceMappingURL=index-zww0a7j9.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/context.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { type RivaClient, RivaScopeError } from '@softistx/riva';\nimport {\n\tcreateContext,\n\tcreateElement,\n\ttype ReactNode,\n\tuseContext,\n} from 'react';\n\nconst RivaContext = createContext<RivaClient | undefined>(undefined);\n\nexport interface RivaProviderProps {\n\t/** The client of this tree: one per request on the server, one in the browser. */\n\tclient: RivaClient;\n\tchildren?: ReactNode;\n}\n\n/** Puts a `RivaClient` in scope for the hooks below it. Hold one provider (and one client) per server request. */\nexport const RivaProvider = ({ client, children }: RivaProviderProps) =>\n\tcreateElement(RivaContext, { value: client }, children);\n\n/** The client of the nearest `RivaProvider`; throws a `RivaScopeError` when there is none. */\nexport function useClient(): RivaClient {\n\tconst client = useContext(RivaContext);\n\tif (!client)\n\t\tthrow new RivaScopeError(\n\t\t\t'No RivaClient in scope: wrap the tree in <RivaProvider client={client}>. On the server, create one client per request.',\n\t\t);\n\treturn client;\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";AAAA;AACA;AAAA;AAAA;AAAA;AAAA;AAOA,IAAM,cAAc,cAAsC,SAAS;AAS5D,IAAM,gBAAe,GAAG,QAAQ,eACtC,cAAc,aAAa,EAAE,OAAO,OAAO,GAAG,QAAQ;AAGhD,SAAS,UAAS,GAAe;AAAA,EACvC,MAAM,SAAS,WAAW,WAAW;AAAA,EACrC,IAAI,CAAC;AAAA,IACJ,MAAM,IAAI,eACT,wHACD;AAAA,EACD,OAAO;AAAA;",
|
|
8
|
+
"debugId": "170BE6D7653AD8CA64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type RivaClient } from '@softistx/riva';
|
|
2
|
+
import { type ReactNode } from 'react';
|
|
3
|
+
export interface RivaProviderProps {
|
|
4
|
+
/** The client of this tree: one per request on the server, one in the browser. */
|
|
5
|
+
client: RivaClient;
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
/** Puts a `RivaClient` in scope for the hooks below it. Hold one provider (and one client) per server request. */
|
|
9
|
+
export declare const RivaProvider: ({ client, children }: RivaProviderProps) => import("react").FunctionComponentElement<import("react").ProviderProps<RivaClient | undefined>>;
|
|
10
|
+
/** The client of the nearest `RivaProvider`; throws a `RivaScopeError` when there is none. */
|
|
11
|
+
export declare function useClient(): RivaClient;
|
|
12
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAkB,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAGN,KAAK,SAAS,EAEd,MAAM,OAAO,CAAC;AAIf,MAAM,WAAW,iBAAiB;IACjC,kFAAkF;IAClF,MAAM,EAAE,UAAU,CAAC;IACnB,QAAQ,CAAC,EAAE,SAAS,CAAC;CACrB;AAED,kHAAkH;AAClH,eAAO,MAAM,YAAY,yBAA0B,iBAAiB,oGACZ,CAAC;AAEzD,8FAA8F;AAC9F,wBAAgB,SAAS,IAAI,UAAU,CAOtC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entities/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useClient2
|
|
3
|
+
} from "../chunks/index-zww0a7j9.js";
|
|
4
|
+
|
|
5
|
+
// src/entities/use-fragment.ts
|
|
6
|
+
import { computed } from "@softistx/riva";
|
|
7
|
+
import { readEntity } from "@softistx/riva/entities";
|
|
8
|
+
import { useCallback, useMemo, useSyncExternalStore } from "react";
|
|
9
|
+
function useFragment(ref) {
|
|
10
|
+
const client = useClient2();
|
|
11
|
+
const def = useMemo(() => computed(() => ref ? readEntity(client, ref) : undefined), [client, ref]);
|
|
12
|
+
const subscribe = useCallback((notify) => client.subscribe(def, notify), [client, def]);
|
|
13
|
+
const read = () => client.get(def);
|
|
14
|
+
return useSyncExternalStore(subscribe, read, read);
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
useFragment
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
//# debugId=978DF31058AD9B9564756E2164756E21
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/entities/use-fragment.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { computed } from '@softistx/riva';\nimport { readEntity } from '@softistx/riva/entities';\nimport { useCallback, useMemo, useSyncExternalStore } from 'react';\nimport { useClient } from '../context';\n\n/**\n * An entity of the normalized cache by its ref (`Type:id`), re-rendering when\n * its fields change and only then. `undefined` while the entity is missing or\n * the ref is empty.\n */\nexport function useFragment<T = Record<string, unknown>>(\n\tref: string | null | undefined,\n): T | undefined {\n\tconst client = useClient();\n\tconst def = useMemo(\n\t\t() => computed(() => (ref ? readEntity<T>(client, ref) : undefined)),\n\t\t[client, ref],\n\t);\n\tconst subscribe = useCallback(\n\t\t(notify: () => void) => client.subscribe(def, notify),\n\t\t[client, def],\n\t);\n\tconst read = () => client.get(def);\n\treturn useSyncExternalStore(subscribe, read, read);\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;AAAA;AACA;AACA;AAQO,SAAS,WAAwC,CACvD,KACgB;AAAA,EAChB,MAAM,SAAS,WAAU;AAAA,EACzB,MAAM,MAAM,QACX,MAAM,SAAS,MAAO,MAAM,WAAc,QAAQ,GAAG,IAAI,SAAU,GACnE,CAAC,QAAQ,GAAG,CACb;AAAA,EACA,MAAM,YAAY,YACjB,CAAC,WAAuB,OAAO,UAAU,KAAK,MAAM,GACpD,CAAC,QAAQ,GAAG,CACb;AAAA,EACA,MAAM,OAAO,MAAM,OAAO,IAAI,GAAG;AAAA,EACjC,OAAO,qBAAqB,WAAW,MAAM,IAAI;AAAA;",
|
|
8
|
+
"debugId": "978DF31058AD9B9564756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An entity of the normalized cache by its ref (`Type:id`), re-rendering when
|
|
3
|
+
* its fields change and only then. `undefined` while the entity is missing or
|
|
4
|
+
* the ref is empty.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useFragment<T = Record<string, unknown>>(ref: string | null | undefined): T | undefined;
|
|
7
|
+
//# sourceMappingURL=use-fragment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-fragment.d.ts","sourceRoot":"","sources":["../../src/entities/use-fragment.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACtD,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAC5B,CAAC,GAAG,SAAS,CAYf"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { RivaProvider, type RivaProviderProps, useClient } from './context';
|
|
2
|
+
export { type MutationResult, useMutation, } from './use-mutation';
|
|
3
|
+
export { type QueryResult, type SuspendedState, type UseQueryOptions, useQuery, } from './use-query';
|
|
4
|
+
export { useSignal, useValue } from './use-signal';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,iBAAiB,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC5E,OAAO,EACN,KAAK,cAAc,EACnB,WAAW,GACX,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,QAAQ,GACR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RivaProvider2,
|
|
3
|
+
useClient2
|
|
4
|
+
} from "./chunks/index-zww0a7j9.js";
|
|
5
|
+
import {
|
|
6
|
+
useQueryState
|
|
7
|
+
} from "./chunks/index-gzs9smr9.js";
|
|
8
|
+
// src/use-mutation.ts
|
|
9
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
10
|
+
var IDLE = Object.freeze({
|
|
11
|
+
status: "idle",
|
|
12
|
+
data: undefined,
|
|
13
|
+
error: undefined
|
|
14
|
+
});
|
|
15
|
+
function useMutation(def) {
|
|
16
|
+
const client = useClient2();
|
|
17
|
+
const [state, setState] = useState(IDLE);
|
|
18
|
+
const live = useRef(false);
|
|
19
|
+
const latest = useRef(0);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
live.current = true;
|
|
22
|
+
return () => {
|
|
23
|
+
live.current = false;
|
|
24
|
+
};
|
|
25
|
+
}, []);
|
|
26
|
+
const report = useCallback((id, next) => {
|
|
27
|
+
if (live.current && id === latest.current)
|
|
28
|
+
setState(next);
|
|
29
|
+
}, []);
|
|
30
|
+
const run = useCallback(async (...args) => {
|
|
31
|
+
const id = ++latest.current;
|
|
32
|
+
report(id, { status: "pending", data: undefined, error: undefined });
|
|
33
|
+
try {
|
|
34
|
+
const data = await def.run(client, ...args);
|
|
35
|
+
report(id, { status: "success", data, error: undefined });
|
|
36
|
+
return data;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
report(id, { status: "error", data: undefined, error });
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}, [client, def, report]);
|
|
42
|
+
const reset = useCallback(() => {
|
|
43
|
+
latest.current++;
|
|
44
|
+
setState(IDLE);
|
|
45
|
+
}, []);
|
|
46
|
+
return useMemo(() => ({ ...state, run, reset, isPending: state.status === "pending" }), [state, run, reset]);
|
|
47
|
+
}
|
|
48
|
+
// src/use-query.ts
|
|
49
|
+
import { useMemo as useMemo2 } from "react";
|
|
50
|
+
|
|
51
|
+
// src/suspend.ts
|
|
52
|
+
var SLOT = {};
|
|
53
|
+
function suspend(client, def, key, vars) {
|
|
54
|
+
let map = client.slots.get(SLOT);
|
|
55
|
+
if (!map) {
|
|
56
|
+
map = new Map;
|
|
57
|
+
client.slots.set(SLOT, map);
|
|
58
|
+
}
|
|
59
|
+
let promise = map.get(key);
|
|
60
|
+
if (!promise) {
|
|
61
|
+
const entries = map;
|
|
62
|
+
const done = () => void entries.delete(key);
|
|
63
|
+
promise = def.ensure(client, vars).then(done, done);
|
|
64
|
+
entries.set(key, promise);
|
|
65
|
+
}
|
|
66
|
+
return promise;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/use-query.ts
|
|
70
|
+
function useQuery(def, vars, opts) {
|
|
71
|
+
const enabled = opts?.enabled !== false;
|
|
72
|
+
const q = useQueryState(def, vars, enabled, opts?.suspense === true);
|
|
73
|
+
const result = useMemo2(() => ({ ...q.state, refetch: q.refetch }), [q.state, q.refetch]);
|
|
74
|
+
let { state } = q;
|
|
75
|
+
if (enabled && opts?.suspense && state.data === undefined) {
|
|
76
|
+
if (state.status === "success" && !state.isFetching) {
|
|
77
|
+
q.client.queries.missing(def, q.vars);
|
|
78
|
+
state = q.client.get(def, q.vars);
|
|
79
|
+
}
|
|
80
|
+
if (state.status === "error")
|
|
81
|
+
throw state.error;
|
|
82
|
+
if (state.status !== "success" || state.isFetching)
|
|
83
|
+
throw suspend(q.client, def, q.key, q.vars);
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
// src/use-signal.ts
|
|
88
|
+
import { useCallback as useCallback2, useSyncExternalStore } from "react";
|
|
89
|
+
function useValue(def) {
|
|
90
|
+
const client = useClient2();
|
|
91
|
+
const subscribe = useCallback2((notify) => client.subscribe(def, notify), [client, def]);
|
|
92
|
+
const read = () => client.get(def);
|
|
93
|
+
return useSyncExternalStore(subscribe, read, read);
|
|
94
|
+
}
|
|
95
|
+
function useSignal(def) {
|
|
96
|
+
const client = useClient2();
|
|
97
|
+
const value = useValue(def);
|
|
98
|
+
const set = useCallback2((next) => client.set(def, next), [client, def]);
|
|
99
|
+
return [value, set];
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
RivaProvider2 as RivaProvider,
|
|
103
|
+
useClient2 as useClient,
|
|
104
|
+
useMutation,
|
|
105
|
+
useQuery,
|
|
106
|
+
useSignal,
|
|
107
|
+
useValue
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
//# debugId=04EEA62034FA530164756E2164756E21
|
|
111
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/use-mutation.ts", "../src/use-query.ts", "../src/suspend.ts", "../src/use-signal.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type {\n\tMutation,\n\tQueryStatus,\n\tRunOptions,\n\tVarsArgs,\n} from '@softistx/riva';\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport { useClient } from './context';\n\nexport interface MutationResult<Vars, Data> {\n\t/** Runs the mutation: resolves with the server data, rejects with the error (also in `error`). */\n\trun: (...args: VarsArgs<Vars, [opts?: RunOptions]>) => Promise<Data>;\n\tstatus: QueryStatus;\n\tdata: Data | undefined;\n\terror: unknown;\n\tisPending: boolean;\n\t/** Back to `idle`; the result of a run still in flight is dropped from the state. */\n\treset: () => void;\n}\n\ninterface Settled<Data> {\n\tstatus: QueryStatus;\n\tdata: Data | undefined;\n\terror: unknown;\n}\n\nconst IDLE: Settled<never> = Object.freeze({\n\tstatus: 'idle',\n\tdata: undefined,\n\terror: undefined,\n});\n\n/**\n * Runs a mutation with its own status, data and error, held by this hook\n * instance. A run finishes when its component unmounts; it just stops\n * reporting. When runs overlap, the latest one owns the state.\n */\nexport function useMutation<Vars, Data>(\n\tdef: Mutation<Vars, Data>,\n): MutationResult<Vars, Data> {\n\tconst client = useClient();\n\tconst [state, setState] = useState<Settled<Data>>(IDLE);\n\tconst live = useRef(false);\n\tconst latest = useRef(0);\n\tuseEffect(() => {\n\t\tlive.current = true;\n\t\treturn () => {\n\t\t\tlive.current = false;\n\t\t};\n\t}, []);\n\tconst report = useCallback((id: number, next: Settled<Data>) => {\n\t\tif (live.current && id === latest.current) setState(next);\n\t}, []);\n\tconst run = useCallback(\n\t\tasync (...args: VarsArgs<Vars, [opts?: RunOptions]>) => {\n\t\t\tconst id = ++latest.current;\n\t\t\treport(id, { status: 'pending', data: undefined, error: undefined });\n\t\t\ttry {\n\t\t\t\tconst data = await (\n\t\t\t\t\tdef.run as (c: typeof client, ...a: unknown[]) => Promise<Data>\n\t\t\t\t)(client, ...args);\n\t\t\t\treport(id, { status: 'success', data, error: undefined });\n\t\t\t\treturn data;\n\t\t\t} catch (error) {\n\t\t\t\treport(id, { status: 'error', data: undefined, error });\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t},\n\t\t[client, def, report],\n\t);\n\tconst reset = useCallback(() => {\n\t\tlatest.current++;\n\t\tsetState(IDLE);\n\t}, []);\n\treturn useMemo(\n\t\t() => ({ ...state, run, reset, isPending: state.status === 'pending' }),\n\t\t[state, run, reset],\n\t);\n}\n",
|
|
6
|
+
"import type { Query, QueryState } from '@softistx/riva';\nimport { useMemo } from 'react';\nimport { type AnyQuery, useQueryState } from './query-state';\nimport { suspend } from './suspend';\n\nexport interface UseQueryOptions {\n\t/** `false` observes nothing and reads `idle`. Default true. */\n\tenabled?: boolean;\n\t/** Suspends while there is no data, and throws an error with no data to the nearest error boundary. Default false. */\n\tsuspense?: boolean;\n}\n\nexport type QueryResult<S> = S & {\n\t/** Marks the entry stale and fetches it; never rejects, read the error from the state. */\n\trefetch: () => Promise<void>;\n};\n\n/** The state of a `suspense: true` query: its data is there. */\nexport type SuspendedState<S extends QueryState<unknown>> = Omit<S, 'data'> & {\n\tdata: Exclude<S['data'], undefined>;\n};\n\n// biome-ignore lint/suspicious/noConfusingVoidType: void variables are optional at call sites\ntype Void = void | undefined;\ntype Args<V, O> = [V] extends [Void]\n\t? [vars?: V, opts?: O]\n\t: [vars: V, opts?: O];\ntype SuspenseArgs<V, O> = [V] extends [Void]\n\t? [vars: V | undefined, opts: O]\n\t: [vars: V, opts: O];\n\n/** A query's state for these variables, fetched while the component is mounted. */\nexport function useQuery<V, D, S extends QueryState<unknown>>(\n\tdef: Query<V, D, S>,\n\t...args: SuspenseArgs<V, UseQueryOptions & { suspense: true }>\n): QueryResult<SuspendedState<S>>;\nexport function useQuery<V, D, S extends QueryState<unknown>>(\n\tdef: Query<V, D, S>,\n\t...args: Args<V, UseQueryOptions>\n): QueryResult<S>;\nexport function useQuery(\n\tdef: AnyQuery,\n\tvars?: unknown,\n\topts?: UseQueryOptions,\n): QueryResult<QueryState<unknown>> {\n\tconst enabled = opts?.enabled !== false;\n\tconst q = useQueryState<QueryState<unknown>>(\n\t\tdef,\n\t\tvars,\n\t\tenabled,\n\t\topts?.suspense === true,\n\t);\n\tconst result = useMemo(\n\t\t() => ({ ...q.state, refetch: q.refetch }),\n\t\t[q.state, q.refetch],\n\t);\n\tlet { state } = q;\n\tif (enabled && opts?.suspense && state.data === undefined) {\n\t\tif (state.status === 'success' && !state.isFetching) {\n\t\t\t// Stored data the cache cannot read (evicted): start its fetch now,\n\t\t\t// not in the mount effect, so the first render never shows undefined.\n\t\t\tq.client.queries.missing(def, q.vars);\n\t\t\tstate = (q.client.get as (d: AnyQuery, v: unknown) => typeof state)(\n\t\t\t\tdef,\n\t\t\t\tq.vars,\n\t\t\t);\n\t\t}\n\t\tif (state.status === 'error') throw state.error;\n\t\tif (state.status !== 'success' || state.isFetching)\n\t\t\tthrow suspend(q.client, def, q.key, q.vars);\n\t}\n\treturn result;\n}\n",
|
|
7
|
+
"import type { Query, RivaClient } from '@softistx/riva';\n\n// Held on the client's slots: a request's pending fetches die with its client.\nconst SLOT = {};\n\n/**\n * The one promise React waits on for a query entry: the core's `ensure`, held\n * until it settles so every render that suspends gets the same promise. A\n * failure is swallowed here; the next render reads it from the state.\n */\nexport function suspend(\n\tclient: RivaClient,\n\t// biome-ignore lint/suspicious/noExplicitAny: any query definition\n\tdef: Query<any, any, any>,\n\tkey: string,\n\tvars: unknown,\n): Promise<void> {\n\tlet map = client.slots.get(SLOT) as Map<string, Promise<void>> | undefined;\n\tif (!map) {\n\t\tmap = new Map();\n\t\tclient.slots.set(SLOT, map);\n\t}\n\tlet promise = map.get(key);\n\tif (!promise) {\n\t\tconst entries = map;\n\t\tconst done = () => void entries.delete(key);\n\t\tpromise = (def.ensure as (c: RivaClient, v: unknown) => Promise<unknown>)(\n\t\t\tclient,\n\t\t\tvars,\n\t\t).then(done, done);\n\t\tentries.set(key, promise);\n\t}\n\treturn promise;\n}\n",
|
|
8
|
+
"import type { Readable, Signal } from '@softistx/riva';\nimport { useCallback, useSyncExternalStore } from 'react';\nimport { useClient } from './context';\n\n/** The value of a signal or computed, re-rendering when it changes. */\nexport function useValue<T>(def: Readable<T>): T {\n\tconst client = useClient();\n\tconst subscribe = useCallback(\n\t\t(notify: () => void) => client.subscribe(def, notify),\n\t\t[client, def],\n\t);\n\tconst read = () => client.get(def);\n\treturn useSyncExternalStore(subscribe, read, read);\n}\n\n/** A signal's value and its setter (a value or an updater, as the signal's own `set`). */\nexport function useSignal<T>(\n\tdef: Signal<T>,\n): [value: T, set: (value: T | ((prev: T) => T)) => void] {\n\tconst client = useClient();\n\tconst value = useValue(def);\n\tconst set = useCallback(\n\t\t(next: T | ((prev: T) => T)) => client.set(def, next),\n\t\t[client, def],\n\t);\n\treturn [value, set];\n}\n"
|
|
9
|
+
],
|
|
10
|
+
"mappings": "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",
|
|
11
|
+
"debugId": "04EEA62034FA530164756E2164756E21",
|
|
12
|
+
"names": []
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/infinite/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,cAAc,EACnB,KAAK,uBAAuB,EAC5B,gBAAgB,GAChB,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import"../chunks/index-zww0a7j9.js";
|
|
2
|
+
import {
|
|
3
|
+
useQueryState
|
|
4
|
+
} from "../chunks/index-gzs9smr9.js";
|
|
5
|
+
|
|
6
|
+
// src/infinite/use-infinite-query.ts
|
|
7
|
+
import { useCallback, useMemo } from "react";
|
|
8
|
+
function useInfiniteQuery(def, vars, opts) {
|
|
9
|
+
const q = useQueryState(def, vars, opts?.enabled !== false);
|
|
10
|
+
const { client, vars: v } = q;
|
|
11
|
+
const more = (method) => def[method](client, v);
|
|
12
|
+
const fetchNext = useCallback(() => more("fetchNext"), [client, def, v]);
|
|
13
|
+
const fetchPrevious = useCallback(() => more("fetchPrevious"), [client, def, v]);
|
|
14
|
+
return useMemo(() => ({ ...q.state, fetchNext, fetchPrevious, refetch: q.refetch }), [q.state, fetchNext, fetchPrevious, q.refetch]);
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
useInfiniteQuery
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
//# debugId=9124118E49219B8564756E2164756E21
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/infinite/use-infinite-query.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { Infinite, InfiniteState } from '@softistx/riva/infinite';\nimport { useCallback, useMemo } from 'react';\nimport { type AnyQuery, useQueryState } from '../query-state';\n\nexport interface UseInfiniteQueryOptions {\n\t/** `false` observes nothing and reads `idle`. Default true. */\n\tenabled?: boolean;\n}\n\nexport type InfiniteResult<Page, Node> = InfiniteState<Page, Node> & {\n\t/** Appends the page after the last one; a no-op without a next page. */\n\tfetchNext: () => Promise<void>;\n\t/** Prepends the page before the first one; a no-op without a previous page. */\n\tfetchPrevious: () => Promise<void>;\n\t/** Re-fetches every loaded page; never rejects, read the error from the state. */\n\trefetch: () => Promise<void>;\n};\n\n// biome-ignore lint/suspicious/noConfusingVoidType: void variables are optional at call sites\ntype Void = void | undefined;\ntype Args<V> = [V] extends [Void]\n\t? [vars?: V, opts?: UseInfiniteQueryOptions]\n\t: [vars: V, opts?: UseInfiniteQueryOptions];\n\n/** An infinite list's state for these variables, with the actions that load more pages. */\nexport function useInfiniteQuery<V, Page, Node>(\n\tdef: Infinite<V, Page, Node>,\n\t...args: Args<V>\n): InfiniteResult<Page, Node>;\nexport function useInfiniteQuery(\n\tdef: Infinite<unknown, unknown, unknown>,\n\tvars?: unknown,\n\topts?: UseInfiniteQueryOptions,\n): InfiniteResult<unknown, unknown> {\n\tconst q = useQueryState<InfiniteState<unknown, unknown>>(\n\t\tdef as AnyQuery,\n\t\tvars,\n\t\topts?.enabled !== false,\n\t);\n\tconst { client, vars: v } = q;\n\tconst more = (method: 'fetchNext' | 'fetchPrevious') =>\n\t\t(def[method] as (c: typeof client, v: unknown) => Promise<void>)(client, v);\n\tconst fetchNext = useCallback(() => more('fetchNext'), [client, def, v]);\n\tconst fetchPrevious = useCallback(\n\t\t() => more('fetchPrevious'),\n\t\t[client, def, v],\n\t);\n\treturn useMemo(\n\t\t() => ({ ...q.state, fetchNext, fetchPrevious, refetch: q.refetch }),\n\t\t[q.state, fetchNext, fetchPrevious, q.refetch],\n\t);\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;AACA;AA4BO,SAAS,gBAAgB,CAC/B,KACA,MACA,MACmC;AAAA,EACnC,MAAM,IAAI,cACT,KACA,MACA,MAAM,YAAY,KACnB;AAAA,EACA,QAAQ,QAAQ,MAAM,MAAM;AAAA,EAC5B,MAAM,OAAO,CAAC,WACZ,IAAI,QAA4D,QAAQ,CAAC;AAAA,EAC3E,MAAM,YAAY,YAAY,MAAM,KAAK,WAAW,GAAG,CAAC,QAAQ,KAAK,CAAC,CAAC;AAAA,EACvE,MAAM,gBAAgB,YACrB,MAAM,KAAK,eAAe,GAC1B,CAAC,QAAQ,KAAK,CAAC,CAChB;AAAA,EACA,OAAO,QACN,OAAO,KAAK,EAAE,OAAO,WAAW,eAAe,SAAS,EAAE,QAAQ,IAClE,CAAC,EAAE,OAAO,WAAW,eAAe,EAAE,OAAO,CAC9C;AAAA;",
|
|
8
|
+
"debugId": "9124118E49219B8564756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Infinite, InfiniteState } from '@softistx/riva/infinite';
|
|
2
|
+
export interface UseInfiniteQueryOptions {
|
|
3
|
+
/** `false` observes nothing and reads `idle`. Default true. */
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export type InfiniteResult<Page, Node> = InfiniteState<Page, Node> & {
|
|
7
|
+
/** Appends the page after the last one; a no-op without a next page. */
|
|
8
|
+
fetchNext: () => Promise<void>;
|
|
9
|
+
/** Prepends the page before the first one; a no-op without a previous page. */
|
|
10
|
+
fetchPrevious: () => Promise<void>;
|
|
11
|
+
/** Re-fetches every loaded page; never rejects, read the error from the state. */
|
|
12
|
+
refetch: () => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
type Void = void | undefined;
|
|
15
|
+
type Args<V> = [V] extends [Void] ? [vars?: V, opts?: UseInfiniteQueryOptions] : [vars: V, opts?: UseInfiniteQueryOptions];
|
|
16
|
+
/** An infinite list's state for these variables, with the actions that load more pages. */
|
|
17
|
+
export declare function useInfiniteQuery<V, Page, Node>(def: Infinite<V, Page, Node>, ...args: Args<V>): InfiniteResult<Page, Node>;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=use-infinite-query.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-infinite-query.d.ts","sourceRoot":"","sources":["../../src/infinite/use-infinite-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAIvE,MAAM,WAAW,uBAAuB;IACvC,+DAA+D;IAC/D,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,MAAM,cAAc,CAAC,IAAI,EAAE,IAAI,IAAI,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG;IACpE,wEAAwE;IACxE,SAAS,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/B,+EAA+E;IAC/E,aAAa,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IACnC,kFAAkF;IAClF,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7B,CAAC;AAGF,KAAK,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC;AAC7B,KAAK,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,GAC9B,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,uBAAuB,CAAC,GAC1C,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,uBAAuB,CAAC,CAAC;AAE7C,2FAA2F;AAC3F,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAC7C,GAAG,EAAE,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,EAC5B,GAAG,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,GACd,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC"}
|