@mmstack/mesh 22.15.0 → 22.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 +5 -1
- package/fesm2022/mmstack-mesh.mjs +18 -3
- package/fesm2022/mmstack-mesh.mjs.map +1 -1
- package/package.json +2 -2
- package/types/mmstack-mesh.d.ts +47 -9
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 { DestroyRef, Injector, computed, effect, inject, isDevMode, runInInjectionContext, signal, untracked } from "@angular/core";
|
|
1
2
|
import { createWatch } from "@angular/core/primitives/signals";
|
|
2
3
|
import { OP_PROTO_VERSION, createConvergingApply, createStoreContext, forkStore, isStoreReservedKey, opSync, registerResource, store, throttled } from "@mmstack/primitives/core";
|
|
3
|
-
import { DestroyRef, Injector, computed, effect, inject, isDevMode, runInInjectionContext, signal, untracked } from "@angular/core";
|
|
4
4
|
import { MESH_DIRECT_VERSION, MESH_PROTO_VERSION, admits, checkEnvelope, createRanges, createRegisterStore, recordAdmission } from "@mmstack/mesh-protocol";
|
|
5
5
|
const emptyAdmission = () => ({
|
|
6
6
|
ranges: /* @__PURE__ */ new Map(),
|
|
@@ -311,6 +311,7 @@ function meshSession(opt) {
|
|
|
311
311
|
cover = createRegisterStore();
|
|
312
312
|
maxSeq = 0;
|
|
313
313
|
owed = void 0;
|
|
314
|
+
hooks.onGeneration?.(void 0);
|
|
314
315
|
};
|
|
315
316
|
const install = (msg, newer) => {
|
|
316
317
|
const registers = msg.seed ?? msg.registers;
|
|
@@ -373,6 +374,12 @@ function meshSession(opt) {
|
|
|
373
374
|
owed = owedAfterInstall(owed, msg.seq);
|
|
374
375
|
if (keep.length > replayed.length) hooks.onConflicts?.(conflicts());
|
|
375
376
|
hooks.onOutboxChange?.();
|
|
377
|
+
if (newer) hooks.onGeneration?.({
|
|
378
|
+
instance: gen.instance,
|
|
379
|
+
index: gen.index,
|
|
380
|
+
reason: gen.reason,
|
|
381
|
+
policy: gen.policy
|
|
382
|
+
});
|
|
376
383
|
};
|
|
377
384
|
const handle = (msg) => {
|
|
378
385
|
if (msg.room !== opt.room) return;
|
|
@@ -702,6 +709,7 @@ function agentSeat(initial, opt) {
|
|
|
702
709
|
const emit = (event) => {
|
|
703
710
|
for (const cb of [...listeners]) cb(event);
|
|
704
711
|
};
|
|
712
|
+
const compaction = signal(void 0, ...ngDevMode ? [{ debugName: "compaction" }] : /* istanbul ignore next */ []);
|
|
705
713
|
let welcomed = false;
|
|
706
714
|
let diverged = false;
|
|
707
715
|
let terminated = false;
|
|
@@ -738,6 +746,7 @@ function agentSeat(initial, opt) {
|
|
|
738
746
|
seq
|
|
739
747
|
}),
|
|
740
748
|
onDirect: (msg) => deliverDirect(directListeners, msg),
|
|
749
|
+
onCompaction: (state) => compaction.set(state),
|
|
741
750
|
onConflicts: (list) => {
|
|
742
751
|
if (list.some((c) => c.cause !== "uncovered")) diverged = true;
|
|
743
752
|
},
|
|
@@ -780,6 +789,8 @@ function agentSeat(initial, opt) {
|
|
|
780
789
|
sync.flush();
|
|
781
790
|
},
|
|
782
791
|
conflicts: () => session.conflicts(),
|
|
792
|
+
compaction: compaction.asReadonly(),
|
|
793
|
+
declareReady: (pressed) => session.declareReady(pressed),
|
|
783
794
|
setAtPath: (path, value) => {
|
|
784
795
|
setAtPath(doc, path, value);
|
|
785
796
|
sync.flush();
|
|
@@ -927,6 +938,7 @@ function meshSync(source, opt) {
|
|
|
927
938
|
const peerMap = signal(/* @__PURE__ */ new Map(), ...ngDevMode ? [{ debugName: "peerMap" }] : /* istanbul ignore next */ []);
|
|
928
939
|
const conflicts = signal([], ...ngDevMode ? [{ debugName: "conflicts" }] : /* istanbul ignore next */ []);
|
|
929
940
|
const compaction = signal(void 0, ...ngDevMode ? [{ debugName: "compaction" }] : /* istanbul ignore next */ []);
|
|
941
|
+
const generation = signal(void 0, ...ngDevMode ? [{ debugName: "generation" }] : /* istanbul ignore next */ []);
|
|
930
942
|
const peers = computed(() => [...peerMap().values()], ...ngDevMode ? [{ debugName: "peers" }] : /* istanbul ignore next */ []);
|
|
931
943
|
const policyVersion = opt.policyVersion ?? 0;
|
|
932
944
|
const health = computed(() => {
|
|
@@ -1099,6 +1111,7 @@ function meshSync(source, opt) {
|
|
|
1099
1111
|
onPeers: (map) => peerMap.set(map),
|
|
1100
1112
|
onConflicts: (list) => conflicts.set(list),
|
|
1101
1113
|
onCompaction: (state) => compaction.set(state),
|
|
1114
|
+
onGeneration: (next) => generation.set(next),
|
|
1102
1115
|
onOutboxChange: () => {
|
|
1103
1116
|
persistOutbox();
|
|
1104
1117
|
acked.set(!session?.hasUnacked());
|
|
@@ -1273,6 +1286,7 @@ function meshSync(source, opt) {
|
|
|
1273
1286
|
},
|
|
1274
1287
|
conflicts: conflicts.asReadonly(),
|
|
1275
1288
|
compaction: compaction.asReadonly(),
|
|
1289
|
+
generation: generation.asReadonly(),
|
|
1276
1290
|
declareReady: (pressed) => session?.declareReady(pressed),
|
|
1277
1291
|
sync: {
|
|
1278
1292
|
override: (fn) => started ? sync.override(fn) : fn(),
|
|
@@ -1326,7 +1340,8 @@ function webSocketTransport(url, protocols) {
|
|
|
1326
1340
|
};
|
|
1327
1341
|
}
|
|
1328
1342
|
const asJson = (msg) => JSON.parse(JSON.stringify(msg));
|
|
1329
|
-
function directTransport(relay, ctx) {
|
|
1343
|
+
function directTransport(relay, ctx, options = {}) {
|
|
1344
|
+
const inbound = options.byReference ? (msg) => msg : asJson;
|
|
1330
1345
|
return () => {
|
|
1331
1346
|
const messageCbs = /* @__PURE__ */ new Set();
|
|
1332
1347
|
const closeCbs = /* @__PURE__ */ new Set();
|
|
@@ -1334,7 +1349,7 @@ function directTransport(relay, ctx) {
|
|
|
1334
1349
|
const connection = relay.connect({
|
|
1335
1350
|
send: (msg) => {
|
|
1336
1351
|
if (!open) return;
|
|
1337
|
-
const wired =
|
|
1352
|
+
const wired = inbound(msg);
|
|
1338
1353
|
for (const cb of [...messageCbs]) cb(wired);
|
|
1339
1354
|
},
|
|
1340
1355
|
close: () => {
|