@mmstack/mesh 21.15.0 → 21.15.2

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/README.md CHANGED
@@ -499,7 +499,11 @@ post-migration by construction, and journal replay stays correct forever.
499
499
  - `webSocketTransport(url)` for a relay over WebSocket.
500
500
  - `directTransport(relay, ctx)` connects straight to an in-process `createRelay`, with no
501
501
  network. It is the backbone of the tests, and useful for a single-process demo or an
502
- SSR-side room.
502
+ SSR-side room. Messages cross as JSON both ways, so a direct peer holds exactly what a socket
503
+ peer would. `directTransport(relay, ctx, { byReference: true })` hands the relay's messages
504
+ over by reference instead: a snapshot welcome then costs the peer no copy of the room, at the
505
+ price that the peer must never mutate what it receives, since it holds the relay's own
506
+ objects. The peer's own messages still cross as JSON.
503
507
 
504
508
  A transport is a small interface (`send`, `onMessage`, `onClose`, `close`), so wiring a custom
505
509
  one is a few lines. `onClose` reports the close `code` and `reason` (a 1009 means the relay
@@ -1,6 +1,6 @@
1
+ import { isDevMode, signal, inject, Injector, computed, runInInjectionContext, DestroyRef, untracked, effect } from '@angular/core';
1
2
  import { createWatch } from '@angular/core/primitives/signals';
2
3
  import { createConvergingApply, createStoreContext, store, opSync, forkStore, isStoreReservedKey, OP_PROTO_VERSION, registerResource, throttled } from '@mmstack/primitives/core';
3
- import { isDevMode, inject, Injector, signal, computed, runInInjectionContext, DestroyRef, untracked, effect } from '@angular/core';
4
4
  import { createRanges, recordAdmission, admits, createRegisterStore, MESH_PROTO_VERSION, MESH_DIRECT_VERSION, checkEnvelope } from '@mmstack/mesh-protocol';
5
5
 
6
6
  const emptyAdmission = () => ({
@@ -380,6 +380,7 @@ function meshSession(opt) {
380
380
  cover = createRegisterStore();
381
381
  maxSeq = 0;
382
382
  owed = undefined; // its threshold belongs to the dead room's sequence
383
+ hooks.onGeneration?.(undefined);
383
384
  };
384
385
  /**
385
386
  * Install a snapshot welcome ATOMICALLY: the registers (a cut's seed or the current registers),
@@ -457,6 +458,13 @@ function meshSession(opt) {
457
458
  if (keep.length > replayed.length)
458
459
  hooks.onConflicts?.(conflicts());
459
460
  hooks.onOutboxChange?.();
461
+ if (newer)
462
+ hooks.onGeneration?.({
463
+ instance: gen.instance,
464
+ index: gen.index,
465
+ reason: gen.reason,
466
+ policy: gen.policy,
467
+ });
460
468
  };
461
469
  const handle = (msg) => {
462
470
  if (msg.room !== opt.room)
@@ -853,6 +861,7 @@ function agentSeat(initial, opt) {
853
861
  for (const cb of [...listeners])
854
862
  cb(event);
855
863
  };
864
+ const compaction = signal(undefined, ...(ngDevMode ? [{ debugName: "compaction" }] : /* istanbul ignore next */ []));
856
865
  let welcomed = false;
857
866
  let diverged = false;
858
867
  let terminated = false;
@@ -886,6 +895,7 @@ function agentSeat(initial, opt) {
886
895
  },
887
896
  onResync: (seq) => emit({ kind: 'resync', seq }),
888
897
  onDirect: (msg) => deliverDirect(directListeners, msg),
898
+ onCompaction: (state) => compaction.set(state),
889
899
  onConflicts: (list) => {
890
900
  // a refused write stays applied until the rehydrate that follows lands
891
901
  if (list.some((c) => c.cause !== 'uncovered'))
@@ -931,6 +941,8 @@ function agentSeat(initial, opt) {
931
941
  sync.flush();
932
942
  },
933
943
  conflicts: () => session.conflicts(),
944
+ compaction: compaction.asReadonly(),
945
+ declareReady: (pressed) => session.declareReady(pressed),
934
946
  setAtPath: (path, value) => {
935
947
  setAtPath(doc, path, value);
936
948
  sync.flush();
@@ -1127,6 +1139,7 @@ function meshSync(source, opt) {
1127
1139
  const peerMap = signal(new Map(), ...(ngDevMode ? [{ debugName: "peerMap" }] : /* istanbul ignore next */ []));
1128
1140
  const conflicts = signal([], ...(ngDevMode ? [{ debugName: "conflicts" }] : /* istanbul ignore next */ []));
1129
1141
  const compaction = signal(undefined, ...(ngDevMode ? [{ debugName: "compaction" }] : /* istanbul ignore next */ []));
1142
+ const generation = signal(undefined, ...(ngDevMode ? [{ debugName: "generation" }] : /* istanbul ignore next */ []));
1130
1143
  const peers = computed(() => [...peerMap().values()], ...(ngDevMode ? [{ debugName: "peers" }] : /* istanbul ignore next */ []));
1131
1144
  const policyVersion = opt.policyVersion ?? 0;
1132
1145
  const health = computed(() => {
@@ -1316,6 +1329,7 @@ function meshSync(source, opt) {
1316
1329
  onPeers: (map) => peerMap.set(map),
1317
1330
  onConflicts: (list) => conflicts.set(list),
1318
1331
  onCompaction: (state) => compaction.set(state),
1332
+ onGeneration: (next) => generation.set(next),
1319
1333
  onOutboxChange: () => {
1320
1334
  void persistOutbox();
1321
1335
  acked.set(!session?.hasUnacked());
@@ -1546,6 +1560,7 @@ function meshSync(source, opt) {
1546
1560
  },
1547
1561
  conflicts: conflicts.asReadonly(),
1548
1562
  compaction: compaction.asReadonly(),
1563
+ generation: generation.asReadonly(),
1549
1564
  declareReady: (pressed) => session?.declareReady(pressed),
1550
1565
  sync: {
1551
1566
  override: (fn) => (started ? sync.override(fn) : fn()),
@@ -1619,9 +1634,13 @@ const asJson = (msg) => JSON.parse(JSON.stringify(msg));
1619
1634
  *
1620
1635
  * Messages cross as JSON in both directions, as they would over a socket, so a direct peer holds
1621
1636
  * exactly what a socket peer would. Handed over by reference, an `undefined` an emitter wrote
1622
- * would reach relay memory where a socket peer's would have been dropped.
1637
+ * would reach relay memory where a socket peer's would have been dropped. The relay's own
1638
+ * messages may skip the hop ({@link DirectTransportOptions.byReference}): they never hold an
1639
+ * `undefined` (every value in a room arrived as JSON), so the two peers read the same state and
1640
+ * differ only in who owns the objects.
1623
1641
  */
1624
- function directTransport(relay, ctx) {
1642
+ function directTransport(relay, ctx, options = {}) {
1643
+ const inbound = options.byReference ? (msg) => msg : asJson;
1625
1644
  return () => {
1626
1645
  const messageCbs = new Set();
1627
1646
  const closeCbs = new Set();
@@ -1630,7 +1649,7 @@ function directTransport(relay, ctx) {
1630
1649
  send: (msg) => {
1631
1650
  if (!open)
1632
1651
  return;
1633
- const wired = asJson(msg);
1652
+ const wired = inbound(msg);
1634
1653
  for (const cb of [...messageCbs])
1635
1654
  cb(wired);
1636
1655
  },