wenay-common2 1.0.63 → 1.0.65

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.
@@ -0,0 +1,338 @@
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
+ ## 🔁 Observe — reactive state + store/mirror API
147
+ > `import { Observe } from "wenay-common2"` or `import * as Observe from "wenay-common2/observe"`.
148
+ > This is the documented v2 reactive/store surface.
149
+ ```
150
+ // coarse reactive object: subscribe to the fact that a subtree changed, then re-read current state
151
+ Observe.reactive<T extends object>(obj, opts?) -> T
152
+ Observe.onUpdate(node, cb: () => void) -> off
153
+ Observe.onUpdatePaths(node, cb: ({paths}) => void) -> off // optional dirty paths, relative to node
154
+ Observe.flushReactive(node) -> Promise<void>
155
+ Observe.toRaw(node) -> raw value behind the proxy // snapshots/serialization without touching lazy nodes
156
+ Observe.listenUpdate(node) -> Listen<void> // RPC bridge for coarse change notifications
157
+ Observe.listenUpdatePaths(node) -> Listen<{paths: PropertyKey[][]}>
158
+ opts: { drain?: "immediate"|"micro"|number|((flush)=>void), depth?, eager? }
159
+
160
+ // path-addressed store facade over reactive()
161
+ Observe.createStore<T extends object>(initial, opts?) -> Store<T>
162
+ store.state // reactive data object; write normally
163
+ store.node.path.to.leaf.get()/snapshot()/replace(v) // set(v) is a deprecated alias of replace(v)
164
+ store.node.path.to.leaf.on((value, ctx) => {}, {current?, drain?, key?}) -> off
165
+ store.node.path.to.leaf.once(cb, opts?) -> off
166
+ store.update(mask, opts?) -> selection // typed selected snapshot
167
+ selection.get() · selection.on((snap, ctx)=>{}, opts?) -> off · selection.onEach((value, ctx)=>{}, opts?) -> off
168
+ store.each(opts?) -> Listen<[key, value, ctx]> // changed TOP-LEVEL keys as a plain Listen — THE per-key feed
169
+ // one call per CHANGED key per drain window: value = current store.state[key] at flush time; undefined = key deleted;
170
+ // two writes to one key in a window = ONE call (last value); deeper dirt (state.a.b = ...) reports 'a' once
171
+ // root replace (store.replace / mirror keyframe) EXPANDS: one call per key of the new state + (key, undefined)
172
+ // per key the replace removed — cold start / reconnect are NOT special cases for per-key consumers
173
+ // plain Listen shape (on(cb) -> off · once · count); zero cost while it has no subscribers
174
+ // NOT update(true).onEach: onEach fires per SELECTED path, and mask true selects the root —
175
+ // ONE call per window with the whole dict (a dev warn points to each())
176
+ store.count() -> number
177
+
178
+ // network shape: backend exposes snapshots + changed Listen; frontend mirrors selected masks locally
179
+ Observe.exposeStore(store, opts?) -> { get(mask?), set(path,value), replace(path,value), changed, changedPaths, patches?, changedData? }
180
+ Observe.createStoreMirror(remote, initial, opts?) -> store & { sync(mask, opts?) -> Promise<off>; syncPatches(mask, opts?) -> Promise<off>; syncChangedData(mask, opts?) -> Promise<off> }
181
+ // changedPaths is optional optimization: mirror pulls mask ∩ dirty paths; fallback is changed -> get(mask).
182
+ // Optional push-data mode: exposeStore(store,{push:true}) + syncPatches/syncChangedData; details in rare docs.
183
+
184
+ // Sequenced sync (replay line): seq-numbered patch stream — keyframe catch-up, reconnect by seq (tail, not snapshot)
185
+ Observe.exposeStoreReplay(store, {history? = 1024}) -> { api /* spread into the RPC server object */, replay, close }
186
+ // the patch line declares its condensing `frame` itself (last patch per exact path) — reconnect tails and
187
+ // lag recovery arrive as a mini-frame (changed paths only), zero config
188
+ Observe.syncStoreReplay(mirror, remote /*{line, since, keyframe, frame?} of api.replay*/, {since?, onSeq?}) -> off
189
+ // off.ready (catch-up done) · off.seq() (save for reconnect: syncStoreReplay(..., {since: prev.seq()}))
190
+ // lagging/late client NEVER gets a backlog: evicted seq -> ONE fresh keyframe + live
191
+ // freshness is an option, not consumer boilerplate: {staleMs, onStale} flags a silent line / stale keyframe (edge-triggered both ways; 🎞️ in rare docs)
192
+ Observe.syncStoreReplayRoute(mirror, remote, {label?}) -> off & {switch(nextRemote, opts), ready, seq(), label(), active()}
193
+ // relay/direct promotion and re-interposition: replacement route catches up by seq before the old route closes
194
+ Observe.syncStoreReplayEach<T>(remote, (key, value, ctx) => {}, opts?) -> off & {store, ready, seq(), isStale(), lastTs()}
195
+ // one-call remote fold: mirror store + syncStoreReplay + store.each() — the callback fires per CHANGED
196
+ // top-level key; first delivery = keyframe EXPANDED per key; (key, undefined) = key deleted
197
+ // opts = all replaySubscribe opts (since/onSeq/policy/staleMs/onStale/onError...) + {drain?, initial?}
198
+ // off() tears down BOTH the store sub and the wire sub; direct reads via off.store.state.KEY
199
+ // reconnect: syncStoreReplayEach(remote, cb, {since: prev.seq(), initial: prev.store.snapshot()})
200
+ // — the tail lands ON TOP of the previous state (a fresh empty mirror would not converge)
201
+ // Offline persisted mirror (snapshot mode): local cache first, then replay catch-up by seq
202
+ Observe.createOfflineStore({key, remote?, initial, storage, version?, debounceMs?, syncOpts?}) -> Promise<store & {ready, flush(), close(), status(), statusListen, reconnect(remote)}>
203
+ Observe.persistStore(store, {key, storage, seq?, debounceMs?}) -> {flush, forceFlush, close, setSeq, seq, status, statusListen}
204
+ Observe.createMemoryOfflineStorage(initial?) -> OfflineStorage
205
+ // persists {version, seq, snapshot, savedAt}; seq is the correctness coordinate, timestamps are UX/freshness only
206
+ // mode:'topLevel' is reserved; first implemented mode is snapshot
207
+
208
+ // Declarative resource manager above mirror/replay/offline: app chooses what to start, not the store core
209
+ Observe.managedStore.mirror({remote, initial, mask, tags?, priority?, explicitOnly?, large?, sync?})
210
+ Observe.managedStore.replay({remote, initial, tags?, priority?, explicitOnly?, large?, syncOpts?})
211
+ Observe.managedStore.offline({remote?, initial, storage, storageKey?, tags?, priority?, explicitOnly?, large?, syncOpts?})
212
+ Observe.createStoreManager(resources) -> {plan(opts?), start(key, opts?), startPlanned(opts?), stop(key), stopAll(), get(key), touch(key, weight?), usage(), statusListen, handles}
213
+ // plan excludes explicitOnly/large by default; {includeExplicit, includeLarge} opts opt them in
214
+ // Slow-client conflation: recipe section 🎞️ below. Full generic surface (any event line, history/time-travel) -> Replay namespace, 🎞️ in rare docs.
215
+ // Object add/delete/deep set are paths. Array mutation dirties the whole array branch, not splice internals.
216
+ ```
217
+ ```
218
+ type Market = {data: {BTC?: number; ETH?: number}; meta: {status?: string}}
219
+ const market = Observe.createStore<Market>({data: {BTC: 1, ETH: 2}, meta: {status: "ok"}})
220
+
221
+ market.state.data.BTC = 3 // plain local mutation
222
+ market.node.data.BTC.on((v, ctx) => {}, {current: true}) // ctx.path = ["data", "BTC"]
223
+ market.node.data.on(data => {}, {current: true, drain: 50}) // branch snapshot, per-sub drain
224
+ market.update({data: {BTC: true, ETH: true}}, {current: true}).on(snap => {})
225
+
226
+ // backend facade over RPC
227
+ const api = Observe.exposeStore(market)
228
+
229
+ // frontend mirror: UI subscribes to local mirror, not RPC directly
230
+ const mirror = Observe.createStoreMirror<Market>(api, {data: {}, meta: {}})
231
+ const stop = await mirror.sync({data: {BTC: true}, meta: {status: true}}, {current: true, drain: 250}) // uses changedPaths when available
232
+ mirror.node.data.BTC.on(v => {}, {current: true})
233
+ stop()
234
+
235
+ // per-key feed — dict store -> grid rows; keyframe / reconnect are just expansion, not special cases
236
+ type Rows = Record<string, {qty: number}>
237
+ const rows = Observe.createStore<Rows>({})
238
+ const offRows = rows.each().on((key, row) => { /* row === undefined ? removeRow(key) : upsertRow(key, row) */ })
239
+
240
+ // the same per-key contract over the wire — ONE call (mirror store + syncStoreReplay + each)
241
+ const exposed = Observe.exposeStoreReplay(rows, {history: 1024}) // server side: spread exposed.api into the RPC object
242
+ const feed = Observe.syncStoreReplayEach<Rows>(exposed.api.replay, (key, row) => {}, {drain: "micro"})
243
+ await feed.ready // catch-up done: keyframe arrived expanded per key
244
+ feed.store.state // the mirror — direct reads / extra subscriptions
245
+ feed() // tears down the store sub AND the wire sub
246
+ // reconnect later: syncStoreReplayEach(remote, cb, {since: feed.seq(), initial: feed.store.snapshot()})
247
+ // offline persisted mirror — cached snapshot first, then replay catch-up over the same remote
248
+ const offline = await Observe.createOfflineStore<Rows>({
249
+ key: "rows",
250
+ remote: exposed.api.replay,
251
+ initial: {},
252
+ storage: Observe.createMemoryOfflineStorage(), // use IndexedDB/SQLite adapter in an app
253
+ debounceMs: 250,
254
+ })
255
+ offline.each().on((key, row) => {})
256
+ await offline.ready
257
+ await offline.flush()
258
+ offline.close()
259
+
260
+ // configurable app-level resource plan
261
+ const manager = Observe.createStoreManager({
262
+ market: Observe.managedStore.mirror({remote: api.market, initial: {data: {}, meta: {}}, mask: {data: {BTC: true}}, tags: ['bootstrap'], priority: 10}),
263
+ rows: Observe.managedStore.offline({remote: exposed.api.replay, initial: {}, storage: Observe.createMemoryOfflineStorage(), tags: ['grid']}),
264
+ video: Observe.managedStore.replay({remote: videoReplay, initial: {}, explicitOnly: true, large: true}),
265
+ })
266
+ await manager.startPlanned({tags: ['bootstrap']})
267
+ manager.touch('rows', 3) // usage can raise future plan score
268
+ await manager.start('video', {explicit: true})
269
+ ```
270
+ Runnable example: `npx tsx observe/store-mirror.example.ts`.
271
+ Offline oracles: `npx tsx replay/offline-store.test.ts`; real Socket.IO/RPC wire: `npx tsx replay/offline-store-socket.test.ts`.
272
+
273
+ ## 🎞️ Fast ticks vs slow client — replay lines + server-owned lag gate (recipe)
274
+ > The problem: the producer emits faster than a bad link drains. Naive streaming grows an unbounded
275
+ > outgoing queue per slow client. The replay stack solves it with ONE mental model — the FRAME:
276
+ > `frame(sinceSeq, hint?)` on the line returns envelopes bringing a consumer from `sinceSeq` to now,
277
+ > as compact as the line allows (exact tail -> condensed mini-frame -> keyframe fallback). The same
278
+ > method serves reconnect (`since`), client pull (own pace) and lag recovery. The transport sees only
279
+ > `seq`; ALL event semantics live in two lambdas declared on the line: `current` (keyframe = pointer
280
+ > to truth) and `frame` (condenser — may honor a client-supplied `hint`, see below).
281
+ ```ts
282
+ import { Observe, Replay } from 'wenay-common2'
283
+
284
+ // ---- producer: declare what the line HAS (its class follows — no mode flags) ----
285
+ const [emitQuote, quotes] = Replay.replayListen<[string, number]>({
286
+ history: 4096,
287
+ frame: (tail, hint) => lastPerSymbol(tail, hint), // mini-frame; hint = client's pick of the condensation rule
288
+ }) // current+frame = condensable · current only = keyframe recovery · neither = sacred queue (never skipped, loud on eviction)
289
+ // store lines: exposeStoreReplay already declares current + frame (last patch per path) — zero config
290
+ const store = Observe.createStore<World>(initial, { drain: 'micro' })
291
+ const exposed = Observe.exposeStoreReplay(store, { history: 1024 })
292
+
293
+ // ---- per CONNECTION: the rpc server owns the gate; the facade does NOT change ----
294
+ io.on('connection', socket => {
295
+ const [disconnect, disconnectListen] = listen<[]>()
296
+ socket.on('disconnect', () => disconnect())
297
+ createRpcServerAuto({
298
+ socket: { emit: (k, d) => socket.emit(k, d), on: (k, cb) => socket.on(k, cb) },
299
+ socketKey: 'world',
300
+ object: { ...exposed.api, quotes }, // replay lines auto-exposed: both surfaces, same key
301
+ disconnectListen, // gates close on disconnect automatically
302
+ replayOpts: { highWater: 64, lowWater: 8 },// arms frameLine; pending defaults to socket.io writeBuffer
303
+ })
304
+ })
305
+
306
+ // ---- client: picks its LAG POLICY per subscription; no conflation logic anywhere ----
307
+ const sub = Replay.replaySubscribe(deep.quotes, cb, {since: saved, policy: 'frame'}) // server may skip; drain -> mini-frame
308
+ const sub2 = Replay.replaySubscribe(deep.quotes, cb2, {since: saved}) // 'queue' (default): nothing ever skipped
309
+ // own pace (e.g. 50ms skips + condensation): pull on YOUR timer — hint picks the rule, server condenses:
310
+ // every(50, async () => { for (const ev of await deep.quotes.frame(mySeq, hint)) apply(ev); })
311
+ // store mirror: Observe.syncStoreReplay(mirror, deep.replay, {since: prev.seq()}) — same contract
312
+ // delivery contract: FIRST delivery = snapshot/tail start (same event type), then strictly-newer,
313
+ // seq-ascending, deduped; reconnect via {since} = mini-frame/tail, not a full snapshot
314
+ ```
315
+ Rules that make it correct (violating any of these silently breaks convergence):
316
+ - **The line declares its recovery sources.** `current` (keyframe: SAMPLED from truth, never computed
317
+ from deltas; `'last'` = last envelope for single-entity lines) and/or `frame` (condenser). A line
318
+ with neither is a sacred queue: full tails only, evicted journal -> `frame()` THROWS (loud) — a
319
+ lagging `'frame'`-policy subscriber gets a stream end, never silent loss.
320
+ - **A `frame` result must be state-equivalent to the tail it replaces** (per the line's own semantics).
321
+ "Can't condense THIS tail" is legal and simple: return the tail as-is. Refuse-loudly is `throw`.
322
+ Multiple condensation standards live INSIDE the lambda, dispatched by the client-supplied `hint`
323
+ (opaque to the transport): `frame(tail, hint)`.
324
+ - **Events must be ABSOLUTE per their entity** for last-per-entity condensing (store patches are).
325
+ - Gate drops never hole the journal — it is written BEFORE any gate. Reconnect via `{since}` still
326
+ gets everything; only an evicted `history` window degrades to keyframe (visible as a seq jump).
327
+ - `pending()` and the watermarks share units (bytes, packets, frames — anything, but the same).
328
+
329
+ Manual path (pre-rev2, still works, `keyOf` @deprecated): build the gate yourself with
330
+ `Replay.conflateReplay(exposed.replay, {pending, highWater, keyOf})` and spread `gated.api` into the
331
+ facade — details in rare docs. New code should declare `frame` on the line instead.
332
+
333
+ Wire-level proof/oracles: `npx ts-node replay/rpc-auto.test.ts` (real Socket.IO: auto-exposure, legacy
334
+ parity, frame equivalence, gate lag sim), plus `replay/conflate-socket.test.ts`, `replay/conflate.test.ts`,
335
+ `replay/coalesce.test.ts`. Full generic surface (history/time-travel, archive) → 🎞️ in rare docs.
336
+
337
+
338
+
@@ -1,6 +1,7 @@
1
1
  import { Store, StorePatch, StoreDrain, StoreEachCtx } from './store';
2
2
  import { ReplayListenOptions, ReplayEvent } from '../events/replay-listen';
3
3
  import { ReplayRemote, ReplaySubscribeOpts } from '../events/replay-wire';
4
+ import { ReplayRouteSubscribeOpts } from '../events/replay-route';
4
5
  import { ReplayStorage } from '../events/replay-history';
5
6
  export type StoreReplayOpts = Pick<ReplayListenOptions<[StorePatch]>, 'history' | 'getSince' | 'onJournal' | 'now'>;
6
7
  export declare function storePatchKey(patch: StorePatch): string | null;
@@ -48,6 +49,13 @@ export declare function syncStoreReplay<T extends object>(store: Store<T>, remot
48
49
  isStale: () => boolean;
49
50
  lastTs: () => number;
50
51
  };
52
+ export declare function syncStoreReplayRoute<T extends object>(store: Store<T>, remote: ReplayRemote<[StorePatch]>, opts?: ReplayRouteSubscribeOpts): (() => void) & {
53
+ ready: Promise<void>;
54
+ switch: (nextRemote: ReplayRemote<[StorePatch]>, nextOpts?: import("../events/replay-route").ReplayRouteSwitchOpts) => Promise<void>;
55
+ seq: () => number;
56
+ label: () => string | undefined;
57
+ active: () => boolean;
58
+ };
51
59
  export declare function syncStoreReplayEach<T extends object>(remote: ReplayRemote<[StorePatch]>, cb: (key: string, value: T[keyof T] | undefined, ctx: StoreEachCtx) => void, opts?: ReplaySubscribeOpts & {
52
60
  drain?: StoreDrain;
53
61
  initial?: T;
@@ -3,11 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.storePatchKey = storePatchKey;
4
4
  exports.exposeStoreReplay = exposeStoreReplay;
5
5
  exports.syncStoreReplay = syncStoreReplay;
6
+ exports.syncStoreReplayRoute = syncStoreReplayRoute;
6
7
  exports.syncStoreReplayEach = syncStoreReplayEach;
7
8
  exports.storeReplayAt = storeReplayAt;
8
9
  const store_1 = require("./store");
9
10
  const replay_listen_1 = require("../events/replay-listen");
10
11
  const replay_wire_1 = require("../events/replay-wire");
12
+ const replay_route_1 = require("../events/replay-route");
11
13
  const replay_history_1 = require("../events/replay-history");
12
14
  function makeStorePatch(store, path) {
13
15
  let node = store.node;
@@ -55,6 +57,9 @@ function exposeStoreReplay(store, opts = {}) {
55
57
  function syncStoreReplay(store, remote, opts = {}) {
56
58
  return (0, replay_wire_1.replaySubscribe)(remote, function applyLine(patch) { (0, store_1.applyStorePatch)(store, patch); }, opts);
57
59
  }
60
+ function syncStoreReplayRoute(store, remote, opts = {}) {
61
+ return (0, replay_route_1.replayRouteSubscribe)(remote, function applyRoutePatch(patch) { (0, store_1.applyStorePatch)(store, patch); }, opts);
62
+ }
58
63
  function syncStoreReplayEach(remote, cb, opts = {}) {
59
64
  const { drain, initial, ...wireOpts } = opts;
60
65
  const store = (0, store_1.createStore)((initial ?? {}), drain !== undefined ? { drain } : {});
@@ -2,3 +2,4 @@ export * from './replay-listen';
2
2
  export * from './replay-wire';
3
3
  export * from './replay-conflate';
4
4
  export * from './replay-history';
5
+ export * from './replay-route';
@@ -18,3 +18,4 @@ __exportStar(require("./replay-listen"), exports);
18
18
  __exportStar(require("./replay-wire"), exports);
19
19
  __exportStar(require("./replay-conflate"), exports);
20
20
  __exportStar(require("./replay-history"), exports);
21
+ __exportStar(require("./replay-route"), exports);
@@ -0,0 +1,25 @@
1
+ import { Listener } from './Listen';
2
+ import { ReplayRemote, ReplaySubscribeOpts } from './replay-wire';
3
+ export type ReplayRoutePhase = 'switching' | 'ready' | 'error' | 'closed';
4
+ export type ReplayRouteEvent = {
5
+ phase: ReplayRoutePhase;
6
+ seq: number;
7
+ from?: string;
8
+ to?: string;
9
+ error?: unknown;
10
+ };
11
+ export type ReplayRouteSwitchOpts = Pick<ReplaySubscribeOpts, 'policy' | 'hint'> & {
12
+ label?: string;
13
+ since?: number;
14
+ reset?: boolean;
15
+ };
16
+ export type ReplayRouteSubscribeOpts = ReplayRouteSwitchOpts & Pick<ReplaySubscribeOpts, 'onSeq' | 'onError'> & {
17
+ onRoute?: (ev: ReplayRouteEvent) => void;
18
+ };
19
+ export declare function replayRouteSubscribe<Z extends any[]>(remote: ReplayRemote<Z>, cb: Listener<Z>, opts?: ReplayRouteSubscribeOpts): (() => void) & {
20
+ ready: Promise<void>;
21
+ switch: (nextRemote: ReplayRemote<Z>, nextOpts?: ReplayRouteSwitchOpts) => Promise<void>;
22
+ seq: () => number;
23
+ label: () => string | undefined;
24
+ active: () => boolean;
25
+ };
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.replayRouteSubscribe = replayRouteSubscribe;
4
+ function unsubscribeHandle(handle) {
5
+ if (typeof handle == 'function') {
6
+ handle();
7
+ return;
8
+ }
9
+ if (typeof handle?.off == 'function')
10
+ handle.off();
11
+ else if (typeof handle?.unsubscribe == 'function')
12
+ handle.unsubscribe();
13
+ }
14
+ function replayRouteSubscribe(remote, cb, opts = {}) {
15
+ const { onSeq, onError, onRoute } = opts;
16
+ let lastDelivered = opts.since ?? -1;
17
+ let closed = false;
18
+ let active = null;
19
+ let currentLabel = opts.label;
20
+ let switchChain = Promise.resolve();
21
+ const slots = new Set();
22
+ function emitRoute(ev) {
23
+ if (!onRoute)
24
+ return;
25
+ try {
26
+ onRoute(ev);
27
+ }
28
+ catch (e) {
29
+ setTimeout(function rethrowRouteEvent() { throw e; }, 0);
30
+ }
31
+ }
32
+ function deliver(ev) {
33
+ if (closed || ev.seq <= lastDelivered)
34
+ return;
35
+ lastDelivered = ev.seq;
36
+ cb(...ev.event);
37
+ onSeq?.(ev.seq);
38
+ }
39
+ function deliverMany(envs, allowReset) {
40
+ if (allowReset && envs.length && envs[0].seq <= lastDelivered) {
41
+ lastDelivered = envs[0].seq - 1;
42
+ }
43
+ for (const ev of envs)
44
+ deliver(ev);
45
+ }
46
+ function attach(nextRemote, since, nextOpts, allowReset) {
47
+ const { policy = 'queue', hint, label } = nextOpts;
48
+ let slot;
49
+ let slotClosed = false;
50
+ let replaying = true;
51
+ let lineError;
52
+ const queue = [];
53
+ const liveLine = policy == 'frame' && nextRemote.frameLine ? nextRemote.frameLine : nextRemote.line;
54
+ const handle = liveLine.on(function liveTap(ev) {
55
+ if (slotClosed)
56
+ return;
57
+ if (ev == null || typeof ev.seq != 'number') {
58
+ lineError = new Error('replayRouteSubscribe: line ended by route (' + String(ev) + ')');
59
+ slot.close();
60
+ if (!replaying)
61
+ onError?.(lineError);
62
+ return;
63
+ }
64
+ if (replaying)
65
+ queue.push(ev);
66
+ else
67
+ deliver(ev);
68
+ });
69
+ function closeSlot() {
70
+ if (slotClosed)
71
+ return;
72
+ slotClosed = true;
73
+ unsubscribeHandle(handle);
74
+ slots.delete(slot);
75
+ }
76
+ async function catchUp() {
77
+ try {
78
+ let done = false;
79
+ if (since >= 0 && nextRemote.frame) {
80
+ const envs = await nextRemote.frame(since, hint);
81
+ if (slotClosed)
82
+ return;
83
+ if (envs) {
84
+ deliverMany(envs, allowReset);
85
+ done = true;
86
+ }
87
+ }
88
+ if (!done) {
89
+ const tail = since >= 0 ? await nextRemote.since(since) : null;
90
+ if (slotClosed)
91
+ return;
92
+ if (tail) {
93
+ deliverMany(tail, false);
94
+ }
95
+ else {
96
+ const kf = await nextRemote.keyframe();
97
+ if (slotClosed)
98
+ return;
99
+ if (kf)
100
+ deliverMany([kf], allowReset);
101
+ }
102
+ }
103
+ if (lineError)
104
+ throw lineError;
105
+ while (queue.length)
106
+ deliver(queue.shift());
107
+ replaying = false;
108
+ }
109
+ catch (e) {
110
+ closeSlot();
111
+ throw e;
112
+ }
113
+ }
114
+ slot = {
115
+ label,
116
+ ready: catchUp(),
117
+ close: closeSlot,
118
+ closed: () => slotClosed,
119
+ };
120
+ slots.add(slot);
121
+ return slot;
122
+ }
123
+ async function doSwitch(nextRemote, nextOpts = {}, initial = false) {
124
+ if (closed)
125
+ throw new Error('replayRouteSubscribe: closed');
126
+ const from = active;
127
+ const fromLabel = from?.label ?? currentLabel;
128
+ const toLabel = nextOpts.label;
129
+ const since = nextOpts.since ?? lastDelivered;
130
+ const allowReset = nextOpts.reset ?? (initial || !from);
131
+ const slot = attach(nextRemote, since, nextOpts, allowReset);
132
+ emitRoute({ phase: 'switching', from: fromLabel, to: toLabel, seq: lastDelivered });
133
+ try {
134
+ await slot.ready;
135
+ if (closed || slot.closed())
136
+ return;
137
+ active = slot;
138
+ currentLabel = slot.label;
139
+ if (from && from !== slot)
140
+ from.close();
141
+ emitRoute({ phase: 'ready', from: fromLabel, to: toLabel, seq: lastDelivered });
142
+ }
143
+ catch (e) {
144
+ slot.close();
145
+ emitRoute({ phase: 'error', from: fromLabel, to: toLabel, seq: lastDelivered, error: e });
146
+ onError?.(e);
147
+ throw e;
148
+ }
149
+ }
150
+ const ready = doSwitch(remote, opts, true);
151
+ switchChain = ready.catch(() => { });
152
+ function switchRoute(nextRemote, nextOpts = {}) {
153
+ const run = () => doSwitch(nextRemote, nextOpts, false);
154
+ const p = switchChain.then(run, run);
155
+ switchChain = p.catch(() => { });
156
+ return p;
157
+ }
158
+ function off() {
159
+ if (closed)
160
+ return;
161
+ closed = true;
162
+ for (const slot of Array.from(slots))
163
+ slot.close();
164
+ active = null;
165
+ emitRoute({ phase: 'closed', seq: lastDelivered, to: currentLabel });
166
+ }
167
+ return Object.assign(off, {
168
+ ready,
169
+ switch: switchRoute,
170
+ seq: () => lastDelivered,
171
+ label: () => currentLabel,
172
+ active: () => active != null && !active.closed(),
173
+ });
174
+ }
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "wenay-common2",
3
- "version": "1.0.63",
3
+ "version": "1.0.65",
4
4
  "description": "Common library",
5
5
  "strict": true,
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/index.d.ts",
8
8
  "files": [
9
9
  "lib/**/*",
10
+ "doc/**/*",
11
+ "CLAUDE.md",
12
+ "rpc.md",
13
+ "!doc/target/**/*",
10
14
  "!**/*.tsbuildinfo"
11
15
  ],
12
16
  "author": "wenay",
@@ -16,7 +20,7 @@
16
20
  },
17
21
  "repository": {
18
22
  "type": "git",
19
- "url": "https://github.com/wenayr/wenay-common2.git"
23
+ "url": "git+https://github.com/wenayr/wenay-common2.git"
20
24
  },
21
25
  "optionalDependencies": {
22
26
  "utf-8-validate": "^6.0.6"