@webmusic/kernel 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 WebScore Contributors
3
+ Copyright (c) 2025 Koperative
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,25 +1,25 @@
1
1
  # @webmusic/kernel
2
2
 
3
- The domain-neutral contracts and runtime utilities used by `@webmusic/score`
4
- and custom WebMusic integrations. It knows nothing about scores or clips —
3
+ The zero-domain shared kernel of the WebMusic ecosystem: the contracts and
4
+ pure logic that both `@webmusic/score` (symbolic music) and `@webmusic/audio`
5
+ (digital audio) build on. It knows nothing about scores or clips —
5
6
  no `Note`, no `Score`, no `AudioClip`, no `BeatGrid`.
6
7
 
7
8
  ```text
8
- @webmusic/score ── required peer ──→ @webmusic/kernel
9
- ↑
10
- custom players, effects and timelines ────┘
9
+ @webmusic/score ─┐
10
+ ├─ required peer ─→ @webmusic/kernel (no dependencies at all)
11
+ @webmusic/audio ─┘ ↑
12
+ │
13
+ @webmusic/bridge ────┘ (peer-depends on both families)
11
14
  ```
12
15
 
13
16
  Zero dependencies, zero peer dependencies, ESM + CJS, `sideEffects: false`,
14
- one subpath export per module. Score declares it as a required
17
+ one subpath export per module. Score, Audio and Bridge declare it as a required
15
18
  peer so applications select a compatible shared implementation. UI does not depend
16
19
  on Kernel. Package resolution does not allocate shared runtime objects: a shared
17
20
  Kernel package, an AudioContext and a session TransportClock are three distinct
18
- things. See the [session-clock design boundary](https://github.com/koperative-lab/WebMusic/blob/main/platform/shared-clock-injection.md).
19
-
20
- The first-release packages are Kernel, UI and Score. Audio and Bridge continue
21
- on the [`dev` branch](https://github.com/mrsteamedbun/WebMusic/tree/dev);
22
- the cross-domain examples below describe those shared contracts.
21
+ things. See the [time model](../../dev/ARCHITECTURE.md) and
22
+ [session-clock design boundary](../shared-clock-injection.md).
23
23
 
24
24
  ## Install and use
25
25
 
@@ -50,14 +50,14 @@ and `/transport` imports also work with an ES-only TypeScript `lib`.
50
50
  ## What qualifies as kernel code
51
51
 
52
52
  The admission rule, enforced by review against the ledger in
53
- [`platform/README.md`](https://github.com/koperative-lab/WebMusic/blob/main/platform/README.md):
53
+ [`platform/README.md`](../README.md):
54
54
 
55
55
  > Code qualifies for the kernel only if **two drifting copies of it already
56
56
  > exist**, or it is a **contract both domains must implement**.
57
57
 
58
58
  Review new work against existing modules before adding another shared mechanism.
59
59
  The ledger records the extraction boundary, including the role-contract basis for
60
- `kernel/sync`. Keep an extraction's justification and retained domain policies in that ledger.
60
+ `kernel/sync`. Decision reasons belong in [DECISIONS.md](../../dev/DECISIONS.md).
61
61
 
62
62
  The counterpart rule matters as much: **policy that legitimately differs
63
63
  between the families stays with the families.** The kernel carries the
@@ -103,18 +103,19 @@ These recur across the modules and explain most of the local decisions.
103
103
 
104
104
  **Player, effect and mapping contracts are structural.** Consumers satisfy these
105
105
  interfaces by shape without inheriting a shared domain base class. The optional
106
- `WebMusicElement` lifecycle base serves a separate browser composition concern. `AudioClipPlayer`
107
- and `ScorePlayer` satisfy `PlayerLike` without either knowing the other
108
- exists; an `Effect` crosses families because it has `createAudioNodes`. Two
109
- consequences are load-bearing: contract members use method-shorthand syntax
106
+ `WebMusicElement` lifecycle base serves a separate browser composition concern.
107
+ `ScorePlayer` and `AudioClipPlayer` satisfy `PlayerLike` structurally without
108
+ either domain importing the other. The `Effect`
109
+ shape is reusable across families because it has `createAudioNodes`. Contract
110
+ members use method-shorthand syntax
110
111
  so that method bivariance lets a typed-emitter `on<K extends keyof E>(…)`
111
112
  satisfy the loose `on(event: string, …)` (arrow-property signatures would
112
- reject every typed-emitter implementer under `strictFunctionTypes`), and
113
- families pin their conformance in type-only `*-contract.ts` modules so drift
114
- fails at the definition site rather than at some distant call.
113
+ reject every typed-emitter implementer under `strictFunctionTypes`). Score pins
114
+ its conformance in a type-only `*-contract.ts` module; Audio does the same so
115
+ drift fails at the definition site.
115
116
 
116
117
  **Optional capability, named and guarded.** `PlayerLike`'s optional members
117
- are the lowest common denominator both families can always promise. Each
118
+ are the lowest common denominator intended for both families. Each
118
119
  optional capability is *named* by an interface — `TimedPlayerLike`,
119
120
  `StatefulPlayerLike`, `RateControlledPlayerLike`, `VolumeControlledPlayerLike`,
120
121
  `DisposablePlayerLike` — and feature-tested through an exported `is*Player()`
@@ -235,9 +236,8 @@ interface TimelineMapping {
235
236
 
236
237
  Implementations should be total over the reals and monotonic non-decreasing;
237
238
  they may clamp negatives and may quantize, so round-trips are approximate by
238
- the mapping's own policy. Both families ship an adapter (`timeMapMapping`
239
- over score's `TimeMap`, `beatGridMapping` over audio's `BeatGrid`), which is
240
- what makes the two musical axes interoperable through seconds.
239
+ the mapping's own policy. Score ships `timeMapMapping` over its `TimeMap`.
240
+ Audio ships `beatGridMapping` over `BeatGrid`.
241
241
 
242
242
  ### TickSource — keeping schedulers fed
243
243
 
@@ -254,8 +254,8 @@ about what the tick does.
254
254
 
255
255
  ### TransportGroup — master, followers, and the join dance
256
256
 
257
- `sync` is the choreography the score↔audio bridge proved out, hoisted once
258
- its class body turned out to reference only kernel types. One **master** whose
257
+ `sync` captures the domain-neutral choreography from cross-domain coordination
258
+ work. Its implementation references only Kernel types. One **master** whose
259
259
  clock is the group's timeline, N **followers** that join it, all positions on
260
260
  the master's axis against one shared reference clock.
261
261
 
@@ -316,7 +316,7 @@ settlement, background reconciliation and errors. Existing controls remain
316
316
  migration surfaces. Strict dispatch checks observable failures and distinguishes
317
317
  superseded commands; the engines cancel and re-arm through their existing methods,
318
318
  retaining their private clocks. See the complete
319
- [command contract](https://github.com/koperative-lab/WebMusic/blob/main/apps/doc/webmusic/src/content/docs/kernel/api.mdx).
319
+ [command contract](../../apps/doc/webmusic/src/content/docs/kernel/api.mdx).
320
320
 
321
321
  `FollowerOptions.positionScale` adds a positive constant local-axis scale to the
322
322
  offset, applied to both positions and rates. Optional `loop` metadata maps into
@@ -329,11 +329,13 @@ audio warping. Group-level loop re-entry still waits for a delivered watcher tic
329
329
 
330
330
  A transport with no readable clock is adapted into the required-clock role by
331
331
  dead-reckoning a mirror `TransportClock` across the command surface and
332
- reconciling it against the transport's actual position on every drift check:
332
+ reconciling it against the transport's actual position on drift checks, or on
333
+ periodic lifecycle samples when follower drift checks are disabled:
333
334
 
334
- - two identical position samples while the mirror advanced past tolerance
335
- mean the transport stopped on its own (ended, clamped, paused out of band):
336
- pause the mirror and report `'stalled'`;
335
+ - after observing forward motion, a stationary interval whose expected advance
336
+ exceeds the largest reported position step plus tolerance suggests the
337
+ transport stopped: pause the mirror and report `'stalled'`. Without a prior
338
+ moving sample, repeated positions alone cannot establish a stop;
337
339
  - a moving transport that diverged re-anchors the mirror — **slope as well as
338
340
  intercept**. Correcting only the position leaves a transport that refused or
339
341
  clamped the shared rate drifting away again before the next check, which is
@@ -349,8 +351,7 @@ own failures: a diagnostic hook must never become a second path out of a
349
351
  scheduler callback. Both snapshot their subscribers at dispatch start, so a
350
352
  listener registered mid-dispatch is not delivered the in-flight event (with
351
353
  live-set iteration, the ordinary self-rearming `once` idiom is an infinite
352
- loop). `once` also stays single-fire when a listener recursively dispatches the
353
- same event before the outer subscriber snapshot reaches it.
354
+ loop).
354
355
 
355
356
  **Generations bound every asynchronous lifetime.** `WebMusicElement` gives
356
357
  each connected lifetime a fresh cleanup scope, so a cleanup returned by an
@@ -380,7 +381,7 @@ Recorded so the boundaries are visible rather than discovered:
380
381
  no event surface. `TransportGroup` now publishes command invalidation and
381
382
  settlement, but each engine still owns its clock; direct mutations outside
382
383
  the group are not automatically versioned. See
383
- [`platform/shared-clock-injection.md`](https://github.com/koperative-lab/WebMusic/blob/main/platform/shared-clock-injection.md).
384
+ [`platform/shared-clock-injection.md`](../shared-clock-injection.md).
384
385
  - **No nonlinear mapped alignment.** `TimelineMapping` is published and both
385
386
  families ship adapters. The group consumes constant offset/scale and optional
386
387
  native-loop phase, not nonlinear rubato or audio-warp mappings.
@@ -407,29 +408,26 @@ To take part in synchronized playback, implement `SyncFollowerTransport`
407
408
 
408
409
  ## Gates
409
410
 
410
- The multi-entry build explicitly enables code splitting for both module formats.
411
- This keeps root/subpath re-exports identical within ESM and within CommonJS,
412
- including the `TransportClock` created by `MirrorClockMaster`. ESM and CommonJS
413
- remain separate module instances; cross-format constructor identity is not promised.
411
+ The multi-entry build enables code splitting for both module formats. Root and
412
+ subpath re-exports therefore use the same constructor within ESM and within
413
+ CommonJS, including the `TransportClock` used by `MirrorClockMaster`. ESM and
414
+ CommonJS remain separate module instances.
414
415
 
415
416
  The kernel is a first-class checked package: its policy declares zero
416
417
  workspace dependencies, so any domain import inside `platform/kernel` fails
417
418
  the architecture gate. Public entries are verified against the export map,
418
419
  and the whole repository runs `npm run check`. The current command graph is in
419
- [root package.json](https://github.com/koperative-lab/WebMusic/blob/main/package.json); development instructions are in
420
- [CONTRIBUTING.md](https://github.com/koperative-lab/WebMusic/blob/main/CONTRIBUTING.md). The executable architecture sources
421
- include [package-policy.mjs](https://github.com/koperative-lab/WebMusic/blob/main/scripts/package-policy.mjs),
422
- [element-composition-policy.mjs](https://github.com/koperative-lab/WebMusic/blob/main/scripts/element-composition-policy.mjs) and
423
- [check-architecture.mjs](https://github.com/koperative-lab/WebMusic/blob/main/scripts/check-architecture.mjs), each with a distinct
424
- scope. Check the exported contracts and relevant behavioral tests when changing them.
420
+ [root package.json](../../package.json); development instructions are in
421
+ [dev/DEVELOPMENT.md](../../dev/DEVELOPMENT.md). The executable architecture sources
422
+ include [package-policy.mjs](../../scripts/package-policy.mjs),
423
+ [element-composition-policy.mjs](../../scripts/element-composition-policy.mjs) and
424
+ [check-architecture.mjs](../../scripts/check-architecture.mjs), each with a distinct
425
+ scope. Current delivery gaps belong in [STATUS.md](../../dev/STATUS.md).
425
426
 
426
427
  The extraction ledger — what moved here, from where, what it replaced, and
427
428
  what deliberately stayed per-family — lives in
428
- [`platform/README.md`](https://github.com/koperative-lab/WebMusic/blob/main/platform/README.md).
429
+ [`platform/README.md`](../README.md).
429
430
 
430
431
  ## License
431
432
 
432
433
  MIT
433
-
434
- For source contributions and release verification, see the
435
- [contribution guide](https://github.com/koperative-lab/WebMusic/blob/main/CONTRIBUTING.md).
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schema": 1,
3
3
  "name": "@webmusic/kernel",
4
- "version": "0.1.0",
5
- "source": "ca14b076ec2a27b111f8fea91f9eacc86c1e25574babb77432dac798698727e8",
6
- "outputs": "04cb1ddc33addf33259975430bc6197f6ce648dc159be622b804eef0915baa54"
4
+ "version": "0.2.0",
5
+ "source": "373019d816fb463de4490f2ad83f17dfccd8defbc7063ad78b1f2e1114e24ae4",
6
+ "outputs": "487eeb45c12c1fdc9d36c4d0a6a21c4e36d488d2f0952fd02acf88d5d3616101"
7
7
  }
@@ -16,7 +16,8 @@ var _chunk7ZTZZ5PNcjs = require('./chunk-7ZTZZ5PN.cjs');
16
16
  // src/sync.ts
17
17
  var MAX_CONSECUTIVE_DRIFT_JOINS = 3;
18
18
  var MIRROR_RATE_TOLERANCE = 0.02;
19
- var _master, _followers, _now, _leadIn, _driftTolerance, _driftIntervalMs, _ticker, _tickOptions, _ticksPerDriftCheck, _tickCount, _loop, _wrapping, _generation, _revision, _pendingCommands, _observers, _lastSnapshot, _pendingTransitions, _commandActive, _queuedCommands, _intent, _disposed, _onOperationError, _TransportGroup_instances, dispatch_fn, publish_fn, setLoop_fn, play_fn, pause_fn, stop_fn, seek_fn, setRate_fn, wrapLocal_fn, localPosition_fn, joinFollowers_fn, applyFollowerRate_fn, resetDriftConvergence_fn, onTick_fn, reconcileMasterStop_fn, handleMasterStopped_fn, checkLoop_fn, wrap_fn, startDriftMonitor_fn, stopDriftMonitor_fn, needsMonitor_fn, isCurrent_fn, reportOperationError_fn, settleMasterStopped_fn, settleFailedResume_fn, assertActive_fn, observeReconciliation_fn, queueJoin_fn, enqueueCommand_fn, finishCommand_fn, readSettledMasterPosition_fn, reconcileStaleSeek_fn, reapplySynchronousIntent_fn, applyPause_fn, applyStop_fn, rollbackFailedPlay_fn;
19
+ var MASTER_RECONCILE_INTERVAL_MS = 250;
20
+ var _master, _followers, _now, _leadIn, _driftTolerance, _driftIntervalMs, _ticker, _tickOptions, _ticksPerDriftCheck, _ticksPerMasterReconcile, _tickCount, _loop, _wrapping, _generation, _revision, _lastCommandRevision, _pendingCommands, _observers, _lastSnapshot, _pendingTransitions, _commandActive, _queuedCommands, _intent, _disposed, _onOperationError, _TransportGroup_instances, dispatch_fn, publish_fn, setLoop_fn, play_fn, pause_fn, stop_fn, seek_fn, setRate_fn, wrapLocal_fn, localPosition_fn, joinFollowers_fn, applyFollowerRate_fn, resetDriftConvergence_fn, onTick_fn, reconcileMasterStop_fn, handleMasterStopped_fn, checkLoop_fn, wrap_fn, startDriftMonitor_fn, stopDriftMonitor_fn, isCurrent_fn, reportOperationError_fn, settleMasterStopped_fn, settleFailedResume_fn, assertActive_fn, observeReconciliation_fn, queueJoin_fn, enqueueCommand_fn, finishCommand_fn, readSettledMasterPosition_fn, reconcileStaleSeek_fn, reapplySynchronousIntent_fn, applyPause_fn, applyStop_fn, rollbackFailedPlay_fn;
20
21
  var TransportGroup = class {
21
22
  constructor(master, now, options = {}) {
22
23
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _TransportGroup_instances);
@@ -29,11 +30,14 @@ var TransportGroup = class {
29
30
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _ticker, null);
30
31
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _tickOptions);
31
32
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _ticksPerDriftCheck);
33
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _ticksPerMasterReconcile);
32
34
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _tickCount, 0);
33
35
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _loop, null);
34
36
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _wrapping, false);
35
37
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _generation, 0);
36
38
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _revision, 0);
39
+ /** Last submitted command, excluding background reconciliation revisions. */
40
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _lastCommandRevision, 0);
37
41
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _pendingCommands, /* @__PURE__ */ new Set());
38
42
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _observers, /* @__PURE__ */ new Set());
39
43
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _lastSnapshot);
@@ -60,7 +64,8 @@ var TransportGroup = class {
60
64
  `tick.intervalMs must be a positive safe integer no greater than ${_chunk3INDOVUCcjs.MAX_TIMER_DELAY_MS}.`
61
65
  );
62
66
  }
63
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _ticksPerDriftCheck, Math.max(1, Math.round(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _driftIntervalMs) / (_nullishCoalesce(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _tickOptions).intervalMs, () => ( 25))))));
67
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _ticksPerDriftCheck, Math.max(1, Math.round(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _driftIntervalMs) / tickInterval)));
68
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _ticksPerMasterReconcile, Math.max(1, Math.ceil(MASTER_RECONCILE_INTERVAL_MS / tickInterval)));
64
69
  if (options.loop) {
65
70
  assertValidLoop(options.loop);
66
71
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _loop, { ...options.loop });
@@ -213,7 +218,10 @@ var TransportGroup = class {
213
218
  }
214
219
  dispose() {
215
220
  if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed)) return;
216
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSnapshot, this.snapshot);
221
+ try {
222
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSnapshot, this.snapshot);
223
+ } catch (e2) {
224
+ }
217
225
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _disposed, true);
218
226
  ++_chunk7ZTZZ5PNcjs.__privateWrapper.call(void 0, this, _revision)._;
219
227
  ++_chunk7ZTZZ5PNcjs.__privateWrapper.call(void 0, this, _generation)._;
@@ -233,7 +241,12 @@ var TransportGroup = class {
233
241
  errors.push(error);
234
242
  }
235
243
  }
236
- _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, publish_fn).call(this, { type: "snapshot", snapshot: this.snapshot });
244
+ let snapshot;
245
+ try {
246
+ snapshot = this.snapshot;
247
+ } catch (e3) {
248
+ }
249
+ if (snapshot) _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, publish_fn).call(this, { type: "snapshot", snapshot });
237
250
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _observers).clear();
238
251
  if (errors.length > 0) throw errors[0];
239
252
  }
@@ -303,11 +316,13 @@ _driftIntervalMs = new WeakMap();
303
316
  _ticker = new WeakMap();
304
317
  _tickOptions = new WeakMap();
305
318
  _ticksPerDriftCheck = new WeakMap();
319
+ _ticksPerMasterReconcile = new WeakMap();
306
320
  _tickCount = new WeakMap();
307
321
  _loop = new WeakMap();
308
322
  _wrapping = new WeakMap();
309
323
  _generation = new WeakMap();
310
324
  _revision = new WeakMap();
325
+ _lastCommandRevision = new WeakMap();
311
326
  _pendingCommands = new WeakMap();
312
327
  _observers = new WeakMap();
313
328
  _lastSnapshot = new WeakMap();
@@ -323,6 +338,7 @@ dispatch_fn = function(input, strict) {
323
338
  validateCommand(input);
324
339
  const command = Object.freeze(input.type === "loop" ? { ...input, loop: input.loop ? Object.freeze({ ...input.loop }) : null } : { ...input });
325
340
  const revision = ++_chunk7ZTZZ5PNcjs.__privateWrapper.call(void 0, this, _revision)._;
341
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastCommandRevision, revision);
326
342
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _pendingCommands).add(revision);
327
343
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, publish_fn).call(this, { type: "invalidate", revision, command, snapshot: this.snapshot });
328
344
  let operation = void 0;
@@ -375,7 +391,7 @@ publish_fn = function(event) {
375
391
  if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _observers).has(listener)) continue;
376
392
  try {
377
393
  listener(event);
378
- } catch (e2) {
394
+ } catch (e4) {
379
395
  }
380
396
  if (event.snapshot.revision !== _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _revision)) break;
381
397
  }
@@ -384,9 +400,7 @@ setLoop_fn = function(loop) {
384
400
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, assertActive_fn).call(this);
385
401
  if (loop) assertValidLoop(loop);
386
402
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _loop, loop ? { ...loop } : null);
387
- if (this.clock.paused) {
388
- if (!_chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, needsMonitor_fn).call(this)) _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, stopDriftMonitor_fn).call(this);
389
- } else if (_chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, needsMonitor_fn).call(this)) {
403
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _intent) === "playing") {
390
404
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, startDriftMonitor_fn).call(this);
391
405
  } else {
392
406
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, stopDriftMonitor_fn).call(this);
@@ -475,6 +489,8 @@ seek_fn = async function(position, strict) {
475
489
  for (const record of [..._chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _followers)]) {
476
490
  if (!_chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, isCurrent_fn).call(this, generation)) break;
477
491
  if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _followers).includes(record)) continue;
492
+ record.transport.pause();
493
+ if (!_chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, isCurrent_fn).call(this, generation) || !_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _followers).includes(record)) continue;
478
494
  record.transport.seek(_chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, localPosition_fn).call(this, record, settled));
479
495
  }
480
496
  } else {
@@ -611,13 +627,17 @@ resetDriftConvergence_fn = function() {
611
627
  /**
612
628
  * One watcher drives every concern off a kernel tick source (worker-backed
613
629
  * where the platform allows, so background-tab throttling does not starve
614
- * it): master-stop reconciliation and the loop boundary every tick, drift
615
- * every `driftCheckIntervalMs` worth of ticks.
630
+ * it): clocked master-stop reconciliation and the loop boundary every
631
+ * tick, drift every `driftCheckIntervalMs` worth of ticks. A clockless
632
+ * adapter is sampled at the default drift cadence when drift is disabled.
616
633
  */
617
634
  onTick_fn = function() {
618
635
  if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed)) return;
619
636
  const revision = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _revision);
620
637
  try {
638
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _driftIntervalMs) === 0 && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _tickCount) % _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _ticksPerMasterReconcile) === 0 && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _intent) === "playing" && !_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _wrapping) && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _pendingTransitions) === 0) {
639
+ _optionalChain([_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _master), 'access', _19 => _19.reconcile, 'optionalCall', _20 => _20(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _now).call(this))]);
640
+ }
621
641
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, reconcileMasterStop_fn).call(this);
622
642
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, checkLoop_fn).call(this);
623
643
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _tickCount, _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _tickCount) + 1);
@@ -664,41 +684,54 @@ checkLoop_fn = function() {
664
684
  */
665
685
  wrap_fn = function(startSeconds) {
666
686
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _wrapping, true);
687
+ const generation = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _generation);
688
+ const wrapCommandRevision = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _revision) + 1;
689
+ const stillCurrent = () => _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, isCurrent_fn).call(this, generation) && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _lastCommandRevision) === wrapCommandRevision;
667
690
  void this.seek(startSeconds).then(() => {
668
- if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed) && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _intent) === "playing" && this.clock.paused) {
691
+ if (stillCurrent() && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _intent) === "playing" && this.clock.paused) {
669
692
  return this.play();
670
693
  }
671
694
  }).catch((error) => {
695
+ if (!stillCurrent()) return;
672
696
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, reportOperationError_fn).call(this, "loop wrap", error);
673
- if (!_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed) && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _intent) === "playing") _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, settleMasterStopped_fn).call(this);
697
+ if (stillCurrent() && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _intent) === "playing") _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, settleMasterStopped_fn).call(this);
674
698
  }).finally(() => {
675
699
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _wrapping, false);
676
700
  });
677
701
  };
678
702
  startDriftMonitor_fn = function() {
679
- if (!_chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, needsMonitor_fn).call(this)) {
680
- _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, stopDriftMonitor_fn).call(this);
681
- return;
682
- }
683
703
  _nullishCoalesce(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _ticker), () => ( _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _ticker, _chunk3INDOVUCcjs.createTickSource.call(void 0, _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _tickOptions)))));
684
704
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _ticker).start(() => _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, onTick_fn).call(this));
685
705
  };
686
706
  stopDriftMonitor_fn = function() {
687
- _optionalChain([_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _ticker), 'optionalAccess', _19 => _19.stop, 'call', _20 => _20()]);
707
+ _optionalChain([_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _ticker), 'optionalAccess', _21 => _21.stop, 'call', _22 => _22()]);
688
708
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _tickCount, 0);
689
709
  };
690
- needsMonitor_fn = function() {
691
- return _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _loop) !== null || _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _driftIntervalMs) > 0;
692
- };
693
710
  isCurrent_fn = function(generation) {
694
711
  return !_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed) && generation === _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _generation);
695
712
  };
696
713
  reportOperationError_fn = function(operation, error) {
697
714
  var _a;
698
- _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, publish_fn).call(this, { type: "operation-error", operation, error, snapshot: this.snapshot });
715
+ let snapshot;
716
+ try {
717
+ snapshot = this.snapshot;
718
+ } catch (e5) {
719
+ if (_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _lastSnapshot)) {
720
+ snapshot = Object.freeze({
721
+ ..._chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _lastSnapshot),
722
+ revision: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _revision),
723
+ paused: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _intent) !== "playing",
724
+ holding: false,
725
+ pending: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _pendingCommands).size > 0 || _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _pendingTransitions) > 0,
726
+ disposed: _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _disposed)
727
+ });
728
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSnapshot, snapshot);
729
+ }
730
+ }
731
+ if (snapshot) _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, publish_fn).call(this, { type: "operation-error", operation, error, snapshot });
699
732
  try {
700
733
  (_a = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _onOperationError)) == null ? void 0 : _a.call(this, operation, error);
701
- } catch (e3) {
734
+ } catch (e6) {
702
735
  }
703
736
  };
704
737
  /**
@@ -864,17 +897,24 @@ rollbackFailedPlay_fn = function() {
864
897
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, stopDriftMonitor_fn).call(this);
865
898
  try {
866
899
  _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _TransportGroup_instances, applyPause_fn).call(this);
867
- } catch (e4) {
900
+ } catch (e7) {
868
901
  }
869
902
  };
870
- var _inner, _mirror, _now2, _tolerance, _lastSample;
903
+ var _inner, _mirror, _now2, _tolerance, _lastSample, _lastChange, _reportedStep, _positiveSteps, _MirrorClockMaster_instances, resetSamples_fn;
871
904
  var MirrorClockMaster = class {
872
905
  constructor(inner, now, options = {}) {
906
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _MirrorClockMaster_instances);
873
907
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _inner);
874
908
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _mirror);
875
909
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _now2);
876
910
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _tolerance);
911
+ /** Last sample for estimating the transport's slope. */
877
912
  _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _lastSample, null);
913
+ /** Last position change, retained across identical quantized samples. */
914
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _lastChange, null);
915
+ /** Largest observed positive step since the last command. */
916
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _reportedStep, 0);
917
+ _chunk7ZTZZ5PNcjs.__privateAdd.call(void 0, this, _positiveSteps, 0);
878
918
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _inner, inner);
879
919
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _now2, now);
880
920
  _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _tolerance, _nullishCoalesce(options.reconcileToleranceSeconds, () => ( 0.03)));
@@ -889,49 +929,63 @@ var MirrorClockMaster = class {
889
929
  }
890
930
  async play(when) {
891
931
  await _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner).play(when);
892
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, null);
932
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _MirrorClockMaster_instances, resetSamples_fn).call(this);
893
933
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).start(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _now2).call(this), _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner).position);
894
934
  }
895
935
  pause() {
896
936
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner).pause();
897
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, null);
937
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _MirrorClockMaster_instances, resetSamples_fn).call(this);
898
938
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).pause(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _now2).call(this));
899
939
  }
900
940
  stop() {
901
941
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner).stop();
902
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, null);
942
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _MirrorClockMaster_instances, resetSamples_fn).call(this);
903
943
  const now = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _now2).call(this);
904
944
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).pause(now);
905
945
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).seekTo(0, now);
906
946
  }
907
947
  async seekPosition(position, when) {
908
948
  await _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner).seekPosition(position, when);
909
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, null);
949
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _MirrorClockMaster_instances, resetSamples_fn).call(this);
910
950
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).seekTo(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner).position, _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _now2).call(this));
911
951
  }
912
952
  setRate(rate) {
913
- _optionalChain([_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner), 'access', _21 => _21.setRate, 'optionalCall', _22 => _22(rate)]);
914
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, null);
953
+ _optionalChain([_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner), 'access', _23 => _23.setRate, 'optionalCall', _24 => _24(rate)]);
954
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _MirrorClockMaster_instances, resetSamples_fn).call(this);
915
955
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).setRate(rate, _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _now2).call(this));
916
956
  }
917
957
  reconcile(now) {
918
958
  const actual = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner).position;
919
- if (!Number.isFinite(actual) || actual < 0) return "ok";
959
+ if (!Number.isFinite(actual) || actual < 0) {
960
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _MirrorClockMaster_instances, resetSamples_fn).call(this);
961
+ return "ok";
962
+ }
920
963
  const mirrorPosition = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).positionAt(now);
921
964
  const sample = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _lastSample);
922
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, { position: actual, mirrorPosition, time: now });
923
- if (sample !== null && actual === sample.position && mirrorPosition - sample.mirrorPosition > _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _tolerance) && !_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).paused) {
924
- _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, null);
965
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, { position: actual, time: now });
966
+ const changed = sample !== null && actual !== sample.position;
967
+ const previousChange = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _lastChange);
968
+ if (sample === null || changed) {
969
+ if (changed && actual > sample.position) {
970
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _reportedStep, Math.max(_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _reportedStep), actual - sample.position));
971
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _positiveSteps, _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _positiveSteps) + 1);
972
+ }
973
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastChange, { position: actual, time: now });
974
+ }
975
+ if (!changed && sample !== null && previousChange !== null && _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _reportedStep) > 0 && (now - previousChange.time) * _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).rate > _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _reportedStep) + _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _tolerance) && !_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).paused) {
976
+ _chunk7ZTZZ5PNcjs.__privateMethod.call(void 0, this, _MirrorClockMaster_instances, resetSamples_fn).call(this);
925
977
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).pause(now);
926
978
  return "stalled";
927
979
  }
928
- if (sample !== null && !_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).paused) {
929
- const elapsed = now - sample.time;
930
- const advanced = actual - sample.position;
980
+ if (changed && previousChange !== null && !_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).paused) {
981
+ const elapsed = now - previousChange.time;
982
+ const advanced = actual - previousChange.position;
931
983
  if (elapsed > 0 && advanced > 0) {
932
984
  const measured = advanced / elapsed;
933
985
  const current = _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).rate;
934
- if (Number.isFinite(measured) && measured > 0 && Math.abs(measured - current) > MIRROR_RATE_TOLERANCE * current) {
986
+ if (Number.isFinite(measured) && measured > 0 && Math.abs(measured - current) > MIRROR_RATE_TOLERANCE * current && // The first boundary jump of a rounded position can look much
987
+ // faster than the true rate. Wait for another step in that case.
988
+ (measured < current || _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _positiveSteps) >= 2 || advanced <= elapsed * current + _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _tolerance))) {
935
989
  _chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _mirror).setRate(measured, now);
936
990
  }
937
991
  }
@@ -942,7 +996,7 @@ var MirrorClockMaster = class {
942
996
  return "ok";
943
997
  }
944
998
  dispose() {
945
- _optionalChain([_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner), 'access', _23 => _23.dispose, 'optionalCall', _24 => _24()]);
999
+ _optionalChain([_chunk7ZTZZ5PNcjs.__privateGet.call(void 0, this, _inner), 'access', _25 => _25.dispose, 'optionalCall', _26 => _26()]);
946
1000
  }
947
1001
  };
948
1002
  _inner = new WeakMap();
@@ -950,6 +1004,16 @@ _mirror = new WeakMap();
950
1004
  _now2 = new WeakMap();
951
1005
  _tolerance = new WeakMap();
952
1006
  _lastSample = new WeakMap();
1007
+ _lastChange = new WeakMap();
1008
+ _reportedStep = new WeakMap();
1009
+ _positiveSteps = new WeakMap();
1010
+ _MirrorClockMaster_instances = new WeakSet();
1011
+ resetSamples_fn = function() {
1012
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastSample, null);
1013
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _lastChange, null);
1014
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _reportedStep, 0);
1015
+ _chunk7ZTZZ5PNcjs.__privateSet.call(void 0, this, _positiveSteps, 0);
1016
+ };
953
1017
  function assertFiniteNonNegative(name, value) {
954
1018
  if (!Number.isFinite(value) || value < 0) {
955
1019
  throw new RangeError(`${name} must be finite and >= 0.`);
@@ -984,4 +1048,4 @@ function validateCommand(command) {
984
1048
 
985
1049
 
986
1050
  exports.TransportGroup = TransportGroup; exports.MirrorClockMaster = MirrorClockMaster;
987
- //# sourceMappingURL=chunk-JNXB74G3.cjs.map
1051
+ //# sourceMappingURL=chunk-54JYWB5X.cjs.map