oysterun 1.6.0-beta.30 → 1.6.0-beta.32
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/dev/client/connectivity/background-sync-gate.d.mts +12 -0
- package/dev/client/connectivity/background-sync-gate.mjs +146 -0
- package/dev/client/connectivity/matrix-page-startup-cache.d.mts +8 -0
- package/dev/client/connectivity/matrix-page-startup-cache.mjs +81 -0
- package/dev/client/connectivity/warm-session-clients.d.mts +26 -0
- package/dev/client/connectivity/warm-session-clients.mjs +102 -0
- package/dev/client/web/index.html +3703 -1571
- package/dev/client/web/notification-sw.js +45 -1
- package/dev/client/web-chat/dist/assets/{ReactPrism-CO4xV6l8.js → ReactPrism-Co-6SgXR.js} +1 -1
- package/dev/client/web-chat/dist/assets/{ReactPrism-CO4xV6l8.js.map → ReactPrism-Co-6SgXR.js.map} +1 -1
- package/dev/client/web-chat/dist/assets/SessionsSetupApp-BmHVrH9Z.js +6137 -0
- package/dev/client/web-chat/dist/assets/{SessionsSetupApp-DDYg7Dal.js.map → SessionsSetupApp-BmHVrH9Z.js.map} +1 -1
- package/dev/client/web-chat/dist/assets/SessionsSetupApp-CsC74INd.css +1 -0
- package/dev/client/web-chat/dist/assets/TerminalSurface-BiFgCZdD.css +32 -0
- package/dev/client/web-chat/dist/assets/TerminalSurface-MUU0Aa9O.js +55 -0
- package/dev/client/web-chat/dist/assets/TerminalSurface-MUU0Aa9O.js.map +1 -0
- package/dev/client/web-chat/dist/assets/{index-D2KjgMpD.js → index-BApEAFLA.js} +1 -1
- package/dev/client/web-chat/dist/assets/{index-D2KjgMpD.js.map → index-BApEAFLA.js.map} +1 -1
- package/dev/client/web-chat/dist/assets/{index-D25Gaecs.js → index-BS_XDtVp.js} +2 -2
- package/dev/client/web-chat/dist/assets/{index-D25Gaecs.js.map → index-BS_XDtVp.js.map} +1 -1
- package/dev/client/web-chat/dist/assets/{index-BQkeZ7W7.js → index-CT1q3te6.js} +2 -2
- package/dev/client/web-chat/dist/assets/{index-BQkeZ7W7.js.map → index-CT1q3te6.js.map} +1 -1
- package/dev/client/web-chat/dist/assets/{matrix-entry-CqyPPDxc.js → matrix-entry-BIVffqZK.js} +171 -171
- package/dev/client/web-chat/dist/assets/matrix-entry-BIVffqZK.js.map +1 -0
- package/dev/client/web-chat/dist/assets/{matrix-entry-B4wx48bc.css → matrix-entry-DftnSt6v.css} +1 -1
- package/dev/client/web-chat/dist/assets/{pdf-CCaMtRCr.js → pdf-BEMt3hbm.js} +5 -5
- package/dev/client/web-chat/dist/assets/pdf-BEMt3hbm.js.map +1 -0
- package/dev/client/web-chat/dist/assets/{web-Cmxm9lj7.js → web-BgmlWJdG.js} +1 -1
- package/dev/client/web-chat/dist/assets/{web-Cmxm9lj7.js.map → web-BgmlWJdG.js.map} +1 -1
- package/dev/client/web-chat/dist/assets/{web-PuV1Famk.js → web-igWDSrBQ.js} +1 -1
- package/dev/client/web-chat/dist/assets/{web-PuV1Famk.js.map → web-igWDSrBQ.js.map} +1 -1
- package/dev/client/web-chat/dist/index.html +1 -1
- package/dev/client/web-chat/dist/public/sound/notification.ogg +0 -0
- package/dev/client/web-chat/dist/sw.js +1 -1
- package/dev/client/web-chat/dist/sw.js.map +1 -1
- package/docs/routec/dev/host_update_runtime_identity.md +36 -0
- package/docs/routec/dev/pagination_doc.md +14 -0
- package/host-service/apns-complete-message-dispatcher.mjs +10 -154
- package/host-service/assets/product-skills/Oysterun/modules/oysterun-session-chat/SKILL.md +15 -0
- package/host-service/assets/product-skills/Oysterun/modules/oysterun-sessions/SKILL.md +3 -2
- package/host-service/assets/product-skills/oysterun-session-chat/SKILL.md +15 -0
- package/host-service/assets/product-skills/oysterun-sessions/SKILL.md +3 -2
- package/host-service/cli/run-staged-lifecycle-recovery.mjs +7 -0
- package/host-service/host-lifecycle-recovery.mjs +9 -1
- package/host-service/lifecycle-compat-preload.mjs +1 -1
- package/host-service/lifecycle-compatibility.mjs +1 -1
- package/host-service/lifecycle-continuity-assets.mjs +53 -1
- package/host-service/lifecycle-continuity-projection.mjs +9 -3
- package/host-service/lifecycle-continuity-runner.mjs +34 -2
- package/host-service/lifecycle-continuity.mjs +55 -13
- package/host-service/lifecycle-user-path.mjs +1 -1
- package/host-service/mail-notification-dispatcher.mjs +3 -0
- package/host-service/notification-sound.mjs +6 -0
- package/host-service/package.json +1 -1
- package/host-service/routec-matrix-sqlite-store.mjs +34 -3
- package/host-service/scheduler-service.mjs +0 -91
- package/host-service/server.mjs +233 -178
- package/host-service/session-loop-definitions.mjs +0 -78
- package/host-service/session-tags.mjs +110 -18
- package/host-service/sqlite-schema-compatibility.mjs +317 -0
- package/host-service/storage-service-binding.mjs +1 -1
- package/host-service/terminal-session-manager.mjs +362 -253
- package/package.json +8 -1
- package/tool_scripts/oysterun_continuity_transaction.mjs +59 -15
- package/tool_scripts/oysterun_npm_package_manifest.mjs +1 -0
- package/tool_scripts/oysterun_service_common.sh +17 -8
- package/tool_scripts/oysterun_update_restart_runner.mjs +62 -5
- package/dev/client/web-chat/dist/assets/SessionsSetupApp-DDYg7Dal.js +0 -6308
- package/dev/client/web-chat/dist/assets/SessionsSetupApp-ovvzywbc.css +0 -1
- package/dev/client/web-chat/dist/assets/matrix-entry-CqyPPDxc.js.map +0 -1
- package/dev/client/web-chat/dist/assets/pdf-CCaMtRCr.js.map +0 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const DIRECT_BACKGROUND_SYNC_SLOTS: number;
|
|
2
|
+
|
|
3
|
+
export type BackgroundSyncGateClient = {
|
|
4
|
+
wrapFetch(fetchFn: typeof fetch): typeof fetch;
|
|
5
|
+
setBackground(value: boolean): void;
|
|
6
|
+
isBehind(): boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function createBackgroundSyncGate(input?: { maxConcurrent?: number }): {
|
|
10
|
+
client(): BackgroundSyncGateClient;
|
|
11
|
+
status(): { active: number; waiting: number; maxConcurrent: number };
|
|
12
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// P408: warm (background) Session clients keep a /sync long-poll open. On a
|
|
2
|
+
// Direct Host address (plain HTTP/1.1 from the browser) Chrome opens at most 6
|
|
3
|
+
// connections per origin, so 5 warm clients plus the shown one would hold all
|
|
4
|
+
// of them for up to 30 s and every other request of the page (sending, the
|
|
5
|
+
// notification sound, Host calls) would wait. Background /sync requests
|
|
6
|
+
// therefore take turns in two slots there; the shown Session's client is
|
|
7
|
+
// never held back. A background client waiting for a slot is "behind": when the
|
|
8
|
+
// page switches to it, its request goes at once and the switch waits for that
|
|
9
|
+
// one answer. A client that is parked while its own long-poll is open (it was
|
|
10
|
+
// the shown Session) gets that long-poll ended at once with "nothing new since
|
|
11
|
+
// <its own since token>", so it frees its connection instead of holding it for
|
|
12
|
+
// up to 30 s; its next request takes its background turn and misses nothing.
|
|
13
|
+
// The Managed transport (WebRTC) has no such limit.
|
|
14
|
+
|
|
15
|
+
const MATRIX_SYNC_PATH = /\/_matrix\/client\/[^/]+\/sync(?:\?|$)/;
|
|
16
|
+
|
|
17
|
+
// A waiting request goes as an immediate catch-up when its Session is shown:
|
|
18
|
+
// with its long-poll timeout the Host would hold it until the next event.
|
|
19
|
+
function withoutLongPoll(input) {
|
|
20
|
+
if (typeof input === "string") return input.replace(/([?&]timeout=)\d+/, "$10");
|
|
21
|
+
if (input instanceof URL) {
|
|
22
|
+
const url = new URL(input.href);
|
|
23
|
+
if (url.searchParams.has("timeout")) url.searchParams.set("timeout", "0");
|
|
24
|
+
return url;
|
|
25
|
+
}
|
|
26
|
+
throw new Error("Background sync request has an unsupported URL form.");
|
|
27
|
+
}
|
|
28
|
+
export const DIRECT_BACKGROUND_SYNC_SLOTS = 2;
|
|
29
|
+
|
|
30
|
+
export function createBackgroundSyncGate({ maxConcurrent = DIRECT_BACKGROUND_SYNC_SLOTS } = {}) {
|
|
31
|
+
if (!(maxConcurrent === Infinity || (Number.isInteger(maxConcurrent) && maxConcurrent > 0))) {
|
|
32
|
+
throw new Error("Background sync gate needs a positive slot count.");
|
|
33
|
+
}
|
|
34
|
+
let active = 0;
|
|
35
|
+
const waiting = [];
|
|
36
|
+
|
|
37
|
+
const releaseSlot = () => {
|
|
38
|
+
active -= 1;
|
|
39
|
+
while (active < maxConcurrent && waiting.length) {
|
|
40
|
+
const next = waiting.shift();
|
|
41
|
+
active += 1;
|
|
42
|
+
next.start(true);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** One Matrix client's view of the gate. */
|
|
47
|
+
function client() {
|
|
48
|
+
let background = false;
|
|
49
|
+
let queued = null;
|
|
50
|
+
let foregroundPoll = null;
|
|
51
|
+
|
|
52
|
+
// The shown Session's long-poll (Direct only), which parking can end early.
|
|
53
|
+
function foregroundLongPoll(fetchFn, input, init, since) {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
const controller = new AbortController();
|
|
56
|
+
const outer = init?.signal;
|
|
57
|
+
const onOuterAbort = () => controller.abort(outer.reason);
|
|
58
|
+
if (outer?.aborted) controller.abort(outer.reason);
|
|
59
|
+
outer?.addEventListener?.("abort", onOuterAbort, { once: true });
|
|
60
|
+
let settled = false;
|
|
61
|
+
const settle = (finish) => {
|
|
62
|
+
if (settled) return;
|
|
63
|
+
settled = true;
|
|
64
|
+
if (foregroundPoll === entry) foregroundPoll = null;
|
|
65
|
+
outer?.removeEventListener?.("abort", onOuterAbort);
|
|
66
|
+
finish();
|
|
67
|
+
};
|
|
68
|
+
const entry = {
|
|
69
|
+
endNow() {
|
|
70
|
+
settle(() => {
|
|
71
|
+
controller.abort();
|
|
72
|
+
resolve(
|
|
73
|
+
new Response(
|
|
74
|
+
JSON.stringify({ next_batch: since, rooms: { join: {}, invite: {}, leave: {} } }),
|
|
75
|
+
{ status: 200, headers: { "content-type": "application/json" } }
|
|
76
|
+
)
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
foregroundPoll = entry;
|
|
82
|
+
Promise.resolve()
|
|
83
|
+
.then(() => fetchFn(input, { ...init, signal: controller.signal }))
|
|
84
|
+
.then(
|
|
85
|
+
(response) => settle(() => resolve(response)),
|
|
86
|
+
(error) => settle(() => reject(error))
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function wrapFetch(fetchFn) {
|
|
92
|
+
return (input, init) => {
|
|
93
|
+
const url = typeof input === "string" ? input : input?.url || String(input);
|
|
94
|
+
if (!MATRIX_SYNC_PATH.test(url)) return fetchFn(input, init);
|
|
95
|
+
if (!background) {
|
|
96
|
+
const since = maxConcurrent === Infinity ? null : new URL(String(url), "http://x").searchParams.get("since");
|
|
97
|
+
return since ? foregroundLongPoll(fetchFn, input, init, since) : fetchFn(input, init);
|
|
98
|
+
}
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
const run = (slotted, promoted = false) => {
|
|
101
|
+
queued = null;
|
|
102
|
+
Promise.resolve()
|
|
103
|
+
.then(() => fetchFn(promoted ? withoutLongPoll(input) : input, init))
|
|
104
|
+
.then(resolve, reject)
|
|
105
|
+
.finally(() => {
|
|
106
|
+
if (slotted) releaseSlot();
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
if (active < maxConcurrent) {
|
|
110
|
+
active += 1;
|
|
111
|
+
run(true);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
queued = { start: run };
|
|
115
|
+
waiting.push(queued);
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
wrapFetch,
|
|
122
|
+
/** Parked: its /sync waits for a slot. Shown again: a waiting request goes at once. */
|
|
123
|
+
setBackground(value) {
|
|
124
|
+
background = value === true;
|
|
125
|
+
if (background) foregroundPoll?.endNow();
|
|
126
|
+
if (!background && queued) {
|
|
127
|
+
const entry = queued;
|
|
128
|
+
const index = waiting.indexOf(entry);
|
|
129
|
+
if (index >= 0) waiting.splice(index, 1);
|
|
130
|
+
entry.start(false, true);
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
/** A background client whose /sync is waiting for a slot has not seen the latest events. */
|
|
134
|
+
isBehind() {
|
|
135
|
+
return Boolean(queued);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
client,
|
|
142
|
+
status() {
|
|
143
|
+
return { active, waiting: waiting.length, maxConcurrent };
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const PAGE_STARTUP_GET_PATHS: readonly string[];
|
|
2
|
+
|
|
3
|
+
export type MatrixPageStartupCache = {
|
|
4
|
+
startupFetch(fetchFn: typeof fetch): typeof fetch;
|
|
5
|
+
seedSyncFilter(mx: unknown, userId: string, FilterClass: unknown): boolean;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export function createMatrixPageStartupCache(): MatrixPageStartupCache;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// P408 requirements 1-2: a Chat page starts one Matrix client per Session it
|
|
2
|
+
// opens. The answers a client needs before its first sync are the same for
|
|
3
|
+
// every Session of the page, so later clients reuse them instead of asking
|
|
4
|
+
// again:
|
|
5
|
+
// - GET /versions and the Host's push rules (a fixed no-op set) are fetched
|
|
6
|
+
// once per page and replayed;
|
|
7
|
+
// - the sync filter is created once per page. The Host facade answers every
|
|
8
|
+
// Session with the same filter id, so a later client gets the page's filter
|
|
9
|
+
// in its store and matrix-js-sdk uses it without the GET/POST round trips.
|
|
10
|
+
// The definition is the one the first client POSTed (matrix-js-sdk later
|
|
11
|
+
// changes its own stored copy in place for the initial sync).
|
|
12
|
+
|
|
13
|
+
export const PAGE_STARTUP_GET_PATHS = Object.freeze([
|
|
14
|
+
"/_matrix/client/versions",
|
|
15
|
+
"/_matrix/client/v3/pushrules/",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
const syncFilterName = (userId) => `FILTER_SYNC_${userId}`;
|
|
19
|
+
const FILTER_CREATE_PATH = /^\/_matrix\/client\/v3\/user\/[^/]+\/filter$/;
|
|
20
|
+
|
|
21
|
+
export function createMatrixPageStartupCache() {
|
|
22
|
+
const responses = new Map();
|
|
23
|
+
let syncFilter;
|
|
24
|
+
|
|
25
|
+
function startupFetch(fetchFn) {
|
|
26
|
+
return (input, init) => {
|
|
27
|
+
const method = String(init?.method ?? "GET").toUpperCase();
|
|
28
|
+
const url = new URL(typeof input === "string" ? input : input?.url ?? String(input));
|
|
29
|
+
if (method === "POST" && FILTER_CREATE_PATH.test(url.pathname) && !syncFilter) {
|
|
30
|
+
return Promise.resolve(fetchFn(input, init)).then(async (response) => {
|
|
31
|
+
if (response.ok && typeof init?.body === "string") {
|
|
32
|
+
const { filter_id: filterId } = await response.clone().json();
|
|
33
|
+
if (typeof filterId === "string" && filterId) {
|
|
34
|
+
syncFilter = { filterId, definition: JSON.parse(init.body) };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return response;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (method !== "GET" || !PAGE_STARTUP_GET_PATHS.includes(url.pathname)) {
|
|
41
|
+
return fetchFn(input, init);
|
|
42
|
+
}
|
|
43
|
+
const key = `${url.origin}${url.pathname}`;
|
|
44
|
+
let recorded = responses.get(key);
|
|
45
|
+
if (!recorded) {
|
|
46
|
+
recorded = Promise.resolve(fetchFn(input, init)).then(async (response) => {
|
|
47
|
+
const headers = [];
|
|
48
|
+
response.headers.forEach((value, name) => headers.push([name, value]));
|
|
49
|
+
const snapshot = {
|
|
50
|
+
status: response.status,
|
|
51
|
+
statusText: response.statusText,
|
|
52
|
+
headers,
|
|
53
|
+
body: await response.text(),
|
|
54
|
+
};
|
|
55
|
+
// Only a successful answer is reused; a failure is asked again.
|
|
56
|
+
if (!response.ok) responses.delete(key);
|
|
57
|
+
return snapshot;
|
|
58
|
+
});
|
|
59
|
+
recorded.catch(() => responses.delete(key));
|
|
60
|
+
responses.set(key, recorded);
|
|
61
|
+
}
|
|
62
|
+
return recorded.then(
|
|
63
|
+
({ status, statusText, headers, body }) =>
|
|
64
|
+
new Response(body, { status, statusText, headers })
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Before startClient: give the client the page's sync filter (if one was created). */
|
|
70
|
+
function seedSyncFilter(mx, userId, FilterClass) {
|
|
71
|
+
if (!syncFilter) return false;
|
|
72
|
+
mx.store.setFilterIdByName(syncFilterName(userId), syncFilter.filterId);
|
|
73
|
+
// A copy each time: matrix-js-sdk changes a stored filter's definition in place.
|
|
74
|
+
mx.store.storeFilter(
|
|
75
|
+
FilterClass.fromJson(userId, syncFilter.filterId, structuredClone(syncFilter.definition))
|
|
76
|
+
);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return { startupFetch, seedSyncFilter };
|
|
81
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const WARM_SESSION_CLIENT_LIMIT: number;
|
|
2
|
+
|
|
3
|
+
export type WarmSessionClientEntry<T> = T & { sessionId: string; parkedAt: number };
|
|
4
|
+
|
|
5
|
+
export type WarmSessionClientPool<T> = {
|
|
6
|
+
park(sessionId: string, value: T): void;
|
|
7
|
+
take(sessionId: string): WarmSessionClientEntry<T> | undefined;
|
|
8
|
+
drop(sessionId: string, reason: string): boolean;
|
|
9
|
+
retainLive(liveSessionIds: Iterable<string>, reason?: string): void;
|
|
10
|
+
clear(reason?: string): void;
|
|
11
|
+
has(sessionId: string): boolean;
|
|
12
|
+
sessionIds(): string[];
|
|
13
|
+
readonly size: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function createWarmSessionClientPool<T>(input: {
|
|
17
|
+
limit?: number;
|
|
18
|
+
retire: (entry: WarmSessionClientEntry<T>, reason: string) => void;
|
|
19
|
+
now?: () => number;
|
|
20
|
+
}): WarmSessionClientPool<T>;
|
|
21
|
+
|
|
22
|
+
export function isWarmSessionClientUsable(input: {
|
|
23
|
+
clientRunning: boolean;
|
|
24
|
+
syncState: string | null | undefined;
|
|
25
|
+
hasRoom: boolean;
|
|
26
|
+
}): boolean;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// P408: the Chat page keeps the Matrix clients of the most recently opened
|
|
2
|
+
// other Sessions running ("warm"), so switching back shows their latest
|
|
3
|
+
// messages at once (Owner Q1 A: 5, not counting the current Session).
|
|
4
|
+
// A warm client has no UI: it sends no read receipt, read-visit or typing and
|
|
5
|
+
// plays no notification (nothing in the page listens to it until it becomes
|
|
6
|
+
// current again). This module owns only the bookkeeping; the caller provides
|
|
7
|
+
// how a client is retired (store save, stop, probe disposal).
|
|
8
|
+
|
|
9
|
+
export const WARM_SESSION_CLIENT_LIMIT = 5;
|
|
10
|
+
|
|
11
|
+
function normalizeSessionId(value) {
|
|
12
|
+
return typeof value === "string" && value.trim() ? value.trim() : "";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createWarmSessionClientPool({
|
|
16
|
+
limit = WARM_SESSION_CLIENT_LIMIT,
|
|
17
|
+
retire,
|
|
18
|
+
now = () => Date.now(),
|
|
19
|
+
} = {}) {
|
|
20
|
+
if (!Number.isInteger(limit) || limit < 0) {
|
|
21
|
+
throw new Error("Warm Session client limit must be a non-negative integer.");
|
|
22
|
+
}
|
|
23
|
+
if (typeof retire !== "function") {
|
|
24
|
+
throw new Error("Warm Session client pool requires a retire function.");
|
|
25
|
+
}
|
|
26
|
+
// Insertion order is the least-recently-used order.
|
|
27
|
+
const entries = new Map();
|
|
28
|
+
|
|
29
|
+
function remove(sessionId, reason) {
|
|
30
|
+
const entry = entries.get(sessionId);
|
|
31
|
+
if (!entry) return false;
|
|
32
|
+
entries.delete(sessionId);
|
|
33
|
+
retire(entry, reason);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
/** Keeps the client of a Session the page is leaving; the oldest beyond the limit is retired. */
|
|
39
|
+
park(sessionId, value) {
|
|
40
|
+
const id = normalizeSessionId(sessionId);
|
|
41
|
+
if (!id) throw new Error("A warm Session client requires a Session id.");
|
|
42
|
+
if (entries.has(id)) remove(id, "replaced");
|
|
43
|
+
if (limit === 0) {
|
|
44
|
+
retire({ sessionId: id, ...value, parkedAt: now() }, "limit");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
entries.set(id, { sessionId: id, ...value, parkedAt: now() });
|
|
48
|
+
while (entries.size > limit) {
|
|
49
|
+
remove(entries.keys().next().value, "limit");
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
/** Takes the warm client of the Session the page switches to (it is no longer warm). */
|
|
54
|
+
take(sessionId) {
|
|
55
|
+
const id = normalizeSessionId(sessionId);
|
|
56
|
+
const entry = entries.get(id);
|
|
57
|
+
if (!entry) return undefined;
|
|
58
|
+
entries.delete(id);
|
|
59
|
+
return entry;
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
/** Retires one Session's warm client (stopped, token rejected, unusable). */
|
|
63
|
+
drop(sessionId, reason) {
|
|
64
|
+
return remove(normalizeSessionId(sessionId), reason);
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/** Retires every warm client whose Session is no longer live. */
|
|
68
|
+
retainLive(liveSessionIds, reason = "session_not_live") {
|
|
69
|
+
const live = new Set([...liveSessionIds].map(normalizeSessionId));
|
|
70
|
+
for (const id of [...entries.keys()]) {
|
|
71
|
+
if (!live.has(id)) remove(id, reason);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
/** Retires every warm client (page teardown, sign-out). */
|
|
76
|
+
clear(reason = "cleared") {
|
|
77
|
+
for (const id of [...entries.keys()]) remove(id, reason);
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
has(sessionId) {
|
|
81
|
+
return entries.has(normalizeSessionId(sessionId));
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
sessionIds() {
|
|
85
|
+
return [...entries.keys()];
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
get size() {
|
|
89
|
+
return entries.size;
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether a warm client can be shown as it is: running, holding the bound
|
|
96
|
+
* room, and its sync loop not stopped or failed (RECONNECTING recovers by
|
|
97
|
+
* itself and is nudged by the caller).
|
|
98
|
+
*/
|
|
99
|
+
export function isWarmSessionClientUsable({ clientRunning, syncState, hasRoom }) {
|
|
100
|
+
if (clientRunning !== true || hasRoom !== true) return false;
|
|
101
|
+
return ["SYNCING", "PREPARED", "CATCHUP", "RECONNECTING"].includes(String(syncState || ""));
|
|
102
|
+
}
|