@spfn/core 0.3.0-beta.27 → 0.3.0-beta.28
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/contract/index.d.ts +1 -1
- package/dist/db/index.d.ts +39 -11
- package/dist/db/index.js +34 -14
- package/dist/db/index.js.map +1 -1
- package/dist/event/index.d.ts +3 -3
- package/dist/event/index.js +10 -0
- package/dist/event/index.js.map +1 -1
- package/dist/event/sse/client.d.ts +2 -2
- package/dist/event/sse/client.js +27 -17
- package/dist/event/sse/client.js.map +1 -1
- package/dist/event/sse/index.d.ts +3 -3
- package/dist/event/sse/index.js +115 -34
- package/dist/event/sse/index.js.map +1 -1
- package/dist/event/ws/client.d.ts +2 -2
- package/dist/event/ws/index.d.ts +3 -3
- package/dist/{router-D5B0cpVN.d.ts → router-CX5uVjmU.d.ts} +29 -1
- package/dist/server/index.d.ts +3 -3
- package/dist/server/index.js +115 -34
- package/dist/server/index.js.map +1 -1
- package/dist/state/client.d.ts +73 -11
- package/dist/state/client.js +231 -31
- package/dist/state/client.js.map +1 -1
- package/dist/state/index.js +11 -1
- package/dist/state/index.js.map +1 -1
- package/dist/state/react.d.ts +18 -3
- package/dist/state/react.js +11 -5
- package/dist/state/react.js.map +1 -1
- package/dist/{types-04gu3uMj.d.ts → types-CurMab2Q.d.ts} +19 -2
- package/dist/{types-CqTsDETV.d.ts → types-DncqXfWN.d.ts} +1 -1
- package/package.json +1 -1
- package/src/db/transaction/README.md +58 -35
- package/src/event/README.md +61 -14
- package/src/state/README.md +161 -35
package/src/state/README.md
CHANGED
|
@@ -23,7 +23,7 @@ import { Type } from '@sinclair/typebox'; // for .key() / .value() / .data() /
|
|
|
23
23
|
|
|
24
24
|
// browser — the router by type only
|
|
25
25
|
import { createStateClient } from '@spfn/core/state/client';
|
|
26
|
-
import { useServerState } from '@spfn/core/state/react';
|
|
26
|
+
import { useConnectionStatus, useServerState } from '@spfn/core/state/react';
|
|
27
27
|
import type { states } from '@/server/states';
|
|
28
28
|
```
|
|
29
29
|
|
|
@@ -191,7 +191,7 @@ With the default `path` `/state`:
|
|
|
191
191
|
| `getConsentState` | `GET /state/consent` → `{ at, value, data }`. Keyed: key fields in the query, validated by the route (missing → 400) |
|
|
192
192
|
| `putConsentState` | `PUT /state/consent`, only when `.write()` exists. Body = the input schema, keyed: key fields in the query. → `{ at, value, data }` |
|
|
193
193
|
| `getStates` | `GET /state` → `{ at, states: { consent: { value, data }, … } }` in declaration order — one for all states of the router |
|
|
194
|
-
| `states.events['state.consent.changed']` | the SSE event, payload `{ at, value, data, key?, subject? }` — `key` on a keyed state, `subject` unless shared |
|
|
194
|
+
| `states.events['state.consent.changed']` | the SSE event, payload `{ at, value, data, key?, subject? }` — `key` on a keyed state, `subject` unless shared. It declares `.canSubscribe` and `.filter` from `canRead` and the key, and `.initial`: a connection it allows receives the state's current frame — one `read()` for its subject and the key in its connect query — right after `connected` |
|
|
195
195
|
| `consent.changed` | the change signal, payload keyless `{ subject }`, keyed `{ ...keyFields, subject }`, shared `{ ...keyFields }` |
|
|
196
196
|
|
|
197
197
|
`data` is present exactly when `.data()` is declared, in every response and frame.
|
|
@@ -240,7 +240,11 @@ Every `read()` result is checked against `{ value, data }` as declared — `data
|
|
|
240
240
|
`.data()` is, no other field — with a schema built once per state when the router is built.
|
|
241
241
|
A result that does not match fails the read, write and snapshot routes with an `Error` (500)
|
|
242
242
|
naming the state and the first validation message, never the value; on the change signal it
|
|
243
|
-
is logged at warn and sends nothing; `grant` rejects with the same error and sends nothing.
|
|
243
|
+
is logged at warn and sends nothing; `grant` rejects with the same error and sends nothing. On
|
|
244
|
+
the stream's connect it is logged at warn naming the state's event (never the error), the state
|
|
245
|
+
is left out of `connected.initial` and sends no initial frame. The client's snapshot read then
|
|
246
|
+
fails too — the snapshot route reads every state — so the entry keeps its last value, `onError`
|
|
247
|
+
is called, and the state recovers with its next frame or the next connect.
|
|
244
248
|
|
|
245
249
|
A write whose `read()` then fails — it threw or returned an undeclared result — answers 500
|
|
246
250
|
and sends no frame. Without a transaction the write has already committed: there is no value
|
|
@@ -314,17 +318,15 @@ Every frame — of the write route, of the change signal and of `grant` — is s
|
|
|
314
318
|
| Where the read ran | Frame |
|
|
315
319
|
| --- | --- |
|
|
316
320
|
| outside a transaction | emitted at once and awaited; an emit that throws fails the write or `grant`; on the change signal it is logged at warn and `changed.emit` resolves |
|
|
317
|
-
| inside a transaction | queued with `onAfterCommit` and published after the root transaction commits; never when the
|
|
321
|
+
| inside a transaction | queued with `onAfterCommit` and published after the root transaction commits; never when the transaction that read it, or any transaction around it, rolls back |
|
|
318
322
|
|
|
319
323
|
- The frame carries the value read inside the transaction, with that read's `at`; nothing is
|
|
320
324
|
read again after the commit. The response is the same snapshot.
|
|
321
325
|
- So a `changed.emit` or a `grant` made inside a transaction is deferred to its commit, like a
|
|
322
326
|
write.
|
|
323
|
-
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
the frame read inside it, because after-commit callbacks belong to the root; emit the signal
|
|
327
|
-
from the outer transaction, or after it, when an inner one may roll back.
|
|
327
|
+
- A `Transactional()` nested in an outer `runInTransaction` is a savepoint: its frame is
|
|
328
|
+
handed to the outer transaction and published at the outer commit, or dropped if the
|
|
329
|
+
handler throws.
|
|
328
330
|
- A deferred emit runs outside the transaction context; an error it throws is logged by the
|
|
329
331
|
transaction runner and reaches nobody.
|
|
330
332
|
|
|
@@ -430,6 +432,10 @@ write and snapshot routes, keeps one SSE stream open for every state, and decide
|
|
|
430
432
|
only** — `import type { states }` — so no server code reaches the browser bundle;
|
|
431
433
|
`@spfn/core/state/client` imports nothing from `@spfn/core/state` at runtime.
|
|
432
434
|
|
|
435
|
+
A page load costs one snapshot read, and the server makes it: the server component's render,
|
|
436
|
+
handed to the client as `initial`. The browser makes no snapshot `GET` of its own — the stream
|
|
437
|
+
serves each allowed state's current frame on connect ([The stream](#the-stream)).
|
|
438
|
+
|
|
433
439
|
```typescript
|
|
434
440
|
// src/client/state.ts
|
|
435
441
|
import { createStateClient } from '@spfn/core/state/client';
|
|
@@ -480,28 +486,70 @@ of fields, goes to `onError`, and the subscription is dropped without reopening
|
|
|
480
486
|
`write` on a state the manifest marks `write: false` rejects naming the state, before any
|
|
481
487
|
request. A first snapshot that fails goes to `onError` (or rejects the call that needed it).
|
|
482
488
|
While subscriptions wait on it, it is read again every `retryDelay` (default 3000 ms, the SSE
|
|
483
|
-
client's `reconnectDelay`), each failure to `onError`, until it succeeds, `close()` runs,
|
|
484
|
-
nothing is subscribed
|
|
485
|
-
|
|
489
|
+
client's `reconnectDelay`), each failure to `onError`, until it succeeds, `close()` runs,
|
|
490
|
+
nothing is subscribed, or `maxRetries` retries failed too (default `0`: no limit) — then the
|
|
491
|
+
status is `'failed'` and no retry is set ([Connection status](#connection-status)); a `get`,
|
|
492
|
+
`write` or `snapshot` that reads it first serves the waiting subscriptions too — whichever
|
|
493
|
+
call brings the manifest opens the stream. `close()` while it is
|
|
486
494
|
in flight discards its answer, and a `get` or `write` waiting on it rejects: the client was
|
|
487
495
|
closed.
|
|
488
496
|
|
|
489
|
-
|
|
497
|
+
The manifest may also come from `initial` — the snapshot the server rendered with (below). Such a
|
|
498
|
+
client knows it from construction: nothing above waits, a refused `subscribe` goes to `onError` at
|
|
499
|
+
once, and the first `subscribe` opens the stream without reading the snapshot first.
|
|
500
|
+
|
|
501
|
+
### `createStateClient<typeof states>({ sse, api?, onError?, retryDelay?, maxRetries?, initial? })`
|
|
502
|
+
|
|
503
|
+
`initial: { snapshot, query? }` is the snapshot a server render read — `api.getStates`'s answer —
|
|
504
|
+
and the query it was read with (default `{}`). The client installs its manifest and applies each
|
|
505
|
+
state it holds at construction, keyed states under the query's key, so `current()` answers at once
|
|
506
|
+
and the first `subscribe` opens the stream without reading the snapshot first. Nothing is marked
|
|
507
|
+
`'revoked'` from it: an omission in the render's snapshot is not a denial by the stream. The
|
|
508
|
+
snapshot type is the router's own — another router's does not compile — and comes from the router
|
|
509
|
+
type parameter: without `<typeof states>` the client is untyped, as before. Pass what
|
|
510
|
+
`api.getStates` answered, not a hand-built snapshot: one whose manifest names a state the server
|
|
511
|
+
does not serve keeps the stream from opening, as the SSE handler refuses that event. A server component reads
|
|
512
|
+
the snapshot and passes it, with its query, as a prop to the client component that builds the
|
|
513
|
+
client:
|
|
514
|
+
|
|
515
|
+
```tsx
|
|
516
|
+
// app/[workspaceId]/page.tsx — a server component
|
|
517
|
+
const query = { workspaceId };
|
|
518
|
+
const snapshot = await api.getStates.call({ query });
|
|
519
|
+
|
|
520
|
+
return <Shell initial={{ snapshot, query }} workspaceId={workspaceId} />;
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
```tsx
|
|
524
|
+
'use client';
|
|
525
|
+
import type { StateClientInitial } from '@spfn/core/state/client';
|
|
526
|
+
|
|
527
|
+
export function Shell({ initial, workspaceId }: { initial: StateClientInitial<typeof states>; workspaceId: string })
|
|
528
|
+
{
|
|
529
|
+
const [state] = useState(() => createStateClient<typeof states>({ sse: createAuthSSEClient(), initial }));
|
|
530
|
+
useEffect(() => () => state.close(), [state]);
|
|
531
|
+
// useServerState(state, 'runtime', { workspaceId }) is 'ready' on the first render
|
|
532
|
+
}
|
|
533
|
+
```
|
|
490
534
|
|
|
491
535
|
| Member | Behaviour |
|
|
492
536
|
| --- | --- |
|
|
493
537
|
| `get(id, key?)` | `GET` `get<Id>State`, key fields in the query. The answer is applied under the ordering rule and returned |
|
|
494
538
|
| `snapshot(query?)` | `GET getStates`, by default with the stream's query. Each state in the answer is applied, and each subscribed state it omits is marked `'revoked'` (below); the snapshot is returned. A rejected fetch rejects and changes nothing |
|
|
495
|
-
| `subscribe(id, key?, listener)` | Registers the listener for `(id, key)` and returns its unsubscribe (idempotent). The client's first subscriber reads the snapshot, then opens the stream (below) |
|
|
539
|
+
| `subscribe(id, key?, listener)` | Registers the listener for `(id, key)` and returns its unsubscribe (idempotent). The listener is called when the entry's `status`, `value` or `data` changes — never for an arrival that differs only by a newer `at` (Ordering, below). The client's first subscriber reads the snapshot, then opens the stream (below) |
|
|
496
540
|
| `write(id, input, key?)` | `put<Id>State` with the input as the body, key fields in the query. The answer is applied at once — before the frame the write also sends — and returned. Typed only for a state with a write, and requires the key on a keyed one |
|
|
497
541
|
| `current(id, key?)` | Synchronous: `{ at, value, data?, status }`, `{ status: 'revoked' }` for a state revoked before any value arrived, or `undefined`. The same frozen object until a change is applied |
|
|
498
|
-
| `
|
|
542
|
+
| `connection()` | Synchronous: the connection status ([Connection status](#connection-status)), a frozen object, the same reference until it changes |
|
|
543
|
+
| `subscribeConnection(listener)` | Registers `listener(status)`, called with the new status on every change, after `connection()` returns it; returns its unsubscribe (idempotent). The same function subscribed twice is two subscriptions |
|
|
544
|
+
| `retry()` | When `'failed'` or `'reconnecting'`: reads the first snapshot now (its retry count reset), or, once the manifest is known, reopens the stream now. A no-op when `'connected'`, while the first snapshot is in flight, and with nothing subscribed — after `close()` too, until a new `subscribe` |
|
|
545
|
+
| `close()` | Closes the stream, drops every listener — connection listeners too — and stops listening to `online`; the status is back to `'reconnecting'` 0. Idempotent. A `subscribe` after it starts the client again, without `online`: `retry()` restarts it as before |
|
|
499
546
|
|
|
500
547
|
`onError` receives a failure of something the client ran on its own — the first snapshot, the
|
|
501
548
|
snapshot after the stream opened, the read after a grant — and a refused `subscribe` (default
|
|
502
549
|
`console.error`). Calls the app makes reject.
|
|
503
550
|
|
|
504
|
-
A listener that throws is logged at warn with the state id and does not stop the others
|
|
551
|
+
A listener that throws is logged at warn with the state id and does not stop the others; so is
|
|
552
|
+
a connection listener.
|
|
505
553
|
|
|
506
554
|
Give the state client an `SSEClient` of its own: an `SSEClient` holds one subscription, and
|
|
507
555
|
the state client's supersedes any other made on it.
|
|
@@ -510,16 +558,24 @@ the state client's supersedes any other made on it.
|
|
|
510
558
|
|
|
511
559
|
Per `(id, key)`, every arrival — a snapshot entry, a frame, a read or a write answer — goes
|
|
512
560
|
through one rule. `at` is the time the server's read started, so a larger `at` is a later value.
|
|
561
|
+
The client keeps, per `(id, key)`, an ordering stamp — the newest `at` seen — apart from the
|
|
562
|
+
entry, and compares each arrival against it.
|
|
513
563
|
|
|
514
564
|
| Arrival | Result |
|
|
515
565
|
| --- | --- |
|
|
516
|
-
| no current entry, or `at` newer than the
|
|
517
|
-
| `at` equal to the
|
|
518
|
-
| `at` older than the
|
|
519
|
-
| a snapshot older than one state's
|
|
566
|
+
| no current entry, or `at` newer than the stamp | the ordering stamp advances; applied and notified only when `status`, `value` or `data` differ (deep-equal) |
|
|
567
|
+
| `at` equal to the stamp | one read stamps one `at`, so this is the same value: applied and notified only when `value` or `data` differ (deep-equal), or its status differs from the entry's |
|
|
568
|
+
| `at` older than the stamp | dropped |
|
|
569
|
+
| a snapshot older than one state's stamp | that state keeps its entry; the others take the snapshot |
|
|
520
570
|
| a write answer | applied like a frame — its own frame, arriving later with the same `at` and value, changes nothing |
|
|
521
571
|
| a frame for a key not subscribed here | dropped (the server already filters by the connect query; this is the second guard) |
|
|
522
572
|
|
|
573
|
+
Every stream open — the first, a reconnect, a reopen on a key change — brings each state's
|
|
574
|
+
current value with a newer `at`: the stream's initial frame, or the snapshot re-read when the
|
|
575
|
+
stream did not serve it. A listener hears it only when `status`, `value` or `data` changed. The
|
|
576
|
+
`at` of `current()` is the `at` of the read that last changed the value; a later read that repeats
|
|
577
|
+
it advances only the stamp, so an older arrival after it is still dropped.
|
|
578
|
+
|
|
523
579
|
The ordering rule decides the value; the status is decided apart (next section): an arrival
|
|
524
580
|
on a revoked `(id, key)` supplies its value and leaves it `'revoked'`.
|
|
525
581
|
|
|
@@ -532,23 +588,79 @@ changed.
|
|
|
532
588
|
- **One stream per client**, opened when the first subscriber arrives and closed when the last
|
|
533
589
|
leaves. Subscriptions made in the same task (components mounting together, a StrictMode
|
|
534
590
|
unmount-remount) are batched into one change.
|
|
535
|
-
- **Snapshot first**: the first stream opens after the first snapshot, whose manifest it needs
|
|
591
|
+
- **Snapshot first**: the first stream opens after the first snapshot, whose manifest it needs —
|
|
592
|
+
at once on a client built with `initial`, which already holds it.
|
|
536
593
|
- **Every event requested**: `events=` lists `state.<id>.changed` for every state of the
|
|
537
594
|
manifest, in its order, never only the subscribed ones. The server's `grant` activates only an event the
|
|
538
595
|
connection requested, so a state subscribed later would otherwise be ungrantable.
|
|
539
596
|
- **Keys in the query**: the connect query is the union of the subscribed keys' fields (SSE
|
|
540
597
|
`subscribe({ query })`). A new key value, or the last subscriber of one leaving, closes the
|
|
541
598
|
stream and opens it with the new query.
|
|
542
|
-
- **
|
|
543
|
-
while no stream was open is lost, whether its key changed or not
|
|
544
|
-
|
|
545
|
-
read
|
|
599
|
+
- **Current frames on every open** — the first, a reconnect, a reopen on a key change. A frame
|
|
600
|
+
sent while no stream was open is lost, whether its key changed or not. The server covers that
|
|
601
|
+
gap: right after the `connected` frame it writes each allowed state's current frame — one
|
|
602
|
+
`read()` for the subject and the carried key, its `at` the read's start — before any live
|
|
603
|
+
frame, and `connected.initial` names the states it served. They are ordinary frames under the
|
|
604
|
+
ordering rule, so whatever changed between the render and the open arrives as a newer `at`.
|
|
605
|
+
The client reads the snapshot only when a state it requested is neither in `initial` nor in
|
|
606
|
+
`deniedEvents` — a state whose read threw on the server — or when `initial` is absent (a
|
|
607
|
+
server before it); a state in `deniedEvents` it watches on the carried key is `'revoked'`.
|
|
608
|
+
(It all runs on the stream's `connected`, not at `onReconnect`: a read before the new stream
|
|
609
|
+
is live would leave the same gap.)
|
|
546
610
|
- **One value per key field per connection.** A connect query carries one `workspaceId`; when
|
|
547
611
|
two subscribed keys disagree on a field, the most recently subscribed key is carried. A key
|
|
548
612
|
the stream does not carry is read once (`get`) when the stream opens and receives no frames
|
|
549
613
|
until it is carried again. A screen that must follow two workspaces live uses a second
|
|
550
614
|
state client.
|
|
551
615
|
|
|
616
|
+
### Connection status
|
|
617
|
+
|
|
618
|
+
The client decides the retries — the SSE client's reconnects of the stream, its own of the
|
|
619
|
+
first snapshot — so it says where they stand. `connection()` is one of:
|
|
620
|
+
|
|
621
|
+
| Status | Holds |
|
|
622
|
+
| --- | --- |
|
|
623
|
+
| `{ status: 'connected' }` | a stream is open (the SSE `onOpen`) |
|
|
624
|
+
| `{ status: 'reconnecting', attempt }` | no stream open — not yet, or nothing subscribed — or the SSE client retrying a dropped one: `attempt` is its count (`onReconnect`), reset when a stream opens; `0` before the first stream opens, while the first snapshot is read or retried, and after `retry()` |
|
|
625
|
+
| `{ status: 'failed' }` | the retries are exhausted: the SSE client's `maxReconnectAttempts` (its `onClose`, the attempts spent), or `maxRetries` for the first snapshot. Nothing more is tried until a restart (below) or, with the manifest unknown, a `get`, `write` or `snapshot` that brings it |
|
|
626
|
+
|
|
627
|
+
- **Start value** `{ status: 'reconnecting', attempt: 0 }`, a client built with `initial` too:
|
|
628
|
+
its entries are ready, its stream is not.
|
|
629
|
+
- A stream error alone changes nothing — a reconnect or the end of the attempts follows. A
|
|
630
|
+
stream the client closes itself — a key change, `close()` — is no failure; a reopen on a
|
|
631
|
+
key change while connected stays `'connected'`.
|
|
632
|
+
- **Restarts.** `retry()` and the browser's `online` event — listened to from construction
|
|
633
|
+
where `addEventListener` exists — restart when `'failed'` or `'reconnecting'`; a `subscribe`
|
|
634
|
+
restarts when `'failed'`, reopening a failed stream even when the query is unchanged. An
|
|
635
|
+
unsubscribe that leaves subscribers never does: the status stays `'failed'` (the last one:
|
|
636
|
+
below). With the manifest unknown, any call that brings it — `get`, `write`, `snapshot` —
|
|
637
|
+
opens the stream; with the manifest known, those calls reopen nothing while `'failed'`. The
|
|
638
|
+
first snapshot is read now with its count reset (one in flight is not doubled), or the stream
|
|
639
|
+
is reopened now as a new SSE subscription, whose attempts start from 0 — a pending reconnect
|
|
640
|
+
timer is superseded. The status is `'reconnecting'` 0 until it opens.
|
|
641
|
+
- **The last unsubscribe** closes the stream: `'reconnecting'` 0, the start value, and the
|
|
642
|
+
first snapshot's retry count starts over for the next wait.
|
|
643
|
+
- **`close()`** sets `'reconnecting'` 0, drops the connection listeners and removes the
|
|
644
|
+
`online` listener; its own teardown is not `'failed'`. A `subscribe` after it starts the
|
|
645
|
+
client again, and `retry()` restarts it; `online` is not listened to again.
|
|
646
|
+
|
|
647
|
+
```tsx
|
|
648
|
+
const connection = useConnectionStatus(state);
|
|
649
|
+
|
|
650
|
+
if (connection.status === 'failed')
|
|
651
|
+
{
|
|
652
|
+
return <Banner action={() => state.retry()}>The server has a problem.</Banner>;
|
|
653
|
+
}
|
|
654
|
+
if (connection.status === 'reconnecting' && connection.attempt > 0)
|
|
655
|
+
{
|
|
656
|
+
return <Banner>The server is not answering, retrying…</Banner>;
|
|
657
|
+
}
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
`maxRetries` bounds the first snapshot only; bound the stream's reconnects with the SSE
|
|
661
|
+
client's `maxReconnectAttempts` (`createAuthSSEClient({ maxReconnectAttempts: 5 })`). Both
|
|
662
|
+
default to `0`, retrying forever: the status never turns `'failed'`.
|
|
663
|
+
|
|
552
664
|
### `'revoked'` — the server does not serve this to you
|
|
553
665
|
|
|
554
666
|
One status says the server stopped serving a `(id, key)` to this subject, however the client
|
|
@@ -558,12 +670,13 @@ value has arrived yet.
|
|
|
558
670
|
| Arrival | Client |
|
|
559
671
|
| --- | --- |
|
|
560
672
|
| `subscription.revoked` for `state.<id>.changed` | the entry of `id` for **the key the connection carries** (keyless: its one entry) gets `status: 'revoked'`, value kept, listeners notified. The server scopes a revoke by the connect query, so a shadowed key of the same state is untouched |
|
|
673
|
+
| `connected.deniedEvents` names a subscribed state — keyless, or keyed on the key the connection carries | `'revoked'`, value kept, listeners notified: the connect check (`canRead` false, or the key absent) denied it. A shadowed key of the same state is untouched |
|
|
561
674
|
| a snapshot that omits a subscribed state — keyless, or keyed with its key in the query | `'revoked'`, value kept if one exists, listeners notified. This is also a state denied at connect (`canRead` false, absent from the snapshot), and a reconnect after access was lost while the stream was down, when no revoke could arrive. A keyed state whose key the query did not carry is not marked |
|
|
562
675
|
| a frame, a read or a snapshot started before the revoke | supplies its value under the ordering rule; the status stays `'revoked'` |
|
|
563
676
|
| `subscription.granted` for it | clears the mark for the carried key and reads it once (`get`); the server also sends a frame, before or after the control frame. Both arrive `'ready'`, and either order ends at the latest value |
|
|
564
|
-
| the snapshot of a connection opened after the revoke, holding the state | clears the mark: that connection's own connect check served it |
|
|
677
|
+
| a frame — the initial one included — or the snapshot of a connection opened after the revoke, holding the state | clears the mark: that connection's own connect check served it |
|
|
565
678
|
| the read of a shadowed key when a connection opened after the revoke, answered | clears the mark the same way: the read route served it |
|
|
566
|
-
| an answer of such a read older than the
|
|
679
|
+
| an answer of such a read older than the stamp — a frame or a `write()` answer of the new connection landed first | its value is dropped under the ordering rule, while the mark is still lifted: the lift goes by the connection, not by `at` |
|
|
567
680
|
|
|
568
681
|
Every automatic read is tagged with the connection it was started for, which is how a
|
|
569
682
|
snapshot in flight across a revoke is told from one read after it.
|
|
@@ -575,29 +688,42 @@ snapshot in flight across a revoke is told from one read after it.
|
|
|
575
688
|
```
|
|
576
689
|
|
|
577
690
|
- Built on `useSyncExternalStore`: `subscribe` is `client.subscribe(id, key, cb)`,
|
|
578
|
-
`getSnapshot` is `client.current(id, key)`, and the server snapshot
|
|
579
|
-
|
|
691
|
+
`getSnapshot` is `client.current(id, key)`, and so is the server snapshot. A server render
|
|
692
|
+
opens nothing; it shows the value when the client was built with `initial`, `'loading'`
|
|
693
|
+
otherwise. Hydration matches when the server and the browser build their clients from the
|
|
694
|
+
same `initial`.
|
|
580
695
|
- The key is compared by its fields (stable-stringified), so `{ workspaceId }` written inline
|
|
581
696
|
does not resubscribe on every render; another `workspaceId` unsubscribes and subscribes. A
|
|
582
697
|
key never seen is `'loading'` until it has an entry; a key seen before shows its cached
|
|
583
|
-
entry as `'ready'` until the reopen snapshot lands (entries are not evicted).
|
|
698
|
+
entry as `'ready'` until the reopen's frame or snapshot lands (entries are not evicted).
|
|
584
699
|
- `write` is typed only for a state with a write, bound to the hook's key. At runtime it is on
|
|
585
700
|
every result — the type decides, so no render waits on the manifest — and on a state
|
|
586
701
|
without a write it rejects naming the state.
|
|
587
702
|
- `react` (`^18 || ^19`) is an **optional peer** of `@spfn/core`: only `@spfn/core/state/react`
|
|
588
703
|
imports it. `@spfn/core/state/client` is framework-neutral.
|
|
589
704
|
|
|
590
|
-
A page's first paint still comes from a server component reading the
|
|
591
|
-
|
|
705
|
+
A page's first paint still comes from a server component reading the snapshot with the app's
|
|
706
|
+
`api.` client; handed to the client as `initial`, the same read serves the hook's first render
|
|
707
|
+
and the client's first subscribe, the stream's initial frames bring anything newer, and the
|
|
708
|
+
hook takes over in the browser.
|
|
709
|
+
|
|
710
|
+
### `useConnectionStatus(client)`
|
|
711
|
+
|
|
712
|
+
`connection()` in a component, on `useSyncExternalStore`: `subscribe` is
|
|
713
|
+
`client.subscribeConnection(cb)`, `getSnapshot` is `client.connection()`, both stable per
|
|
714
|
+
client, so a render does not resubscribe. The server snapshot is `{ status: 'reconnecting',
|
|
715
|
+
attempt: 0 }` — what every client starts with, so hydration matches.
|
|
592
716
|
|
|
593
717
|
### Types
|
|
594
718
|
|
|
595
719
|
From `@spfn/core/state/client`: `createStateClient`, `StateClient<TStates>`,
|
|
596
|
-
`StateClientConfig`, `
|
|
597
|
-
`
|
|
720
|
+
`StateClientConfig<TStates>` (`sse`, `api?`, `onError?`, `retryDelay?`, `maxRetries?`, `initial?`),
|
|
721
|
+
`ConnectionStatus`, `ConnectionListener`,
|
|
722
|
+
`StateClientInitial<TStates>` (`{ snapshot, query? }`), `StateFrame<V, D>`, `StateEntry<V, D>`
|
|
723
|
+
(a frame plus `status`, or `{ status: 'revoked' }` alone), `StateListener`, `StateId`, `StateKeyArgs`, `WritableStateId`,
|
|
598
724
|
`StateCatalog`, `StateFrameOf`, `InferStateData`, and — re-exported type-only from the server
|
|
599
725
|
module — `InferStateValue`, `InferStateKey`, `InferStateInput`. From `@spfn/core/state/react`:
|
|
600
|
-
`useServerState`, `ServerState<TStates, Id
|
|
726
|
+
`useServerState`, `ServerState<TStates, Id>`, `useConnectionStatus`.
|
|
601
727
|
|
|
602
728
|
---
|
|
603
729
|
|