@serfab/cadre-client 2.0.0 → 2.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/src/stores.ts CHANGED
@@ -1,59 +1,62 @@
1
- /**
2
- * The stores a cadre screen binds to. Each reads through a view (`CadreView`,
3
- * `ReachabilityView`), so it works over the Sereus client, an app's mock, or an app's
4
- * whole model when that has the same two members.
5
- */
6
- import type { CadreStatus, CadreView, InvitationReadiness, InvitationsView, Reachability, ReachabilityView } from './model.js';
7
- import { writable, type Readable } from './store.js';
8
- import { cadreOk } from './result.js';
9
- import { loader, type Loader } from './loader.js';
10
-
11
- /** How often a store re-reads with nothing announcing a change, while subscribed. */
12
- export interface StoreOptions {
13
- /** Milliseconds; 0 turns the poll off. Default 30 s. */
14
- pollMs?: number;
15
- }
16
-
17
- /**
18
- * The cadre: this device, its cadrons, and whether it is an enrolled owner. Re-read on every
19
- * change that has an event: this node starting or stopping, a cadron connecting or dropping, the
20
- * cadron preferences. The poll catches what has none: a cadron added or removed on another
21
- * device (it arrives by control replication), and a cadron's `lastSeenAt` ageing, so its
22
- * presence turns from recent to quiet. A store kept subscribed app-wide (a status badge) can poll
23
- * less often, or not at all if those can wait for the next screen.
24
- */
25
- export function cadreStore(view: CadreView, opts: StoreOptions = {}): Loader<CadreStatus> {
26
- return loader({
27
- load: () => view.cadre.status(),
28
- changes: onChange => view.changes.cadre(onChange),
29
- pollMs: opts.pollMs ?? 30_000,
30
- });
31
- }
32
-
33
- /**
34
- * Reachability: can anybody answer an invitation from this device, which relays it is
35
- * configured with, and whether the relay reservation holds — read live, because a
36
- * configured relay is not a working one and a reservation can be lost after it was
37
- * granted. Re-read when this device's addresses change; a slow backstop poll covers a
38
- * change no event announces (a machine of the party's coming up or going quiet).
39
- */
40
- export function reachabilityStore(view: ReachabilityView, opts: StoreOptions = {}): Loader<Reachability> {
41
- return loader({
42
- load: async () => cadreOk(await view.reachability.get()),
43
- changes: onChange => view.changes.reachability(onChange),
44
- debounceMs: 250,
45
- pollMs: opts.pollMs ?? 30_000,
46
- });
47
- }
48
-
49
- /**
50
- * Whether a new invitation to `strandId` is ready to hand out (`InvitationReadiness`), live: it
51
- * turns `ready` when a cadron comes to hold the strand.
52
- */
53
- export function invitationReadinessStore(view: InvitationsView, strandId: string, opts: { newStrand?: boolean } = {}): Readable<InvitationReadiness> {
54
- const read = () => view.invitations.readiness(strandId, opts);
55
- return writable<InvitationReadiness>(read(), set => {
56
- set(read());
57
- return view.changes.invitations(() => set(read()));
58
- });
59
- }
1
+ /**
2
+ * The stores a cadre screen binds to. Each reads through a view (`CadreView`,
3
+ * `ReachabilityView`), so it works over the Sereus client, an app's mock, or an app's
4
+ * whole model when that has the same two members.
5
+ */
6
+ import type { CadreStatus, CadreView, InvitationReadiness, InvitationsView, Reachability, ReachabilityView } from './model.js';
7
+ import { writable, type Readable } from './store.js';
8
+ import { cadreOk } from './result.js';
9
+ import { loader, type Loader } from './loader.js';
10
+
11
+ /** How often a store re-reads with nothing announcing a change, while subscribed. */
12
+ export interface StoreOptions {
13
+ /** Milliseconds; 0 turns the poll off. Default 30 s. */
14
+ pollMs?: number;
15
+ }
16
+
17
+ /**
18
+ * The cadre: this device, its cadrons, and whether it is an enrolled owner. Re-read on every
19
+ * change that has an event: this node starting or stopping, a cadron connecting or dropping, the
20
+ * cadron preferences. The poll catches what has none: a cadron added or removed on another
21
+ * device (it arrives by control replication), and a cadron's `lastSeenAt` ageing, so its
22
+ * presence turns from recent to quiet. A store kept subscribed app-wide (a status badge) can poll
23
+ * less often, or not at all if those can wait for the next screen.
24
+ */
25
+ export function cadreStore(view: CadreView, opts: StoreOptions = {}): Loader<CadreStatus> {
26
+ return loader({
27
+ load: () => view.cadre.status(),
28
+ changes: onChange => view.changes.cadre(onChange),
29
+ pollMs: opts.pollMs ?? 30_000,
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Reachability: can anybody answer an invitation from this device, which relays it is
35
+ * configured with, and whether the relay reservation holds — read live, because a
36
+ * configured relay is not a working one and a reservation can be lost after it was
37
+ * granted. Re-read when this device's addresses change; a slow backstop poll covers a
38
+ * change no event announces (a machine of the party's coming up or going quiet).
39
+ */
40
+ export function reachabilityStore(view: ReachabilityView, opts: StoreOptions = {}): Loader<Reachability> {
41
+ return loader({
42
+ load: async () => cadreOk(await view.reachability.get()),
43
+ changes: onChange => view.changes.reachability(onChange),
44
+ debounceMs: 250,
45
+ pollMs: opts.pollMs ?? 30_000,
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Whether the invitation `token` is ready to hand out (`InvitationReadiness`), live: it turns
51
+ * `ready` once one of the machines the invitation names can answer a join through it, and until
52
+ * then says what those machines are missing. The answer comes from cadre-core's probe of those
53
+ * machines, which the model asks in the background and asks again on its own ladder while the
54
+ * answer is still `waiting`; this store re-reads each time one lands.
55
+ */
56
+ export function invitationReadinessStore(view: InvitationsView, token: string): Readable<InvitationReadiness> {
57
+ const read = () => view.invitations.readiness(token);
58
+ return writable<InvitationReadiness>(read(), set => {
59
+ set(read());
60
+ return view.changes.invitations(() => set(read()));
61
+ });
62
+ }