@spooky-sync/core 0.0.1-canary.98 → 1.0.0-beta.10
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/AGENTS.md +58 -21
- package/README.md +59 -16
- package/dist/index.d.ts +2235 -908
- package/dist/index.js +20424 -5052
- package/dist/live.d.ts +149 -0
- package/dist/live.js +739 -0
- package/dist/otel/index.d.ts +1 -1
- package/dist/pure.d.ts +401 -0
- package/dist/pure.js +24 -0
- package/dist/rows-worker.d.ts +1 -0
- package/dist/rows-worker.js +2639 -0
- package/dist/sqlite-open.js +392 -0
- package/dist/sqlite-worker.d.ts +1 -0
- package/dist/sqlite-worker.js +439 -0
- package/dist/sw.d.ts +207 -0
- package/dist/sw.js +393 -0
- package/dist/tabs-broker-worker.d.ts +8 -0
- package/dist/tabs-broker-worker.js +477 -0
- package/dist/types.d.ts +2084 -82
- package/dist/worker-edges.js +498 -0
- package/dist/worker-impersonation.js +60 -0
- package/dist/worker-ref-tables.d.ts +48 -0
- package/dist/worker-types.d.ts +112 -0
- package/dist/worker-types.js +73 -0
- package/package.json +39 -8
- package/scripts/check-broker-bundle.mjs +33 -0
- package/scripts/check-worker-entries.mjs +186 -0
- package/skills/sp00ky-core/SKILL.md +44 -8
- package/skills/sp00ky-core/references/config.md +8 -2
- package/src/boot/auth-flip.saga.test.ts +174 -0
- package/src/boot/auth-flip.saga.ts +77 -0
- package/src/boot/boot.saga.test.ts +203 -0
- package/src/boot/boot.saga.ts +146 -0
- package/src/boot/bucket-switch.saga.test.ts +191 -0
- package/src/boot/bucket-switch.saga.ts +151 -0
- package/src/boot/preload.saga.test.ts +96 -0
- package/src/boot/preload.saga.ts +45 -0
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/client/bucket-handle.test.ts +195 -0
- package/src/client/bucket-handle.ts +372 -0
- package/src/client/eager-paint.test.ts +82 -0
- package/src/client/router.test.ts +59 -0
- package/src/client/router.ts +104 -0
- package/src/client/runtime.test.ts +319 -0
- package/src/client/runtime.ts +307 -0
- package/src/client/services.test.ts +74 -0
- package/src/client/services.ts +440 -0
- package/src/client/sp00ky-client.test.ts +570 -0
- package/src/client/sp00ky-client.ts +875 -0
- package/src/index.ts +87 -0
- package/src/kernel/constants.test.ts +19 -0
- package/src/kernel/constants.ts +94 -0
- package/src/kernel/effects.test.ts +40 -0
- package/src/kernel/effects.ts +181 -0
- package/src/kernel/events.ts +60 -0
- package/src/kernel/interpreter.test.ts +133 -0
- package/src/kernel/interpreter.ts +151 -0
- package/src/kernel/saga.test.ts +89 -0
- package/src/kernel/saga.ts +73 -0
- package/src/live/index.test.ts +512 -0
- package/src/live/index.ts +1059 -0
- package/src/modules/app-release/index.test.ts +125 -0
- package/src/modules/app-release/index.ts +209 -0
- package/src/modules/auth/auth.local-first.test.ts +228 -0
- package/src/modules/auth/auth.session-recheck.test.ts +140 -0
- package/src/modules/auth/events/index.ts +7 -1
- package/src/modules/auth/impersonation-banner.ts +443 -0
- package/src/modules/auth/impersonation.integration.test.ts +209 -0
- package/src/modules/auth/impersonation.test.ts +377 -0
- package/src/modules/auth/impersonation.ts +179 -0
- package/src/modules/auth/index.ts +516 -34
- package/src/modules/channel/channel.test.ts +752 -0
- package/src/modules/channel/hub.test.ts +223 -0
- package/src/modules/channel/hub.ts +355 -0
- package/src/modules/channel/index.ts +1497 -0
- package/src/modules/channel/room.test.ts +340 -0
- package/src/modules/channel/room.ts +367 -0
- package/src/modules/channel/rooms.fixture.ts +383 -0
- package/src/modules/channel/share.test.ts +535 -0
- package/src/modules/channel/webrtc.ts +472 -0
- package/src/modules/crdt/acceptance.ts +75 -0
- package/src/modules/crdt/bytes.ts +50 -0
- package/src/modules/crdt/crdt-field.ts +82 -238
- package/src/modules/crdt/crdt.test.ts +546 -0
- package/src/modules/crdt/hash.ts +103 -0
- package/src/modules/crdt/index.ts +740 -271
- package/src/modules/crdt/loro-loader.ts +25 -0
- package/src/modules/crdt/mirror.ts +37 -0
- package/src/modules/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +661 -99
- package/src/modules/devtools/mutations.test.ts +92 -0
- package/src/modules/devtools/mutations.ts +160 -0
- package/src/modules/devtools/notify-throttle.test.ts +166 -0
- package/src/modules/devtools/state-shape.test.ts +263 -0
- package/src/modules/devtools/storage-info.test.ts +79 -0
- package/src/modules/devtools/storage-info.ts +169 -0
- package/src/modules/devtools/versions.ts +29 -0
- package/src/modules/feature-flag/index.test.ts +179 -1
- package/src/modules/feature-flag/index.ts +117 -10
- package/src/modules/ref-tables.test.ts +28 -1
- package/src/modules/ref-tables.ts +23 -1
- package/src/modules/web-push/index.test.ts +824 -0
- package/src/modules/web-push/index.ts +1027 -0
- package/src/mutation/ingest-outage.integration.test.ts +400 -0
- package/src/mutation/jobs.test.ts +25 -0
- package/src/mutation/jobs.ts +41 -0
- package/src/mutation/mutation-id.test.ts +37 -0
- package/src/mutation/mutation-id.ts +35 -0
- package/src/mutation/pending-writes.integration.test.ts +175 -0
- package/src/mutation/push.saga.test.ts +361 -0
- package/src/mutation/push.saga.ts +245 -0
- package/src/mutation/rows.test.ts +278 -0
- package/src/mutation/rows.ts +515 -0
- package/src/mutation/tray.saga.test.ts +63 -0
- package/src/mutation/tray.saga.ts +48 -0
- package/src/mutation/write.saga.test.ts +436 -0
- package/src/mutation/write.saga.ts +384 -0
- package/src/pure.ts +66 -0
- package/src/push/types.ts +174 -0
- package/src/query/anon-query-isolation.integration.test.ts +531 -0
- package/src/query/env.test.ts +23 -0
- package/src/query/env.ts +65 -0
- package/src/query/fetch.saga.test.ts +199 -0
- package/src/query/fetch.saga.ts +120 -0
- package/src/query/hash.test.ts +14 -0
- package/src/query/hash.ts +18 -0
- package/src/query/id-set-plan.test.ts +132 -0
- package/src/query/lifecycle.saga.test.ts +274 -0
- package/src/query/lifecycle.saga.ts +186 -0
- package/src/query/list-ref-live.integration.test.ts +203 -0
- package/src/query/materialize.saga.test.ts +216 -0
- package/src/query/materialize.saga.ts +83 -0
- package/src/query/materialize.test.ts +58 -0
- package/src/query/materialize.ts +133 -0
- package/src/query/membership.saga.test.ts +514 -0
- package/src/query/membership.saga.ts +339 -0
- package/src/query/membership.test.ts +151 -0
- package/src/query/membership.ts +234 -0
- package/src/query/register.saga.test.ts +430 -0
- package/src/query/register.saga.ts +330 -0
- package/src/query/render-set.test.ts +47 -0
- package/src/query/render-set.ts +71 -0
- package/src/query/session-guard.integration.test.ts +304 -0
- package/src/query/sql.test.ts +118 -0
- package/src/query/sql.ts +174 -0
- package/src/{modules/data → query}/window-query.test.ts +22 -4
- package/src/{modules/data → query}/window-query.ts +86 -8
- package/src/services/blobs/blob-cache.test.ts +359 -0
- package/src/services/blobs/blob-cache.ts +603 -0
- package/src/services/blobs/blob-manifest.ts +227 -0
- package/src/services/blobs/blob-store.test.ts +77 -0
- package/src/services/blobs/blob-store.ts +359 -0
- package/src/services/blobs/blob.fixture.ts +90 -0
- package/src/services/blobs/index.ts +70 -0
- package/src/services/database/cache-engine.ts +223 -0
- package/src/services/database/connection-supervisor.test.ts +318 -0
- package/src/services/database/connection-supervisor.ts +422 -0
- package/src/services/database/database.query-timeout.test.ts +140 -0
- package/src/services/database/database.responses.test.ts +43 -0
- package/src/services/database/database.ts +151 -56
- package/src/services/database/engine-factory.ts +40 -0
- package/src/services/database/errors.ts +34 -0
- package/src/services/database/index.ts +8 -0
- package/src/services/database/landed-body-clear.integration.test.ts +236 -0
- package/src/services/database/local-migrator.integration.test.ts +110 -0
- package/src/services/database/local-migrator.ts +54 -9
- package/src/services/database/local.test.ts +117 -0
- package/src/services/database/local.ts +364 -74
- package/src/services/database/plan-render.test.ts +159 -0
- package/src/services/database/plan-render.ts +108 -0
- package/src/services/database/relation-resolver.test.ts +484 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +283 -4
- package/src/services/database/rows-cache-engine.test.ts +1014 -0
- package/src/services/database/rows-cache-engine.ts +921 -0
- package/src/services/database/rows-exec.integration.test.ts +589 -0
- package/src/services/database/rows-exec.ts +1027 -0
- package/src/services/database/rows-prewarm.ts +225 -0
- package/src/services/database/rows-store.test.ts +635 -0
- package/src/services/database/rows-store.ts +1208 -0
- package/src/services/database/rows-transport.fixture.ts +151 -0
- package/src/services/database/rows-worker.ts +588 -0
- package/src/services/database/sqlite-cache-engine.test.ts +649 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1422 -0
- package/src/services/database/sqlite-column-reset.integration.test.ts +100 -0
- package/src/services/database/sqlite-column-reset.ts +91 -0
- package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
- package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
- package/src/services/database/sqlite-lock-verify.test.ts +33 -0
- package/src/services/database/sqlite-lock-verify.ts +45 -0
- package/src/services/database/sqlite-open.test.ts +150 -0
- package/src/services/database/sqlite-open.ts +164 -0
- package/src/services/database/sqlite-plan-sql.test.ts +164 -0
- package/src/services/database/sqlite-plan-sql.ts +196 -0
- package/src/services/database/sqlite-projection.test.ts +121 -0
- package/src/services/database/sqlite-select.integration.test.ts +185 -0
- package/src/services/database/sqlite-select.test.ts +261 -0
- package/src/services/database/sqlite-select.ts +131 -0
- package/src/services/database/sqlite-transport.fixture.ts +30 -0
- package/src/services/database/sqlite-transport.ts +231 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +417 -0
- package/src/services/database/surreal-cache-engine.ts +163 -0
- package/src/services/logger/index.test.ts +132 -0
- package/src/services/logger/index.ts +187 -6
- package/src/services/persistence/surrealdb.ts +2 -2
- package/src/services/stream-processor/index.ts +621 -93
- package/src/services/stream-processor/stream-processor.batch.test.ts +57 -7
- package/src/services/stream-processor/stream-processor.prime.test.ts +234 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
- package/src/services/stream-processor/wasm-types.ts +41 -1
- package/src/services/tabs/broker-client.ts +284 -0
- package/src/services/tabs/broker.test.ts +381 -0
- package/src/services/tabs/coordinator.test.ts +386 -0
- package/src/services/tabs/coordinator.ts +646 -0
- package/src/services/tabs/fake-ports.fixture.ts +112 -0
- package/src/services/tabs/leader-locks.ts +75 -0
- package/src/services/tabs/protocol.ts +262 -0
- package/src/services/tabs/support.ts +38 -0
- package/src/services/tabs/tabs-broker-worker.ts +652 -0
- package/src/sp00ky.ts +5 -648
- package/src/state/client-state.ts +231 -0
- package/src/state/lifecycle.test.ts +184 -0
- package/src/state/lifecycle.ts +127 -0
- package/src/state/reducers.test.ts +337 -0
- package/src/state/reducers.ts +571 -0
- package/src/state/selectors.test.ts +203 -0
- package/src/state/selectors.ts +264 -0
- package/src/sw/index.test.ts +582 -0
- package/src/sw/index.ts +607 -0
- package/src/sw/store.ts +96 -0
- package/src/sync/connection.saga.test.ts +97 -0
- package/src/sync/connection.saga.ts +89 -0
- package/src/sync/live.saga.test.ts +229 -0
- package/src/sync/live.saga.ts +116 -0
- package/src/sync/policy.health.test.ts +56 -0
- package/src/{modules/sync/utils.test.ts → sync/policy.test.ts} +96 -11
- package/src/{modules/sync/utils.ts → sync/policy.ts} +186 -7
- package/src/sync/poll.saga.test.ts +51 -0
- package/src/sync/poll.saga.ts +36 -0
- package/src/sync/tabs.saga.test.ts +78 -0
- package/src/sync/tabs.saga.ts +96 -0
- package/src/testing/adapters.ts +137 -0
- package/src/testing/build.ts +63 -0
- package/src/testing/fake-services.ts +63 -0
- package/src/testing/run-pure.test.ts +92 -0
- package/src/testing/run-pure.ts +128 -0
- package/src/testing/services.ts +17 -0
- package/src/types.ts +601 -25
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/edges.ts +57 -0
- package/src/utils/error-classification.test.ts +116 -0
- package/src/utils/error-classification.ts +74 -0
- package/src/utils/index.ts +58 -3
- package/src/utils/parser.test.ts +67 -102
- package/src/utils/parser.ts +62 -1
- package/src/utils/semver.test.ts +32 -0
- package/src/utils/semver.ts +30 -0
- package/src/utils/sha256.ts +15 -0
- package/src/utils/surql.ts +17 -15
- package/src/utils/utils.test.ts +3 -2
- package/tsdown.config.ts +105 -4
- package/vitest.config.ts +52 -0
- package/src/modules/cache/index.ts +0 -253
- package/src/modules/cache/types.ts +0 -19
- package/src/modules/crdt/crdt-hydration.test.ts +0 -206
- package/src/modules/data/data.status.test.ts +0 -108
- package/src/modules/data/data.test.ts +0 -58
- package/src/modules/data/index.ts +0 -1491
- package/src/modules/sync/engine.ts +0 -227
- package/src/modules/sync/events/index.ts +0 -84
- package/src/modules/sync/index.ts +0 -3
- package/src/modules/sync/queue/index.ts +0 -2
- package/src/modules/sync/queue/queue-down.ts +0 -90
- package/src/modules/sync/queue/queue-up.ts +0 -224
- package/src/modules/sync/scheduler.ts +0 -121
- package/src/modules/sync/sync.ts +0 -1244
- package/src/sp00ky.auth-order.test.ts +0 -65
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import type { ChannelWire, TransportHooks } from './index';
|
|
3
|
+
import { hubTransport, type HubOptions } from './hub';
|
|
4
|
+
import { roomLink } from './room';
|
|
5
|
+
import { FakeRelay, fakeBrowser, memoryRoom, settle } from './rooms.fixture';
|
|
6
|
+
|
|
7
|
+
type Browser = ReturnType<typeof fakeBrowser>;
|
|
8
|
+
type Room = ReturnType<typeof memoryRoom>;
|
|
9
|
+
|
|
10
|
+
const USER = 'user:ann';
|
|
11
|
+
const op = (n: number): ChannelWire => ({ t: 'op', table: 'note', id: `note:${n}`, patch: { n }, hlc: [n, 0, 'x'] });
|
|
12
|
+
|
|
13
|
+
/** One tab of `USER` in `browser`: the transport and what it told the manager. */
|
|
14
|
+
function tab(browser: Browser, room: Room, peer: string, extra: Partial<HubOptions> = {}) {
|
|
15
|
+
const events: string[] = [];
|
|
16
|
+
const messages: Array<[string, unknown]> = [];
|
|
17
|
+
const status: string[] = [];
|
|
18
|
+
let signedIn = true;
|
|
19
|
+
const hooks: TransportHooks = {
|
|
20
|
+
self: () => (signedIn ? { user: USER, peer } : null),
|
|
21
|
+
onMessage: (from, m) => messages.push([from.peer, m]),
|
|
22
|
+
onPeer: (p, up) => events.push(`${up ? 'up' : 'down'} ${p.peer}`),
|
|
23
|
+
onStatus: (_user, s) => status.push(s),
|
|
24
|
+
};
|
|
25
|
+
const t = hubTransport(hooks, { scope: (user) => `ns/db/${user}`, link: room.link, locks: browser.locks, bus: browser.bus, supported: true, ...extra });
|
|
26
|
+
return {
|
|
27
|
+
t,
|
|
28
|
+
events,
|
|
29
|
+
messages,
|
|
30
|
+
status,
|
|
31
|
+
peer,
|
|
32
|
+
join: () => t.want(new Set([USER])),
|
|
33
|
+
to: (...peers: string[]) => peers.map((p) => ({ user: USER, peer: p })),
|
|
34
|
+
signOut: () => (signedIn = false),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Two browsers, two tabs each, all in the room. */
|
|
39
|
+
async function twoByTwo() {
|
|
40
|
+
const room = memoryRoom();
|
|
41
|
+
const left = fakeBrowser();
|
|
42
|
+
const right = fakeBrowser();
|
|
43
|
+
const a1 = tab(left, room, 'a1');
|
|
44
|
+
const a2 = tab(left, room, 'a2');
|
|
45
|
+
const b1 = tab(right, room, 'b1');
|
|
46
|
+
const b2 = tab(right, room, 'b2');
|
|
47
|
+
for (const t of [a1, a2, b1, b2]) t.join();
|
|
48
|
+
await settle();
|
|
49
|
+
return { room, left, right, a1, a2, b1, b2 };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe('hubTransport', () => {
|
|
53
|
+
it('keeps one link per browser, and shows every tab the tabs of the other browsers', async () => {
|
|
54
|
+
const { room, a1, a2, b1, b2 } = await twoByTwo();
|
|
55
|
+
expect([...room.members.keys()].sort()).toEqual(['a1', 'b1']);
|
|
56
|
+
for (const t of [a1, a2]) expect([...t.events].sort()).toEqual(['up b1', 'up b2']);
|
|
57
|
+
for (const t of [b1, b2]) expect([...t.events].sort()).toEqual(['up a1', 'up a2']);
|
|
58
|
+
for (const t of [a1, a2, b1, b2]) expect(t.status.at(-1)).toBe('connected');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('carries a message from any tab to the tabs it names, as one frame', async () => {
|
|
62
|
+
const { room, a1, a2, b1, b2 } = await twoByTwo();
|
|
63
|
+
const before = room.frames.length;
|
|
64
|
+
// The manager sends one message to each peer in turn.
|
|
65
|
+
const m = op(1);
|
|
66
|
+
for (const to of a2.to('b1', 'b2')) a2.t.send(to, m);
|
|
67
|
+
await settle();
|
|
68
|
+
expect(b1.messages).toEqual([['a2', m]]);
|
|
69
|
+
expect(b2.messages).toEqual([['a2', m]]);
|
|
70
|
+
expect(a1.messages).toEqual([]);
|
|
71
|
+
expect(room.frames.length - before).toBe(1);
|
|
72
|
+
|
|
73
|
+
b1.t.send(b1.to('a2')[0], op(2));
|
|
74
|
+
await settle();
|
|
75
|
+
expect(a2.messages).toEqual([['b1', op(2)]]);
|
|
76
|
+
expect(a1.messages).toEqual([]);
|
|
77
|
+
|
|
78
|
+
// Another user, or a tab nobody speaks for: nowhere to go.
|
|
79
|
+
a1.t.send({ user: 'user:bob', peer: 'b1' }, op(3));
|
|
80
|
+
a1.t.send({ user: USER, peer: 'ghost' }, op(4));
|
|
81
|
+
await settle();
|
|
82
|
+
expect(b1.messages).toHaveLength(1);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('hands the link to the next tab when the one holding it closes', async () => {
|
|
86
|
+
const { room, a1, a2, b1, b2 } = await twoByTwo();
|
|
87
|
+
a1.t.close();
|
|
88
|
+
await settle();
|
|
89
|
+
expect([...room.members.keys()].sort()).toEqual(['a2', 'b1']);
|
|
90
|
+
// The other browser saw the old link go and the remaining tab return.
|
|
91
|
+
expect(b1.events.slice(2).sort()).toEqual(['down a1', 'down a2', 'up a2']);
|
|
92
|
+
expect(b2.events.slice(2).sort()).toEqual(['down a1', 'down a2', 'up a2']);
|
|
93
|
+
expect(a2.events.filter((e) => e.startsWith('up')).length).toBe(4);
|
|
94
|
+
a2.t.send(a2.to('b2')[0], op(5));
|
|
95
|
+
b1.t.send(b1.to('a2')[0], op(6));
|
|
96
|
+
await settle();
|
|
97
|
+
expect(b2.messages).toEqual([['a2', op(5)]]);
|
|
98
|
+
expect(a2.messages).toEqual([['b1', op(6)]]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('tells the other browsers when a tab is gone, however it went', async () => {
|
|
102
|
+
const { a1, a2, b1, b2 } = await twoByTwo();
|
|
103
|
+
a2.t.close();
|
|
104
|
+
await settle();
|
|
105
|
+
expect(b1.events.at(-1)).toBe('down a2');
|
|
106
|
+
expect(b2.events.at(-1)).toBe('down a2');
|
|
107
|
+
expect(a1.events).toHaveLength(2);
|
|
108
|
+
|
|
109
|
+
// A tab that crashes says no goodbye: its lock going free is what tells.
|
|
110
|
+
const { room, left, right } = { room: memoryRoom(), left: fakeBrowser(), right: fakeBrowser() };
|
|
111
|
+
const c1 = tab(left, room, 'c1');
|
|
112
|
+
const c2 = tab(left, room, 'c2');
|
|
113
|
+
const d1 = tab(right, room, 'd1');
|
|
114
|
+
for (const t of [c1, c2, d1]) t.join();
|
|
115
|
+
await settle();
|
|
116
|
+
expect([...d1.events].sort()).toEqual(['up c1', 'up c2']);
|
|
117
|
+
for (const b of left.opened) (b as unknown as { silent: boolean }).silent = true;
|
|
118
|
+
c2.t.close();
|
|
119
|
+
for (const b of left.opened) (b as unknown as { silent: boolean }).silent = false;
|
|
120
|
+
await settle();
|
|
121
|
+
expect(d1.events.at(-1)).toBe('down c2');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('brings a tab opened later up to date', async () => {
|
|
125
|
+
const { room, left, a1, b1 } = await twoByTwo();
|
|
126
|
+
const a3 = tab(left, room, 'a3');
|
|
127
|
+
a3.join();
|
|
128
|
+
await settle();
|
|
129
|
+
expect([...a3.events].sort()).toEqual(['up b1', 'up b2']);
|
|
130
|
+
expect(a3.status.at(-1)).toBe('connected');
|
|
131
|
+
expect(b1.events.at(-1)).toBe('up a3');
|
|
132
|
+
expect(a1.events).toHaveLength(2);
|
|
133
|
+
expect(room.members.size).toBe(2);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('leaves when the user is no longer wanted, and returns when wanted again', async () => {
|
|
137
|
+
const { room, a1, a2, b1 } = await twoByTwo();
|
|
138
|
+
a1.t.want(new Set());
|
|
139
|
+
await settle();
|
|
140
|
+
expect(a1.events.slice(2).sort()).toEqual(['down b1', 'down b2']);
|
|
141
|
+
expect([...room.members.keys()].sort()).toEqual(['a2', 'b1']);
|
|
142
|
+
a1.t.want(new Set([USER]));
|
|
143
|
+
await settle();
|
|
144
|
+
expect(a1.events.slice(4).sort()).toEqual(['up b1', 'up b2']);
|
|
145
|
+
expect(b1.events.at(-1)).toBe('up a1');
|
|
146
|
+
// Signed out: nothing to join as.
|
|
147
|
+
a2.signOut();
|
|
148
|
+
a2.t.want(new Set([USER]));
|
|
149
|
+
await settle();
|
|
150
|
+
expect(room.members.has('a2')).toBe(false);
|
|
151
|
+
a2.t.send(a2.to('b1')[0], op(7));
|
|
152
|
+
await settle();
|
|
153
|
+
expect(b1.messages).toEqual([]);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('gives every tab a link of its own without Web Locks or a BroadcastChannel', async () => {
|
|
157
|
+
const room = memoryRoom();
|
|
158
|
+
const browser = fakeBrowser();
|
|
159
|
+
const a1 = tab(browser, room, 'a1', { locks: null });
|
|
160
|
+
const a2 = tab(browser, room, 'a2', { bus: null });
|
|
161
|
+
a1.join();
|
|
162
|
+
a2.join();
|
|
163
|
+
await settle();
|
|
164
|
+
expect([...room.members.keys()].sort()).toEqual(['a1', 'a2']);
|
|
165
|
+
expect(a1.events).toEqual(['up a2']);
|
|
166
|
+
a1.t.send(a1.to('a2')[0], op(8));
|
|
167
|
+
await settle();
|
|
168
|
+
expect(a2.messages).toEqual([['a1', op(8)]]);
|
|
169
|
+
a1.t.close();
|
|
170
|
+
await settle();
|
|
171
|
+
expect(a2.events).toEqual(['up a1', 'down a1']);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('knows the other browser\'s tabs again after its socket was replaced', async () => {
|
|
175
|
+
// Over the real link and a relay: a socket that died where only the
|
|
176
|
+
// client could tell comes back under the same id.
|
|
177
|
+
const relay = new FakeRelay();
|
|
178
|
+
const viaRelay = (hooks: Parameters<typeof roomLink>[0]) =>
|
|
179
|
+
roomLink(hooks, { url: 'ws://relay.test/room', token: () => 't-ann', WebSocket: relay.WebSocket, direct: false });
|
|
180
|
+
const room = { link: viaRelay } as unknown as Room;
|
|
181
|
+
const a1 = tab(fakeBrowser(), room, 'a1');
|
|
182
|
+
const right = fakeBrowser();
|
|
183
|
+
const b1 = tab(right, room, 'b1');
|
|
184
|
+
const b2 = tab(right, room, 'b2');
|
|
185
|
+
for (const t of [a1, b1, b2]) t.join();
|
|
186
|
+
await settle();
|
|
187
|
+
expect([...a1.events].sort()).toEqual(['up b1', 'up b2']);
|
|
188
|
+
|
|
189
|
+
relay.sockets[0].sever();
|
|
190
|
+
await new Promise((r) => setTimeout(r, 900));
|
|
191
|
+
await settle();
|
|
192
|
+
expect(a1.status.at(-1)).toBe('connected');
|
|
193
|
+
// Down with the socket, and up again once the other hub said its tabs anew.
|
|
194
|
+
expect(a1.events.slice(2).sort()).toEqual(['down b1', 'down b2', 'up b1', 'up b2']);
|
|
195
|
+
expect(b1.events).toEqual(['up a1', 'down a1', 'up a1']);
|
|
196
|
+
a1.t.send(a1.to('b2')[0], op(11));
|
|
197
|
+
b1.t.send(b1.to('a1')[0], op(12));
|
|
198
|
+
await settle();
|
|
199
|
+
expect(b2.messages).toEqual([['a1', op(11)]]);
|
|
200
|
+
expect(a1.messages).toEqual([['b1', op(12)]]);
|
|
201
|
+
for (const t of [a1, b1, b2]) t.t.close();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('takes a message only from a tab its browser speaks for', async () => {
|
|
205
|
+
const { room, b1, b2 } = await twoByTwo();
|
|
206
|
+
const a1Link = room.members.get('a1');
|
|
207
|
+
const forge = (data: unknown) => room.members.get('b1')?.onMessage('a1', data);
|
|
208
|
+
forge({ h: 'm', from: 'zz', to: null, m: op(9) });
|
|
209
|
+
forge({ h: 'm', from: 'a1', to: 'b1', m: op(9) });
|
|
210
|
+
forge({ h: 'tabs', tabs: 'nope' });
|
|
211
|
+
forge({ h: 'what' });
|
|
212
|
+
forge(null);
|
|
213
|
+
room.members.get('b1')?.onMessage('stranger', { h: 'm', from: 'a1', to: null, m: op(9) });
|
|
214
|
+
await settle();
|
|
215
|
+
expect(b1.messages).toEqual([]);
|
|
216
|
+
expect(b2.messages).toEqual([]);
|
|
217
|
+
forge({ h: 'm', from: 'a1', to: null, m: op(10) });
|
|
218
|
+
await settle();
|
|
219
|
+
expect(b1.messages).toEqual([['a1', op(10)]]);
|
|
220
|
+
expect(b2.messages).toEqual([['a1', op(10)]]);
|
|
221
|
+
expect(a1Link).toBeDefined();
|
|
222
|
+
});
|
|
223
|
+
});
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import type { Logger } from '../../services/logger/index';
|
|
2
|
+
import type { ChannelTransport, PeerRef, TransportHooks } from './index';
|
|
3
|
+
import { roomSupported, type RoomLink, type RoomLinkFactory, type RoomPeer } from './room';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* One link per browser, shared by its tabs.
|
|
7
|
+
*
|
|
8
|
+
* Every tab of a user runs this transport. One of them, the hub, holds the
|
|
9
|
+
* browser's link to the user's other browsers (`RoomLink`); which one is
|
|
10
|
+
* decided by a Web Lock, so when the hub's tab closes the next one takes over
|
|
11
|
+
* and connects. The other tabs reach the hub over a `BroadcastChannel`.
|
|
12
|
+
*
|
|
13
|
+
* To the `ChannelManager` nothing changes: a peer is still one tab. The hubs
|
|
14
|
+
* tell each other which tabs they speak for, every tab is told about the
|
|
15
|
+
* tabs of the other browsers, and a message names the tab it comes from and
|
|
16
|
+
* the tabs it is for. So a value a tab wrote still disappears with that tab,
|
|
17
|
+
* not with its browser.
|
|
18
|
+
*
|
|
19
|
+
* Tabs of one browser are not peers of each other here: they may share one
|
|
20
|
+
* local store, where a peer's write would be undone under the tab that made
|
|
21
|
+
* it.
|
|
22
|
+
*
|
|
23
|
+
* Where there are no Web Locks or no `BroadcastChannel`, every tab is a hub
|
|
24
|
+
* with a link of its own.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
type Status = 'connecting' | 'connected' | 'failed';
|
|
28
|
+
|
|
29
|
+
/** What tabs of one browser say to each other. */
|
|
30
|
+
type Bus =
|
|
31
|
+
| { k: 'here'; tab: string }
|
|
32
|
+
| { k: 'bye'; tab: string }
|
|
33
|
+
| { k: 'hub'; tab: string }
|
|
34
|
+
| { k: 'remote'; browsers: Record<RoomPeer, string[]>; status: Status }
|
|
35
|
+
| { k: 'out'; tab: string; to: string[]; m: unknown }
|
|
36
|
+
| { k: 'in'; from: string; to: string[] | null; m: unknown };
|
|
37
|
+
|
|
38
|
+
/** What hubs say to each other over the link. */
|
|
39
|
+
type Between = { h: 'tabs'; tabs: string[] } | { h: 'm'; from: string; to: string[] | null; m: unknown };
|
|
40
|
+
|
|
41
|
+
/** The part of `navigator.locks` this needs. */
|
|
42
|
+
export interface HubLocks {
|
|
43
|
+
request(name: string, callback: () => Promise<void>): Promise<unknown>;
|
|
44
|
+
request(name: string, options: { mode: 'shared' | 'exclusive' }, callback: () => Promise<void> | void): Promise<unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The part of a `BroadcastChannel` this needs. */
|
|
48
|
+
export interface HubBus {
|
|
49
|
+
postMessage(message: unknown): void;
|
|
50
|
+
onmessage: ((event: { data: unknown }) => void) | null;
|
|
51
|
+
close(): void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface HubOptions {
|
|
55
|
+
/** Names the room of one user in this browser; the lock and the bus carry it. */
|
|
56
|
+
scope: (user: string) => string;
|
|
57
|
+
/** Builds the browser's link. Called in the tab that holds it. */
|
|
58
|
+
link: RoomLinkFactory;
|
|
59
|
+
logger?: Pick<Logger, 'debug' | 'warn'>;
|
|
60
|
+
/** `null` forces a link per tab. Default `navigator.locks`. */
|
|
61
|
+
locks?: HubLocks | null;
|
|
62
|
+
/** `null` forces a link per tab. Default `BroadcastChannel`. */
|
|
63
|
+
bus?: ((name: string) => HubBus) | null;
|
|
64
|
+
supported?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const MAX_TABS = 64;
|
|
68
|
+
const isTabId = (v: unknown): v is string => typeof v === 'string' && v.length > 0 && v.length <= 64;
|
|
69
|
+
const tabList = (v: unknown): string[] | null => (Array.isArray(v) && v.length <= MAX_TABS && v.every(isTabId) ? (v as string[]) : null);
|
|
70
|
+
|
|
71
|
+
const defaultLocks = (): HubLocks | null => (globalThis as { navigator?: { locks?: HubLocks } }).navigator?.locks ?? null;
|
|
72
|
+
const defaultBus = (): ((name: string) => HubBus) | null => {
|
|
73
|
+
const BC = (globalThis as { BroadcastChannel?: new (name: string) => HubBus }).BroadcastChannel;
|
|
74
|
+
return BC ? (name) => new BC(name) : null;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export function hubTransport(hooks: TransportHooks, opts: HubOptions): ChannelTransport {
|
|
78
|
+
const locks = opts.locks === undefined ? defaultLocks() : opts.locks;
|
|
79
|
+
const openBus = opts.bus === undefined ? defaultBus() : opts.bus;
|
|
80
|
+
const log = opts.logger;
|
|
81
|
+
|
|
82
|
+
/** This tab while it takes part, else null. */
|
|
83
|
+
let me: PeerRef | null = null;
|
|
84
|
+
/** Counts starts, so a lock granted for an earlier one does nothing. */
|
|
85
|
+
let run = 0;
|
|
86
|
+
let bus: HubBus | null = null;
|
|
87
|
+
let isHub = false;
|
|
88
|
+
let link: RoomLink | null = null;
|
|
89
|
+
let releaseHub: (() => void) | null = null;
|
|
90
|
+
let releaseTab: (() => void) | null = null;
|
|
91
|
+
/** The hub's: the other tabs of this browser. */
|
|
92
|
+
const localTabs = new Set<string>();
|
|
93
|
+
/** The other browsers and the tabs each speaks for. The hub's is the truth, the other tabs hold a copy. */
|
|
94
|
+
const browsers = new Map<RoomPeer, Set<string>>();
|
|
95
|
+
/** The tabs of other browsers this tab has reported as up. */
|
|
96
|
+
const up = new Set<string>();
|
|
97
|
+
let status: Status = 'connecting';
|
|
98
|
+
let reported: Status | null = null;
|
|
99
|
+
/** Messages of this turn, each with the tabs it goes to: one frame per message, not one per tab. */
|
|
100
|
+
const pending = new Map<unknown, string[]>();
|
|
101
|
+
let flushing = false;
|
|
102
|
+
|
|
103
|
+
const post = (message: Bus): void => {
|
|
104
|
+
try {
|
|
105
|
+
bus?.postMessage(message);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
log?.debug({ error, Category: 'sp00ky-client::channel' }, 'channel bus post failed');
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/** Tell the manager which tabs of other browsers came and went, and how the link is doing. */
|
|
112
|
+
const apply = (): void => {
|
|
113
|
+
const self = me;
|
|
114
|
+
if (!self) return;
|
|
115
|
+
const now = new Set<string>();
|
|
116
|
+
for (const tabs of browsers.values()) for (const t of tabs) if (t !== self.peer) now.add(t);
|
|
117
|
+
for (const t of [...up]) {
|
|
118
|
+
if (now.has(t)) continue;
|
|
119
|
+
up.delete(t);
|
|
120
|
+
hooks.onPeer({ user: self.user, peer: t }, false);
|
|
121
|
+
}
|
|
122
|
+
for (const t of now) {
|
|
123
|
+
if (up.has(t)) continue;
|
|
124
|
+
up.add(t);
|
|
125
|
+
hooks.onPeer({ user: self.user, peer: t }, true);
|
|
126
|
+
}
|
|
127
|
+
if (status !== reported) {
|
|
128
|
+
reported = status;
|
|
129
|
+
hooks.onStatus(self.user, status);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// ---- the hub -----------------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
const publish = (): void => {
|
|
136
|
+
apply();
|
|
137
|
+
const out: Record<RoomPeer, string[]> = {};
|
|
138
|
+
for (const [b, tabs] of browsers) out[b] = [...tabs];
|
|
139
|
+
post({ k: 'remote', browsers: out, status });
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** The tabs this browser speaks for changed: the other hubs hear it. */
|
|
143
|
+
const announceTabs = (to: ReadonlyArray<RoomPeer> | null): void => {
|
|
144
|
+
if (me) link?.send(to, { h: 'tabs', tabs: [me.peer, ...localTabs] } satisfies Between);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const hubSend = (from: string, to: string[], m: unknown): void => {
|
|
148
|
+
const dest: RoomPeer[] = [];
|
|
149
|
+
for (const [b, tabs] of browsers) if (to.some((t) => tabs.has(t))) dest.push(b);
|
|
150
|
+
if (dest.length > 0) link?.send(dest.length === browsers.size ? null : dest, { h: 'm', from, to, m } satisfies Between);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/** A message of another browser's tab: to this tab, and on to the others it names. */
|
|
154
|
+
const deliver = (from: string, to: string[] | null, m: unknown): void => {
|
|
155
|
+
const self = me;
|
|
156
|
+
if (!self) return;
|
|
157
|
+
if (localTabs.size > 0 && (to === null || to.some((t) => localTabs.has(t)))) post({ k: 'in', from, to, m });
|
|
158
|
+
if (to === null || to.includes(self.peer)) hooks.onMessage({ user: self.user, peer: from }, m);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** Hold on to a tab of this browser until it is gone, however it goes. */
|
|
162
|
+
const watch = (tab: string, at: number): void => {
|
|
163
|
+
if (!locks) return;
|
|
164
|
+
// Every tab holds a lock under its own name for as long as it lives, so
|
|
165
|
+
// this request is granted the moment that tab is gone.
|
|
166
|
+
void locks
|
|
167
|
+
.request(tabLock(tab), { mode: 'shared' }, () => {
|
|
168
|
+
if (at === run && isHub && localTabs.delete(tab)) announceTabs(null);
|
|
169
|
+
})
|
|
170
|
+
.catch(() => {});
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const becomeHub = (self: PeerRef): void => {
|
|
174
|
+
isHub = true;
|
|
175
|
+
browsers.clear();
|
|
176
|
+
status = 'connecting';
|
|
177
|
+
link = opts.link({
|
|
178
|
+
self: () => (me ? { user: me.user, peer: me.peer } : null),
|
|
179
|
+
onPeer: (browser, isUp) => {
|
|
180
|
+
if (isUp) {
|
|
181
|
+
browsers.set(browser, new Set());
|
|
182
|
+
announceTabs([browser]);
|
|
183
|
+
} else if (!browsers.delete(browser)) return;
|
|
184
|
+
publish();
|
|
185
|
+
},
|
|
186
|
+
onMessage: (browser, data) => {
|
|
187
|
+
const tabs = browsers.get(browser);
|
|
188
|
+
if (!tabs || data === null || typeof data !== 'object') return;
|
|
189
|
+
const d = data as Partial<Between> & Record<string, unknown>;
|
|
190
|
+
if (d.h === 'tabs') {
|
|
191
|
+
const list = tabList(d.tabs);
|
|
192
|
+
if (!list) return;
|
|
193
|
+
browsers.set(browser, new Set(list));
|
|
194
|
+
publish();
|
|
195
|
+
} else if (d.h === 'm') {
|
|
196
|
+
const to = d.to === null ? null : tabList(d.to);
|
|
197
|
+
// A hub speaks for its own tabs only.
|
|
198
|
+
if (!isTabId(d.from) || !tabs.has(d.from) || (d.to !== null && to === null)) return;
|
|
199
|
+
deliver(d.from, to, d.m);
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
onStatus: (next) => {
|
|
203
|
+
status = next;
|
|
204
|
+
publish();
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
post({ k: 'hub', tab: self.peer });
|
|
208
|
+
publish();
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// ---- every tab ---------------------------------------------------------------
|
|
212
|
+
|
|
213
|
+
const hubLock = (): string => `sp00ky-channels:${opts.scope(me?.user ?? '')}`;
|
|
214
|
+
const tabLock = (tab: string): string => `${hubLock()}:tab:${tab}`;
|
|
215
|
+
|
|
216
|
+
const onBus = (data: unknown): void => {
|
|
217
|
+
const self = me;
|
|
218
|
+
if (!self || data === null || typeof data !== 'object') return;
|
|
219
|
+
const msg = data as Bus;
|
|
220
|
+
switch (msg.k) {
|
|
221
|
+
case 'here':
|
|
222
|
+
if (!isHub || !isTabId(msg.tab) || msg.tab === self.peer) return;
|
|
223
|
+
if (!localTabs.has(msg.tab)) {
|
|
224
|
+
localTabs.add(msg.tab);
|
|
225
|
+
watch(msg.tab, run);
|
|
226
|
+
announceTabs(null);
|
|
227
|
+
}
|
|
228
|
+
publish();
|
|
229
|
+
return;
|
|
230
|
+
case 'bye':
|
|
231
|
+
if (isHub && localTabs.delete(msg.tab)) announceTabs(null);
|
|
232
|
+
return;
|
|
233
|
+
case 'hub':
|
|
234
|
+
// A tab took the link over: it starts out knowing nobody.
|
|
235
|
+
if (!isHub) post({ k: 'here', tab: self.peer });
|
|
236
|
+
return;
|
|
237
|
+
case 'remote': {
|
|
238
|
+
if (isHub || msg.browsers === null || typeof msg.browsers !== 'object') return;
|
|
239
|
+
browsers.clear();
|
|
240
|
+
for (const [b, tabs] of Object.entries(msg.browsers)) {
|
|
241
|
+
const list = tabList(tabs);
|
|
242
|
+
if (list) browsers.set(b, new Set(list));
|
|
243
|
+
}
|
|
244
|
+
status = msg.status;
|
|
245
|
+
apply();
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
case 'out':
|
|
249
|
+
if (isHub && localTabs.has(msg.tab) && tabList(msg.to)) hubSend(msg.tab, msg.to, msg.m);
|
|
250
|
+
return;
|
|
251
|
+
case 'in':
|
|
252
|
+
if (!isHub && (msg.to === null || (Array.isArray(msg.to) && msg.to.includes(self.peer)))) {
|
|
253
|
+
hooks.onMessage({ user: self.user, peer: msg.from }, msg.m);
|
|
254
|
+
}
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const flush = (): void => {
|
|
260
|
+
flushing = false;
|
|
261
|
+
const self = me;
|
|
262
|
+
const batch = [...pending];
|
|
263
|
+
pending.clear();
|
|
264
|
+
if (!self) return;
|
|
265
|
+
for (const [m, to] of batch) {
|
|
266
|
+
if (isHub) hubSend(self.peer, to, m);
|
|
267
|
+
else post({ k: 'out', tab: self.peer, to, m });
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const start = (self: PeerRef): void => {
|
|
272
|
+
me = self;
|
|
273
|
+
const at = ++run;
|
|
274
|
+
status = 'connecting';
|
|
275
|
+
reported = null;
|
|
276
|
+
if (!locks || !openBus) {
|
|
277
|
+
becomeHub(self);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
bus = openBus(hubLock());
|
|
281
|
+
bus.onmessage = (event) => onBus(event.data);
|
|
282
|
+
// The tab's own lock first: a hub that hears `here` waits on it to learn
|
|
283
|
+
// when this tab is gone, so it has to be held by then.
|
|
284
|
+
void locks
|
|
285
|
+
.request(
|
|
286
|
+
tabLock(self.peer),
|
|
287
|
+
() =>
|
|
288
|
+
new Promise<void>((resolve) => {
|
|
289
|
+
if (at !== run) return resolve();
|
|
290
|
+
releaseTab = resolve;
|
|
291
|
+
if (!isHub) post({ k: 'here', tab: self.peer });
|
|
292
|
+
})
|
|
293
|
+
)
|
|
294
|
+
.catch(() => {});
|
|
295
|
+
void locks
|
|
296
|
+
.request(
|
|
297
|
+
hubLock(),
|
|
298
|
+
() =>
|
|
299
|
+
new Promise<void>((resolve) => {
|
|
300
|
+
if (at !== run) return resolve();
|
|
301
|
+
releaseHub = resolve;
|
|
302
|
+
becomeHub(self);
|
|
303
|
+
})
|
|
304
|
+
)
|
|
305
|
+
.catch(() => {});
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const stop = (): void => {
|
|
309
|
+
const self = me;
|
|
310
|
+
if (!self) return;
|
|
311
|
+
if (!isHub) post({ k: 'bye', tab: self.peer });
|
|
312
|
+
run++;
|
|
313
|
+
link?.close();
|
|
314
|
+
link = null;
|
|
315
|
+
isHub = false;
|
|
316
|
+
releaseHub?.();
|
|
317
|
+
releaseTab?.();
|
|
318
|
+
releaseHub = null;
|
|
319
|
+
releaseTab = null;
|
|
320
|
+
try {
|
|
321
|
+
bus?.close();
|
|
322
|
+
} catch {
|
|
323
|
+
// already closed
|
|
324
|
+
}
|
|
325
|
+
bus = null;
|
|
326
|
+
localTabs.clear();
|
|
327
|
+
browsers.clear();
|
|
328
|
+
pending.clear();
|
|
329
|
+
for (const t of [...up]) hooks.onPeer({ user: self.user, peer: t }, false);
|
|
330
|
+
up.clear();
|
|
331
|
+
me = null;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
supported: opts.supported ?? roomSupported(),
|
|
336
|
+
want(users) {
|
|
337
|
+
const self = hooks.self();
|
|
338
|
+
const wanted = self !== null && users.has(self.user);
|
|
339
|
+
if (me && (!wanted || !self || self.user !== me.user || self.peer !== me.peer)) stop();
|
|
340
|
+
if (wanted && self && !me) start(self);
|
|
341
|
+
},
|
|
342
|
+
send(to, message) {
|
|
343
|
+
if (!me || to.user !== me.user) return;
|
|
344
|
+
const list = pending.get(message);
|
|
345
|
+
if (list) list.push(to.peer);
|
|
346
|
+
else pending.set(message, [to.peer]);
|
|
347
|
+
if (flushing) return;
|
|
348
|
+
flushing = true;
|
|
349
|
+
void Promise.resolve().then(flush);
|
|
350
|
+
},
|
|
351
|
+
close() {
|
|
352
|
+
stop();
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
}
|