@aperture-browser/session-react 0.0.0-nightly.98.95679814d178 → 1.0.0-beta.1
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 +10 -15
- package/dist/headless.d.ts +202 -292
- package/dist/headless.js +633 -2
- package/dist/headless.js.map +1 -0
- package/dist/index.d.ts +613 -68
- package/dist/index.js +5887 -1883
- package/dist/index.js.map +1 -1
- package/dist/styles.css +2 -2
- package/package.json +9 -9
- package/dist/headless-CeulI4r8.js +0 -2216
- package/dist/headless-CeulI4r8.js.map +0 -1
package/dist/headless.js
CHANGED
|
@@ -1,2 +1,633 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import { SessionError, connect, connect as connect$1 } from "@aperture-browser/session-client";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
//#region src/session.tsx
|
|
5
|
+
/**
|
|
6
|
+
* Connects to a session for as long as the component is mounted. Another address, name or
|
|
7
|
+
* first way of the picture makes another connection.
|
|
8
|
+
*/
|
|
9
|
+
function useConnectedSession({ url, token, name, pixels }) {
|
|
10
|
+
const [session, setSession] = useState(null);
|
|
11
|
+
const [attempt, setAttempt] = useState(0);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const connected = connect$1({
|
|
14
|
+
url,
|
|
15
|
+
token,
|
|
16
|
+
name,
|
|
17
|
+
pixels
|
|
18
|
+
});
|
|
19
|
+
setSession(connected);
|
|
20
|
+
return () => {
|
|
21
|
+
setSession(null);
|
|
22
|
+
connected.close();
|
|
23
|
+
};
|
|
24
|
+
}, [
|
|
25
|
+
url,
|
|
26
|
+
token,
|
|
27
|
+
name,
|
|
28
|
+
pixels,
|
|
29
|
+
attempt
|
|
30
|
+
]);
|
|
31
|
+
return useMemo(() => ({
|
|
32
|
+
session,
|
|
33
|
+
reconnect: () => setAttempt((current) => current + 1)
|
|
34
|
+
}), [session]);
|
|
35
|
+
}
|
|
36
|
+
const SessionContext = createContext(null);
|
|
37
|
+
function SessionProvider({ session, reconnect, children }) {
|
|
38
|
+
const value = useMemo(() => ({
|
|
39
|
+
session,
|
|
40
|
+
reconnect
|
|
41
|
+
}), [session, reconnect]);
|
|
42
|
+
return /* @__PURE__ */ jsx(SessionContext.Provider, {
|
|
43
|
+
value,
|
|
44
|
+
children
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function useSessionContext() {
|
|
48
|
+
const value = useContext(SessionContext);
|
|
49
|
+
if (value === null) throw new Error("The session's components are used outside a SessionProvider");
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function useSession() {
|
|
53
|
+
return useSessionContext().session;
|
|
54
|
+
}
|
|
55
|
+
function useReconnect() {
|
|
56
|
+
return useSessionContext().reconnect;
|
|
57
|
+
}
|
|
58
|
+
function useSessionSnapshot() {
|
|
59
|
+
const session = useSession();
|
|
60
|
+
return useSyncExternalStore(session.subscribe, session.getSnapshot, session.getSnapshot);
|
|
61
|
+
}
|
|
62
|
+
/** The target that is watched. */
|
|
63
|
+
function useWatchedTarget() {
|
|
64
|
+
const snapshot = useSessionSnapshot();
|
|
65
|
+
return snapshot.targets.find((target) => target.tab === snapshot.selected);
|
|
66
|
+
}
|
|
67
|
+
/** Who holds the lease of the watched tab, and what this member may do about it. */
|
|
68
|
+
function useLease() {
|
|
69
|
+
const snapshot = useSessionSnapshot();
|
|
70
|
+
const { presence } = snapshot;
|
|
71
|
+
const watched = snapshot.targets.find((target) => target.tab === snapshot.selected);
|
|
72
|
+
const you = presence?.members.find((member) => member.id === presence.you);
|
|
73
|
+
const held = presence?.held.find((one) => one.tab === watched?.tab);
|
|
74
|
+
const holder = presence?.members.find((member) => member.id === held?.holder);
|
|
75
|
+
const asker = presence?.members.find((member) => member.id === held?.askedBy);
|
|
76
|
+
const role = you?.role;
|
|
77
|
+
const givesInput = role !== "view";
|
|
78
|
+
let state = "free";
|
|
79
|
+
if (!givesInput) state = "view-only";
|
|
80
|
+
else if (held === void 0 || presence === void 0) state = "free";
|
|
81
|
+
else if (held.holder === presence.you) state = held.askedBy === void 0 ? "yours" : "asked-of-you";
|
|
82
|
+
else if (held.askedBy === presence.you) state = "you-asked";
|
|
83
|
+
else state = held.askedBy === void 0 ? "theirs" : "theirs-asked";
|
|
84
|
+
return {
|
|
85
|
+
state,
|
|
86
|
+
you,
|
|
87
|
+
role,
|
|
88
|
+
givesInput,
|
|
89
|
+
held,
|
|
90
|
+
holder,
|
|
91
|
+
asker
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/components/session-viewport.tsx
|
|
96
|
+
const PICTURE_WITHIN_MS = 1e3;
|
|
97
|
+
/** How long after the room for the picture last changed it counts as still changing. */
|
|
98
|
+
const RESIZED_AFTER_MS = 200;
|
|
99
|
+
const SETTLING_BLUR_PX = 6;
|
|
100
|
+
const SETTLING_FADE_MS = 160;
|
|
101
|
+
/**
|
|
102
|
+
* The watched target's picture, and its input: the pointer, the wheel, the keyboard, the
|
|
103
|
+
* clipboard and what is dropped on it. The picture is shown at the target's own size, and
|
|
104
|
+
* smaller where the stage has no room for that.
|
|
105
|
+
*/
|
|
106
|
+
function SessionViewport({ className, style, children }) {
|
|
107
|
+
const session = useSession();
|
|
108
|
+
const snapshot = useSessionSnapshot();
|
|
109
|
+
const stageRef = useRef(null);
|
|
110
|
+
const videoRef = useRef(null);
|
|
111
|
+
const canvasRef = useRef(null);
|
|
112
|
+
const keysRef = useRef(null);
|
|
113
|
+
const [stage, setStage] = useState({
|
|
114
|
+
width: 0,
|
|
115
|
+
height: 0
|
|
116
|
+
});
|
|
117
|
+
/** The size of the picture that is drawn now, in its own pixels. */
|
|
118
|
+
const [drawn, setDrawn] = useState({
|
|
119
|
+
width: 0,
|
|
120
|
+
height: 0
|
|
121
|
+
});
|
|
122
|
+
/** Whether the room for the picture is being changed, as by dragging the window's edge. */
|
|
123
|
+
const [resizing, setResizing] = useState(false);
|
|
124
|
+
const way = snapshot.picture.way;
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
const surfaces = {
|
|
127
|
+
video: videoRef.current,
|
|
128
|
+
canvas: canvasRef.current,
|
|
129
|
+
keys: keysRef.current,
|
|
130
|
+
stage: stageRef.current
|
|
131
|
+
};
|
|
132
|
+
if (surfaces.video === null || surfaces.canvas === null || surfaces.keys === null || surfaces.stage === null) return;
|
|
133
|
+
return session.attach({
|
|
134
|
+
video: surfaces.video,
|
|
135
|
+
canvas: surfaces.canvas,
|
|
136
|
+
keys: surfaces.keys,
|
|
137
|
+
stage: surfaces.stage
|
|
138
|
+
});
|
|
139
|
+
}, [session]);
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
const element = stageRef.current;
|
|
142
|
+
if (element === null) return;
|
|
143
|
+
let settling;
|
|
144
|
+
const measure = () => {
|
|
145
|
+
const next = {
|
|
146
|
+
width: element.clientWidth,
|
|
147
|
+
height: element.clientHeight
|
|
148
|
+
};
|
|
149
|
+
setStage((before) => {
|
|
150
|
+
if (before.width > 0 && (before.width !== next.width || before.height !== next.height)) {
|
|
151
|
+
setResizing(true);
|
|
152
|
+
clearTimeout(settling);
|
|
153
|
+
settling = setTimeout(() => setResizing(false), RESIZED_AFTER_MS);
|
|
154
|
+
}
|
|
155
|
+
return next;
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
measure();
|
|
159
|
+
const sizes = new ResizeObserver(measure);
|
|
160
|
+
sizes.observe(element);
|
|
161
|
+
return () => {
|
|
162
|
+
sizes.disconnect();
|
|
163
|
+
clearTimeout(settling);
|
|
164
|
+
};
|
|
165
|
+
}, []);
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
const video = videoRef.current;
|
|
168
|
+
const canvas = canvasRef.current;
|
|
169
|
+
if (video === null || canvas === null) return;
|
|
170
|
+
const measure = () => {
|
|
171
|
+
const size = way === "video" ? {
|
|
172
|
+
width: video.videoWidth,
|
|
173
|
+
height: video.videoHeight
|
|
174
|
+
} : {
|
|
175
|
+
width: canvas.width,
|
|
176
|
+
height: canvas.height
|
|
177
|
+
};
|
|
178
|
+
setDrawn((before) => before.width === size.width && before.height === size.height ? before : size);
|
|
179
|
+
};
|
|
180
|
+
measure();
|
|
181
|
+
video.addEventListener("resize", measure);
|
|
182
|
+
video.addEventListener("loadedmetadata", measure);
|
|
183
|
+
const sizes = new MutationObserver(measure);
|
|
184
|
+
sizes.observe(canvas, {
|
|
185
|
+
attributes: true,
|
|
186
|
+
attributeFilter: ["width", "height"]
|
|
187
|
+
});
|
|
188
|
+
return () => {
|
|
189
|
+
video.removeEventListener("resize", measure);
|
|
190
|
+
video.removeEventListener("loadedmetadata", measure);
|
|
191
|
+
sizes.disconnect();
|
|
192
|
+
};
|
|
193
|
+
}, [way]);
|
|
194
|
+
const watched = snapshot.targets.find((target) => target.tab === snapshot.selected);
|
|
195
|
+
const shown = watched === void 0 ? void 0 : way;
|
|
196
|
+
const agrees = watched !== void 0 && drawn.width > 0 && sameShape(drawn, watched);
|
|
197
|
+
/** The picture's pixels to a logical pixel of its target, when the two last agreed. */
|
|
198
|
+
const density = useRef(1);
|
|
199
|
+
if (agrees) density.current = drawn.width / watched.width;
|
|
200
|
+
const awaited = watched !== void 0 && drawn.width > 0 && !agrees ? `${watched.id} ${watched.width}x${watched.height}` : void 0;
|
|
201
|
+
const [overdue, setOverdue] = useState(void 0);
|
|
202
|
+
useEffect(() => {
|
|
203
|
+
if (awaited === void 0) return;
|
|
204
|
+
const waiting = setTimeout(() => setOverdue(awaited), PICTURE_WITHIN_MS);
|
|
205
|
+
return () => clearTimeout(waiting);
|
|
206
|
+
}, [awaited]);
|
|
207
|
+
const settled = awaited === void 0 || awaited === overdue;
|
|
208
|
+
const box = watched === void 0 ? void 0 : pictureBox(stage, settled ? watched : {
|
|
209
|
+
width: drawn.width / density.current,
|
|
210
|
+
height: drawn.height / density.current
|
|
211
|
+
});
|
|
212
|
+
const picture = (visible) => ({
|
|
213
|
+
display: visible ? "block" : "none",
|
|
214
|
+
flex: "none",
|
|
215
|
+
width: box?.width,
|
|
216
|
+
height: box?.height,
|
|
217
|
+
outline: "none",
|
|
218
|
+
touchAction: "none",
|
|
219
|
+
cursor: snapshot.cursor,
|
|
220
|
+
filter: resizing || !settled ? `blur(${SETTLING_BLUR_PX}px)` : "none",
|
|
221
|
+
transition: `filter ${SETTLING_FADE_MS}ms ease-out`
|
|
222
|
+
});
|
|
223
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
224
|
+
ref: stageRef,
|
|
225
|
+
className,
|
|
226
|
+
"data-aperture-stage": "",
|
|
227
|
+
"data-aperture-drawn": drawn.width > 0 ? "" : void 0,
|
|
228
|
+
style: {
|
|
229
|
+
position: "relative",
|
|
230
|
+
display: "flex",
|
|
231
|
+
flex: "1 1 0%",
|
|
232
|
+
minWidth: 0,
|
|
233
|
+
minHeight: 0,
|
|
234
|
+
alignItems: "center",
|
|
235
|
+
justifyContent: "center",
|
|
236
|
+
overflow: "hidden",
|
|
237
|
+
...style
|
|
238
|
+
},
|
|
239
|
+
children: [
|
|
240
|
+
/* @__PURE__ */ jsx("video", {
|
|
241
|
+
ref: videoRef,
|
|
242
|
+
"data-aperture-surface": "video",
|
|
243
|
+
tabIndex: -1,
|
|
244
|
+
autoPlay: true,
|
|
245
|
+
muted: true,
|
|
246
|
+
playsInline: true,
|
|
247
|
+
style: picture(shown === "video")
|
|
248
|
+
}),
|
|
249
|
+
/* @__PURE__ */ jsx("canvas", {
|
|
250
|
+
ref: canvasRef,
|
|
251
|
+
"data-aperture-surface": "canvas",
|
|
252
|
+
tabIndex: -1,
|
|
253
|
+
style: picture(shown === "frames" || shown === "tiles")
|
|
254
|
+
}),
|
|
255
|
+
/* @__PURE__ */ jsx("textarea", {
|
|
256
|
+
ref: keysRef,
|
|
257
|
+
"data-aperture-surface": "keys",
|
|
258
|
+
"aria-label": "Keyboard of the session",
|
|
259
|
+
autoCapitalize: "off",
|
|
260
|
+
autoComplete: "off",
|
|
261
|
+
autoCorrect: "off",
|
|
262
|
+
spellCheck: false,
|
|
263
|
+
style: {
|
|
264
|
+
position: "absolute",
|
|
265
|
+
left: 0,
|
|
266
|
+
bottom: 0,
|
|
267
|
+
width: 1,
|
|
268
|
+
height: 1,
|
|
269
|
+
padding: 0,
|
|
270
|
+
border: 0,
|
|
271
|
+
opacity: 0,
|
|
272
|
+
resize: "none",
|
|
273
|
+
fontSize: 16
|
|
274
|
+
}
|
|
275
|
+
}),
|
|
276
|
+
children
|
|
277
|
+
]
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/** Whether two sizes have the same proportions, give or take a pixel's rounding. */
|
|
281
|
+
function sameShape(one, other) {
|
|
282
|
+
if (one.height <= 0 || other.height <= 0) return false;
|
|
283
|
+
const ratio = one.width / one.height * (other.height / other.width);
|
|
284
|
+
return Math.abs(ratio - 1) < .01;
|
|
285
|
+
}
|
|
286
|
+
/** The place the picture of a target has in a stage: its own size, or smaller to fit. */
|
|
287
|
+
function pictureBox(stage, target) {
|
|
288
|
+
if (target.width <= 0 || target.height <= 0) return {
|
|
289
|
+
width: 0,
|
|
290
|
+
height: 0
|
|
291
|
+
};
|
|
292
|
+
const scale = Math.min(1, stage.width / target.width, stage.height / target.height);
|
|
293
|
+
return {
|
|
294
|
+
width: Math.floor(target.width * scale),
|
|
295
|
+
height: Math.floor(target.height * scale)
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
//#endregion
|
|
299
|
+
//#region src/hooks/use-actions.ts
|
|
300
|
+
/** A tab's sound, as this client hears it. */
|
|
301
|
+
function useTabSound(target) {
|
|
302
|
+
const session = useSession();
|
|
303
|
+
const muted = useSessionSnapshot().muted.includes(target?.tab ?? -1);
|
|
304
|
+
const tab = target?.tab;
|
|
305
|
+
const toggle = useCallback(() => {
|
|
306
|
+
if (tab !== void 0) session.setMuted(tab, !muted);
|
|
307
|
+
}, [
|
|
308
|
+
session,
|
|
309
|
+
tab,
|
|
310
|
+
muted
|
|
311
|
+
]);
|
|
312
|
+
return {
|
|
313
|
+
sounds: target?.sounds ?? false,
|
|
314
|
+
muted,
|
|
315
|
+
toggle
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
/** What this member may do in the session now. */
|
|
319
|
+
function useInputState() {
|
|
320
|
+
const snapshot = useSessionSnapshot();
|
|
321
|
+
const lease = useLease();
|
|
322
|
+
const open = snapshot.connection.state === "open";
|
|
323
|
+
return {
|
|
324
|
+
open,
|
|
325
|
+
changes: open && snapshot.features.tabs.write,
|
|
326
|
+
gives: open && lease.givesInput && snapshot.selected !== void 0
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region src/hooks/use-address.ts
|
|
331
|
+
/** The watched tab's address, as a field to type another into. */
|
|
332
|
+
function useAddress() {
|
|
333
|
+
const session = useSession();
|
|
334
|
+
const snapshot = useSessionSnapshot();
|
|
335
|
+
const watched = snapshot.targets.find((target) => target.tab === snapshot.selected);
|
|
336
|
+
const [draft, setDraft] = useState(null);
|
|
337
|
+
return {
|
|
338
|
+
value: (draft !== null && draft.tab === watched?.tab ? draft.url : null) ?? watched?.url ?? "",
|
|
339
|
+
navigates: snapshot.connection.state === "open" && snapshot.features.tabs.write && watched !== void 0,
|
|
340
|
+
type: (url) => setDraft({
|
|
341
|
+
tab: watched?.tab,
|
|
342
|
+
url
|
|
343
|
+
}),
|
|
344
|
+
reset: () => setDraft(null),
|
|
345
|
+
submit: (value) => {
|
|
346
|
+
const url = value.trim();
|
|
347
|
+
if (url === "") return false;
|
|
348
|
+
session.go({ url: addressOf(url) });
|
|
349
|
+
setDraft(null);
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
/** What is typed into an address field as a URL: an address as it is, anything else a search. */
|
|
355
|
+
function addressOf(value) {
|
|
356
|
+
if (isLocalHost(value)) return `http://${value}`;
|
|
357
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//iu.test(value) || /^(?:about|chrome|devtools|data|file|view-source):/iu.test(value)) return value;
|
|
358
|
+
if (isLikelyHost(value)) return `https://${value}`;
|
|
359
|
+
return `https://www.google.com/search?q=${encodeURIComponent(value)}`;
|
|
360
|
+
}
|
|
361
|
+
function isLocalHost(value) {
|
|
362
|
+
return /^localhost(?::\d+)?(?:[/?#].*)?$/iu.test(value) || /^127(?:\.\d{1,3}){3}(?::\d+)?(?:[/?#].*)?$/u.test(value) || /^[a-z0-9-]+:\d+(?:[/?#].*)?$/iu.test(value);
|
|
363
|
+
}
|
|
364
|
+
function isLikelyHost(value) {
|
|
365
|
+
return !/\s/u.test(value) && /^[a-z0-9-]+(?:\.[a-z0-9-]+)+(?:[/:?#].*)?$/iu.test(value);
|
|
366
|
+
}
|
|
367
|
+
//#endregion
|
|
368
|
+
//#region src/hooks/use-stored.ts
|
|
369
|
+
/** What each key holds now, as it was read or last set, for all who use the key. */
|
|
370
|
+
const values = /* @__PURE__ */ new Map();
|
|
371
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
372
|
+
function read(key) {
|
|
373
|
+
if (!values.has(key)) {
|
|
374
|
+
let stored;
|
|
375
|
+
try {
|
|
376
|
+
const text = window.localStorage.getItem(key);
|
|
377
|
+
stored = text === null ? void 0 : JSON.parse(text);
|
|
378
|
+
} catch {
|
|
379
|
+
stored = void 0;
|
|
380
|
+
}
|
|
381
|
+
values.set(key, stored);
|
|
382
|
+
}
|
|
383
|
+
return values.get(key);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* A choice this browser remembers, the same for everything on the page that uses the key.
|
|
387
|
+
* It lasts for the page where storage is unavailable.
|
|
388
|
+
*/
|
|
389
|
+
function useStored(key, fallback, valid) {
|
|
390
|
+
const stored = useSyncExternalStore(useCallback((listener) => {
|
|
391
|
+
const heard = listeners.get(key) ?? /* @__PURE__ */ new Set();
|
|
392
|
+
listeners.set(key, heard);
|
|
393
|
+
heard.add(listener);
|
|
394
|
+
return () => {
|
|
395
|
+
heard.delete(listener);
|
|
396
|
+
};
|
|
397
|
+
}, [key]), () => read(key), () => void 0);
|
|
398
|
+
return [valid(stored) ? stored : fallback, useCallback((next) => {
|
|
399
|
+
values.set(key, next);
|
|
400
|
+
try {
|
|
401
|
+
window.localStorage.setItem(key, JSON.stringify(next));
|
|
402
|
+
} catch {}
|
|
403
|
+
for (const listener of listeners.get(key) ?? []) listener();
|
|
404
|
+
}, [key])];
|
|
405
|
+
}
|
|
406
|
+
//#endregion
|
|
407
|
+
//#region src/hooks/use-fit.ts
|
|
408
|
+
const FIT_KEY = "aperture.session.fit";
|
|
409
|
+
const isBoolean = (value) => typeof value === "boolean";
|
|
410
|
+
/**
|
|
411
|
+
* Has the viewport fit the window, unless that was switched off when this browser was last
|
|
412
|
+
* here, and remembers when it is switched on or off. `follows` is false where the embedder
|
|
413
|
+
* wants targets left at the size they have.
|
|
414
|
+
*/
|
|
415
|
+
function useRememberedFit(follows = true) {
|
|
416
|
+
const session = useSession();
|
|
417
|
+
const snapshot = useSessionSnapshot();
|
|
418
|
+
const [remembered, remember] = useStored(FIT_KEY, true, isBoolean);
|
|
419
|
+
const applied = useRef(void 0);
|
|
420
|
+
const ready = follows && snapshot.connection.state === "open" && snapshot.selected !== void 0 && snapshot.features.tabs.write;
|
|
421
|
+
useEffect(() => {
|
|
422
|
+
if (!ready) return;
|
|
423
|
+
if (applied.current !== session) {
|
|
424
|
+
applied.current = session;
|
|
425
|
+
if (remembered && !snapshot.fit) session.setViewport("fit", { whenFree: true });
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (snapshot.fit !== remembered) remember(snapshot.fit);
|
|
429
|
+
}, [
|
|
430
|
+
ready,
|
|
431
|
+
session,
|
|
432
|
+
snapshot.fit,
|
|
433
|
+
remembered,
|
|
434
|
+
remember
|
|
435
|
+
]);
|
|
436
|
+
}
|
|
437
|
+
//#endregion
|
|
438
|
+
//#region src/hooks/use-recordings.ts
|
|
439
|
+
/** How often the recordings are asked for while one is being made or edited. */
|
|
440
|
+
const BUSY_EVERY_MS = 2e3;
|
|
441
|
+
/** How often otherwise: a recording may be started by the session's automation too. */
|
|
442
|
+
const IDLE_EVERY_MS = 15e3;
|
|
443
|
+
const recordingRuns = (recording) => recording.state === "recording";
|
|
444
|
+
const recordingIsEdited = (recording) => recording.edit?.state === "editing";
|
|
445
|
+
/**
|
|
446
|
+
* The session's recordings and screenshots. The live protocol does not tell of them, so they are asked for
|
|
447
|
+
* again and again: often while one runs or is edited, seldom otherwise.
|
|
448
|
+
*/
|
|
449
|
+
function useRecordings(enabled = true) {
|
|
450
|
+
const session = useSession();
|
|
451
|
+
const open = useSessionSnapshot().connection.state === "open" && enabled;
|
|
452
|
+
const [recordings, setRecordings] = useState([]);
|
|
453
|
+
const [screenshots, setScreenshots] = useState([]);
|
|
454
|
+
const [busy, setBusy] = useState(false);
|
|
455
|
+
const [asked, setAsked] = useState(0);
|
|
456
|
+
const active = recordings.some((one) => recordingRuns(one) || recordingIsEdited(one));
|
|
457
|
+
const latest = useRef(0);
|
|
458
|
+
useEffect(() => {
|
|
459
|
+
if (!open) return;
|
|
460
|
+
let ended = false;
|
|
461
|
+
const ask = () => {
|
|
462
|
+
latest.current += 1;
|
|
463
|
+
const turn = latest.current;
|
|
464
|
+
session.recordings.list().then((listed) => {
|
|
465
|
+
if (!ended && turn === latest.current) setRecordings(listed.toSorted((a, b) => b.started_at_ms - a.started_at_ms));
|
|
466
|
+
}, () => {});
|
|
467
|
+
session.screenshots.list().then((listed) => {
|
|
468
|
+
if (!ended && turn === latest.current) setScreenshots(listed);
|
|
469
|
+
}, () => {});
|
|
470
|
+
};
|
|
471
|
+
ask();
|
|
472
|
+
const timer = setInterval(ask, active ? BUSY_EVERY_MS : IDLE_EVERY_MS);
|
|
473
|
+
return () => {
|
|
474
|
+
ended = true;
|
|
475
|
+
clearInterval(timer);
|
|
476
|
+
};
|
|
477
|
+
}, [
|
|
478
|
+
session,
|
|
479
|
+
open,
|
|
480
|
+
active,
|
|
481
|
+
asked
|
|
482
|
+
]);
|
|
483
|
+
const refresh = useCallback(() => setAsked((current) => current + 1), []);
|
|
484
|
+
return {
|
|
485
|
+
recordings,
|
|
486
|
+
screenshots,
|
|
487
|
+
busy,
|
|
488
|
+
refresh,
|
|
489
|
+
start: useCallback(async (options) => {
|
|
490
|
+
setBusy(true);
|
|
491
|
+
try {
|
|
492
|
+
return await session.recordings.start(options);
|
|
493
|
+
} finally {
|
|
494
|
+
setBusy(false);
|
|
495
|
+
refresh();
|
|
496
|
+
}
|
|
497
|
+
}, [session, refresh]),
|
|
498
|
+
stop: useCallback(async (id, options) => {
|
|
499
|
+
setBusy(true);
|
|
500
|
+
try {
|
|
501
|
+
return await session.recordings.stop(id, options);
|
|
502
|
+
} finally {
|
|
503
|
+
setBusy(false);
|
|
504
|
+
refresh();
|
|
505
|
+
}
|
|
506
|
+
}, [session, refresh]),
|
|
507
|
+
edit: useCallback(async (id, options) => {
|
|
508
|
+
try {
|
|
509
|
+
return await session.recordings.edit(id, options);
|
|
510
|
+
} finally {
|
|
511
|
+
refresh();
|
|
512
|
+
}
|
|
513
|
+
}, [session, refresh]),
|
|
514
|
+
takeScreenshot: useCallback(async (tab) => {
|
|
515
|
+
try {
|
|
516
|
+
return await session.screenshots.take(tab);
|
|
517
|
+
} finally {
|
|
518
|
+
refresh();
|
|
519
|
+
}
|
|
520
|
+
}, [session, refresh])
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
/** The recordings that ended and the screenshots as one list, newest first. */
|
|
524
|
+
function keptOf(control) {
|
|
525
|
+
const recordings = control.recordings.filter((recording) => !recordingRuns(recording)).map((recording) => ({
|
|
526
|
+
kind: "recording",
|
|
527
|
+
id: recording.id,
|
|
528
|
+
at: recording.started_at_ms,
|
|
529
|
+
recording
|
|
530
|
+
}));
|
|
531
|
+
const screenshots = control.screenshots.map((screenshot) => ({
|
|
532
|
+
kind: "screenshot",
|
|
533
|
+
id: screenshot.id,
|
|
534
|
+
at: screenshot.taken_at_ms,
|
|
535
|
+
screenshot
|
|
536
|
+
}));
|
|
537
|
+
return [...recordings, ...screenshots].toSorted((left, right) => right.at - left.at);
|
|
538
|
+
}
|
|
539
|
+
//#endregion
|
|
540
|
+
//#region src/hooks/use-uploads.ts
|
|
541
|
+
/** The session's uploads. A file put there by another member shows at the next `refresh`. */
|
|
542
|
+
function useUploads(enabled = true) {
|
|
543
|
+
const session = useSession();
|
|
544
|
+
const open = useSessionSnapshot().connection.state === "open" && enabled;
|
|
545
|
+
const [uploads, setUploads] = useState([]);
|
|
546
|
+
const [inProgress, setInProgress] = useState([]);
|
|
547
|
+
const [asked, setAsked] = useState(0);
|
|
548
|
+
useEffect(() => {
|
|
549
|
+
if (!open) return;
|
|
550
|
+
let ended = false;
|
|
551
|
+
session.uploads().then((listed) => {
|
|
552
|
+
if (!ended) setUploads(listed.toSorted((a, b) => a.name.localeCompare(b.name)));
|
|
553
|
+
}, () => {});
|
|
554
|
+
return () => {
|
|
555
|
+
ended = true;
|
|
556
|
+
};
|
|
557
|
+
}, [
|
|
558
|
+
session,
|
|
559
|
+
open,
|
|
560
|
+
asked
|
|
561
|
+
]);
|
|
562
|
+
const refresh = useCallback(() => setAsked((current) => current + 1), []);
|
|
563
|
+
return {
|
|
564
|
+
uploads,
|
|
565
|
+
inProgress,
|
|
566
|
+
refresh,
|
|
567
|
+
upload: useCallback(async (files, onFailure) => {
|
|
568
|
+
const put = await Promise.all(files.map(async (file) => {
|
|
569
|
+
const progress = (loaded, total) => setInProgress((current) => [...current.filter((one) => one.name !== file.name), {
|
|
570
|
+
name: file.name,
|
|
571
|
+
loaded,
|
|
572
|
+
total
|
|
573
|
+
}]);
|
|
574
|
+
progress(0, file.size);
|
|
575
|
+
try {
|
|
576
|
+
return await session.upload(file, ({ loaded, total }) => progress(loaded, total));
|
|
577
|
+
} catch (cause) {
|
|
578
|
+
onFailure?.(file, cause);
|
|
579
|
+
return;
|
|
580
|
+
} finally {
|
|
581
|
+
setInProgress((current) => current.filter((one) => one.name !== file.name));
|
|
582
|
+
}
|
|
583
|
+
}));
|
|
584
|
+
refresh();
|
|
585
|
+
return put.filter((one) => one !== void 0);
|
|
586
|
+
}, [session, refresh]),
|
|
587
|
+
remove: useCallback(async (name) => {
|
|
588
|
+
try {
|
|
589
|
+
await session.removeUpload(name);
|
|
590
|
+
} finally {
|
|
591
|
+
refresh();
|
|
592
|
+
}
|
|
593
|
+
}, [session, refresh])
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
//#endregion
|
|
597
|
+
//#region src/hooks/use-timeline.ts
|
|
598
|
+
const isTimeline = (value) => {
|
|
599
|
+
if (typeof value !== "object" || value === null) return false;
|
|
600
|
+
const { duration_ms, map, events } = value;
|
|
601
|
+
return typeof duration_ms === "number" && Array.isArray(map) && Array.isArray(events);
|
|
602
|
+
};
|
|
603
|
+
/** The timeline of a recording whose edit is made; none before, and where there is no edit. */
|
|
604
|
+
function useRecordingTimeline(recording) {
|
|
605
|
+
const session = useSession();
|
|
606
|
+
const file = recording?.edit?.timeline ?? void 0;
|
|
607
|
+
const made = recording?.edit?.made_at_ms ?? 0;
|
|
608
|
+
const [read, setRead] = useState();
|
|
609
|
+
useEffect(() => {
|
|
610
|
+
if (file === void 0) return;
|
|
611
|
+
const asking = new AbortController();
|
|
612
|
+
fetch(session.fileUrl(file), {
|
|
613
|
+
signal: asking.signal,
|
|
614
|
+
cache: "no-store"
|
|
615
|
+
}).then((answer) => answer.ok ? answer.json() : void 0).then((timeline) => {
|
|
616
|
+
if (isTimeline(timeline)) setRead({
|
|
617
|
+
file,
|
|
618
|
+
made,
|
|
619
|
+
timeline
|
|
620
|
+
});
|
|
621
|
+
}, () => {});
|
|
622
|
+
return () => asking.abort();
|
|
623
|
+
}, [
|
|
624
|
+
session,
|
|
625
|
+
file,
|
|
626
|
+
made
|
|
627
|
+
]);
|
|
628
|
+
return read?.file === file && read?.made === made ? read?.timeline : void 0;
|
|
629
|
+
}
|
|
630
|
+
//#endregion
|
|
631
|
+
export { SessionError, SessionProvider, SessionViewport, addressOf, connect, keptOf, pictureBox, recordingIsEdited, recordingRuns, useAddress, useConnectedSession, useInputState, useLease, useReconnect, useRecordingTimeline, useRecordings, useRememberedFit, useSession, useSessionSnapshot, useStored, useTabSound, useUploads, useWatchedTarget };
|
|
632
|
+
|
|
633
|
+
//# sourceMappingURL=headless.js.map
|