@serfab/cadre-client 0.0.0-stage → 2.0.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/ADOPTING.md +220 -0
- package/README.md +129 -3
- package/dist/address.d.ts +45 -0
- package/dist/address.js +109 -0
- package/dist/address.js.map +1 -0
- package/dist/app-link/index.d.ts +16 -0
- package/dist/app-link/index.js +17 -0
- package/dist/app-link/index.js.map +1 -0
- package/dist/app-link/join-response.d.ts +31 -0
- package/dist/app-link/join-response.js +87 -0
- package/dist/app-link/join-response.js.map +1 -0
- package/dist/app-link/known-apps.d.ts +32 -0
- package/dist/app-link/known-apps.js +51 -0
- package/dist/app-link/known-apps.js.map +1 -0
- package/dist/app-link/link-request-store.d.ts +50 -0
- package/dist/app-link/link-request-store.js +101 -0
- package/dist/app-link/link-request-store.js.map +1 -0
- package/dist/app-link/links.d.ts +47 -0
- package/dist/app-link/links.js +76 -0
- package/dist/app-link/links.js.map +1 -0
- package/dist/app-link/recommend-links.d.ts +30 -0
- package/dist/app-link/recommend-links.js +33 -0
- package/dist/app-link/recommend-links.js.map +1 -0
- package/dist/app-link/recommendation-store.d.ts +54 -0
- package/dist/app-link/recommendation-store.js +99 -0
- package/dist/app-link/recommendation-store.js.map +1 -0
- package/dist/bootstrap.d.ts +106 -0
- package/dist/bootstrap.js +106 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/inbox.d.ts +10 -0
- package/dist/inbox.js +28 -0
- package/dist/inbox.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/invitation-text.d.ts +81 -0
- package/dist/invitation-text.js +80 -0
- package/dist/invitation-text.js.map +1 -0
- package/dist/lifecycle/background-runner.d.ts +94 -0
- package/dist/lifecycle/background-runner.js +278 -0
- package/dist/lifecycle/background-runner.js.map +1 -0
- package/dist/lifecycle/index.d.ts +9 -0
- package/dist/lifecycle/index.js +10 -0
- package/dist/lifecycle/index.js.map +1 -0
- package/dist/loader.d.ts +47 -0
- package/dist/loader.js +93 -0
- package/dist/loader.js.map +1 -0
- package/dist/model.d.ts +284 -0
- package/dist/model.js +22 -0
- package/dist/model.js.map +1 -0
- package/dist/node-code-text.d.ts +10 -0
- package/dist/node-code-text.js +30 -0
- package/dist/node-code-text.js.map +1 -0
- package/dist/result.d.ts +79 -0
- package/dist/result.js +84 -0
- package/dist/result.js.map +1 -0
- package/dist/sereus/client.d.ts +49 -0
- package/dist/sereus/client.js +420 -0
- package/dist/sereus/client.js.map +1 -0
- package/dist/sereus/edge-host.d.ts +22 -0
- package/dist/sereus/edge-host.js +61 -0
- package/dist/sereus/edge-host.js.map +1 -0
- package/dist/sereus/host.d.ts +57 -0
- package/dist/sereus/host.js +2 -0
- package/dist/sereus/host.js.map +1 -0
- package/dist/sereus/index.d.ts +8 -0
- package/dist/sereus/index.js +9 -0
- package/dist/sereus/index.js.map +1 -0
- package/dist/sereus/invitations.d.ts +16 -0
- package/dist/sereus/invitations.js +196 -0
- package/dist/sereus/invitations.js.map +1 -0
- package/dist/sereus/node-code.d.ts +52 -0
- package/dist/sereus/node-code.js +51 -0
- package/dist/sereus/node-code.js.map +1 -0
- package/dist/store.d.ts +27 -0
- package/dist/store.js +51 -0
- package/dist/store.js.map +1 -0
- package/dist/stores.d.ts +37 -0
- package/dist/stores.js +45 -0
- package/dist/stores.js.map +1 -0
- package/examples/README.md +20 -0
- package/examples/nativescript/cadrons-page.xml +24 -0
- package/examples/nativescript/cadrons-view-model.ts +68 -0
- package/examples/react-native/CadronList.tsx +204 -0
- package/examples/react-native/InvitationPanel.tsx +162 -0
- package/examples/react-native/useStore.ts +11 -0
- package/examples/svelte/CadronList.svelte +86 -0
- package/examples/svelte/InvitationPanel.svelte +79 -0
- package/package.json +65 -3
- package/src/address.ts +125 -0
- package/src/app-link/index.ts +45 -0
- package/src/app-link/join-response.ts +109 -0
- package/src/app-link/known-apps.ts +70 -0
- package/src/app-link/link-request-store.ts +129 -0
- package/src/app-link/links.ts +90 -0
- package/src/app-link/recommend-links.ts +49 -0
- package/src/app-link/recommendation-store.ts +132 -0
- package/src/bootstrap.ts +184 -0
- package/src/globals.d.ts +26 -0
- package/src/inbox.ts +36 -0
- package/src/index.ts +15 -0
- package/src/invitation-text.ts +156 -0
- package/src/lifecycle/background-runner.ts +363 -0
- package/src/lifecycle/index.ts +17 -0
- package/src/loader.ts +103 -0
- package/src/model.ts +299 -0
- package/src/node-code-text.ts +25 -0
- package/src/result.ts +131 -0
- package/src/sereus/client.ts +454 -0
- package/src/sereus/edge-host.ts +81 -0
- package/src/sereus/host.ts +56 -0
- package/src/sereus/index.ts +8 -0
- package/src/sereus/invitations.ts +206 -0
- package/src/sereus/node-code.ts +80 -0
- package/src/store.ts +61 -0
- package/src/stores.ts +59 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* background-runner.ts — owns a phone's {@link CadreNode} lifecycle across OS
|
|
3
|
+
* foreground/background transitions.
|
|
4
|
+
*
|
|
5
|
+
* This runner subscribes to `AppState` and drives the cadre-core background
|
|
6
|
+
* primitives (`hibernateAll`, the `control:*` readiness edges) so the footprint
|
|
7
|
+
* quiesces on background and a managed resume re-syncs the control network on
|
|
8
|
+
* foreground return.
|
|
9
|
+
*
|
|
10
|
+
* Scope is **AppState-driven lifecycle only** — a push wake (FCM/APNs receive path,
|
|
11
|
+
* native background task) is the app's. This file is platform-agnostic apart from
|
|
12
|
+
* the injectable {@link AppStateLike}: react-native's `AppState` is assignable to
|
|
13
|
+
* it, so the app passes that, tests pass a fake, and this module never imports
|
|
14
|
+
* `react-native`.
|
|
15
|
+
*
|
|
16
|
+
* State machine:
|
|
17
|
+
*
|
|
18
|
+
* FOREGROUND ──background──► BACKGROUND_CONNECTED ──control:disconnected──►
|
|
19
|
+
* BACKGROUND_HIBERNATING ──active──► (resume) ──► FOREGROUND
|
|
20
|
+
*
|
|
21
|
+
* BACKGROUND_CONNECTED is **best-effort**: iOS suspends sockets within seconds
|
|
22
|
+
* regardless of `hibernateAll`, Android (with the future foreground service) can
|
|
23
|
+
* hold it longer. We never assume the connection survived — the drop to
|
|
24
|
+
* BACKGROUND_HIBERNATING is driven by the real `control:disconnected` edge.
|
|
25
|
+
*/
|
|
26
|
+
import type { CadreNode } from '@serfab/cadre-core';
|
|
27
|
+
export type RunnerState = 'foreground' | 'background-connected' | 'background-hibernating' | 'terminated';
|
|
28
|
+
/**
|
|
29
|
+
* Subset of an `AppState` event subscription we depend on. Matches the object
|
|
30
|
+
* react-native's `AppState.addEventListener` returns.
|
|
31
|
+
*/
|
|
32
|
+
export interface AppStateSubscription {
|
|
33
|
+
remove(): void;
|
|
34
|
+
}
|
|
35
|
+
/** AppState status values the runner reacts to (others are ignored). */
|
|
36
|
+
export type AppStateValue = 'active' | 'background' | 'inactive' | string;
|
|
37
|
+
/**
|
|
38
|
+
* The part of react-native's `AppState` the runner uses. `AppState` itself is
|
|
39
|
+
* assignable to it; unit tests drive transitions with a fake and no device.
|
|
40
|
+
*/
|
|
41
|
+
export interface AppStateLike {
|
|
42
|
+
addEventListener(type: 'change', handler: (status: AppStateValue) => void): AppStateSubscription;
|
|
43
|
+
}
|
|
44
|
+
export interface BackgroundRunner {
|
|
45
|
+
/** Current lifecycle state. */
|
|
46
|
+
readonly state: RunnerState;
|
|
47
|
+
/** True while a foreground resume is settling (awaiting control reconnect). */
|
|
48
|
+
readonly resuming: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* True after a resume settled without the control network reconnecting
|
|
51
|
+
* (bounded settle timeout fired) — the UI should show an offline/degraded
|
|
52
|
+
* hint rather than spin. Cleared whenever a fresh resume begins.
|
|
53
|
+
*/
|
|
54
|
+
readonly degraded: boolean;
|
|
55
|
+
/** Subscribe to AppState. Assumes the node is already (or will be) started. Idempotent. */
|
|
56
|
+
start(): void;
|
|
57
|
+
/** Unsubscribe and drop node listeners (component unmount / logout). Idempotent. */
|
|
58
|
+
stop(): void;
|
|
59
|
+
/**
|
|
60
|
+
* Observe state/resuming/degraded changes. The callback receives the current
|
|
61
|
+
* {@link RunnerState}; read `runner.resuming`/`runner.degraded` for the rest.
|
|
62
|
+
* Returns an unsubscribe fn.
|
|
63
|
+
*/
|
|
64
|
+
onStateChange(cb: (s: RunnerState) => void): () => void;
|
|
65
|
+
}
|
|
66
|
+
export interface BackgroundRunnerDeps {
|
|
67
|
+
/** Returns the live node (e.g. `PhoneNode.node`), or null if stopped. */
|
|
68
|
+
getNode: () => CadreNode | null;
|
|
69
|
+
/** Injectable AppState (react-native in prod, a fake in tests). */
|
|
70
|
+
appState: AppStateLike;
|
|
71
|
+
/**
|
|
72
|
+
* Cold-start hook: start the node again when a foreground return finds it
|
|
73
|
+
* stopped/killed (`phoneNodeLifecycle` in `./index.ts` provides one). Optional —
|
|
74
|
+
* without it the runner only awaits an already-present node's control
|
|
75
|
+
* connection. Must be idempotent.
|
|
76
|
+
*/
|
|
77
|
+
ensureNode?: () => Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Bounded resume settle: max wait for `control:connected` on foreground return
|
|
80
|
+
* before clearing `resuming` and surfacing a degraded state. Default 15s.
|
|
81
|
+
*/
|
|
82
|
+
settleTimeoutMs?: number;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create a {@link BackgroundRunner}. Construction is inert — call `start()` to
|
|
86
|
+
* subscribe to AppState.
|
|
87
|
+
*
|
|
88
|
+
* Concurrency model: every AppState transition bumps a monotonic `epoch`. Async
|
|
89
|
+
* handlers (hibernate sweep, resume settle) capture the epoch at entry and bail
|
|
90
|
+
* after each `await` if a newer transition has superseded them. This makes rapid
|
|
91
|
+
* foreground/background flapping deterministic — a late-completing resume can
|
|
92
|
+
* never clobber the state a subsequent background event already set.
|
|
93
|
+
*/
|
|
94
|
+
export declare function createBackgroundRunner(deps: BackgroundRunnerDeps): BackgroundRunner;
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* background-runner.ts — owns a phone's {@link CadreNode} lifecycle across OS
|
|
3
|
+
* foreground/background transitions.
|
|
4
|
+
*
|
|
5
|
+
* This runner subscribes to `AppState` and drives the cadre-core background
|
|
6
|
+
* primitives (`hibernateAll`, the `control:*` readiness edges) so the footprint
|
|
7
|
+
* quiesces on background and a managed resume re-syncs the control network on
|
|
8
|
+
* foreground return.
|
|
9
|
+
*
|
|
10
|
+
* Scope is **AppState-driven lifecycle only** — a push wake (FCM/APNs receive path,
|
|
11
|
+
* native background task) is the app's. This file is platform-agnostic apart from
|
|
12
|
+
* the injectable {@link AppStateLike}: react-native's `AppState` is assignable to
|
|
13
|
+
* it, so the app passes that, tests pass a fake, and this module never imports
|
|
14
|
+
* `react-native`.
|
|
15
|
+
*
|
|
16
|
+
* State machine:
|
|
17
|
+
*
|
|
18
|
+
* FOREGROUND ──background──► BACKGROUND_CONNECTED ──control:disconnected──►
|
|
19
|
+
* BACKGROUND_HIBERNATING ──active──► (resume) ──► FOREGROUND
|
|
20
|
+
*
|
|
21
|
+
* BACKGROUND_CONNECTED is **best-effort**: iOS suspends sockets within seconds
|
|
22
|
+
* regardless of `hibernateAll`, Android (with the future foreground service) can
|
|
23
|
+
* hold it longer. We never assume the connection survived — the drop to
|
|
24
|
+
* BACKGROUND_HIBERNATING is driven by the real `control:disconnected` edge.
|
|
25
|
+
*/
|
|
26
|
+
const DEFAULT_SETTLE_TIMEOUT_MS = 15_000;
|
|
27
|
+
// ── Implementation ──────────────────────────────────────────────────────────────
|
|
28
|
+
/**
|
|
29
|
+
* Create a {@link BackgroundRunner}. Construction is inert — call `start()` to
|
|
30
|
+
* subscribe to AppState.
|
|
31
|
+
*
|
|
32
|
+
* Concurrency model: every AppState transition bumps a monotonic `epoch`. Async
|
|
33
|
+
* handlers (hibernate sweep, resume settle) capture the epoch at entry and bail
|
|
34
|
+
* after each `await` if a newer transition has superseded them. This makes rapid
|
|
35
|
+
* foreground/background flapping deterministic — a late-completing resume can
|
|
36
|
+
* never clobber the state a subsequent background event already set.
|
|
37
|
+
*/
|
|
38
|
+
export function createBackgroundRunner(deps) {
|
|
39
|
+
const settleTimeoutMs = deps.settleTimeoutMs ?? DEFAULT_SETTLE_TIMEOUT_MS;
|
|
40
|
+
let state = 'foreground';
|
|
41
|
+
let resuming = false;
|
|
42
|
+
let degraded = false;
|
|
43
|
+
let epoch = 0;
|
|
44
|
+
let active = false;
|
|
45
|
+
let subscription = null;
|
|
46
|
+
let listenedNode = null;
|
|
47
|
+
const pendingTimers = new Set();
|
|
48
|
+
// In-flight `awaitControlConnected` teardowns, so `stop()` can abort a settle
|
|
49
|
+
// mid-flight (removing its `control:connected` listener + timer) instead of
|
|
50
|
+
// orphaning them — `clearTimeout` alone would leave the listener attached.
|
|
51
|
+
const pendingSettles = new Set();
|
|
52
|
+
const stateListeners = new Set();
|
|
53
|
+
// ── notification ────────────────────────────────────────────────────────────
|
|
54
|
+
function notify() {
|
|
55
|
+
for (const cb of stateListeners) {
|
|
56
|
+
try {
|
|
57
|
+
cb(state);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
console.warn('[background-runner] state listener threw:', err);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function setState(next) {
|
|
65
|
+
if (state === next)
|
|
66
|
+
return;
|
|
67
|
+
state = next;
|
|
68
|
+
notify();
|
|
69
|
+
}
|
|
70
|
+
function setResuming(next) {
|
|
71
|
+
if (resuming === next)
|
|
72
|
+
return;
|
|
73
|
+
resuming = next;
|
|
74
|
+
notify();
|
|
75
|
+
}
|
|
76
|
+
function setDegraded(next) {
|
|
77
|
+
if (degraded === next)
|
|
78
|
+
return;
|
|
79
|
+
degraded = next;
|
|
80
|
+
notify();
|
|
81
|
+
}
|
|
82
|
+
// ── node event listeners ──────────────────────────────────────────────────────
|
|
83
|
+
// A `control:disconnected` while backgrounded is the signal that the OS tore
|
|
84
|
+
// down sockets / suspended us — drop BACKGROUND_CONNECTED → BACKGROUND_HIBERNATING.
|
|
85
|
+
// While foreground it's transient churn the node will reconnect from; ignore it.
|
|
86
|
+
function onControlDisconnected() {
|
|
87
|
+
if (state === 'background-connected') {
|
|
88
|
+
setState('background-hibernating');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// A `control:connected` that arrives while we are already foregrounded and
|
|
92
|
+
// degraded means the control network recovered on its own (no app-lifecycle
|
|
93
|
+
// transition to drive a fresh resume). Clear the stale degraded hint so the UI
|
|
94
|
+
// stops showing offline. During a resume settle, state is not yet 'foreground',
|
|
95
|
+
// so this no-ops and the settle's own one-shot listener owns the outcome.
|
|
96
|
+
function onControlConnected() {
|
|
97
|
+
if (state === 'foreground' && degraded) {
|
|
98
|
+
setDegraded(false);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function attachNodeListeners() {
|
|
102
|
+
const node = deps.getNode();
|
|
103
|
+
if (node === listenedNode)
|
|
104
|
+
return;
|
|
105
|
+
detachNodeListeners();
|
|
106
|
+
if (!node)
|
|
107
|
+
return;
|
|
108
|
+
node.on('control:disconnected', onControlDisconnected);
|
|
109
|
+
node.on('control:connected', onControlConnected);
|
|
110
|
+
listenedNode = node;
|
|
111
|
+
}
|
|
112
|
+
function detachNodeListeners() {
|
|
113
|
+
if (!listenedNode)
|
|
114
|
+
return;
|
|
115
|
+
listenedNode.off('control:disconnected', onControlDisconnected);
|
|
116
|
+
listenedNode.off('control:connected', onControlConnected);
|
|
117
|
+
listenedNode = null;
|
|
118
|
+
}
|
|
119
|
+
// ── settle helper ────────────────────────────────────────────────────────────
|
|
120
|
+
/**
|
|
121
|
+
* Resolve when the node's control network connects, or after the bounded
|
|
122
|
+
* settle timeout — whichever first. Resolves to `true` if connected, `false`
|
|
123
|
+
* if it timed out (degraded). Always cleans up its listener + timer.
|
|
124
|
+
*/
|
|
125
|
+
function awaitControlConnected(node) {
|
|
126
|
+
if (node.controlConnected)
|
|
127
|
+
return Promise.resolve(true);
|
|
128
|
+
return new Promise((resolve) => {
|
|
129
|
+
let settled = false;
|
|
130
|
+
const finish = (connected) => {
|
|
131
|
+
if (settled)
|
|
132
|
+
return;
|
|
133
|
+
settled = true;
|
|
134
|
+
node.off('control:connected', onConnected);
|
|
135
|
+
pendingTimers.delete(timer);
|
|
136
|
+
clearTimeout(timer);
|
|
137
|
+
pendingSettles.delete(finish);
|
|
138
|
+
resolve(connected);
|
|
139
|
+
};
|
|
140
|
+
const onConnected = () => finish(true);
|
|
141
|
+
node.on('control:connected', onConnected);
|
|
142
|
+
const timer = setTimeout(() => finish(false), settleTimeoutMs);
|
|
143
|
+
pendingTimers.add(timer);
|
|
144
|
+
pendingSettles.add(finish);
|
|
145
|
+
// Guard a connect that raced in between the initial check and subscribe.
|
|
146
|
+
if (node.controlConnected)
|
|
147
|
+
finish(true);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
// ── transition handlers ────────────────────────────────────────────────────────
|
|
151
|
+
// active → background: quiesce non-realtime strands and move to
|
|
152
|
+
// BACKGROUND_CONNECTED. We do NOT stop the node — the control connection and
|
|
153
|
+
// realtime strands stay up for as long as the OS permits. A superseding
|
|
154
|
+
// foreground event (flap) bumps the epoch and owns state after our await.
|
|
155
|
+
async function handleBackground() {
|
|
156
|
+
const myEpoch = ++epoch;
|
|
157
|
+
setResuming(false); // cancel any in-flight resume settle
|
|
158
|
+
setDegraded(false);
|
|
159
|
+
setState('background-connected');
|
|
160
|
+
const node = deps.getNode();
|
|
161
|
+
if (!node || !node.running)
|
|
162
|
+
return; // node null/stopped → no-op safely
|
|
163
|
+
try {
|
|
164
|
+
await node.hibernateAll();
|
|
165
|
+
}
|
|
166
|
+
catch (err) {
|
|
167
|
+
console.warn('[background-runner] hibernateAll failed:', err);
|
|
168
|
+
}
|
|
169
|
+
// No post-await state write: if a foreground flap superseded us
|
|
170
|
+
// (epoch changed), that handler now owns the state.
|
|
171
|
+
void myEpoch;
|
|
172
|
+
}
|
|
173
|
+
// background → active: full resume. Ensure the node is running (cold-start via
|
|
174
|
+
// `ensureNode`), await the control connection (bounded), then settle to
|
|
175
|
+
// FOREGROUND. Each await is epoch-guarded so a background flap mid-resume aborts
|
|
176
|
+
// cleanly rather than racing a hibernate.
|
|
177
|
+
async function handleForeground() {
|
|
178
|
+
const myEpoch = ++epoch;
|
|
179
|
+
setDegraded(false);
|
|
180
|
+
setResuming(true);
|
|
181
|
+
if (!deps.getNode() && deps.ensureNode) {
|
|
182
|
+
await runEnsureNode();
|
|
183
|
+
if (epoch !== myEpoch)
|
|
184
|
+
return; // superseded by a background flap
|
|
185
|
+
}
|
|
186
|
+
const node = deps.getNode();
|
|
187
|
+
if (node && !node.running && deps.ensureNode) {
|
|
188
|
+
await runEnsureNode();
|
|
189
|
+
if (epoch !== myEpoch)
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
attachNodeListeners();
|
|
193
|
+
const live = deps.getNode();
|
|
194
|
+
if (live) {
|
|
195
|
+
const connected = await awaitControlConnected(live);
|
|
196
|
+
if (epoch !== myEpoch)
|
|
197
|
+
return;
|
|
198
|
+
setDegraded(!connected);
|
|
199
|
+
}
|
|
200
|
+
setResuming(false);
|
|
201
|
+
setState('foreground');
|
|
202
|
+
}
|
|
203
|
+
async function runEnsureNode() {
|
|
204
|
+
try {
|
|
205
|
+
await deps.ensureNode();
|
|
206
|
+
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
console.warn('[background-runner] ensureNode (cold start) failed:', err);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// 'inactive' (iOS transient overlay: incoming call, control center, app
|
|
212
|
+
// switcher peek) is deliberately a no-op — hibernating on it would tear down
|
|
213
|
+
// strands during a brief overlay the user immediately dismisses. Only a real
|
|
214
|
+
// 'background' triggers the hibernate sweep.
|
|
215
|
+
function handleAppState(status) {
|
|
216
|
+
if (!active)
|
|
217
|
+
return;
|
|
218
|
+
if (status === 'background') {
|
|
219
|
+
void handleBackground();
|
|
220
|
+
}
|
|
221
|
+
else if (status === 'active') {
|
|
222
|
+
void handleForeground();
|
|
223
|
+
}
|
|
224
|
+
// 'inactive' and any other value: no-op.
|
|
225
|
+
}
|
|
226
|
+
// ── public surface ──────────────────────────────────────────────────────────
|
|
227
|
+
function start() {
|
|
228
|
+
if (subscription)
|
|
229
|
+
return; // idempotent
|
|
230
|
+
active = true;
|
|
231
|
+
subscription = deps.appState.addEventListener('change', handleAppState);
|
|
232
|
+
attachNodeListeners();
|
|
233
|
+
// Node is assumed already started (Settings start, or cold-start completed
|
|
234
|
+
// before the hook mounts the runner) → land in FOREGROUND.
|
|
235
|
+
setState('foreground');
|
|
236
|
+
}
|
|
237
|
+
function stop() {
|
|
238
|
+
active = false;
|
|
239
|
+
epoch++; // invalidate any in-flight resume/hibernate
|
|
240
|
+
if (subscription) {
|
|
241
|
+
subscription.remove();
|
|
242
|
+
subscription = null;
|
|
243
|
+
}
|
|
244
|
+
detachNodeListeners();
|
|
245
|
+
// Abort any in-flight settle (removes its `control:connected` listener +
|
|
246
|
+
// timer and resolves it); the epoch bump above makes the resolved handler
|
|
247
|
+
// bail before touching state. Snapshot first — `finish` mutates the set.
|
|
248
|
+
for (const finish of [...pendingSettles])
|
|
249
|
+
finish(false);
|
|
250
|
+
pendingSettles.clear();
|
|
251
|
+
for (const timer of pendingTimers)
|
|
252
|
+
clearTimeout(timer);
|
|
253
|
+
pendingTimers.clear();
|
|
254
|
+
setResuming(false);
|
|
255
|
+
setDegraded(false);
|
|
256
|
+
}
|
|
257
|
+
function onStateChange(cb) {
|
|
258
|
+
stateListeners.add(cb);
|
|
259
|
+
return () => {
|
|
260
|
+
stateListeners.delete(cb);
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
get state() {
|
|
265
|
+
return state;
|
|
266
|
+
},
|
|
267
|
+
get resuming() {
|
|
268
|
+
return resuming;
|
|
269
|
+
},
|
|
270
|
+
get degraded() {
|
|
271
|
+
return degraded;
|
|
272
|
+
},
|
|
273
|
+
start,
|
|
274
|
+
stop,
|
|
275
|
+
onStateChange,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
//# sourceMappingURL=background-runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"background-runner.js","sourceRoot":"","sources":["../../src/lifecycle/background-runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AA4EH,MAAM,yBAAyB,GAAG,MAAM,CAAC;AAEzC,mFAAmF;AAEnF;;;;;;;;;GASG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAA0B;IAChE,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,IAAI,yBAAyB,CAAC;IAE1E,IAAI,KAAK,GAAgB,YAAY,CAAC;IACtC,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,MAAM,GAAG,KAAK,CAAC;IAEnB,IAAI,YAAY,GAAgC,IAAI,CAAC;IACrD,IAAI,YAAY,GAAqB,IAAI,CAAC;IAC1C,MAAM,aAAa,GAAG,IAAI,GAAG,EAAiC,CAAC;IAC/D,8EAA8E;IAC9E,4EAA4E;IAC5E,2EAA2E;IAC3E,MAAM,cAAc,GAAG,IAAI,GAAG,EAAgC,CAAC;IAC/D,MAAM,cAAc,GAAG,IAAI,GAAG,EAA4B,CAAC;IAE3D,+EAA+E;IAE/E,SAAS,MAAM;QACd,KAAK,MAAM,EAAE,IAAI,cAAc,EAAE,CAAC;YACjC,IAAI,CAAC;gBACJ,EAAE,CAAC,KAAK,CAAC,CAAC;YACX,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,OAAO,CAAC,IAAI,CAAC,2CAA2C,EAAE,GAAG,CAAC,CAAC;YAChE,CAAC;QACF,CAAC;IACF,CAAC;IAED,SAAS,QAAQ,CAAC,IAAiB;QAClC,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO;QAC3B,KAAK,GAAG,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AAE,CAAC;YAClB,YAAY,CAAC,MAAM,EAAE,CAAC;YACtB,YAAY,GAAG,IAAI,CAAC;QACrB,CAAC;QACD,mBAAmB,EAAE,CAAC;QACtB,yEAAyE;QACzE,0EAA0E;QAC1E,yEAAyE;QACzE,KAAK,MAAM,MAAM,IAAI,CAAC,GAAG,cAAc,CAAC;YAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QACxD,cAAc,CAAC,KAAK,EAAE,CAAC;QACvB,KAAK,MAAM,KAAK,IAAI,aAAa;YAAE,YAAY,CAAC,KAAK,CAAC,CAAC;QACvD,aAAa,CAAC,KAAK,EAAE,CAAC;QACtB,WAAW,CAAC,KAAK,CAAC,CAAC;QACnB,WAAW,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,SAAS,aAAa,CAAC,EAA4B;QAClD,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACvB,OAAO,GAAG,EAAE;YACX,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC3B,CAAC,CAAC;IACH,CAAC;IAED,OAAO;QACN,IAAI,KAAK;YACR,OAAO,KAAK,CAAC;QACd,CAAC;QACD,IAAI,QAAQ;YACX,OAAO,QAAQ,CAAC;QACjB,CAAC;QACD,IAAI,QAAQ;YACX,OAAO,QAAQ,CAAC;QACjB,CAAC;QACD,KAAK;QACL,IAAI;QACJ,aAAa;KACb,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@serfab/cadre-client/lifecycle` — hibernate a device's node when the app goes to the background
|
|
3
|
+
* and resume it, bounded, when it returns (`createBackgroundRunner`).
|
|
4
|
+
*
|
|
5
|
+
* Platform-neutral: the app passes its platform's app-state events in (`AppStateLike`; React
|
|
6
|
+
* Native's `AppState` is one) and the node hooks. On React Native, `@serfab/cadre-rn/lifecycle`
|
|
7
|
+
* adds `phoneNodeLifecycle`, the hooks over a `PhoneNode`.
|
|
8
|
+
*/
|
|
9
|
+
export { createBackgroundRunner, type AppStateLike, type AppStateSubscription, type AppStateValue, type BackgroundRunner, type BackgroundRunnerDeps, type RunnerState, } from './background-runner.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@serfab/cadre-client/lifecycle` — hibernate a device's node when the app goes to the background
|
|
3
|
+
* and resume it, bounded, when it returns (`createBackgroundRunner`).
|
|
4
|
+
*
|
|
5
|
+
* Platform-neutral: the app passes its platform's app-state events in (`AppStateLike`; React
|
|
6
|
+
* Native's `AppState` is one) and the node hooks. On React Native, `@serfab/cadre-rn/lifecycle`
|
|
7
|
+
* adds `phoneNodeLifecycle`, the hooks over a `PhoneNode`.
|
|
8
|
+
*/
|
|
9
|
+
export { createBackgroundRunner, } from './background-runner.js';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lifecycle/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACN,sBAAsB,GAOtB,MAAM,wBAAwB,CAAC"}
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The loader store: one load state machine for a cadre screen.
|
|
3
|
+
*
|
|
4
|
+
* { state: 'loading' } nothing read yet
|
|
5
|
+
* { state: 'ready', value, error? } read; `error` set when the LAST refresh
|
|
6
|
+
* failed and the value shown is the previous one
|
|
7
|
+
* { state: 'failed', error } the first read failed
|
|
8
|
+
*
|
|
9
|
+
* It reads when its first subscriber arrives, then again on each change signal it is
|
|
10
|
+
* given — debounced, one read at a time, one more read if a change arrives during a
|
|
11
|
+
* read — and on an optional backstop poll, for what no signal covers. It stops all of
|
|
12
|
+
* that when its last subscriber leaves.
|
|
13
|
+
*/
|
|
14
|
+
import { type CadreError, type CadreResult } from './result.js';
|
|
15
|
+
import { type Readable } from './store.js';
|
|
16
|
+
export type LoadState<T> = {
|
|
17
|
+
state: 'loading';
|
|
18
|
+
value?: undefined;
|
|
19
|
+
error?: undefined;
|
|
20
|
+
refreshing: boolean;
|
|
21
|
+
} | {
|
|
22
|
+
state: 'ready';
|
|
23
|
+
value: T;
|
|
24
|
+
error?: CadreError;
|
|
25
|
+
refreshing: boolean;
|
|
26
|
+
} | {
|
|
27
|
+
state: 'failed';
|
|
28
|
+
value?: undefined;
|
|
29
|
+
error: CadreError;
|
|
30
|
+
refreshing: boolean;
|
|
31
|
+
};
|
|
32
|
+
export interface LoaderOptions<T> {
|
|
33
|
+
load: () => Promise<CadreResult<T>>;
|
|
34
|
+
/** Subscribe to whatever should trigger a re-read; returns an unsubscribe. */
|
|
35
|
+
changes?: (onChange: () => void) => () => void;
|
|
36
|
+
/** Backstop poll, ms. */
|
|
37
|
+
pollMs?: number;
|
|
38
|
+
/** Coalesce a burst of change signals into one read. Default 500 ms. */
|
|
39
|
+
debounceMs?: number;
|
|
40
|
+
}
|
|
41
|
+
export interface Loader<T> extends Readable<LoadState<T>> {
|
|
42
|
+
/** Read now (coalesced with a read in flight). */
|
|
43
|
+
reload(): void;
|
|
44
|
+
/** Read now and resolve when that read has settled. */
|
|
45
|
+
refresh(): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
export declare function loader<T>(opts: LoaderOptions<T>): Loader<T>;
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The loader store: one load state machine for a cadre screen.
|
|
3
|
+
*
|
|
4
|
+
* { state: 'loading' } nothing read yet
|
|
5
|
+
* { state: 'ready', value, error? } read; `error` set when the LAST refresh
|
|
6
|
+
* failed and the value shown is the previous one
|
|
7
|
+
* { state: 'failed', error } the first read failed
|
|
8
|
+
*
|
|
9
|
+
* It reads when its first subscriber arrives, then again on each change signal it is
|
|
10
|
+
* given — debounced, one read at a time, one more read if a change arrives during a
|
|
11
|
+
* read — and on an optional backstop poll, for what no signal covers. It stops all of
|
|
12
|
+
* that when its last subscriber leaves.
|
|
13
|
+
*/
|
|
14
|
+
import { describeThrown } from './result.js';
|
|
15
|
+
import { writable } from './store.js';
|
|
16
|
+
export function loader(opts) {
|
|
17
|
+
const debounceMs = opts.debounceMs ?? 500;
|
|
18
|
+
let active = false;
|
|
19
|
+
let inFlight = null;
|
|
20
|
+
let again = false;
|
|
21
|
+
let debounce = null;
|
|
22
|
+
let poll = null;
|
|
23
|
+
let unsubscribeChanges = null;
|
|
24
|
+
const store = writable({ state: 'loading', refreshing: false }, () => {
|
|
25
|
+
active = true;
|
|
26
|
+
unsubscribeChanges = opts.changes?.(scheduleFromChange) ?? null;
|
|
27
|
+
void run();
|
|
28
|
+
return () => {
|
|
29
|
+
active = false;
|
|
30
|
+
unsubscribeChanges?.();
|
|
31
|
+
unsubscribeChanges = null;
|
|
32
|
+
if (debounce !== null)
|
|
33
|
+
clearTimeout(debounce);
|
|
34
|
+
if (poll !== null)
|
|
35
|
+
clearTimeout(poll);
|
|
36
|
+
debounce = poll = null;
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
function scheduleFromChange() {
|
|
40
|
+
if (!active || debounce !== null)
|
|
41
|
+
return;
|
|
42
|
+
debounce = setTimeout(() => { debounce = null; void run(); }, debounceMs);
|
|
43
|
+
}
|
|
44
|
+
function schedulePoll() {
|
|
45
|
+
if (poll !== null)
|
|
46
|
+
clearTimeout(poll);
|
|
47
|
+
poll = null;
|
|
48
|
+
if (active && opts.pollMs)
|
|
49
|
+
poll = setTimeout(() => { poll = null; void run(); }, opts.pollMs);
|
|
50
|
+
}
|
|
51
|
+
function run() {
|
|
52
|
+
if (inFlight) {
|
|
53
|
+
again = true;
|
|
54
|
+
return inFlight;
|
|
55
|
+
}
|
|
56
|
+
store.update(s => (s.refreshing ? s : { ...s, refreshing: true }));
|
|
57
|
+
inFlight = (async () => {
|
|
58
|
+
let result;
|
|
59
|
+
try {
|
|
60
|
+
result = await opts.load();
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
// Operations report failures as results; a throw is a defect, shown rather than lost.
|
|
64
|
+
result = { ok: false, error: { kind: 'unexpected', retryable: false, message: describeThrown(err), cause: err } };
|
|
65
|
+
}
|
|
66
|
+
store.update(s => {
|
|
67
|
+
if (result.ok)
|
|
68
|
+
return { state: 'ready', value: result.value, refreshing: false };
|
|
69
|
+
if (s.state === 'ready')
|
|
70
|
+
return { state: 'ready', value: s.value, error: result.error, refreshing: false };
|
|
71
|
+
return { state: 'failed', error: result.error, refreshing: false };
|
|
72
|
+
});
|
|
73
|
+
})().finally(() => {
|
|
74
|
+
inFlight = null;
|
|
75
|
+
if (again && active) {
|
|
76
|
+
again = false;
|
|
77
|
+
void run();
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
again = false;
|
|
81
|
+
schedulePoll();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return inFlight;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
subscribe: store.subscribe,
|
|
88
|
+
get: store.get,
|
|
89
|
+
reload: () => { void run(); },
|
|
90
|
+
refresh: () => run(),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,cAAc,EAAqC,MAAM,aAAa,CAAC;AAChF,OAAO,EAAE,QAAQ,EAAiB,MAAM,YAAY,CAAC;AAwBrD,MAAM,UAAU,MAAM,CAAI,IAAsB;IAC/C,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,GAAG,CAAC;IAC1C,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,IAAI,QAAQ,GAAyB,IAAI,CAAC;IAC1C,IAAI,KAAK,GAAG,KAAK,CAAC;IAClB,IAAI,QAAQ,GAAyC,IAAI,CAAC;IAC1D,IAAI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