@caprid/relay 0.4.1 → 0.6.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/README.md +55 -53
- package/dist/client-diagnostics.d.ts +235 -0
- package/dist/client-diagnostics.d.ts.map +1 -0
- package/dist/client-diagnostics.js +582 -0
- package/dist/client-diagnostics.js.map +1 -0
- package/dist/connect-session.d.ts +43 -3
- package/dist/connect-session.d.ts.map +1 -1
- package/dist/connect-session.js +46 -3
- package/dist/connect-session.js.map +1 -1
- package/dist/connect.d.ts +41 -6
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +19 -3
- package/dist/connect.js.map +1 -1
- package/dist/diagnostics-report.d.ts +106 -0
- package/dist/diagnostics-report.d.ts.map +1 -0
- package/dist/diagnostics-report.js +450 -0
- package/dist/diagnostics-report.js.map +1 -0
- package/dist/diagnostics.d.ts +20 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +20 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/discovery.d.ts +38 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +273 -0
- package/dist/discovery.js.map +1 -0
- package/dist/frame-reader.d.ts +12 -0
- package/dist/frame-reader.d.ts.map +1 -1
- package/dist/frame-reader.js +65 -0
- package/dist/frame-reader.js.map +1 -1
- package/dist/frame-writer.d.ts +16 -2
- package/dist/frame-writer.d.ts.map +1 -1
- package/dist/frame-writer.js +73 -1
- package/dist/frame-writer.js.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/media-track-properties.js +1 -1
- package/dist/media-track-properties.js.map +1 -1
- package/dist/media.d.ts +4 -1
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +4 -1
- package/dist/media.js.map +1 -1
- package/dist/namespace-events.d.ts +32 -3
- package/dist/namespace-events.d.ts.map +1 -1
- package/dist/namespace-events.js +82 -7
- package/dist/namespace-events.js.map +1 -1
- package/dist/node.d.ts +13 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +17 -0
- package/dist/node.js.map +1 -1
- package/dist/presence.d.ts +155 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +485 -0
- package/dist/presence.js.map +1 -0
- package/dist/publish.d.ts +61 -7
- package/dist/publish.d.ts.map +1 -1
- package/dist/publish.js +208 -87
- package/dist/publish.js.map +1 -1
- package/dist/publishing-roots.d.ts +189 -0
- package/dist/publishing-roots.d.ts.map +1 -0
- package/dist/publishing-roots.js +426 -0
- package/dist/publishing-roots.js.map +1 -0
- package/dist/relay-session.d.ts +31 -0
- package/dist/relay-session.d.ts.map +1 -0
- package/dist/relay-session.js +64 -0
- package/dist/relay-session.js.map +1 -0
- package/dist/subscription-events.d.ts +5 -2
- package/dist/subscription-events.d.ts.map +1 -1
- package/dist/subscription-events.js +20 -6
- package/dist/subscription-events.js.map +1 -1
- package/dist/supervised-publisher.d.ts +7 -0
- package/dist/supervised-publisher.d.ts.map +1 -1
- package/dist/supervised-publisher.js +46 -809
- package/dist/supervised-publisher.js.map +1 -1
- package/dist/supervised-relay.d.ts +120 -0
- package/dist/supervised-relay.d.ts.map +1 -0
- package/dist/supervised-relay.js +83 -0
- package/dist/supervised-relay.js.map +1 -0
- package/dist/supervised-subscriber.d.ts +74 -4
- package/dist/supervised-subscriber.d.ts.map +1 -1
- package/dist/supervised-subscriber.js +259 -561
- package/dist/supervised-subscriber.js.map +1 -1
- package/dist/supervised-track.d.ts +63 -0
- package/dist/supervised-track.d.ts.map +1 -0
- package/dist/supervised-track.js +430 -0
- package/dist/supervised-track.js.map +1 -0
- package/dist/supervisor.d.ts +101 -0
- package/dist/supervisor.d.ts.map +1 -0
- package/dist/supervisor.js +744 -0
- package/dist/supervisor.js.map +1 -0
- package/dist/switchable.d.ts +1 -1
- package/dist/version.d.ts +6 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/dist/write-slot.d.ts +5 -0
- package/dist/write-slot.d.ts.map +1 -1
- package/dist/write-slot.js +3 -0
- package/dist/write-slot.js.map +1 -1
- package/package.json +13 -9
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { PublishingRoot } from './publishing-roots.js';
|
|
2
|
+
import type { SupervisedPublisher } from './supervised-publisher.js';
|
|
3
|
+
import type { SupervisedSubscriber } from './supervised-subscriber.js';
|
|
4
|
+
/** The presence track's default name. */
|
|
5
|
+
export declare const PRESENCE_TRACK = "presence";
|
|
6
|
+
/**
|
|
7
|
+
* How often a presence publisher resends an unchanged document by default. A view calls a member stale after three
|
|
8
|
+
* heartbeats with nothing new (6 s), before the relay withdraws a crashed publisher at its idle timeout (about 10 s).
|
|
9
|
+
*/
|
|
10
|
+
export declare const PRESENCE_HEARTBEAT_MS = 2000;
|
|
11
|
+
/** Options for {@link publishPresence}. */
|
|
12
|
+
export interface PublishPresenceOptions {
|
|
13
|
+
/** The track name. Default {@link PRESENCE_TRACK}; listeners use the same. */
|
|
14
|
+
track?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Resend an unchanged document this long after the last change or resend, in milliseconds: an integer from 100 to
|
|
17
|
+
* 715827882.
|
|
18
|
+
* Default {@link PRESENCE_HEARTBEAT_MS}; listeners use the same.
|
|
19
|
+
*/
|
|
20
|
+
heartbeatMs?: number;
|
|
21
|
+
}
|
|
22
|
+
/** This participant's presence document, served on its supervised publisher. It lasts as long as the publisher. */
|
|
23
|
+
export interface PresencePublisher<T> {
|
|
24
|
+
/** The latest document given. */
|
|
25
|
+
readonly document: T;
|
|
26
|
+
/**
|
|
27
|
+
* Replace the document. It is encoded at once, and a `TypeError` or `RangeError` leaves the current one in place;
|
|
28
|
+
* it is sent only if its JSON differs. Returns at once.
|
|
29
|
+
*/
|
|
30
|
+
update(document: T): void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Publish this participant's presence: register `track` on the supervised publisher and serve `document` as compact
|
|
34
|
+
* UTF-8 JSON, one group per send, on every relay subscription, on every change, and again `heartbeatMs` after the last
|
|
35
|
+
* change or resend, which keeps listeners from calling it stale. A heartbeat with nobody subscribed sends nothing. Presence ends
|
|
36
|
+
* with the publisher: close the publisher to leave. Arguments are checked at once, and a document `JSON.stringify`
|
|
37
|
+
* cannot encode is a `TypeError`, and one over the 16 MiB object limit a `RangeError`, before anything is registered.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* const me = publishPresence(publisher, { name: 'Ada', joinedAt: Date.now() });
|
|
41
|
+
* me.update({ ...me.document, name: 'Ada L.' });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function publishPresence<T>(publisher: Pick<SupervisedPublisher, 'registerTrack' | 'closed'>, document: T, options?: PublishPresenceOptions): PresencePublisher<T>;
|
|
45
|
+
/** One member with a presence document, as a view reports it. Replaced, never changed, when anything in it changes. */
|
|
46
|
+
export interface PresenceMember<T> extends PublishingRoot {
|
|
47
|
+
/** The newest document read from the member. */
|
|
48
|
+
readonly document: T;
|
|
49
|
+
/** `Date.now()` when the view first read the member's document; kept across updates, staleness and rotations. */
|
|
50
|
+
readonly since: number;
|
|
51
|
+
/**
|
|
52
|
+
* No new document for three heartbeats. The count pauses only while the view's own connection is down (the member's
|
|
53
|
+
* want restarted for `connection`), and the next subscription starts a full window; any other restart leaves it
|
|
54
|
+
* running. The member is still announced; most UIs hide it.
|
|
55
|
+
*/
|
|
56
|
+
readonly stale: boolean;
|
|
57
|
+
}
|
|
58
|
+
/** What `onChanged` receives: what changed since the last call, and who is present now. */
|
|
59
|
+
export interface PresenceChange<T> {
|
|
60
|
+
/** Members whose first document arrived. On the first call (`initial`), everyone with one at the load. */
|
|
61
|
+
readonly joined: readonly PresenceMember<T>[];
|
|
62
|
+
/** Reported members that left, as last reported. A member that never had a document leaves unreported. */
|
|
63
|
+
readonly left: readonly PresenceMember<T>[];
|
|
64
|
+
/**
|
|
65
|
+
* Members whose document bytes changed and parsed, even when `parse` returned a value equal to the old one. A stale
|
|
66
|
+
* member with a changed document is in `resumed` too.
|
|
67
|
+
*/
|
|
68
|
+
readonly updated: readonly PresenceMember<T>[];
|
|
69
|
+
/**
|
|
70
|
+
* Members that went stale. On the first call, the members in `joined` that are stale at the load, so a member is
|
|
71
|
+
* always in `stale` before it is in `resumed`.
|
|
72
|
+
*/
|
|
73
|
+
readonly stale: readonly PresenceMember<T>[];
|
|
74
|
+
/** Stale members with a new document. */
|
|
75
|
+
readonly resumed: readonly PresenceMember<T>[];
|
|
76
|
+
/**
|
|
77
|
+
* Every reported member after this change, by id, in the order `publishingRoots()` reported them. The same map as
|
|
78
|
+
* `present`.
|
|
79
|
+
*/
|
|
80
|
+
readonly present: ReadonlyMap<string, PresenceMember<T>>;
|
|
81
|
+
/** True on the first call only: the view has just loaded. */
|
|
82
|
+
readonly initial: boolean;
|
|
83
|
+
}
|
|
84
|
+
/** What a presence view follows. */
|
|
85
|
+
export interface PresenceViewOptions<T> {
|
|
86
|
+
/**
|
|
87
|
+
* The prefix whose members to follow, as for `publishingRoots()`: each member is a root, whose supervised publisher
|
|
88
|
+
* serves `<prefix>/<id>`.
|
|
89
|
+
*/
|
|
90
|
+
prefix: readonly string[];
|
|
91
|
+
/** The track name. Default {@link PRESENCE_TRACK}. */
|
|
92
|
+
track?: string;
|
|
93
|
+
/**
|
|
94
|
+
* The publishers' heartbeat, in milliseconds: an integer from 100 to 715827882. Default
|
|
95
|
+
* {@link PRESENCE_HEARTBEAT_MS}. A member is stale after three heartbeats with no new document.
|
|
96
|
+
*/
|
|
97
|
+
heartbeatMs?: number;
|
|
98
|
+
/**
|
|
99
|
+
* Check a peer's parsed JSON and return its document, or throw. A throw skips that document: the member keeps the
|
|
100
|
+
* one it had, or stays unreported. The view reports refusals nowhere, so log inside `parse` to see them. Without it,
|
|
101
|
+
* documents are the parsed JSON, typed `unknown`.
|
|
102
|
+
*/
|
|
103
|
+
parse?(value: unknown): T;
|
|
104
|
+
}
|
|
105
|
+
/** The callbacks a presence view delivers to. Like `PublishingRootsHandlers`, they must be synchronous. */
|
|
106
|
+
export interface PresenceViewHandlers<T> {
|
|
107
|
+
/**
|
|
108
|
+
* The view loaded, or a member joined, left, changed its document, went stale or resumed. The first call is the
|
|
109
|
+
* load and runs once, after every member present at `publishingRoots()`'s load has a document or after 1 s; every
|
|
110
|
+
* later call names at least one member. One input calls it at most once, with everything that input changed. A
|
|
111
|
+
* member's publisher rotating with the same document, the relay repeating a document, and the subscriber's
|
|
112
|
+
* reconnects and rotations call nothing.
|
|
113
|
+
*/
|
|
114
|
+
onChanged?(change: PresenceChange<T>): void;
|
|
115
|
+
/**
|
|
116
|
+
* The publishing-roots view failed in a way no wait fixes, as `PublishingRootsHandlers.onError` describes. The view
|
|
117
|
+
* is closed. A prefix outside the token's grant is not one: the roster loads empty and this is never called.
|
|
118
|
+
*/
|
|
119
|
+
onError?(error: unknown): void;
|
|
120
|
+
}
|
|
121
|
+
/** A live roster: the members under a prefix with a presence document. Create one with {@link createPresenceView}. */
|
|
122
|
+
export interface PresenceView<T> {
|
|
123
|
+
/** False until the first `onChanged`. Stays true across reconnects. */
|
|
124
|
+
readonly loaded: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Reported members by id; empty until loaded. The same map until the next `onChanged`, so a UI can compare it by
|
|
127
|
+
* identity.
|
|
128
|
+
*/
|
|
129
|
+
readonly present: ReadonlyMap<string, PresenceMember<T>>;
|
|
130
|
+
/**
|
|
131
|
+
* Stop: unwant every member's presence track and close the publishing-roots view. Idempotent; nothing is delivered
|
|
132
|
+
* after it, even when called from `onChanged`. The view also stops on its own, reporting nothing, once the subscriber closes.
|
|
133
|
+
*/
|
|
134
|
+
close(): void;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Follow the presence documents of everyone under a prefix: `subscriber.publishingRoots({ prefix })` plus one want per
|
|
138
|
+
* root that follows the member's supervised publisher to its newest generation. A member is reported once its first
|
|
139
|
+
* readable document arrives, and goes stale after three heartbeats with nothing new (see `PresenceMember.stale`). The
|
|
140
|
+
* view ends with the subscriber. Arguments are checked at once; handlers run from the next turn on. Throws a
|
|
141
|
+
* `RelayError` with code `CLOSED` when the subscriber is already closed.
|
|
142
|
+
*
|
|
143
|
+
* ```ts
|
|
144
|
+
* const room = createPresenceView(subscriber, { prefix: ['room', id], parse: parseProfile }, {
|
|
145
|
+
* onChanged: ({ present }) => render([...present.values()].filter((member) => !member.stale)),
|
|
146
|
+
* });
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export declare function createPresenceView(subscriber: Pick<SupervisedSubscriber, 'publishingRoots' | 'want' | 'unwant' | 'status' | 'closed'>, options: PresenceViewOptions<unknown> & {
|
|
150
|
+
parse?: undefined;
|
|
151
|
+
}, handlers?: PresenceViewHandlers<unknown>): PresenceView<unknown>;
|
|
152
|
+
export declare function createPresenceView<T>(subscriber: Pick<SupervisedSubscriber, 'publishingRoots' | 'want' | 'unwant' | 'status' | 'closed'>, options: PresenceViewOptions<T> & {
|
|
153
|
+
parse(value: unknown): T;
|
|
154
|
+
}, handlers?: PresenceViewHandlers<T>): PresenceView<T>;
|
|
155
|
+
//# sourceMappingURL=presence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presence.d.ts","sourceRoot":"","sources":["../src/presence.ts"],"names":[],"mappings":"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"}
|
package/dist/presence.js
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
import { createDocumentService } from './document-service.js';
|
|
2
|
+
import { synchronousCallback } from './subscription.js';
|
|
3
|
+
import { DEFAULT_PUBLISHER_PRIORITY, payloadBytes, trackName } from './validation.js';
|
|
4
|
+
/** The presence track's default name. */
|
|
5
|
+
export const PRESENCE_TRACK = 'presence';
|
|
6
|
+
/**
|
|
7
|
+
* How often a presence publisher resends an unchanged document by default. A view calls a member stale after three
|
|
8
|
+
* heartbeats with nothing new (6 s), before the relay withdraws a crashed publisher at its idle timeout (about 10 s).
|
|
9
|
+
*/
|
|
10
|
+
export const PRESENCE_HEARTBEAT_MS = 2000;
|
|
11
|
+
/**
|
|
12
|
+
* Presence's Default Publisher Priority and SUBSCRIBER_PRIORITY: between audio (16) and video (96), so heartbeats do
|
|
13
|
+
* not queue behind video on a congested link and turn congestion into false stale reports.
|
|
14
|
+
*/
|
|
15
|
+
const PRESENCE_PRIORITY = 32;
|
|
16
|
+
/** How long a view's first report waits for the documents of the roots publishing when `publishingRoots()` loaded. */
|
|
17
|
+
const PRESENCE_LOAD_LIMIT_MS = 1000;
|
|
18
|
+
const STALE_HEARTBEATS = 3;
|
|
19
|
+
const MIN_HEARTBEAT_MS = 100;
|
|
20
|
+
/** Three heartbeats, the stale window, must fit a timer. */
|
|
21
|
+
const MAX_HEARTBEAT_MS = 715_827_882;
|
|
22
|
+
/**
|
|
23
|
+
* Publish this participant's presence: register `track` on the supervised publisher and serve `document` as compact
|
|
24
|
+
* UTF-8 JSON, one group per send, on every relay subscription, on every change, and again `heartbeatMs` after the last
|
|
25
|
+
* change or resend, which keeps listeners from calling it stale. A heartbeat with nobody subscribed sends nothing. Presence ends
|
|
26
|
+
* with the publisher: close the publisher to leave. Arguments are checked at once, and a document `JSON.stringify`
|
|
27
|
+
* cannot encode is a `TypeError`, and one over the 16 MiB object limit a `RangeError`, before anything is registered.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* const me = publishPresence(publisher, { name: 'Ada', joinedAt: Date.now() });
|
|
31
|
+
* me.update({ ...me.document, name: 'Ada L.' });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function publishPresence(publisher, document, options = {}) {
|
|
35
|
+
if (typeof options !== 'object' || options === null)
|
|
36
|
+
throw new TypeError('options must be an object');
|
|
37
|
+
const track = trackName(options.track ?? PRESENCE_TRACK);
|
|
38
|
+
const heartbeatMs = heartbeatOption(options.heartbeatMs);
|
|
39
|
+
let current = document;
|
|
40
|
+
let bytes = presenceBytes(document);
|
|
41
|
+
const service = createDocumentService(publisher.registerTrack({
|
|
42
|
+
track,
|
|
43
|
+
trackProperties: [{ type: DEFAULT_PUBLISHER_PRIORITY, value: PRESENCE_PRIORITY }],
|
|
44
|
+
}), bytes);
|
|
45
|
+
// The service ends with the track, and the track with the publisher.
|
|
46
|
+
void service.serve().catch(() => { });
|
|
47
|
+
let stopped = false;
|
|
48
|
+
let timer;
|
|
49
|
+
const beat = () => {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
if (stopped)
|
|
52
|
+
return;
|
|
53
|
+
timer = setTimeout(() => {
|
|
54
|
+
service.update(bytes);
|
|
55
|
+
beat();
|
|
56
|
+
}, heartbeatMs);
|
|
57
|
+
};
|
|
58
|
+
beat();
|
|
59
|
+
void publisher.closed.then(() => {
|
|
60
|
+
stopped = true;
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
});
|
|
63
|
+
return {
|
|
64
|
+
get document() {
|
|
65
|
+
return current;
|
|
66
|
+
},
|
|
67
|
+
update(next) {
|
|
68
|
+
const encoded = presenceBytes(next);
|
|
69
|
+
current = next;
|
|
70
|
+
if (sameBytes(encoded, bytes))
|
|
71
|
+
return;
|
|
72
|
+
bytes = encoded;
|
|
73
|
+
service.update(bytes);
|
|
74
|
+
beat();
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export function createPresenceView(subscriber, options, handlers = {}) {
|
|
79
|
+
if (typeof options !== 'object' || options === null)
|
|
80
|
+
throw new TypeError('options must be an object');
|
|
81
|
+
const track = trackName(options.track ?? PRESENCE_TRACK);
|
|
82
|
+
const heartbeatMs = heartbeatOption(options.heartbeatMs);
|
|
83
|
+
const { parse } = options;
|
|
84
|
+
if (parse !== undefined && typeof parse !== 'function')
|
|
85
|
+
throw new TypeError('parse must be a function');
|
|
86
|
+
if (typeof handlers !== 'object' || handlers === null)
|
|
87
|
+
throw new TypeError('presence view handlers must be an object');
|
|
88
|
+
const onChanged = handler('onChanged', handlers.onChanged, handlers);
|
|
89
|
+
const onError = handler('onError', handlers.onError, handlers);
|
|
90
|
+
const ctx = { staleAfterMs: STALE_HEARTBEATS * heartbeatMs, decode: decoder(parse) };
|
|
91
|
+
const view = ++views;
|
|
92
|
+
let joins = 0;
|
|
93
|
+
let phase = { kind: 'loading' };
|
|
94
|
+
const inbox = [];
|
|
95
|
+
let draining = false;
|
|
96
|
+
const wants = new Map();
|
|
97
|
+
let timer;
|
|
98
|
+
let armedFor;
|
|
99
|
+
/** Set by `close()` at once, so a step that `parse` closed from inside delivers nothing either. */
|
|
100
|
+
let closing = false;
|
|
101
|
+
/** The subscriber's `close()` reaches `status` at once and `closed` only a microtask later. */
|
|
102
|
+
const ended = () => closing || subscriber.status === 'closed';
|
|
103
|
+
/** What `onChanged` last delivered; `undefined` until the first. */
|
|
104
|
+
let shown;
|
|
105
|
+
const arm = () => {
|
|
106
|
+
const due = deadline(phase);
|
|
107
|
+
if (due === armedFor)
|
|
108
|
+
return;
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
armedFor = due;
|
|
111
|
+
if (due === undefined)
|
|
112
|
+
return;
|
|
113
|
+
timer = setTimeout(() => {
|
|
114
|
+
armedFor = undefined;
|
|
115
|
+
feed({ kind: 'tick', at: performance.now() });
|
|
116
|
+
}, Math.max(0, due - performance.now()));
|
|
117
|
+
};
|
|
118
|
+
const feed = (input, first = false) => {
|
|
119
|
+
if (first)
|
|
120
|
+
inbox.unshift(input);
|
|
121
|
+
else
|
|
122
|
+
inbox.push(input);
|
|
123
|
+
if (draining)
|
|
124
|
+
return;
|
|
125
|
+
draining = true;
|
|
126
|
+
try {
|
|
127
|
+
for (let next = inbox.shift(); next; next = inbox.shift()) {
|
|
128
|
+
const step = transition(phase, next, ctx);
|
|
129
|
+
phase = step.phase;
|
|
130
|
+
for (const effect of step.effects)
|
|
131
|
+
run(effect);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
draining = false;
|
|
136
|
+
arm();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const open = (member) => {
|
|
140
|
+
const { id } = member;
|
|
141
|
+
const key = `\0presence:${view}:${++joins}:${id}`;
|
|
142
|
+
wants.set(id, key);
|
|
143
|
+
// A late callback from the want of a member that left, even one that has joined again, is not this want's.
|
|
144
|
+
const from = (input) => {
|
|
145
|
+
if (wants.get(id) === key)
|
|
146
|
+
feed(input);
|
|
147
|
+
};
|
|
148
|
+
subscriber.want(key, { namespace: member.namespace, track, generations: true, parameters: { subscriberPriority: PRESENCE_PRIORITY } }, {
|
|
149
|
+
onSubscribed: (_, { namespace }) => from({ kind: 'subscribed', id, generation: generationOf(namespace), at: performance.now() }),
|
|
150
|
+
onSwitched: ({ to }) => from({ kind: 'switched', id, generation: generationOf(to.namespace) }),
|
|
151
|
+
onRestarted: ({ reason }) => from({ kind: 'restarted', id, reason }),
|
|
152
|
+
onObject: ({ status, groupId, payload }) => {
|
|
153
|
+
if (status !== 'normal')
|
|
154
|
+
return;
|
|
155
|
+
from({ kind: 'object', id, group: groupId, payload, at: performance.now(), date: Date.now() });
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
const run = (effect) => {
|
|
160
|
+
switch (effect.kind) {
|
|
161
|
+
case 'want':
|
|
162
|
+
if (!ended())
|
|
163
|
+
open(effect.member);
|
|
164
|
+
return;
|
|
165
|
+
case 'unwant': {
|
|
166
|
+
const key = wants.get(effect.id);
|
|
167
|
+
wants.delete(effect.id);
|
|
168
|
+
if (key !== undefined)
|
|
169
|
+
subscriber.unwant(key);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
case 'emit':
|
|
173
|
+
if (ended())
|
|
174
|
+
return;
|
|
175
|
+
shown = effect.change.present;
|
|
176
|
+
deliver('onChanged', () => onChanged?.(effect.change));
|
|
177
|
+
return;
|
|
178
|
+
case 'error':
|
|
179
|
+
if (!ended())
|
|
180
|
+
deliver('onError', () => onError?.(effect.error));
|
|
181
|
+
return;
|
|
182
|
+
case 'close-roots':
|
|
183
|
+
roots.close();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
const roots = subscriber.publishingRoots({ prefix: options.prefix }, {
|
|
188
|
+
onChanged: (change) => feed({ kind: 'roots', change, at: performance.now() }),
|
|
189
|
+
onError: (error) => feed({ kind: 'roots-error', error }),
|
|
190
|
+
});
|
|
191
|
+
// The supervisor closes the publishing-roots view and drops the wants without calling either, so the view ends
|
|
192
|
+
// itself.
|
|
193
|
+
let unsubscribe = () => { };
|
|
194
|
+
const stop = () => {
|
|
195
|
+
closing = true;
|
|
196
|
+
unsubscribe();
|
|
197
|
+
feed({ kind: 'close' }, true);
|
|
198
|
+
};
|
|
199
|
+
unsubscribe = whenClosed(subscriber, stop);
|
|
200
|
+
return {
|
|
201
|
+
get loaded() {
|
|
202
|
+
return shown !== undefined;
|
|
203
|
+
},
|
|
204
|
+
get present() {
|
|
205
|
+
return shown ?? NOBODY;
|
|
206
|
+
},
|
|
207
|
+
close: stop,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
let views = 0;
|
|
211
|
+
/** One `closed` listener per subscriber, so a closed view is not kept alive until its subscriber closes. */
|
|
212
|
+
const stops = new WeakMap();
|
|
213
|
+
function whenClosed(subscriber, stop) {
|
|
214
|
+
const pending = stops.get(subscriber) ?? listen(subscriber);
|
|
215
|
+
pending.add(stop);
|
|
216
|
+
return () => pending.delete(stop);
|
|
217
|
+
}
|
|
218
|
+
/** Its own function, so the listener's closure holds the set and no view's `stop`. */
|
|
219
|
+
function listen(subscriber) {
|
|
220
|
+
const pending = new Set();
|
|
221
|
+
stops.set(subscriber, pending);
|
|
222
|
+
void subscriber.closed.then(() => {
|
|
223
|
+
for (const stop of [...pending])
|
|
224
|
+
stop();
|
|
225
|
+
});
|
|
226
|
+
return pending;
|
|
227
|
+
}
|
|
228
|
+
function handler(name, callback, receiver) {
|
|
229
|
+
if (callback === undefined)
|
|
230
|
+
return undefined;
|
|
231
|
+
if (typeof callback !== 'function')
|
|
232
|
+
throw new TypeError(`${name} must be a function`);
|
|
233
|
+
return synchronousCallback(name, callback, receiver);
|
|
234
|
+
}
|
|
235
|
+
function deliver(name, call) {
|
|
236
|
+
try {
|
|
237
|
+
call();
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
// biome-ignore lint/suspicious/noConsole: the application's callback failed and has no caller to report to
|
|
241
|
+
console.error(new Error(`${name} threw; the presence view carries on`, { cause: error }));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function heartbeatOption(value) {
|
|
245
|
+
if (value === undefined)
|
|
246
|
+
return PRESENCE_HEARTBEAT_MS;
|
|
247
|
+
if (typeof value !== 'number')
|
|
248
|
+
throw new TypeError('heartbeatMs must be a number of milliseconds');
|
|
249
|
+
if (!Number.isInteger(value) || value < MIN_HEARTBEAT_MS || value > MAX_HEARTBEAT_MS)
|
|
250
|
+
throw new RangeError(`heartbeatMs must be an integer from ${MIN_HEARTBEAT_MS} to ${MAX_HEARTBEAT_MS} milliseconds`);
|
|
251
|
+
return value;
|
|
252
|
+
}
|
|
253
|
+
const ENCODER = new TextEncoder();
|
|
254
|
+
const DECODER = new TextDecoder('utf-8', { fatal: true });
|
|
255
|
+
function presenceBytes(document) {
|
|
256
|
+
const json = JSON.stringify(document);
|
|
257
|
+
if (typeof json !== 'string')
|
|
258
|
+
throw new TypeError('A presence document must be a value JSON.stringify can encode');
|
|
259
|
+
return payloadBytes(ENCODER.encode(json));
|
|
260
|
+
}
|
|
261
|
+
function decoder(parse) {
|
|
262
|
+
return (payload) => {
|
|
263
|
+
try {
|
|
264
|
+
const value = JSON.parse(DECODER.decode(payload));
|
|
265
|
+
return { document: parse ? parse(value) : value };
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function sameBytes(a, b) {
|
|
273
|
+
return a.length === b.length && a.every((byte, index) => byte === b[index]);
|
|
274
|
+
}
|
|
275
|
+
function generationOf(namespace) {
|
|
276
|
+
return namespace[namespace.length - 1];
|
|
277
|
+
}
|
|
278
|
+
const NOBODY = new Map();
|
|
279
|
+
const PAUSED = { kind: 'paused' };
|
|
280
|
+
const stay = (phase) => ({ phase, effects: [] });
|
|
281
|
+
const quiet = (state) => ({ state, reports: [] });
|
|
282
|
+
function entryOf(member, document, since, stale) {
|
|
283
|
+
return Object.freeze({ id: member.id, namespace: member.namespace, document, since, stale });
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* The key of an object in group `group` of the current subscription, when it is newer than what `state` counted. A
|
|
287
|
+
* replay repeats the last key and an out-of-order group is smaller, so neither counts. Generations compare as strings,
|
|
288
|
+
* which sort by start time: a live member ignores an older, retiring generation so its document never regresses, and a
|
|
289
|
+
* stale member takes any other generation, since its publisher came back, even on a clock behind.
|
|
290
|
+
*/
|
|
291
|
+
function freshKey(state, group) {
|
|
292
|
+
const { route, last } = state;
|
|
293
|
+
if (route === undefined)
|
|
294
|
+
return undefined;
|
|
295
|
+
const key = { generation: route, group };
|
|
296
|
+
if (!last)
|
|
297
|
+
return key;
|
|
298
|
+
if (route === last.generation)
|
|
299
|
+
return group > last.group ? key : undefined;
|
|
300
|
+
return state.kind === 'stale' || route > last.generation ? key : undefined;
|
|
301
|
+
}
|
|
302
|
+
function heard(state, input, ctx) {
|
|
303
|
+
const last = freshKey(state, input.group);
|
|
304
|
+
if (!last)
|
|
305
|
+
return quiet(state);
|
|
306
|
+
const clock = { kind: 'running', dueAt: input.at + ctx.staleAfterMs };
|
|
307
|
+
if (state.kind === 'awaiting') {
|
|
308
|
+
const read = ctx.decode(input.payload);
|
|
309
|
+
if (!read)
|
|
310
|
+
return quiet({ ...state, last });
|
|
311
|
+
const entry = entryOf(state.member, read.document, input.date, false);
|
|
312
|
+
return {
|
|
313
|
+
state: { kind: 'live', route: state.route, last, bytes: input.payload, entry, clock },
|
|
314
|
+
reports: [{ event: 'joined', member: entry }],
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
const read = sameBytes(state.bytes, input.payload) ? undefined : ctx.decode(input.payload);
|
|
318
|
+
if (state.kind === 'live' && !read)
|
|
319
|
+
return quiet({ ...state, last, clock });
|
|
320
|
+
const { entry: was } = state;
|
|
321
|
+
const entry = entryOf(was, read ? read.document : was.document, was.since, false);
|
|
322
|
+
const events = state.kind === 'stale' ? ['resumed'] : [];
|
|
323
|
+
if (read)
|
|
324
|
+
events.push('updated');
|
|
325
|
+
return {
|
|
326
|
+
state: { kind: 'live', route: state.route, last, bytes: read ? input.payload : state.bytes, entry, clock },
|
|
327
|
+
reports: events.map((event) => ({ event, member: entry })),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function memberStep(state, input, ctx) {
|
|
331
|
+
switch (input.kind) {
|
|
332
|
+
case 'subscribed': {
|
|
333
|
+
const routed = { ...state, route: input.generation };
|
|
334
|
+
if (routed.kind !== 'live' || routed.clock.kind === 'running')
|
|
335
|
+
return quiet(routed);
|
|
336
|
+
return quiet({ ...routed, clock: { kind: 'running', dueAt: input.at + ctx.staleAfterMs } });
|
|
337
|
+
}
|
|
338
|
+
case 'switched':
|
|
339
|
+
return quiet({ ...state, route: input.generation });
|
|
340
|
+
case 'restarted':
|
|
341
|
+
if (state.kind !== 'live' || input.reason !== 'connection')
|
|
342
|
+
return quiet(state);
|
|
343
|
+
return quiet({ ...state, clock: PAUSED });
|
|
344
|
+
case 'object':
|
|
345
|
+
return heard(state, input, ctx);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
function due(state, at) {
|
|
349
|
+
if (state.kind !== 'live' || state.clock.kind !== 'running' || at < state.clock.dueAt)
|
|
350
|
+
return quiet(state);
|
|
351
|
+
const { route, last, bytes, entry: was } = state;
|
|
352
|
+
const entry = entryOf(was, was.document, was.since, true);
|
|
353
|
+
return { state: { kind: 'stale', route, last, bytes, entry }, reports: [{ event: 'stale', member: entry }] };
|
|
354
|
+
}
|
|
355
|
+
function follow(states, change) {
|
|
356
|
+
const next = new Map(states);
|
|
357
|
+
const reports = [];
|
|
358
|
+
const effects = [];
|
|
359
|
+
for (const { id } of change.removed) {
|
|
360
|
+
const state = next.get(id);
|
|
361
|
+
if (!state)
|
|
362
|
+
continue;
|
|
363
|
+
next.delete(id);
|
|
364
|
+
effects.push({ kind: 'unwant', id });
|
|
365
|
+
if (state.kind !== 'awaiting')
|
|
366
|
+
reports.push({ event: 'left', member: state.entry });
|
|
367
|
+
}
|
|
368
|
+
for (const member of change.added) {
|
|
369
|
+
if (next.has(member.id))
|
|
370
|
+
continue;
|
|
371
|
+
next.set(member.id, { kind: 'awaiting', member, route: undefined, last: undefined });
|
|
372
|
+
effects.push({ kind: 'want', member });
|
|
373
|
+
}
|
|
374
|
+
return { states: next, reports, effects };
|
|
375
|
+
}
|
|
376
|
+
function apply(states, input, ctx) {
|
|
377
|
+
if (input.kind === 'roots')
|
|
378
|
+
return follow(states, input.change);
|
|
379
|
+
if (input.kind === 'tick') {
|
|
380
|
+
const next = new Map(states);
|
|
381
|
+
const reports = [];
|
|
382
|
+
for (const [id, state] of states) {
|
|
383
|
+
const step = due(state, input.at);
|
|
384
|
+
next.set(id, step.state);
|
|
385
|
+
reports.push(...step.reports);
|
|
386
|
+
}
|
|
387
|
+
return { states: reports.length > 0 ? next : states, reports, effects: [] };
|
|
388
|
+
}
|
|
389
|
+
const state = states.get(input.id);
|
|
390
|
+
if (!state)
|
|
391
|
+
return { states, reports: [], effects: [] };
|
|
392
|
+
const step = memberStep(state, input, ctx);
|
|
393
|
+
if (step.state === state)
|
|
394
|
+
return { states, reports: [], effects: [] };
|
|
395
|
+
return { states: new Map(states).set(input.id, step.state), reports: step.reports, effects: [] };
|
|
396
|
+
}
|
|
397
|
+
function reported(states) {
|
|
398
|
+
const present = new Map();
|
|
399
|
+
for (const [id, state] of states)
|
|
400
|
+
if (state.kind !== 'awaiting')
|
|
401
|
+
present.set(id, state.entry);
|
|
402
|
+
return present;
|
|
403
|
+
}
|
|
404
|
+
function changeOf(reports, present, initial) {
|
|
405
|
+
const of = (event) => reports.filter((report) => report.event === event).map((report) => report.member);
|
|
406
|
+
return {
|
|
407
|
+
joined: of('joined'),
|
|
408
|
+
left: of('left'),
|
|
409
|
+
updated: of('updated'),
|
|
410
|
+
stale: of('stale'),
|
|
411
|
+
resumed: of('resumed'),
|
|
412
|
+
present,
|
|
413
|
+
initial,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
function gather(phase, states, at, effects) {
|
|
417
|
+
const waiting = new Set([...phase.waiting].filter((id) => states.get(id)?.kind === 'awaiting'));
|
|
418
|
+
if (waiting.size > 0 && (at === undefined || at < phase.loadBy)) {
|
|
419
|
+
return { phase: { ...phase, states, waiting }, effects };
|
|
420
|
+
}
|
|
421
|
+
const present = reported(states);
|
|
422
|
+
const reports = [...present.values()].flatMap((member) => [
|
|
423
|
+
{ event: 'joined', member },
|
|
424
|
+
...(member.stale ? [{ event: 'stale', member }] : []),
|
|
425
|
+
]);
|
|
426
|
+
const loaded = changeOf(reports, present, true);
|
|
427
|
+
return { phase: { kind: 'live', states }, effects: [...effects, { kind: 'emit', change: loaded }] };
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* The view's transition. Pure: times arrive on inputs and effects leave as data. The emit comes last so a `close()`
|
|
431
|
+
* from `onChanged` follows every other effect of the step.
|
|
432
|
+
*/
|
|
433
|
+
function transition(phase, input, ctx) {
|
|
434
|
+
if (phase.kind === 'closed')
|
|
435
|
+
return stay(phase);
|
|
436
|
+
if (input.kind === 'close' || input.kind === 'roots-error') {
|
|
437
|
+
const states = phase.kind === 'loading' ? NOBODY : phase.states;
|
|
438
|
+
return {
|
|
439
|
+
phase: { kind: 'closed' },
|
|
440
|
+
effects: [
|
|
441
|
+
...[...states.keys()].map((id) => ({ kind: 'unwant', id })),
|
|
442
|
+
{ kind: 'close-roots' },
|
|
443
|
+
...(input.kind === 'roots-error' ? [{ kind: 'error', error: input.error }] : []),
|
|
444
|
+
],
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
if (phase.kind === 'loading') {
|
|
448
|
+
if (input.kind !== 'roots' || !input.change.initial)
|
|
449
|
+
return stay(phase);
|
|
450
|
+
const { states, effects } = follow(NOBODY, input.change);
|
|
451
|
+
const gathering = {
|
|
452
|
+
kind: 'gathering',
|
|
453
|
+
states,
|
|
454
|
+
waiting: new Set(states.keys()),
|
|
455
|
+
loadBy: input.at + PRESENCE_LOAD_LIMIT_MS,
|
|
456
|
+
};
|
|
457
|
+
return gather(gathering, states, undefined, effects);
|
|
458
|
+
}
|
|
459
|
+
const { states, reports, effects } = apply(phase.states, input, ctx);
|
|
460
|
+
if (phase.kind === 'gathering') {
|
|
461
|
+
if (states === phase.states && input.kind !== 'tick')
|
|
462
|
+
return stay(phase);
|
|
463
|
+
return gather(phase, states, input.kind === 'tick' ? input.at : undefined, effects);
|
|
464
|
+
}
|
|
465
|
+
if (reports.length === 0)
|
|
466
|
+
return states === phase.states ? stay(phase) : { phase: { ...phase, states }, effects };
|
|
467
|
+
return {
|
|
468
|
+
phase: { kind: 'live', states },
|
|
469
|
+
effects: [...effects, { kind: 'emit', change: changeOf(reports, reported(states), false) }],
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
/** When the view's timer is next due: the load limit while gathering, and every running stale clock. */
|
|
473
|
+
function deadline(phase) {
|
|
474
|
+
if (phase.kind !== 'gathering' && phase.kind !== 'live')
|
|
475
|
+
return undefined;
|
|
476
|
+
let next = phase.kind === 'gathering' ? phase.loadBy : undefined;
|
|
477
|
+
for (const state of phase.states.values()) {
|
|
478
|
+
if (state.kind !== 'live' || state.clock.kind !== 'running')
|
|
479
|
+
continue;
|
|
480
|
+
if (next === undefined || state.clock.dueAt < next)
|
|
481
|
+
next = state.clock.dueAt;
|
|
482
|
+
}
|
|
483
|
+
return next;
|
|
484
|
+
}
|
|
485
|
+
//# sourceMappingURL=presence.js.map
|