wenay-common2 1.0.73 → 1.0.75

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.
Files changed (41) hide show
  1. package/README.md +3 -2
  2. package/demo/client.ts +137 -32
  3. package/demo/index.html +7 -0
  4. package/demo/server.ts +79 -42
  5. package/doc/INTENT.md +31 -31
  6. package/doc/ROADMAP.md +233 -230
  7. package/doc/changes/1.0.74.md +12 -0
  8. package/doc/changes/1.0.75.md +10 -0
  9. package/doc/wenay-common2-rare.md +705 -649
  10. package/doc/wenay-common2.md +438 -396
  11. package/lib/Common/events/replay-route.js +5 -2
  12. package/lib/Common/events/replay-wire.js +137 -47
  13. package/lib/Common/events/route-signal-webrtc.d.ts +1 -1
  14. package/lib/Common/events/route-signal-webrtc.js +15 -5
  15. package/lib/Common/events/transport-lifecycle.d.ts +23 -0
  16. package/lib/Common/events/transport-lifecycle.js +94 -0
  17. package/lib/Common/media/media-view.d.ts +2 -2
  18. package/lib/Common/media/media-view.js +23 -4
  19. package/lib/Common/peer/peer-call.d.ts +65 -0
  20. package/lib/Common/peer/peer-call.js +173 -0
  21. package/lib/Common/peer/peer-client.d.ts +9 -0
  22. package/lib/Common/peer/peer-host.d.ts +14 -1
  23. package/lib/Common/peer/peer-host.js +48 -4
  24. package/lib/Common/peer/peer-index.d.ts +2 -0
  25. package/lib/Common/peer/peer-index.js +2 -0
  26. package/lib/Common/peer/peer-media-relay.d.ts +22 -0
  27. package/lib/Common/peer/peer-media-relay.js +155 -0
  28. package/lib/Common/rcp/rpc-client.js +346 -111
  29. package/lib/Common/rcp/rpc-clientHub.d.ts +2 -0
  30. package/lib/Common/rcp/rpc-clientHub.js +120 -25
  31. package/lib/Common/rcp/rpc-server.js +33 -9
  32. package/observe/listen-core.test.ts +1 -2
  33. package/oracle/realsocket/lifecycle.spec.ts +3 -3
  34. package/oracle/realsocket/replay-reconnect-auth.spec.ts +111 -0
  35. package/oracle/realsocket/replay-reconnect.spec.ts +592 -0
  36. package/package.json +1 -4
  37. package/replay/media-view.test.ts +4 -4
  38. package/replay/peer-call.test.ts +226 -0
  39. package/rpc.md +19 -4
  40. package/doc/changes/1.0.64.md +0 -10
  41. package/doc/changes/1.0.65.md +0 -8
@@ -1,396 +1,438 @@
1
- # wenay-common2 — BRIEF cheat sheet (notation)
2
-
3
- > Root import: `import { ... } from "wenay-common2"`.
4
- > Notation: `name(args: types) -> ret // note`. Types are shown where they decide a correct call (callback shape,
5
- > overloads, return). Short names are **canonical**; removed old names are listed in `NAMING_RENAMES.md`.
6
- > Full surface → **`wenay-common2-rare.md`**. Code style → `CLAUDE.md`. Full RPC guide → `rpc.md`.
7
-
8
- ## ⭐ events — `listen` / `listenStore`
9
- ```
10
- import { listen, listenStore, mapListen } from "wenay-common2"
11
-
12
- listen<T>(opts?) -> [emit, listen] // pure event list: no local value storage, no current replay
13
- emit(...args: T) // dispatch event only
14
- listen.on(cb: (...args: T) => void, {key?, cbClose?}) -> off
15
- listen.once(cb, {key?}) -> off // one future event
16
- listen.onClose(cb: () => void) -> off
17
- listen.close() // clear listeners + fire close hooks + teardown producer
18
- listen.count() -> number
19
-
20
- listenStore<T>({current, ...opts}) -> [emit, listen]
21
- // store wrapper: current() reads external store by reference; the listener does not keep its own value copy
22
- listen.on(cb, {current: true}) -> off // current store value first, then future events
23
- listen.on(cb) -> off // future events only
24
- listen.once(cb, {current: true}) -> off // one value from current store, if present; otherwise waits future event
25
- listen.once(cb) -> off // one future event
26
- listen.on(cb, {current: () => argsOrUndefined}) -> off
27
-
28
- opts: { fast? = true, event?(t: 'add'|'remove', count, api), closeOn? }
29
- ```
30
- ```
31
- mapListen<TIn, TOut>(src, map: (...a: TIn) => TOut | null, opts?) -> [emit, listen] // map+filter (null skips); lazy subscribe
32
- joinListens(listens | ports, keyExtractor?) -> { listen, add(port, key?), pending: number, clear(tid?) } // zip by key
33
- ```
34
- ## ⭐ sleep
35
- ```
36
- sleepAsync(ms = 0) -> Promise<void>
37
- ```
38
-
39
- ## ⏱️ async
40
- ```
41
- createThrottle() -> { throttle(ms: number, fn: () => void) -> void,
42
- debounce(ms: number, fn: () => void) -> Promise<void> }
43
- // fn is a ZERO-ARG thunk the scheduler runs itself; NOT a lodash-style wrapper that returns a callable
44
- // ONE createThrottle() = ONE shared limiter (shared busy/pending) — use a SEPARATE instance per operation;
45
- // throttle + debounce on the SAME instance contend (a busy throttle silently drops the debounce's trailing run)
46
- createAsyncQueue(concurrency = 1) -> { add<R>(task: () => Promise<R>) -> Promise<R>, onIdle() -> Promise<void>, size: number } // p-queue
47
- createReadyGate() -> { add(fn: () => void), ready() } // buffer fns until ready(), then run them in order
48
- promiseProgress<T>(arr: (Promise<T> | (() => Promise<T>))[]) -> {
49
- onOk(cb), onError(cb), all() -> Promise<any[]>, allSettled(), items(), stats() -> { ok: number, error: number, count: number } }
50
- // factory entries start on .all()/.allSettled()/items() (once); .all() rejects like Promise.all — read aggregate progress via stats()
51
- // alias: enhancedWaitRun->createThrottle · createTaskQueue->createReadyGate(.setReady->.ready) · createAsyncQueue.enqueue->add · .getQueueSize->size
52
- ```
53
-
54
- ## 🧰 core — clone / compare
55
- ```
56
- clone<T>(v: T) -> T // deep: cycles + Map/Set/Date, rebinds functions (alias: deepClone)
57
- shallowClone<T>(v: T) -> T
58
- isEqual(a, b) -> boolean // PLAIN object/array trees ONLY; on Map/Set/Date it returns a vacuous `true` — don't use it on them (alias: deepEqual)
59
- shallowEqual(a, b) · arrayShallowEqual(a, b) -> boolean // strict-by-key vs loose-by-index — both kept on purpose
60
- toImmutable<T>(o: T) -> T // deep-frozen clone + Mutable:false marker
61
- JSON_clone<T>(o: T) -> T // JSON round-trip; DROPS Map/Set (-> {}) and turns Date -> string. NOT a rich clone — use clone() for those
62
- ```
63
-
64
- ## 🧰 core — binary search / maps / mutex / memo
65
- ```
66
- BSearch<T>(arr: ArrayLike<T>, value: T | comparer, match?: 'equal'|'lessOrEqual'|'greatOrEqual', sort?: SortMode) -> number
67
- // comparer is (item: T) => number OR (a: T, b) => number; without it, T must have valueOf():number
68
- BSearchNearest<T>(arr: ArrayLike<T>, value: number, getter?: (el: T) => number, maxDelta?: number) -> number
69
- new MapExt<K,V>() / new WeakMapExt<K,V>(): .getOrSet(k, () => v) -> V // lazy insert (a plain value is also accepted)
70
- new Mutex(): .runExclusive<T>(fn: () => T | Promise<T>) -> Promise<T> | .lock() -> Promise<release: () => void> // runExclusive: was dispatch
71
- MemoFunc<A extends any[], R>(opts?: { timeDelta?: number, maxLimits?: number }) -> { func(...a: A) -> R, cleanAll(), memo } // TTL + LRU; per-call {timeDelta, reSave}
72
- ```
73
-
74
- ## 🔢 core — number (frequent)
75
- ```
76
- round(value: number, digits = 0) -> number // round to N decimals (alias: NormalizeDouble)
77
- roundSig(value: number, { digitsR?: number /*total significant digits*/, digitsPoint? = 4, type?: 'max'|'min' }) -> number
78
- // round to N significant digits — roundSig(1234.5678, {digitsR: 3}) -> 1230 (alias: NormalizeDoubleAnd)
79
- gcd(a: number, b: number, digits = 8) -> number | gcd(values: Iterable<number>, digits = 8) -> number
80
- // floats ok; in the ITERABLE overload the 2nd arg is PRECISION digits, not an operand (alias: MaxCommonDivisor[OnArray])
81
- formatAuto(value: number, maxDigits = 8) -> string // shortest decimal string (alias: DblToStrAuto)
82
- decimals(value: number, maxDigits = 8, minDigits = 0) -> number // count of meaningful decimals (alias: GetDblPrecision)
83
- ```
84
-
85
- ## ⏰ time
86
- ```
87
- format(date: Date, pattern, { utc? = true }) -> string // (alias of 11 timeToStr_*/timeLocalToStr_*)
88
- pattern: 'HH:mm:ss' | 'HH:mm:ss.SSS' | 'yyyy-MM-dd'
89
- | 'yyyy-MM-dd HH:mm' | 'yyyy-MM-dd HH:mm:ss' | 'yyyy-MM-dd HH:mm:ss.SSS'
90
- | 'yyyy-MM-dd HH:mm O' | 'yyyy-MM-dd HH:mm:ss O' // O = GMT offset (local)
91
- { utc: false } -> local variants
92
- formatDuration(ms: number, pattern?: 'H:mm:ss' | 'H:mm:ss.SSS') -> string // clock; hours unbounded
93
- durationToStr(ms: number) -> string // humanized
94
- minDate(a: Date | null, b: Date | null) / maxDate(a, b) -> Date | null // null-tolerant (alias: MinTime/MaxTime)
95
- convertDatesToStrings(obj) // Date -> strings, recursively (logs)
96
- const: H1_S D1_S W1_S · M1_MS H1_MS D1_MS W1_MS
97
- ```
98
-
99
- ## 🌐 rpc (brief) — transport is ALWAYS caller-supplied (`{emit,on}`); there is NO url / built-in socket
100
- ```
101
- // SERVER: `object` is the impl tree, `socket` is a {emit,on} transport adapter
102
- createRpcServerAuto({ socket: {emit, on}, object, socketKey: string, auth?, limits?, maxPerListen?, throttle?, opt?, replay?, replayOpts? }) -> { api, ... }
103
- // replay: false|'auto' (default)|'force' — facade members that are replay lines (replayListen) are exposed
104
- // with BOTH surfaces under the SAME key: legacy plain-Listen path byte-for-byte + line/frameLine/since/keyframe/frame.
105
- // Upgrading listen -> replayListen is a declaration-site-only change; the facade and clients don't move.
106
- // replayOpts: {pending?, highWater?, lowWater?, pollMs?} — per-connection lag gate for 'frame'-policy subscribers
107
- // (pending defaults to socket.io writeBuffer; gates close on disconnect automatically). Replay lines are never throttled.
108
- createRpcServer(opts) // lower-level core (same { socket, object, socketKey })
109
- noStrict(obj) // mark a dynamic subtree (no schema)
110
- endCallback(fn) // mark an RPC stream-callback's end (alias: rpcEndCallback)
111
-
112
- // CLIENT hub: takes TWO functions — a socket factory + a schema builder; it is NOT an {url} or an options bag
113
- createRpcClientHub(
114
- createSocket: (token: string | null) => socket, // YOU build the socket, e.g. socket.io io(url, {auth:{token}})
115
- schemaBuilder: (rpc) => ({ key: rpc<Api>('socketKey') }), // declare each socketKey's typed API
116
- hubOpts?: { opt? },
117
- ) -> hub
118
- hub: connect(token) -> Promise<clients> · reauth(token) · facade · promise · socket · onConnect/onDisconnect // connect: was setToken
119
- // connect()'s promise resolves on the socket's 'connect' event; for in-proc/loopback (no 'connect') use hub.facade + await hub.promise
120
- client (on clients[key], NOT on the hub): func (proxy) · strict (schema-safe) · close() · ready() · init() · subscriptions()
121
- // pipe = batch a server chain in one packet · space = fire-and-forget
122
-
123
- // minimal wiring (the part no signature can show):
124
- const [tick, ticks] = listen<[number]>()
125
- createRpcServerAuto({ socket, object: { math: { add: (a, b) => a + b, ticks } }, socketKey: 'math' })
126
- const hub = createRpcClientHub((t) => io(url, { auth: { t } }), (rpc) => ({ math: rpc<Api>('math') }))
127
- const c = await hub.connect(token) // c = facade of per-socketKey clients
128
- await c.math.ready(); await c.math.func.add(2, 3)
129
- const l = c.math.func as unknown as DeepSocketListen<Api> // typed Listen projection; wrap as webListen(c.math) in app code
130
- const off = l.ticks.on(v => console.log(v)) // canonical stream subscribe; off is callable and awaitable
131
- off() // unsubscribe; .callback/.removeCallback are legacy compat, don't teach them
132
- l.ticks.once(v => console.log(v)) // one event, then auto-off
133
-
134
- // replay upgrade ONE WORD at the declaration site, everything below follows automatically:
135
- // const [tick, ticks] = listen<[number]>() // before
136
- const [tick, ticks] = replayListen<[number]>({history: 1024, current: 'last'}) // after — same facade, same key
137
- // legacy subscribers unchanged (byte-for-byte). Replay consumers now also get:
138
- const sub = replaySubscribe(l.ticks, v => {}, {since: saved, onSeq: s => saved = s}) // catch-up + live, no gaps/dups
139
- const sub2 = replaySubscribe(c.math.func.ticks, v => {}) // replay members project on func/strict directly — no cast needed
140
- const routed = replayRouteSubscribe(l.ticks, v => {}, {label: 'relay'})
141
- await routed.switch(nextRemoteTicks, {label: 'direct'}) // relay/direct hand-off: old route closes after catch-up
142
- await l.ticks.frame(mySeq) // pull at YOUR pace (50ms timer etc.) server condenses via the line's frame lambda
143
- // full guide + examples rpc.md; frame model / lag policies 🎞️ recipe below and rare docs
144
- ```
145
-
146
- ## 🎙️ Media over socket binary Listen frames
147
- ```
148
- import { Media } from "wenay-common2" // or: import * as Media from "wenay-common2/media"
149
-
150
- Media.createAudioSource({format?: 'int16'|'float32', mode?: 'pcm'|'record', replay?}) -> [emit, listen] & control
151
- Media.createVideoSource({fps? = 3, codec? = 'jpeg', quality?, replay?}) -> [emit, listen] & control
152
- control: start() -> Promise<'idle'|'requesting'|'live'|'denied'|'no-device'|'error'> · stop() · getStats() · setDevice(id) · listDevices() · state
153
- Media.encodeMediaFrame(meta, payload) / Media.decodeMediaFrame(frame) // one Uint8Array = 40-byte fixed header + raw payload
154
-
155
- // viewer/publisher one-liners (the demo stand is built on these):
156
- Media.attachVideoCanvas(line, canvas, {onError?}) -> {stats(), off} // decode+render any video line; codec/size come from frame headers
157
- Media.attachAudioPlayer(line, {maxBacklogSec? = 0.35}) -> {enable(), disable(), enabled, stats(), off} // live PCM playback, backlog drops
158
- Media.pipeMediaPublish(line, publish, {stamp? = true, onError?}) -> off // source -> RPC publish fn; stamp lets viewers measure latency
159
- ```
160
- Audio default is PCM frames from `AudioWorklet` where available (`mode:'record'` uses MediaRecorder chunks). Video default is camera snapshots (JPEG, low fps for vision) captured hidden-tab-proof: a worker timer ticks (setInterval is throttled to ~1/s in hidden tabs), `ImageCapture.grabFrame()` reads the track (a hidden `<video>` stops painting), and JPEG encode runs in a worker (main-thread `convertToBlob` stalls ~1s hidden) — `worker:false` opts back into the plain in-page path. Screen share is the same video source with an injected stream: `createVideoSource({stream: () => navigator.mediaDevices.getDisplayMedia({video: true})})`. Put `listen` into `createRpcServerAuto` like any other Listen; with `replay:true`, the returned listen is a replay line, so RPC auto exposes legacy + replay surfaces under the same key. Backpressure policy: audio is lossless queue; video `replay:true` defaults to keep-latest frame recovery. `transport:'webrtc'` is reserved for a future SFU/signaling adapter; socket binary is the default today. Living example: the demo stand (`npm run demo`) streams camera / mic / screen share between two tabs through a tiny server-side relay of replay lines (`demo/server.ts` + `demo/client.ts`).
161
-
162
- ## 🤝 Peer accounts see each other's stores (one-call SDK)
163
- > `import { Peer } from "wenay-common2"` or `import * as Peer from "wenay-common2/peer"`.
164
- > The happy-path facade over rpc + store + replay + route coordinator. Legacy-friendly by design:
165
- > the server side is a FRAGMENT spread into your EXISTING `createRpcServerAuto` object, the client
166
- > side rides your existing connection — old keys keep working untouched.
167
- ```
168
- // SERVER next to your legacy object:
169
- const host = Peer.createPeerHost({authorize?, history?}) // authorize(env) = server-side canExposeEndpoint
170
- io.on('connection', socket => {
171
- const peer = host.connection(accountOf(socket)) // per-account signal port + relay journal
172
- createRpcServerAuto({socket, socketKey, object: {...legacyObject, peer: peer.fragment}, disconnectListen})
173
- disconnectListen.on(peer.close)
174
- })
175
- host: connection(account) · relay(account) · accounts() · revoke(pair, accounts, reason?) · close()
176
-
177
- // CLIENT the whole happy path:
178
- const me = Peer.createPeerClient<World>({
179
- remote: c.app.func.peer, // deep proxy of the fragment — rest of the connection is yours
180
- account: 'a',
181
- initial: {...}, // own store: write me.store.state — others see it
182
- rtc?: () => new RTCPeerConnection(cfg), // omit = relay-only (promoteDirect unavailable)
183
- })
184
- const bob = me.peer('b') // mirror + route control for another account
185
- await bob.ready // keyframe/tail landed
186
- bob.store.state // live mirror reads survive ANY route change
187
- await bob.promoteDirect() // relay -> WebRTC direct; {ok, state, reason?} result, not a throw
188
- bob.route() // 'relay' | 'direct' · bob.reinterposeRelay() · bob.fallback() · bob.block()
189
- me.onRoute(ev => {}) // route transitions for metrics/UI
190
- ```
191
- Key property: the relay journal stores the owner's envelopes VERBATIM (owner seq space), so a
192
- relay <-> direct hand-off is a plain seq resume — no keyframe reset, no gaps, no dups. Late joiners
193
- get a keyframe folded server-side even while the owner is offline.
194
- Reconnect correctness is self-healing: a publisher gap makes the relay reject the push WITH its last
195
- seq, and the client repairs from that coordinate automatically (`repair: 'tail'` lossless (default)
196
- | `'keyframe'` cheap reset for ephemeral state). Server declares journal semantics
197
- (`createPeerHost({gap: 'resume' | 'sacred'})`); a declared `journal: 'sacred'` TYPE-forbids the cheap
198
- repair. `me.resync()` after a transport reconnect repairs without waiting for the next write;
199
- failures surface via `onPublishError`, never silently. Policy/session material:
200
- `createPeerClient({session, accept, policy})` + host `authorize` see rare docs for the envelope
201
- contract and the underlying primitives (`createRouteCoordinator`, `createSignalHub`,
202
- `createWebRtcConnector`). Oracle: `replay/peer-sdk.test.ts`.
203
-
204
- ## 🔁 Observe reactive state + store/mirror API
205
- > `import { Observe } from "wenay-common2"` or `import * as Observe from "wenay-common2/observe"`.
206
- > This is the documented v2 reactive/store surface.
207
- ```
208
- // coarse reactive object: subscribe to the fact that a subtree changed, then re-read current state
209
- Observe.reactive<T extends object>(obj, opts?) -> T
210
- Observe.onUpdate(node, cb: () => void) -> off
211
- Observe.onUpdatePaths(node, cb: ({paths}) => void) -> off // optional dirty paths, relative to node
212
- Observe.flushReactive(node) -> Promise<void>
213
- Observe.toRaw(node) -> raw value behind the proxy // snapshots/serialization without touching lazy nodes
214
- Observe.listenUpdate(node) -> Listen<void> // RPC bridge for coarse change notifications
215
- Observe.listenUpdatePaths(node) -> Listen<{paths: PropertyKey[][]}>
216
- opts: { drain?: "immediate"|"micro"|number|((flush)=>void), depth?, eager? }
217
-
218
- // path-addressed store facade over reactive()
219
- Observe.createStore<T extends object>(initial, opts?) -> Store<T>
220
- store.state // reactive data object; write normally
221
- store.node.path.to.leaf.get()/snapshot()/replace(v) // set(v) is a deprecated alias of replace(v)
222
- store.node.path.to.leaf.on((value, ctx) => {}, {current?, drain?, key?}) -> off
223
- store.node.path.to.leaf.once(cb, opts?) -> off
224
- store.update(mask, opts?) -> selection // typed selected snapshot
225
- selection.get() · selection.on((snap, ctx)=>{}, opts?) -> off · selection.onEach((value, ctx)=>{}, opts?) -> off
226
- store.each(opts?) -> Listen<[key, value, ctx]> // changed TOP-LEVEL keys as a plain Listen — THE per-key feed
227
- // one call per CHANGED key per drain window: value = current store.state[key] at flush time; undefined = key deleted;
228
- // two writes to one key in a window = ONE call (last value); deeper dirt (state.a.b = ...) reports 'a' once
229
- // root replace (store.replace / mirror keyframe) EXPANDS: one call per key of the new state + (key, undefined)
230
- // per key the replace removed — cold start / reconnect are NOT special cases for per-key consumers
231
- // plain Listen shape (on(cb) -> off · once · count); zero cost while it has no subscribers
232
- // NOT update(true).onEach: onEach fires per SELECTED path, and mask true selects the root —
233
- // ONE call per window with the whole dict (a dev warn points to each())
234
- store.count() -> number
235
-
236
- // network shape: backend exposes snapshots + changed Listen; frontend mirrors selected masks locally
237
- Observe.exposeStore(store, opts?) -> { get(mask?), set(path,value), replace(path,value), changed, changedPaths, patches?, changedData? }
238
- Observe.createStoreMirror(remote, initial, opts?) -> store & { sync(mask, opts?) -> Promise<off>; syncPatches(mask, opts?) -> Promise<off>; syncChangedData(mask, opts?) -> Promise<off> }
239
- // changedPaths is optional optimization: mirror pulls mask dirty paths; fallback is changed -> get(mask).
240
- // Optional push-data mode: exposeStore(store,{push:true}) + syncPatches/syncChangedData; details in rare docs.
241
-
242
- // Sequenced sync (replay line): seq-numbered patch stream keyframe catch-up, reconnect by seq (tail, not snapshot)
243
- Observe.exposeStoreReplay(store, {history? = 1024}) -> { api /* spread into the RPC server object */, replay, close }
244
- // the patch line declares its condensing `frame` itself (last patch per exact path) — reconnect tails and
245
- // lag recovery arrive as a mini-frame (changed paths only), zero config
246
- Observe.syncStoreReplay(mirror, remote /*{line, since, keyframe, frame?} of api.replay*/, {since?, onSeq?}) -> off
247
- // off.ready (catch-up done) · off.seq() (save for reconnect: syncStoreReplay(..., {since: prev.seq()}))
248
- // lagging/late client NEVER gets a backlog: evicted seq -> ONE fresh keyframe + live
249
- // freshness is an option, not consumer boilerplate: {staleMs, onStale} flags a silent line / stale keyframe (edge-triggered both ways; 🎞️ in rare docs)
250
- Observe.syncStoreReplayRoute(mirror, remote, {label?}) -> off & {switch(nextRemote, opts), ready, seq(), label(), active()}
251
- // relay/direct promotion and re-interposition: replacement route catches up by seq before the old route closes
252
- Observe.syncStoreReplayEach<T>(remote, (key, value, ctx) => {}, opts?) -> off & {store, ready, seq(), isStale(), lastTs()}
253
- // one-call remote fold: mirror store + syncStoreReplay + store.each() the callback fires per CHANGED
254
- // top-level key; first delivery = keyframe EXPANDED per key; (key, undefined) = key deleted
255
- // opts = all replaySubscribe opts (since/onSeq/policy/staleMs/onStale/onError...) + {drain?, initial?}
256
- // off() tears down BOTH the store sub and the wire sub; direct reads via off.store.state.KEY
257
- // reconnect: syncStoreReplayEach(remote, cb, {since: prev.seq(), initial: prev.store.snapshot()})
258
- // — the tail lands ON TOP of the previous state (a fresh empty mirror would not converge)
259
- // Offline persisted mirror (snapshot mode): local cache first, then replay catch-up by seq
260
- Observe.createOfflineStore({key, remote?, initial, storage, version?, debounceMs?, syncOpts?}) -> Promise<store & {ready, flush(), close(), status(), statusListen, reconnect(remote)}>
261
- Observe.persistStore(store, {key, storage, seq?, debounceMs?}) -> {flush, forceFlush, close, setSeq, seq, status, statusListen}
262
- Observe.createMemoryOfflineStorage(initial?) -> OfflineStorage
263
- // persists {version, seq, snapshot, savedAt}; seq is the correctness coordinate, timestamps are UX/freshness only
264
- // mode:'topLevel' is reserved; first implemented mode is snapshot
265
-
266
- // Declarative resource manager above mirror/replay/offline: app chooses what to start, not the store core
267
- Observe.managedStore.mirror({remote, initial, mask, tags?, priority?, explicitOnly?, large?, sync?})
268
- Observe.managedStore.replay({remote, initial, tags?, priority?, explicitOnly?, large?, syncOpts?})
269
- Observe.managedStore.offline({remote?, initial, storage, storageKey?, tags?, priority?, explicitOnly?, large?, syncOpts?})
270
- Observe.createStoreManager(resources) -> {plan(opts?), start(key, opts?), startPlanned(opts?), stop(key), stopAll(), get(key), touch(key, weight?), usage(), statusListen, handles}
271
- // plan excludes explicitOnly/large by default; {includeExplicit, includeLarge} opts opt them in
272
- // Slow-client conflation: recipe section 🎞️ below. Full generic surface (any event line, history/time-travel) -> Replay namespace, 🎞️ in rare docs.
273
- // Object add/delete/deep set are paths. Array mutation dirties the whole array branch, not splice internals.
274
- ```
275
- ```
276
- type Market = {data: {BTC?: number; ETH?: number}; meta: {status?: string}}
277
- const market = Observe.createStore<Market>({data: {BTC: 1, ETH: 2}, meta: {status: "ok"}})
278
-
279
- market.state.data.BTC = 3 // plain local mutation
280
- market.node.data.BTC.on((v, ctx) => {}, {current: true}) // ctx.path = ["data", "BTC"]
281
- market.node.data.on(data => {}, {current: true, drain: 50}) // branch snapshot, per-sub drain
282
- market.update({data: {BTC: true, ETH: true}}, {current: true}).on(snap => {})
283
-
284
- // backend facade over RPC
285
- const api = Observe.exposeStore(market)
286
-
287
- // frontend mirror: UI subscribes to local mirror, not RPC directly
288
- const mirror = Observe.createStoreMirror<Market>(api, {data: {}, meta: {}})
289
- const stop = await mirror.sync({data: {BTC: true}, meta: {status: true}}, {current: true, drain: 250}) // uses changedPaths when available
290
- mirror.node.data.BTC.on(v => {}, {current: true})
291
- stop()
292
-
293
- // per-key feed dict store -> grid rows; keyframe / reconnect are just expansion, not special cases
294
- type Rows = Record<string, {qty: number}>
295
- const rows = Observe.createStore<Rows>({})
296
- const offRows = rows.each().on((key, row) => { /* row === undefined ? removeRow(key) : upsertRow(key, row) */ })
297
-
298
- // the same per-key contract over the wire ONE call (mirror store + syncStoreReplay + each)
299
- const exposed = Observe.exposeStoreReplay(rows, {history: 1024}) // server side: spread exposed.api into the RPC object
300
- const feed = Observe.syncStoreReplayEach<Rows>(exposed.api.replay, (key, row) => {}, {drain: "micro"})
301
- await feed.ready // catch-up done: keyframe arrived expanded per key
302
- feed.store.state // the mirror direct reads / extra subscriptions
303
- feed() // tears down the store sub AND the wire sub
304
- // reconnect later: syncStoreReplayEach(remote, cb, {since: feed.seq(), initial: feed.store.snapshot()})
305
- // offline persisted mirror cached snapshot first, then replay catch-up over the same remote
306
- const offline = await Observe.createOfflineStore<Rows>({
307
- key: "rows",
308
- remote: exposed.api.replay,
309
- initial: {},
310
- storage: Observe.createMemoryOfflineStorage(), // use IndexedDB/SQLite adapter in an app
311
- debounceMs: 250,
312
- })
313
- offline.each().on((key, row) => {})
314
- await offline.ready
315
- await offline.flush()
316
- offline.close()
317
-
318
- // configurable app-level resource plan
319
- const manager = Observe.createStoreManager({
320
- market: Observe.managedStore.mirror({remote: api.market, initial: {data: {}, meta: {}}, mask: {data: {BTC: true}}, tags: ['bootstrap'], priority: 10}),
321
- rows: Observe.managedStore.offline({remote: exposed.api.replay, initial: {}, storage: Observe.createMemoryOfflineStorage(), tags: ['grid']}),
322
- video: Observe.managedStore.replay({remote: videoReplay, initial: {}, explicitOnly: true, large: true}),
323
- })
324
- await manager.startPlanned({tags: ['bootstrap']})
325
- manager.touch('rows', 3) // usage can raise future plan score
326
- await manager.start('video', {explicit: true})
327
- ```
328
- Runnable example: `npx tsx observe/store-mirror.example.ts`.
329
- Offline oracles: `npx tsx replay/offline-store.test.ts`; real Socket.IO/RPC wire: `npx tsx replay/offline-store-socket.test.ts`.
330
-
331
- ## 🎞️ Fast ticks vs slow client replay lines + server-owned lag gate (recipe)
332
- > The problem: the producer emits faster than a bad link drains. Naive streaming grows an unbounded
333
- > outgoing queue per slow client. The replay stack solves it with ONE mental model — the FRAME:
334
- > `frame(sinceSeq, hint?)` on the line returns envelopes bringing a consumer from `sinceSeq` to now,
335
- > as compact as the line allows (exact tail -> condensed mini-frame -> keyframe fallback). The same
336
- > method serves reconnect (`since`), client pull (own pace) and lag recovery. The transport sees only
337
- > `seq`; ALL event semantics live in two lambdas declared on the line: `current` (keyframe = pointer
338
- > to truth) and `frame` (condenser may honor a client-supplied `hint`, see below).
339
- ```ts
340
- import { Observe, Replay } from 'wenay-common2'
341
-
342
- // ---- producer: declare what the line HAS (its class follows no mode flags) ----
343
- const [emitQuote, quotes] = Replay.replayListen<[string, number]>({
344
- history: 4096,
345
- frame: (tail, hint) => lastPerSymbol(tail, hint), // mini-frame; hint = client's pick of the condensation rule
346
- }) // current+frame = condensable · current only = keyframe recovery · neither = sacred queue (never skipped, loud on eviction)
347
- // store lines: exposeStoreReplay already declares current + frame (last patch per path) zero config
348
- const store = Observe.createStore<World>(initial, { drain: 'micro' })
349
- const exposed = Observe.exposeStoreReplay(store, { history: 1024 })
350
-
351
- // ---- per CONNECTION: the rpc server owns the gate; the facade does NOT change ----
352
- io.on('connection', socket => {
353
- const [disconnect, disconnectListen] = listen<[]>()
354
- socket.on('disconnect', () => disconnect())
355
- createRpcServerAuto({
356
- socket: { emit: (k, d) => socket.emit(k, d), on: (k, cb) => socket.on(k, cb) },
357
- socketKey: 'world',
358
- object: { ...exposed.api, quotes }, // replay lines auto-exposed: both surfaces, same key
359
- disconnectListen, // gates close on disconnect automatically
360
- replayOpts: { highWater: 64, lowWater: 8 },// arms frameLine; pending defaults to socket.io writeBuffer
361
- })
362
- })
363
-
364
- // ---- client: picks its LAG POLICY per subscription; no conflation logic anywhere ----
365
- const sub = Replay.replaySubscribe(deep.quotes, cb, {since: saved, policy: 'frame'}) // server may skip; drain -> mini-frame
366
- const sub2 = Replay.replaySubscribe(deep.quotes, cb2, {since: saved}) // 'queue' (default): nothing ever skipped
367
- // own pace (e.g. 50ms skips + condensation): pull on YOUR timer hint picks the rule, server condenses:
368
- // every(50, async () => { for (const ev of await deep.quotes.frame(mySeq, hint)) apply(ev); })
369
- // store mirror: Observe.syncStoreReplay(mirror, deep.replay, {since: prev.seq()}) — same contract
370
- // delivery contract: FIRST delivery = snapshot/tail start (same event type), then strictly-newer,
371
- // seq-ascending, deduped; reconnect via {since} = mini-frame/tail, not a full snapshot
372
- ```
373
- Rules that make it correct (violating any of these silently breaks convergence):
374
- - **The line declares its recovery sources.** `current` (keyframe: SAMPLED from truth, never computed
375
- from deltas; `'last'` = last envelope for single-entity lines) and/or `frame` (condenser). A line
376
- with neither is a sacred queue: full tails only, evicted journal -> `frame()` THROWS (loud) — a
377
- lagging `'frame'`-policy subscriber gets a stream end, never silent loss.
378
- - **A `frame` result must be state-equivalent to the tail it replaces** (per the line's own semantics).
379
- "Can't condense THIS tail" is legal and simple: return the tail as-is. Refuse-loudly is `throw`.
380
- Multiple condensation standards live INSIDE the lambda, dispatched by the client-supplied `hint`
381
- (opaque to the transport): `frame(tail, hint)`.
382
- - **Events must be ABSOLUTE per their entity** for last-per-entity condensing (store patches are).
383
- - Gate drops never hole the journal — it is written BEFORE any gate. Reconnect via `{since}` still
384
- gets everything; only an evicted `history` window degrades to keyframe (visible as a seq jump).
385
- - `pending()` and the watermarks share units (bytes, packets, frames — anything, but the same).
386
-
387
- Manual path (pre-rev2, still works, `keyOf` @deprecated): build the gate yourself with
388
- `Replay.conflateReplay(exposed.replay, {pending, highWater, keyOf})` and spread `gated.api` into the
389
- facade details in rare docs. New code should declare `frame` on the line instead.
390
-
391
- Wire-level proof/oracles: `npx ts-node replay/rpc-auto.test.ts` (real Socket.IO: auto-exposure, legacy
392
- parity, frame equivalence, gate lag sim), plus `replay/conflate-socket.test.ts`, `replay/conflate.test.ts`,
393
- `replay/coalesce.test.ts`. Full generic surface (history/time-travel, archive) 🎞️ in rare docs.
394
-
395
-
396
-
1
+ # wenay-common2 — BRIEF cheat sheet (notation)
2
+
3
+ > Root import: `import { ... } from "wenay-common2"`.
4
+ > Notation: `name(args: types) -> ret // note`. Types are shown where they decide a correct call (callback shape,
5
+ > overloads, return). Short names are **canonical**; removed old names are listed in `NAMING_RENAMES.md`.
6
+ > Full surface → **`wenay-common2-rare.md`**. Code style → `CLAUDE.md`. Full RPC guide → `rpc.md`.
7
+
8
+ ## ⭐ events — `listen` / `listenStore`
9
+ ```
10
+ import { listen, listenStore, mapListen } from "wenay-common2"
11
+
12
+ listen<T>(opts?) -> [emit, listen] // pure event list: no local value storage, no current replay
13
+ emit(...args: T) // dispatch event only
14
+ listen.on(cb: (...args: T) => void, {key?, cbClose?}) -> off
15
+ listen.once(cb, {key?}) -> off // one future event
16
+ listen.onClose(cb: () => void) -> off
17
+ listen.close() // clear listeners + fire close hooks + teardown producer
18
+ listen.count() -> number
19
+
20
+ listenStore<T>({current, ...opts}) -> [emit, listen]
21
+ // store wrapper: current() reads external store by reference; the listener does not keep its own value copy
22
+ listen.on(cb, {current: true}) -> off // current store value first, then future events
23
+ listen.on(cb) -> off // future events only
24
+ listen.once(cb, {current: true}) -> off // one value from current store, if present; otherwise waits future event
25
+ listen.once(cb) -> off // one future event
26
+ listen.on(cb, {current: () => argsOrUndefined}) -> off
27
+
28
+ opts: { fast? = true, event?(t: 'add'|'remove', count, api), closeOn? }
29
+ ```
30
+ ```
31
+ mapListen<TIn, TOut>(src, map: (...a: TIn) => TOut | null, opts?) -> [emit, listen] // map+filter (null skips); lazy subscribe
32
+ joinListens(listens | ports, keyExtractor?) -> { listen, add(port, key?), pending: number, clear(tid?) } // zip by key
33
+ ```
34
+ ## ⭐ sleep
35
+ ```
36
+ sleepAsync(ms = 0) -> Promise<void>
37
+ ```
38
+
39
+ ## ⏱️ async
40
+ ```
41
+ createThrottle() -> { throttle(ms: number, fn: () => void) -> void,
42
+ debounce(ms: number, fn: () => void) -> Promise<void> }
43
+ // fn is a ZERO-ARG thunk the scheduler runs itself; NOT a lodash-style wrapper that returns a callable
44
+ // ONE createThrottle() = ONE shared limiter (shared busy/pending) — use a SEPARATE instance per operation;
45
+ // throttle + debounce on the SAME instance contend (a busy throttle silently drops the debounce's trailing run)
46
+ createAsyncQueue(concurrency = 1) -> { add<R>(task: () => Promise<R>) -> Promise<R>, onIdle() -> Promise<void>, size: number } // p-queue
47
+ createReadyGate() -> { add(fn: () => void), ready() } // buffer fns until ready(), then run them in order
48
+ promiseProgress<T>(arr: (Promise<T> | (() => Promise<T>))[]) -> {
49
+ onOk(cb), onError(cb), all() -> Promise<any[]>, allSettled(), items(), stats() -> { ok: number, error: number, count: number } }
50
+ // factory entries start on .all()/.allSettled()/items() (once); .all() rejects like Promise.all — read aggregate progress via stats()
51
+ // alias: enhancedWaitRun->createThrottle · createTaskQueue->createReadyGate(.setReady->.ready) · createAsyncQueue.enqueue->add · .getQueueSize->size
52
+ ```
53
+
54
+ ## 🧰 core — clone / compare
55
+ ```
56
+ clone<T>(v: T) -> T // deep: cycles + Map/Set/Date, rebinds functions (alias: deepClone)
57
+ shallowClone<T>(v: T) -> T
58
+ isEqual(a, b) -> boolean // PLAIN object/array trees ONLY; on Map/Set/Date it returns a vacuous `true` — don't use it on them (alias: deepEqual)
59
+ shallowEqual(a, b) · arrayShallowEqual(a, b) -> boolean // strict-by-key vs loose-by-index — both kept on purpose
60
+ toImmutable<T>(o: T) -> T // deep-frozen clone + Mutable:false marker
61
+ JSON_clone<T>(o: T) -> T // JSON round-trip; DROPS Map/Set (-> {}) and turns Date -> string. NOT a rich clone — use clone() for those
62
+ ```
63
+
64
+ ## 🧰 core — binary search / maps / mutex / memo
65
+ ```
66
+ BSearch<T>(arr: ArrayLike<T>, value: T | comparer, match?: 'equal'|'lessOrEqual'|'greatOrEqual', sort?: SortMode) -> number
67
+ // comparer is (item: T) => number OR (a: T, b) => number; without it, T must have valueOf():number
68
+ BSearchNearest<T>(arr: ArrayLike<T>, value: number, getter?: (el: T) => number, maxDelta?: number) -> number
69
+ new MapExt<K,V>() / new WeakMapExt<K,V>(): .getOrSet(k, () => v) -> V // lazy insert (a plain value is also accepted)
70
+ new Mutex(): .runExclusive<T>(fn: () => T | Promise<T>) -> Promise<T> | .lock() -> Promise<release: () => void> // runExclusive: was dispatch
71
+ MemoFunc<A extends any[], R>(opts?: { timeDelta?: number, maxLimits?: number }) -> { func(...a: A) -> R, cleanAll(), memo } // TTL + LRU; per-call {timeDelta, reSave}
72
+ ```
73
+
74
+ ## 🔢 core — number (frequent)
75
+ ```
76
+ round(value: number, digits = 0) -> number // round to N decimals (alias: NormalizeDouble)
77
+ roundSig(value: number, { digitsR?: number /*total significant digits*/, digitsPoint? = 4, type?: 'max'|'min' }) -> number
78
+ // round to N significant digits — roundSig(1234.5678, {digitsR: 3}) -> 1230 (alias: NormalizeDoubleAnd)
79
+ gcd(a: number, b: number, digits = 8) -> number | gcd(values: Iterable<number>, digits = 8) -> number
80
+ // floats ok; in the ITERABLE overload the 2nd arg is PRECISION digits, not an operand (alias: MaxCommonDivisor[OnArray])
81
+ formatAuto(value: number, maxDigits = 8) -> string // shortest decimal string (alias: DblToStrAuto)
82
+ decimals(value: number, maxDigits = 8, minDigits = 0) -> number // count of meaningful decimals (alias: GetDblPrecision)
83
+ ```
84
+
85
+ ## ⏰ time
86
+ ```
87
+ format(date: Date, pattern, { utc? = true }) -> string // (alias of 11 timeToStr_*/timeLocalToStr_*)
88
+ pattern: 'HH:mm:ss' | 'HH:mm:ss.SSS' | 'yyyy-MM-dd'
89
+ | 'yyyy-MM-dd HH:mm' | 'yyyy-MM-dd HH:mm:ss' | 'yyyy-MM-dd HH:mm:ss.SSS'
90
+ | 'yyyy-MM-dd HH:mm O' | 'yyyy-MM-dd HH:mm:ss O' // O = GMT offset (local)
91
+ { utc: false } -> local variants
92
+ formatDuration(ms: number, pattern?: 'H:mm:ss' | 'H:mm:ss.SSS') -> string // clock; hours unbounded
93
+ durationToStr(ms: number) -> string // humanized
94
+ minDate(a: Date | null, b: Date | null) / maxDate(a, b) -> Date | null // null-tolerant (alias: MinTime/MaxTime)
95
+ convertDatesToStrings(obj) // Date -> strings, recursively (logs)
96
+ const: H1_S D1_S W1_S · M1_MS H1_MS D1_MS W1_MS
97
+ ```
98
+
99
+ ## 🌐 rpc (brief) — transport is ALWAYS caller-supplied (`{emit,on}`); there is NO url / built-in socket
100
+ ```
101
+ // SERVER: `object` is the impl tree, `socket` is a {emit,on} transport adapter
102
+ createRpcServerAuto({ socket: {emit, on}, object, socketKey: string, auth?, limits?, maxPerListen?, throttle?, opt?, replay?, replayOpts? }) -> { api, ... }
103
+ // replay: false|'auto' (default)|'force' — facade members that are replay lines (replayListen) are exposed
104
+ // with BOTH surfaces under the SAME key: legacy plain-Listen path byte-for-byte + line/frameLine/since/keyframe/frame.
105
+ // Upgrading listen -> replayListen is a declaration-site-only change; the facade and clients don't move.
106
+ // replayOpts: {pending?, highWater?, lowWater?, pollMs?} — per-connection lag gate for 'frame'-policy subscribers
107
+ // (pending defaults to socket.io writeBuffer; gates close on disconnect automatically). Replay lines are never throttled.
108
+ createRpcServer(opts) // lower-level core (same { socket, object, socketKey })
109
+ noStrict(obj) // mark a dynamic subtree (no schema)
110
+ endCallback(fn) // mark an RPC stream-callback's end (alias: rpcEndCallback)
111
+
112
+ // CLIENT hub: takes TWO functions — a socket factory + a schema builder; it is NOT an {url} or an options bag
113
+ createRpcClientHub(
114
+ createSocket: (token: string | null) => socket, // YOU build the socket, e.g. socket.io io(url, {auth:{token}})
115
+ schemaBuilder: (rpc) => ({ key: rpc<Api>('socketKey') }), // declare each socketKey's typed API
116
+ hubOpts?: { opt? },
117
+ ) -> hub
118
+ hub: connect(token) -> Promise<clients> · reauth(token) · facade · promise · socket · onConnect/onDisconnect · connectListen/disconnectListen // connect: was setToken
119
+ // connect() resolves after the socket 'connect' event and RPC route/auth handshake; for in-proc/loopback (no 'connect') use hub.facade + await hub.promise
120
+ client (on clients[key], NOT on the hub): func (proxy) · strict (schema-safe) · close() · ready() · init() · subscriptions()
121
+ // pipe = batch a server chain in one packet · space = fire-and-forget
122
+ // func/space/strict paths have stable identity per client+surface: c.func.a.b === c.func.a.b;
123
+ // all === func. Identity survives reauth/transient reconnect; connect/setToken creates a new client generation.
124
+ // onConnect/onDisconnect keep their legacy single-slot contract; connectListen(cb)/disconnectListen(cb) are additive and return individual off functions.
125
+ // transient reconnect of the SAME Socket.IO object restores active logical Listen subscriptions once the new handshake is ready.
126
+ // close()/dispose() and connect()/setToken() are terminal for the old generation. Ordinary RPC calls are NEVER retried.
127
+
128
+ // minimal wiring (the part no signature can show):
129
+ const [tick, ticks] = listen<[number]>()
130
+ createRpcServerAuto({ socket, object: { math: { add: (a, b) => a + b, ticks } }, socketKey: 'math' })
131
+ const hub = createRpcClientHub((t) => io(url, { auth: { t } }), (rpc) => ({ math: rpc<Api>('math') }))
132
+ const c = await hub.connect(token) // c = facade of per-socketKey clients
133
+ await c.math.ready(); await c.math.func.add(2, 3)
134
+ const l = c.math.func as unknown as DeepSocketListen<Api> // typed Listen projection; wrap as webListen(c.math) in app code
135
+ const off = l.ticks.on(v => console.log(v)) // canonical stream subscribe; off is callable and awaitable
136
+ off() // unsubscribe; .callback/.removeCallback are legacy compat, don't teach them
137
+ l.ticks.once(v => console.log(v)) // one event, then auto-off
138
+
139
+ // replay upgrade ONE WORD at the declaration site, everything below follows automatically:
140
+ // const [tick, ticks] = listen<[number]>() // before
141
+ const [tick, ticks] = replayListen<[number]>({history: 1024, current: 'last'}) // after same facade, same key
142
+ // legacy subscribers unchanged (byte-for-byte). Replay consumers now also get:
143
+ const sub = replaySubscribe(l.ticks, v => {}, {since: saved, onSeq: s => saved = s}) // catch-up + live; no uncovered loss/dups (a producer frame/keyframe may jump raw seq)
144
+ const sub2 = replaySubscribe(c.math.func.ticks, v => {}) // replay members project on func/strict directly — no cast needed
145
+ const routed = replayRouteSubscribe(l.ticks, v => {}, {label: 'relay'})
146
+ await routed.switch(nextRemoteTicks, {label: 'direct'}) // relay/direct hand-off: old route closes after catch-up
147
+ await l.ticks.frame(mySeq) // pull at YOUR pace (50ms timer etc.) — server condenses via the line's frame lambda
148
+ // full guide + examples rpc.md; frame model / lag policies → 🎞️ recipe below and rare docs
149
+ ```
150
+
151
+ ## 🎙️ Media over socket binary Listen frames
152
+ ```
153
+ import { Media } from "wenay-common2" // or: import * as Media from "wenay-common2/media"
154
+
155
+ Media.createAudioSource({format?: 'int16'|'float32', mode?: 'pcm'|'record', replay?}) -> [emit, listen] & control
156
+ Media.createVideoSource({fps? = 3, codec? = 'jpeg', quality?, replay?}) -> [emit, listen] & control
157
+ control: start() -> Promise<'idle'|'requesting'|'live'|'denied'|'no-device'|'error'> · stop() · getStats() · setDevice(id) · listDevices() · state
158
+ Media.encodeMediaFrame(meta, payload) / Media.decodeMediaFrame(frame) // one Uint8Array = 40-byte fixed header + raw payload
159
+
160
+ // viewer/publisher one-liners (the demo stand is built on these):
161
+ Media.attachVideoCanvas(line, canvas, {onError?}) -> {stats(), off} // decode+render any video line; codec/size come from frame headers
162
+ Media.attachAudioPlayer(line, {maxBacklogSec? = 0.35}) -> {enable(), disable(), enabled, stats(), off} // live PCM playback, backlog drops
163
+ Media.pipeMediaPublish(line, publish, {stamp? = true, onError?}) -> off // source -> RPC publish fn; stamp lets viewers measure latency
164
+ ```
165
+ Audio default is PCM frames from `AudioWorklet` where available (`mode:'record'` uses MediaRecorder chunks). Video default is camera snapshots (JPEG, low fps for vision) captured hidden-tab-proof: a worker timer ticks (setInterval is throttled to ~1/s in hidden tabs), `ImageCapture.grabFrame()` reads the track (a hidden `<video>` stops painting), and JPEG encode runs in a worker (main-thread `convertToBlob` stalls ~1s hidden) — `worker:false` opts back into the plain in-page path. Screen share is the same video source with an injected stream: `createVideoSource({stream: () => navigator.mediaDevices.getDisplayMedia({video: true})})`. Put `listen` into `createRpcServerAuto` like any other Listen; with `replay:true`, the returned listen is a replay line, so RPC auto exposes legacy + replay surfaces under the same key. Backpressure policy: audio is lossless queue; video `replay:true` defaults to keep-latest frame recovery. `transport:'webrtc'` is reserved for a future SFU/signaling adapter; socket binary is the default today. Living example: the demo stand (`npm run demo`) streams camera / mic / screen share between two tabs through a tiny server-side relay of replay lines (`demo/server.ts` + `demo/client.ts`).
166
+
167
+ ## 🤝 Peer — accounts see each other's stores (one-call SDK)
168
+ > `import { Peer } from "wenay-common2"` or `import * as Peer from "wenay-common2/peer"`.
169
+ > The happy-path facade over rpc + store + replay + route coordinator. Legacy-friendly by design:
170
+ > the server side is a FRAGMENT spread into your EXISTING `createRpcServerAuto` object, the client
171
+ > side rides your existing connection old keys keep working untouched.
172
+ ```
173
+ // SERVER — next to your legacy object:
174
+ const host = Peer.createPeerHost({authorize?, history?}) // authorize(env) = server-side canExposeEndpoint
175
+ io.on('connection', socket => {
176
+ const peer = host.connection(accountOf(socket)) // per-account signal port + relay journal
177
+ createRpcServerAuto({socket, socketKey, object: {...legacyObject, peer: peer.fragment}, disconnectListen})
178
+ disconnectListen.on(peer.close)
179
+ })
180
+ host: connection(account) · relay(account) · accounts() · revoke(pair, accounts, reason?) · close()
181
+
182
+ // CLIENT the whole happy path:
183
+ const me = Peer.createPeerClient<World>({
184
+ remote: c.app.func.peer, // deep proxy of the fragment rest of the connection is yours
185
+ account: 'a',
186
+ initial: {...}, // own store: write me.store.state — others see it
187
+ rtc?: () => new RTCPeerConnection(cfg), // omit = relay-only (promoteDirect unavailable)
188
+ })
189
+ const bob = me.peer('b') // mirror + route control for another account
190
+ await bob.ready // keyframe/tail landed
191
+ bob.store.state // live mirror reads survive ANY route change
192
+ await bob.promoteDirect() // relay -> WebRTC direct; {ok, state, reason?} result, not a throw
193
+ bob.route() // 'relay' | 'direct' · bob.reinterposeRelay() · bob.fallback() · bob.block()
194
+ me.onRoute(ev => {}) // route transitions for metrics/UI
195
+ ```
196
+ Key property: the relay journal stores the owner's envelopes VERBATIM (owner seq space), so a
197
+ relay <-> direct hand-off is a plain seq resume no uncovered loss or duplicate delivery. Late joiners
198
+ get a keyframe folded server-side even while the owner is offline.
199
+ Reconnect correctness is self-healing: a publisher gap makes the relay reject the push WITH its last
200
+ seq, and the client repairs from that coordinate automatically (`repair: 'tail'` lossless (default)
201
+ | `'keyframe'` cheap reset for ephemeral state). Server declares journal semantics
202
+ (`createPeerHost({gap: 'resume' | 'sacred'})`); a declared `journal: 'sacred'` TYPE-forbids the cheap
203
+ repair. `me.resync()` after a transport reconnect repairs without waiting for the next write;
204
+ failures surface via `onPublishError`, never silently. Policy/session material:
205
+ `createPeerClient({session, accept, policy})` + host `authorize` see rare docs for the envelope
206
+ contract and the underlying primitives (`createRouteCoordinator`, `createSignalHub`,
207
+ `createWebRtcConnector`). Oracle: `replay/peer-sdk.test.ts`.
208
+
209
+ ### Calls, presence and the media relay (messenger-style, on the same parts)
210
+ ```
211
+ // presence rides in the fragment (host >= 1.0.74): subscribe FIRST, then list()
212
+ c.app.func.peer.presence.changes.on(({account, online}) => {})
213
+ await c.app.func.peer.presence.list() // ['a', 'b', ...] connected right now
214
+
215
+ // SERVER — media relay next to the fragment (per-account named lines, policy-gated watch):
216
+ const media = Peer.createMediaRelay({
217
+ lines: {cam: 'video', mic: 'audio', screen: 'video'},
218
+ canWatch?: (watcher, owner, line) => bool, // ACL, checked on paths + every forwarded frame
219
+ })
220
+ object: {peer: peer.fragment, media: {publish: media.publishOf(account), watch: media.watchOf(account)}}
221
+ media.dropAccount(account) // retire lines + keyspace (e.g. on presence offline); next publish revives
222
+
223
+ // CLIENT calls are envelopes over the SAME signal port; media attach stays app code:
224
+ const calls = Peer.createCallManager({port: Peer.callPortOf(c.app.func.peer), self: 'a'})
225
+ await calls.ready // signal subscription confirmed server-side
226
+ calls.rings.on(call => { call.accept() /* or call.decline() */ })
227
+ const call = calls.call('b', {kinds: ['cam', 'mic']}) // meta rides opaquely
228
+ call.changed.on(state => {}) // 'active' | 'ended'
229
+ await call.ended // 'declined'|'busy'|'offline'|'timeout'|'hangup'|'canceled'
230
+ call.hangup()
231
+ // while active: publish own frames + attach the peer's lines (Media.attach* viewers)
232
+ media.publish('cam', frame, Date.now()); attachVideoCanvas(c.app.func.media.watch.b.cam, canvas)
233
+ ```
234
+ Relay-first is the privacy default (mainstream messengers route calls through their servers too);
235
+ `promoteDirect` stays the policy-gated opt-in for the data path. Ring/accept/decline/hangup ride the
236
+ existing signal hub the host `authorize` hook sees them too (single server-side policy point), and
237
+ an offline callee fails fast (`'offline'`), no timeout wait. The default incoming gate auto-declines
238
+ with `'busy'` during a live call, which also settles simultaneous cross-calls. `video` relay lines
239
+ are keep-latest (late joiner pulls `keyframe()` instantly), `audio` is a short lossless queue; frames
240
+ ride as `[frame, sentAt]` so viewer latency stats work out of the box. Watch access is an ACL, not a
241
+ convention: `watchOf(account)` views run `canWatch` on every new subscribe/keyframe and every live
242
+ frame. "Media access follows the call" is a few lines of app code (grant on `'active'`, revoke on
243
+ `'ended'`); after revocation an already-open policy view receives no more data. Oracle:
244
+ `replay/peer-call.test.ts`.
245
+
246
+ ## 🔁 Observe reactive state + store/mirror API
247
+ > `import { Observe } from "wenay-common2"` or `import * as Observe from "wenay-common2/observe"`.
248
+ > This is the documented v2 reactive/store surface.
249
+ ```
250
+ // coarse reactive object: subscribe to the fact that a subtree changed, then re-read current state
251
+ Observe.reactive<T extends object>(obj, opts?) -> T
252
+ Observe.onUpdate(node, cb: () => void) -> off
253
+ Observe.onUpdatePaths(node, cb: ({paths}) => void) -> off // optional dirty paths, relative to node
254
+ Observe.flushReactive(node) -> Promise<void>
255
+ Observe.toRaw(node) -> raw value behind the proxy // snapshots/serialization without touching lazy nodes
256
+ Observe.listenUpdate(node) -> Listen<void> // RPC bridge for coarse change notifications
257
+ Observe.listenUpdatePaths(node) -> Listen<{paths: PropertyKey[][]}>
258
+ opts: { drain?: "immediate"|"micro"|number|((flush)=>void), depth?, eager? }
259
+
260
+ // path-addressed store facade over reactive()
261
+ Observe.createStore<T extends object>(initial, opts?) -> Store<T>
262
+ store.state // reactive data object; write normally
263
+ store.node.path.to.leaf.get()/snapshot()/replace(v) // set(v) is a deprecated alias of replace(v)
264
+ store.node.path.to.leaf.on((value, ctx) => {}, {current?, drain?, key?}) -> off
265
+ store.node.path.to.leaf.once(cb, opts?) -> off
266
+ store.update(mask, opts?) -> selection // typed selected snapshot
267
+ selection.get() · selection.on((snap, ctx)=>{}, opts?) -> off · selection.onEach((value, ctx)=>{}, opts?) -> off
268
+ store.each(opts?) -> Listen<[key, value, ctx]> // changed TOP-LEVEL keys as a plain Listen — THE per-key feed
269
+ // one call per CHANGED key per drain window: value = current store.state[key] at flush time; undefined = key deleted;
270
+ // two writes to one key in a window = ONE call (last value); deeper dirt (state.a.b = ...) reports 'a' once
271
+ // root replace (store.replace / mirror keyframe) EXPANDS: one call per key of the new state + (key, undefined)
272
+ // per key the replace removed cold start / reconnect are NOT special cases for per-key consumers
273
+ // plain Listen shape (on(cb) -> off · once · count); zero cost while it has no subscribers
274
+ // NOT update(true).onEach: onEach fires per SELECTED path, and mask true selects the root —
275
+ // ONE call per window with the whole dict (a dev warn points to each())
276
+ store.count() -> number
277
+
278
+ // network shape: backend exposes snapshots + changed Listen; frontend mirrors selected masks locally
279
+ Observe.exposeStore(store, opts?) -> { get(mask?), set(path,value), replace(path,value), changed, changedPaths, patches?, changedData? }
280
+ Observe.createStoreMirror(remote, initial, opts?) -> store & { sync(mask, opts?) -> Promise<off>; syncPatches(mask, opts?) -> Promise<off>; syncChangedData(mask, opts?) -> Promise<off> }
281
+ // changedPaths is optional optimization: mirror pulls mask dirty paths; fallback is changed -> get(mask).
282
+ // Optional push-data mode: exposeStore(store,{push:true}) + syncPatches/syncChangedData; details in rare docs.
283
+
284
+ // Sequenced sync (replay line): seq-numbered patch stream — keyframe catch-up, reconnect by seq (tail, not snapshot)
285
+ Observe.exposeStoreReplay(store, {history? = 1024}) -> { api /* spread into the RPC server object */, replay, close }
286
+ // the patch line declares its condensing `frame` itself (last patch per exact path) — reconnect tails and
287
+ // lag recovery arrive as a mini-frame (changed paths only), zero config
288
+ Observe.syncStoreReplay(mirror, remote /*{line, since, keyframe, frame?} of api.replay*/, {since?, onSeq?}) -> off
289
+ // off.ready (catch-up done) · off.seq() (save for reconnect: syncStoreReplay(..., {since: prev.seq()}))
290
+ // lagging/late client NEVER gets a backlog: evicted seq -> ONE fresh keyframe + live
291
+ // freshness is an option, not consumer boilerplate: {staleMs, onStale} flags a silent line / stale keyframe (edge-triggered both ways; 🎞️ in rare docs)
292
+ Observe.syncStoreReplayRoute(mirror, remote, {label?}) -> off & {switch(nextRemote, opts), ready, seq(), label(), active()}
293
+ // relay/direct promotion and re-interposition: replacement route catches up by seq before the old route closes
294
+ Observe.syncStoreReplayEach<T>(remote, (key, value, ctx) => {}, opts?) -> off & {store, ready, seq(), isStale(), lastTs()}
295
+ // one-call remote fold: mirror store + syncStoreReplay + store.each() — the callback fires per CHANGED
296
+ // top-level key; first delivery = keyframe EXPANDED per key; (key, undefined) = key deleted
297
+ // opts = all replaySubscribe opts (since/onSeq/policy/staleMs/onStale/onError...) + {drain?, initial?}
298
+ // off() tears down BOTH the store sub and the wire sub; direct reads via off.store.state.KEY
299
+ // reconnect: syncStoreReplayEach(remote, cb, {since: prev.seq(), initial: prev.store.snapshot()})
300
+ // — the tail lands ON TOP of the previous state (a fresh empty mirror would not converge)
301
+ // Offline persisted mirror (snapshot mode): local cache first, then replay catch-up by seq
302
+ Observe.createOfflineStore({key, remote?, initial, storage, version?, debounceMs?, syncOpts?}) -> Promise<store & {ready, flush(), close(), status(), statusListen, reconnect(remote)}>
303
+ Observe.persistStore(store, {key, storage, seq?, debounceMs?}) -> {flush, forceFlush, close, setSeq, seq, status, statusListen}
304
+ Observe.createMemoryOfflineStorage(initial?) -> OfflineStorage
305
+ // persists {version, seq, snapshot, savedAt}; seq is the correctness coordinate, timestamps are UX/freshness only
306
+ // mode:'topLevel' is reserved; first implemented mode is snapshot
307
+
308
+ // Declarative resource manager above mirror/replay/offline: app chooses what to start, not the store core
309
+ Observe.managedStore.mirror({remote, initial, mask, tags?, priority?, explicitOnly?, large?, sync?})
310
+ Observe.managedStore.replay({remote, initial, tags?, priority?, explicitOnly?, large?, syncOpts?})
311
+ Observe.managedStore.offline({remote?, initial, storage, storageKey?, tags?, priority?, explicitOnly?, large?, syncOpts?})
312
+ Observe.createStoreManager(resources) -> {plan(opts?), start(key, opts?), startPlanned(opts?), stop(key), stopAll(), get(key), touch(key, weight?), usage(), statusListen, handles}
313
+ // plan excludes explicitOnly/large by default; {includeExplicit, includeLarge} opts opt them in
314
+ // Slow-client conflation: recipe section 🎞️ below. Full generic surface (any event line, history/time-travel) -> Replay namespace, 🎞️ in rare docs.
315
+ // Object add/delete/deep set are paths. Array mutation dirties the whole array branch, not splice internals.
316
+ ```
317
+ ```
318
+ type Market = {data: {BTC?: number; ETH?: number}; meta: {status?: string}}
319
+ const market = Observe.createStore<Market>({data: {BTC: 1, ETH: 2}, meta: {status: "ok"}})
320
+
321
+ market.state.data.BTC = 3 // plain local mutation
322
+ market.node.data.BTC.on((v, ctx) => {}, {current: true}) // ctx.path = ["data", "BTC"]
323
+ market.node.data.on(data => {}, {current: true, drain: 50}) // branch snapshot, per-sub drain
324
+ market.update({data: {BTC: true, ETH: true}}, {current: true}).on(snap => {})
325
+
326
+ // backend facade over RPC
327
+ const api = Observe.exposeStore(market)
328
+
329
+ // frontend mirror: UI subscribes to local mirror, not RPC directly
330
+ const mirror = Observe.createStoreMirror<Market>(api, {data: {}, meta: {}})
331
+ const stop = await mirror.sync({data: {BTC: true}, meta: {status: true}}, {current: true, drain: 250}) // uses changedPaths when available
332
+ mirror.node.data.BTC.on(v => {}, {current: true})
333
+ stop()
334
+
335
+ // per-key feed dict store -> grid rows; keyframe / reconnect are just expansion, not special cases
336
+ type Rows = Record<string, {qty: number}>
337
+ const rows = Observe.createStore<Rows>({})
338
+ const offRows = rows.each().on((key, row) => { /* row === undefined ? removeRow(key) : upsertRow(key, row) */ })
339
+
340
+ // the same per-key contract over the wire — ONE call (mirror store + syncStoreReplay + each)
341
+ const exposed = Observe.exposeStoreReplay(rows, {history: 1024}) // server side: spread exposed.api into the RPC object
342
+ const feed = Observe.syncStoreReplayEach<Rows>(exposed.api.replay, (key, row) => {}, {drain: "micro"})
343
+ await feed.ready // catch-up done: keyframe arrived expanded per key
344
+ feed.store.state // the mirror — direct reads / extra subscriptions
345
+ feed() // tears down the store sub AND the wire sub
346
+ // reconnect later: syncStoreReplayEach(remote, cb, {since: feed.seq(), initial: feed.store.snapshot()})
347
+ // offline persisted mirror cached snapshot first, then replay catch-up over the same remote
348
+ const offline = await Observe.createOfflineStore<Rows>({
349
+ key: "rows",
350
+ remote: exposed.api.replay,
351
+ initial: {},
352
+ storage: Observe.createMemoryOfflineStorage(), // use IndexedDB/SQLite adapter in an app
353
+ debounceMs: 250,
354
+ })
355
+ offline.each().on((key, row) => {})
356
+ await offline.ready
357
+ await offline.flush()
358
+ offline.close()
359
+
360
+ // configurable app-level resource plan
361
+ const manager = Observe.createStoreManager({
362
+ market: Observe.managedStore.mirror({remote: api.market, initial: {data: {}, meta: {}}, mask: {data: {BTC: true}}, tags: ['bootstrap'], priority: 10}),
363
+ rows: Observe.managedStore.offline({remote: exposed.api.replay, initial: {}, storage: Observe.createMemoryOfflineStorage(), tags: ['grid']}),
364
+ video: Observe.managedStore.replay({remote: videoReplay, initial: {}, explicitOnly: true, large: true}),
365
+ })
366
+ await manager.startPlanned({tags: ['bootstrap']})
367
+ manager.touch('rows', 3) // usage can raise future plan score
368
+ await manager.start('video', {explicit: true})
369
+ ```
370
+ Runnable example: `npx tsx observe/store-mirror.example.ts`.
371
+ Offline oracles: `npx tsx replay/offline-store.test.ts`; real Socket.IO/RPC wire: `npx tsx replay/offline-store-socket.test.ts`.
372
+
373
+ ## 🎞️ Fast ticks vs slow client replay lines + server-owned lag gate (recipe)
374
+ > The problem: the producer emits faster than a bad link drains. Naive streaming grows an unbounded
375
+ > outgoing queue per slow client. The replay stack solves it with ONE mental model — the FRAME:
376
+ > `frame(sinceSeq, hint?)` on the line returns envelopes bringing a consumer from `sinceSeq` to now,
377
+ > as compact as the line allows (exact tail -> condensed mini-frame -> keyframe fallback). The same
378
+ > method serves reconnect (`since`), client pull (own pace) and lag recovery. The transport sees only
379
+ > `seq`; ALL event semantics live in two lambdas declared on the line: `current` (keyframe = pointer
380
+ > to truth) and `frame` (condenser may honor a client-supplied `hint`, see below).
381
+ ```ts
382
+ import { Observe, Replay } from 'wenay-common2'
383
+
384
+ // ---- producer: declare what the line HAS (its class follows no mode flags) ----
385
+ const [emitQuote, quotes] = Replay.replayListen<[string, number]>({
386
+ history: 4096,
387
+ frame: (tail, hint) => lastPerSymbol(tail, hint), // mini-frame; hint = client's pick of the condensation rule
388
+ }) // current+frame = compact + keyframe fallback · current only = exact tail + keyframe · frame only = compact while retained, loud on eviction · neither = sacred exact queue
389
+ // store lines: exposeStoreReplay already declares current + frame (last patch per path) zero config
390
+ const store = Observe.createStore<World>(initial, { drain: 'micro' })
391
+ const exposed = Observe.exposeStoreReplay(store, { history: 1024 })
392
+
393
+ // ---- per CONNECTION: the rpc server owns the gate; the facade does NOT change ----
394
+ io.on('connection', socket => {
395
+ const [disconnect, disconnectListen] = listen<[]>()
396
+ socket.on('disconnect', () => disconnect())
397
+ createRpcServerAuto({
398
+ socket: { emit: (k, d) => socket.emit(k, d), on: (k, cb) => socket.on(k, cb) },
399
+ socketKey: 'world',
400
+ object: { ...exposed.api, quotes }, // replay lines auto-exposed: both surfaces, same key
401
+ disconnectListen, // gates close on disconnect automatically
402
+ replayOpts: { highWater: 64, lowWater: 8 },// arms frameLine; pending defaults to socket.io writeBuffer
403
+ })
404
+ })
405
+
406
+ // ---- client: picks its LAG POLICY per subscription; no conflation logic anywhere ----
407
+ const sub = Replay.replaySubscribe(deep.quotes, cb, {since: saved, policy: 'frame'}) // server may skip live envelopes; drain -> state-equivalent mini-frame
408
+ const sub2 = Replay.replaySubscribe(deep.quotes, cb2, {since: saved}) // 'queue' (default): ungated live; catch-up may still use producer frame/keyframe
409
+ // own pace (e.g. 50ms skips + condensation): pull on YOUR timer — hint picks the rule, server condenses:
410
+ // every(50, async () => { for (const ev of await deep.quotes.frame(mySeq, hint)) apply(ev); })
411
+ // store mirror: Observe.syncStoreReplay(mirror, deep.replay, {since: prev.seq()}) — same contract
412
+ // delivery contract: FIRST delivery = snapshot/tail start (same event type), then strictly-newer,
413
+ // seq-ascending and deduped. Raw seq jumps are legal only when a producer frame/keyframe covers them;
414
+ // a sacred retained tail is exact, while sacred eviction closes with onError instead of continuing past a hole.
415
+ ```
416
+ Rules that make it correct (violating any of these silently breaks convergence):
417
+ - **The line declares its recovery sources.** `current` (keyframe: SAMPLED from truth, never computed
418
+ from deltas; `'last'` = last envelope for single-entity lines) and/or `frame` (condenser). Without
419
+ `current`, eviction is terminal even when a condenser exists: a true sacred queue (neither source)
420
+ replays the retained tail exactly, and `frame()` THROWS once that tail is gone. A lagging
421
+ `'frame'`-policy sacred subscriber gets a stream end, never silent loss.
422
+ - **A `frame` result must be state-equivalent to the tail it replaces** (per the line's own semantics).
423
+ "Can't condense THIS tail" is legal and simple: return the tail as-is. Refuse-loudly is `throw`.
424
+ Multiple condensation standards live INSIDE the lambda, dispatched by the client-supplied `hint`
425
+ (opaque to the transport): `frame(tail, hint)`.
426
+ - **Events must be ABSOLUTE per their entity** for last-per-entity condensing (store patches are).
427
+ - Gate drops never hole the journal — it is written BEFORE any gate. Queue-policy live delivery is
428
+ ungated; reconnect catch-up may use the producer's state-equivalent mini-frame. An evicted state
429
+ line falls back to a keyframe (visible raw seq jump); an evicted sacred line fails terminally.
430
+ - `pending()` and the watermarks share units (bytes, packets, frames — anything, but the same).
431
+
432
+ Manual path (pre-rev2, still works, `keyOf` @deprecated): build the gate yourself with
433
+ `Replay.conflateReplay(exposed.replay, {pending, highWater, keyOf})` and spread `gated.api` into the
434
+ facade — details in rare docs. New code should declare `frame` on the line instead.
435
+
436
+ Wire-level proof/oracles: `npx ts-node replay/rpc-auto.test.ts` (real Socket.IO: auto-exposure, legacy
437
+ parity, frame equivalence, gate lag sim), plus `replay/conflate-socket.test.ts`, `replay/conflate.test.ts`,
438
+ `replay/coalesce.test.ts`. Full generic surface (history/time-travel, archive) → 🎞️ in rare docs.