@oxyhq/bloom 1.2.1 → 1.3.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/docs/getting-started.mdx +29 -0
- package/docs/media-flight.mdx +108 -0
- package/lib/commonjs/media-flight/MediaFlightHost.js +56 -0
- package/lib/commonjs/media-flight/MediaFlightHost.js.map +1 -0
- package/lib/commonjs/media-flight/MediaFlightHost.web.js +85 -0
- package/lib/commonjs/media-flight/MediaFlightHost.web.js.map +1 -0
- package/lib/commonjs/media-flight/MediaFlightLayer.js +9 -1
- package/lib/commonjs/media-flight/MediaFlightLayer.js.map +1 -1
- package/lib/commonjs/media-flight/MediaFlightLayer.web.js +212 -0
- package/lib/commonjs/media-flight/MediaFlightLayer.web.js.map +1 -0
- package/lib/commonjs/media-flight/MediaSurface.js +3 -2
- package/lib/commonjs/media-flight/MediaSurface.js.map +1 -1
- package/lib/commonjs/media-flight/index.js +8 -1
- package/lib/commonjs/media-flight/index.js.map +1 -1
- package/lib/commonjs/media-flight/index.web.js +115 -0
- package/lib/commonjs/media-flight/index.web.js.map +1 -0
- package/lib/commonjs/media-flight/media-node.web.js +291 -0
- package/lib/commonjs/media-flight/media-node.web.js.map +1 -0
- package/lib/commonjs/media-flight/store.js +61 -2
- package/lib/commonjs/media-flight/store.js.map +1 -1
- package/lib/module/media-flight/MediaFlightHost.js +51 -0
- package/lib/module/media-flight/MediaFlightHost.js.map +1 -0
- package/lib/module/media-flight/MediaFlightHost.web.js +80 -0
- package/lib/module/media-flight/MediaFlightHost.web.js.map +1 -0
- package/lib/module/media-flight/MediaFlightLayer.js +9 -1
- package/lib/module/media-flight/MediaFlightLayer.js.map +1 -1
- package/lib/module/media-flight/MediaFlightLayer.web.js +206 -0
- package/lib/module/media-flight/MediaFlightLayer.web.js.map +1 -0
- package/lib/module/media-flight/MediaSurface.js +3 -2
- package/lib/module/media-flight/MediaSurface.js.map +1 -1
- package/lib/module/media-flight/index.js +2 -1
- package/lib/module/media-flight/index.js.map +1 -1
- package/lib/module/media-flight/index.web.js +19 -0
- package/lib/module/media-flight/index.web.js.map +1 -0
- package/lib/module/media-flight/media-node.web.js +281 -0
- package/lib/module/media-flight/media-node.web.js.map +1 -0
- package/lib/module/media-flight/store.js +61 -2
- package/lib/module/media-flight/store.js.map +1 -1
- package/lib/typescript/commonjs/media-flight/MediaFlightHost.d.ts +6 -0
- package/lib/typescript/commonjs/media-flight/MediaFlightHost.d.ts.map +1 -0
- package/lib/typescript/commonjs/media-flight/MediaFlightHost.web.d.ts +6 -0
- package/lib/typescript/commonjs/media-flight/MediaFlightHost.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/media-flight/MediaFlightLayer.web.d.ts +5 -0
- package/lib/typescript/commonjs/media-flight/MediaFlightLayer.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/media-flight/MediaSurface.d.ts +17 -0
- package/lib/typescript/commonjs/media-flight/MediaSurface.d.ts.map +1 -1
- package/lib/typescript/commonjs/media-flight/index.d.ts +2 -1
- package/lib/typescript/commonjs/media-flight/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/media-flight/index.web.d.ts +19 -0
- package/lib/typescript/commonjs/media-flight/index.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/media-flight/media-node.web.d.ts +79 -0
- package/lib/typescript/commonjs/media-flight/media-node.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/media-flight/store.d.ts +39 -1
- package/lib/typescript/commonjs/media-flight/store.d.ts.map +1 -1
- package/lib/typescript/commonjs/media-flight/types.d.ts +57 -0
- package/lib/typescript/commonjs/media-flight/types.d.ts.map +1 -1
- package/lib/typescript/module/media-flight/MediaFlightHost.d.ts +6 -0
- package/lib/typescript/module/media-flight/MediaFlightHost.d.ts.map +1 -0
- package/lib/typescript/module/media-flight/MediaFlightHost.web.d.ts +6 -0
- package/lib/typescript/module/media-flight/MediaFlightHost.web.d.ts.map +1 -0
- package/lib/typescript/module/media-flight/MediaFlightLayer.web.d.ts +5 -0
- package/lib/typescript/module/media-flight/MediaFlightLayer.web.d.ts.map +1 -0
- package/lib/typescript/module/media-flight/MediaSurface.d.ts +17 -0
- package/lib/typescript/module/media-flight/MediaSurface.d.ts.map +1 -1
- package/lib/typescript/module/media-flight/index.d.ts +2 -1
- package/lib/typescript/module/media-flight/index.d.ts.map +1 -1
- package/lib/typescript/module/media-flight/index.web.d.ts +19 -0
- package/lib/typescript/module/media-flight/index.web.d.ts.map +1 -0
- package/lib/typescript/module/media-flight/media-node.web.d.ts +79 -0
- package/lib/typescript/module/media-flight/media-node.web.d.ts.map +1 -0
- package/lib/typescript/module/media-flight/store.d.ts +39 -1
- package/lib/typescript/module/media-flight/store.d.ts.map +1 -1
- package/lib/typescript/module/media-flight/types.d.ts +57 -0
- package/lib/typescript/module/media-flight/types.d.ts.map +1 -1
- package/package.json +6 -1
- package/src/media-flight/MediaFlightHost.tsx +54 -0
- package/src/media-flight/MediaFlightHost.web.tsx +84 -0
- package/src/media-flight/MediaFlightLayer.tsx +7 -1
- package/src/media-flight/MediaFlightLayer.web.tsx +219 -0
- package/src/media-flight/MediaSurface.tsx +19 -1
- package/src/media-flight/index.ts +2 -0
- package/src/media-flight/index.web.ts +45 -0
- package/src/media-flight/media-node.web.ts +335 -0
- package/src/media-flight/store.ts +61 -1
- package/src/media-flight/types.ts +58 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ONE DOM node per media id, for its whole life.
|
|
3
|
+
*
|
|
4
|
+
* ## Why this exists
|
|
5
|
+
*
|
|
6
|
+
* expo-video's web player keeps a SET of mounted `<video>` elements and keeps
|
|
7
|
+
* them in step: `mountVideoView` adds an element and then copies play state and
|
|
8
|
+
* `currentTime` onto it from `[...set][0]`, and `_addListeners` gives every
|
|
9
|
+
* element an `onpause` that pauses all the OTHERS. `unmountVideoView` deletes
|
|
10
|
+
* the element from the set and nothing more — the handlers it installed are
|
|
11
|
+
* never removed by anything in the package.
|
|
12
|
+
*
|
|
13
|
+
* So an element on its way out still governs the player. The browser pauses a
|
|
14
|
+
* `<video>` the moment it leaves the document, that pause runs the dead
|
|
15
|
+
* element's handler, and the handler stops the element the viewer is watching.
|
|
16
|
+
*
|
|
17
|
+
* Measured against the real `VideoPlayerWeb` (expo-video 57.0.2), across every
|
|
18
|
+
* topology a consumer can produce on one shared player:
|
|
19
|
+
*
|
|
20
|
+
* ```
|
|
21
|
+
* origin dies BEFORE the destination mounts ct=0 playing (restarts)
|
|
22
|
+
* origin dies AFTER the destination mounts ct=4.28 paused (freezes)
|
|
23
|
+
* one element, re-parented ct=4.28 playing
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* TWO ELEMENTS GIVE THE POSITION OR THE PLAYBACK, NEVER BOTH. That is not a
|
|
27
|
+
* property of the flight layer — the first two rows have no Bloom surface in
|
|
28
|
+
* them at all — it is a property of two elements sharing one player. The only
|
|
29
|
+
* topology that gives both is one element that is never unmounted and never
|
|
30
|
+
* removed from the document.
|
|
31
|
+
*
|
|
32
|
+
* ## How a node survives its host
|
|
33
|
+
*
|
|
34
|
+
* A `<video>` React renders belongs to React: when the host unmounts, React
|
|
35
|
+
* removes it, and if anything has moved it in the meantime React's
|
|
36
|
+
* `parent.removeChild(node)` throws. So the node cannot be owned by either end.
|
|
37
|
+
*
|
|
38
|
+
* Instead Bloom creates a WRAPPER outside React (`document.createElement`) and
|
|
39
|
+
* the layer renders the media into it with `createPortal`. A portal's container
|
|
40
|
+
* is not part of React's tree — React reconciles the media against the wrapper
|
|
41
|
+
* and never against the document — so moving the wrapper between hosts with
|
|
42
|
+
* `appendChild` is invisible to React. The container's IDENTITY never changes,
|
|
43
|
+
* which is the whole trick: `createPortal` to a container that CHANGES unmounts
|
|
44
|
+
* and remounts, which is exactly what we are avoiding.
|
|
45
|
+
*
|
|
46
|
+
* And a synchronous move does not pause the video. HTML's removal steps for a
|
|
47
|
+
* media element await a stable state and then abort if the element is back in a
|
|
48
|
+
* document — so `appendChild` from one parent to another is not a removal as
|
|
49
|
+
* far as playback is concerned. (This is why re-parenting a `<video>` works and
|
|
50
|
+
* re-parenting an `<iframe>` does not: an iframe reloads.)
|
|
51
|
+
*
|
|
52
|
+
* ## Claims
|
|
53
|
+
*
|
|
54
|
+
* Hosts do not move the node themselves; they CLAIM the id and the registry
|
|
55
|
+
* decides. A flight outranks a host, so the layer keeps the node for the whole
|
|
56
|
+
* leg even though the destination has already mounted and claimed it — and the
|
|
57
|
+
* frame the flight lets go is the frame the destination takes it, with no gap
|
|
58
|
+
* and no second element. With no claim at all the node parks in a holder that
|
|
59
|
+
* is attached to the document but invisible, because a parked node that was
|
|
60
|
+
* DETACHED would pause.
|
|
61
|
+
*/
|
|
62
|
+
import { hasFlight } from './store';
|
|
63
|
+
import type { MediaSurfaceContent } from './types';
|
|
64
|
+
|
|
65
|
+
/** What the layer paints into a shared node, as its current holder wants it. */
|
|
66
|
+
export interface MediaNodeRender {
|
|
67
|
+
content: MediaSurfaceContent;
|
|
68
|
+
contentFit: 'contain' | 'cover';
|
|
69
|
+
nativeControls: boolean;
|
|
70
|
+
accessibilityLabel: string | undefined;
|
|
71
|
+
/** Forwarded to the media, so a consumer keeps its own first-frame signal. */
|
|
72
|
+
flightId: string | undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A flight outranks a host: the layer holds the node until the leg ends. */
|
|
76
|
+
export const HOST_RANK = 0;
|
|
77
|
+
export const FLIGHT_RANK = 1;
|
|
78
|
+
|
|
79
|
+
interface MediaNodeClaim {
|
|
80
|
+
el: HTMLElement;
|
|
81
|
+
rank: number;
|
|
82
|
+
/** Registration order, so the most recent host wins among equals. */
|
|
83
|
+
seq: number;
|
|
84
|
+
render: MediaNodeRender;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
interface MediaNodeRecord {
|
|
88
|
+
wrapper: HTMLDivElement;
|
|
89
|
+
claims: MediaNodeClaim[];
|
|
90
|
+
/**
|
|
91
|
+
* How the media is painted right now: the top claim's, or — while the node is
|
|
92
|
+
* PARKED between hosts — the last claim's.
|
|
93
|
+
*
|
|
94
|
+
* Parking has to keep painting. A node the layer stopped rendering would lose
|
|
95
|
+
* its element to React, which is the removal this whole module exists to
|
|
96
|
+
* avoid, and it would lose it exactly in the gap between the origin
|
|
97
|
+
* unmounting and the destination mounting: the moment of the flight.
|
|
98
|
+
*/
|
|
99
|
+
render: MediaNodeRender;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** What the layer needs in order to paint one shared node. */
|
|
103
|
+
export interface MediaNodeView {
|
|
104
|
+
id: string;
|
|
105
|
+
wrapper: HTMLDivElement;
|
|
106
|
+
render: MediaNodeRender;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface NodeRegistry {
|
|
110
|
+
nodes: Map<string, MediaNodeRecord>;
|
|
111
|
+
listeners: Set<() => void>;
|
|
112
|
+
/** Cached for `useSyncExternalStore`, which compares snapshots by identity. */
|
|
113
|
+
snapshot: readonly MediaNodeView[];
|
|
114
|
+
holder: HTMLElement | null;
|
|
115
|
+
seq: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
declare global {
|
|
119
|
+
// eslint-disable-next-line no-var
|
|
120
|
+
var __oxyhq_bloom_media_nodes__: NodeRegistry | undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function registry(): NodeRegistry {
|
|
124
|
+
globalThis.__oxyhq_bloom_media_nodes__ ??= {
|
|
125
|
+
nodes: new Map(),
|
|
126
|
+
listeners: new Set(),
|
|
127
|
+
snapshot: [],
|
|
128
|
+
holder: null,
|
|
129
|
+
seq: 0,
|
|
130
|
+
};
|
|
131
|
+
return globalThis.__oxyhq_bloom_media_nodes__;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Where a node with no holder waits: in the document, and not on screen. */
|
|
135
|
+
const HOLDER_ID = 'bloom-media-holder';
|
|
136
|
+
|
|
137
|
+
function holder(reg: NodeRegistry): HTMLElement | null {
|
|
138
|
+
if (typeof document === 'undefined') return null;
|
|
139
|
+
if (reg.holder !== null && reg.holder.isConnected) return reg.holder;
|
|
140
|
+
const existing = document.getElementById(HOLDER_ID);
|
|
141
|
+
if (existing !== null) {
|
|
142
|
+
reg.holder = existing;
|
|
143
|
+
return existing;
|
|
144
|
+
}
|
|
145
|
+
const created = document.createElement('div');
|
|
146
|
+
created.id = HOLDER_ID;
|
|
147
|
+
// Attached, so nothing pauses; 1x1 and transparent, so nothing shows. NOT
|
|
148
|
+
// `display: none` — a video in a `display: none` subtree keeps playing in
|
|
149
|
+
// every browser that matters, but it also stops presenting frames, and the
|
|
150
|
+
// point of parking is that the media survives untouched.
|
|
151
|
+
created.style.cssText =
|
|
152
|
+
'position:fixed;top:0;left:0;width:1px;height:1px;overflow:hidden;opacity:0;pointer-events:none;';
|
|
153
|
+
created.setAttribute('aria-hidden', 'true');
|
|
154
|
+
document.body.appendChild(created);
|
|
155
|
+
reg.holder = created;
|
|
156
|
+
return created;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function makeWrapper(): HTMLDivElement {
|
|
160
|
+
const wrapper = document.createElement('div');
|
|
161
|
+
// Fills whichever box currently hosts it. `pointer-events: auto` is explicit
|
|
162
|
+
// and load-bearing: a host may sit inside a `pointer-events: none` layer (a
|
|
163
|
+
// poster overlay, a flying surface), and the media inside this wrapper is
|
|
164
|
+
// the one thing in there that a viewer may need to press.
|
|
165
|
+
wrapper.style.cssText =
|
|
166
|
+
'position:absolute;top:0;left:0;right:0;bottom:0;pointer-events:auto;';
|
|
167
|
+
wrapper.setAttribute('data-bloom-media-node', '');
|
|
168
|
+
return wrapper;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function recordFor(id: string, render: MediaNodeRender): MediaNodeRecord {
|
|
172
|
+
const reg = registry();
|
|
173
|
+
let record = reg.nodes.get(id);
|
|
174
|
+
if (!record) {
|
|
175
|
+
record = { wrapper: makeWrapper(), claims: [], render };
|
|
176
|
+
reg.nodes.set(id, record);
|
|
177
|
+
}
|
|
178
|
+
return record;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The claim that currently holds the node: highest rank, then most recent. */
|
|
182
|
+
function topClaim(record: MediaNodeRecord): MediaNodeClaim | null {
|
|
183
|
+
let best: MediaNodeClaim | null = null;
|
|
184
|
+
for (const claim of record.claims) {
|
|
185
|
+
if (best === null || claim.rank > best.rank || (claim.rank === best.rank && claim.seq > best.seq)) {
|
|
186
|
+
best = claim;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return best;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Put the wrapper where its top claim says, in ONE synchronous move.
|
|
194
|
+
*
|
|
195
|
+
* The `parentElement` check is not an optimisation: re-appending to the same
|
|
196
|
+
* parent is a remove-then-insert, and doing that per render would put the media
|
|
197
|
+
* through the removal path it exists to avoid.
|
|
198
|
+
*/
|
|
199
|
+
function place(record: MediaNodeRecord): void {
|
|
200
|
+
const reg = registry();
|
|
201
|
+
const target = topClaim(record)?.el ?? holder(reg);
|
|
202
|
+
if (target === null) return;
|
|
203
|
+
if (record.wrapper.parentElement === target) return;
|
|
204
|
+
target.appendChild(record.wrapper);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function sameRender(a: MediaNodeRender, b: MediaNodeRender): boolean {
|
|
208
|
+
if (a.contentFit !== b.contentFit) return false;
|
|
209
|
+
if (a.nativeControls !== b.nativeControls) return false;
|
|
210
|
+
if (a.accessibilityLabel !== b.accessibilityLabel) return false;
|
|
211
|
+
if (a.flightId !== b.flightId) return false;
|
|
212
|
+
const x = a.content;
|
|
213
|
+
const y = b.content;
|
|
214
|
+
if (x.kind !== y.kind) return false;
|
|
215
|
+
if (x.kind === 'video' && y.kind === 'video') {
|
|
216
|
+
return x.player === y.player && x.poster === y.poster;
|
|
217
|
+
}
|
|
218
|
+
return x.kind !== 'video' && y.kind !== 'video' && x.uri === y.uri;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function publish(reg: NodeRegistry): void {
|
|
222
|
+
const next: MediaNodeView[] = [];
|
|
223
|
+
for (const [id, record] of reg.nodes) {
|
|
224
|
+
record.render = topClaim(record)?.render ?? record.render;
|
|
225
|
+
next.push({ id, wrapper: record.wrapper, render: record.render });
|
|
226
|
+
}
|
|
227
|
+
const previous = reg.snapshot;
|
|
228
|
+
const unchanged =
|
|
229
|
+
previous.length === next.length &&
|
|
230
|
+
previous.every((view, i) => {
|
|
231
|
+
const candidate = next[i];
|
|
232
|
+
return (
|
|
233
|
+
candidate !== undefined &&
|
|
234
|
+
candidate.id === view.id &&
|
|
235
|
+
candidate.wrapper === view.wrapper &&
|
|
236
|
+
sameRender(candidate.render, view.render)
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
// Identity is the signal `useSyncExternalStore` reads. Publishing a fresh
|
|
240
|
+
// array for an unchanged set renders the layer forever.
|
|
241
|
+
if (unchanged) {
|
|
242
|
+
for (const listener of reg.listeners) listener();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
reg.snapshot = next;
|
|
246
|
+
for (const listener of reg.listeners) listener();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Claim the shared node for `id` into `el`.
|
|
251
|
+
*
|
|
252
|
+
* Idempotent for an unchanged claim: called from a layout effect on every
|
|
253
|
+
* render, it moves the node once and publishes nothing afterwards.
|
|
254
|
+
*/
|
|
255
|
+
export function claimMediaNode(
|
|
256
|
+
id: string,
|
|
257
|
+
el: HTMLElement,
|
|
258
|
+
rank: number,
|
|
259
|
+
render: MediaNodeRender,
|
|
260
|
+
): void {
|
|
261
|
+
const reg = registry();
|
|
262
|
+
const record = recordFor(id, render);
|
|
263
|
+
const existing = record.claims.find((claim) => claim.el === el && claim.rank === rank);
|
|
264
|
+
if (existing !== undefined) {
|
|
265
|
+
if (sameRender(existing.render, render) && record.wrapper.parentElement === topClaim(record)?.el) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
existing.render = render;
|
|
269
|
+
} else {
|
|
270
|
+
reg.seq += 1;
|
|
271
|
+
record.claims.push({ el, rank, seq: reg.seq, render });
|
|
272
|
+
}
|
|
273
|
+
place(record);
|
|
274
|
+
publish(reg);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Drop a claim. The node goes to the next claim, or parks — or is DISPOSED, if
|
|
279
|
+
* nothing is going to want it back.
|
|
280
|
+
*
|
|
281
|
+
* "Nothing is going to want it back" is a fact the registry can read rather
|
|
282
|
+
* than a timeout it has to guess: no claim left AND no flight live for this id.
|
|
283
|
+
* During a navigation there IS a flight live — that is what a flight IS — so
|
|
284
|
+
* the node survives the gap between the origin unmounting and the destination
|
|
285
|
+
* mounting. Without a flight, an unmounted host is just a host that went away.
|
|
286
|
+
*/
|
|
287
|
+
export function releaseMediaNode(id: string, el: HTMLElement, rank: number): void {
|
|
288
|
+
const reg = registry();
|
|
289
|
+
const record = reg.nodes.get(id);
|
|
290
|
+
if (record === undefined) return;
|
|
291
|
+
const before = record.claims.length;
|
|
292
|
+
record.claims = record.claims.filter((claim) => !(claim.el === el && claim.rank === rank));
|
|
293
|
+
if (record.claims.length === before) return;
|
|
294
|
+
if (record.claims.length === 0 && !hasFlight(id)) {
|
|
295
|
+
reg.nodes.delete(id);
|
|
296
|
+
record.wrapper.remove();
|
|
297
|
+
publish(reg);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
place(record);
|
|
301
|
+
publish(reg);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Whether anything holds a shared node for this id. */
|
|
305
|
+
export function hasMediaNode(id: string): boolean {
|
|
306
|
+
const record = registry().nodes.get(id);
|
|
307
|
+
return record !== undefined && record.claims.length > 0;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Subscribe to the shared-node set. The layer's `useSyncExternalStore` half. */
|
|
311
|
+
export function subscribeToMediaNodes(listener: () => void): () => void {
|
|
312
|
+
const reg = registry();
|
|
313
|
+
reg.listeners.add(listener);
|
|
314
|
+
return () => {
|
|
315
|
+
reg.listeners.delete(listener);
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Every shared node that something is currently holding. Stable identity. */
|
|
320
|
+
export function getMediaNodes(): readonly MediaNodeView[] {
|
|
321
|
+
return registry().snapshot;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Test seam: drop every node and take its wrapper out of the document. */
|
|
325
|
+
export function resetMediaNodes(): void {
|
|
326
|
+
const reg = registry();
|
|
327
|
+
for (const record of reg.nodes.values()) record.wrapper.remove();
|
|
328
|
+
reg.nodes.clear();
|
|
329
|
+
reg.snapshot = [];
|
|
330
|
+
if (reg.holder !== null) {
|
|
331
|
+
reg.holder.remove();
|
|
332
|
+
reg.holder = null;
|
|
333
|
+
}
|
|
334
|
+
for (const listener of reg.listeners) listener();
|
|
335
|
+
}
|
|
@@ -77,6 +77,8 @@ interface FlightStatus {
|
|
|
77
77
|
/** Waiters on `flyTo`'s promise, resolved once `mounted` turns true. */
|
|
78
78
|
mountWaiters: Array<() => void>;
|
|
79
79
|
mountTimer: ReturnType<typeof setTimeout> | null;
|
|
80
|
+
/** Pending final removal while the surface unbinds from its player. */
|
|
81
|
+
unbindTimer: ReturnType<typeof setTimeout> | null;
|
|
80
82
|
}
|
|
81
83
|
|
|
82
84
|
interface Registry {
|
|
@@ -119,6 +121,7 @@ function statusFor(id: string): FlightStatus {
|
|
|
119
121
|
handedOff: false,
|
|
120
122
|
mountWaiters: [],
|
|
121
123
|
mountTimer: null,
|
|
124
|
+
unbindTimer: null,
|
|
122
125
|
};
|
|
123
126
|
reg.status.set(id, status);
|
|
124
127
|
}
|
|
@@ -127,7 +130,9 @@ function statusFor(id: string): FlightStatus {
|
|
|
127
130
|
|
|
128
131
|
function clearTimers(status: FlightStatus): void {
|
|
129
132
|
if (status.mountTimer !== null) clearTimeout(status.mountTimer);
|
|
133
|
+
if (status.unbindTimer !== null) clearTimeout(status.unbindTimer);
|
|
130
134
|
status.mountTimer = null;
|
|
135
|
+
status.unbindTimer = null;
|
|
131
136
|
}
|
|
132
137
|
|
|
133
138
|
function resolveMountWaiters(status: FlightStatus): void {
|
|
@@ -263,6 +268,7 @@ export function flyTo(
|
|
|
263
268
|
// silently ignored by the view it is meant to configure.
|
|
264
269
|
surfaceType: existing.surfaceType,
|
|
265
270
|
landing: false,
|
|
271
|
+
unbinding: false,
|
|
266
272
|
generation: existing.generation + 1,
|
|
267
273
|
};
|
|
268
274
|
reg.flights.set(id, next);
|
|
@@ -285,6 +291,7 @@ export function flyTo(
|
|
|
285
291
|
surfaceType: options?.surfaceType ?? 'textureView',
|
|
286
292
|
progress: makeMutable(0),
|
|
287
293
|
landing: false,
|
|
294
|
+
unbinding: false,
|
|
288
295
|
generation: 0,
|
|
289
296
|
};
|
|
290
297
|
reg.flights.set(id, flight);
|
|
@@ -394,9 +401,62 @@ export function flyBack(id: string): void {
|
|
|
394
401
|
});
|
|
395
402
|
}
|
|
396
403
|
|
|
397
|
-
/**
|
|
404
|
+
/**
|
|
405
|
+
* Drop the surface for `id`. Called when a landing leg settles, and on teardown.
|
|
406
|
+
*
|
|
407
|
+
* A VIDEO surface does not go straight out. It renders for one more commit with
|
|
408
|
+
* NO player first, because expo-video's web player mirrors pause across every
|
|
409
|
+
* element bound to it and a `<video>` removed from the DOM is auto-paused by the
|
|
410
|
+
* browser — while `unmountVideoView` only runs in a PASSIVE effect cleanup,
|
|
411
|
+
* i.e. after removal and after that auto-pause. So the dying element pauses the
|
|
412
|
+
* one the viewer is now watching. Measured: the reel's video paused 1 ms after
|
|
413
|
+
* the flying surface left the DOM, with `currentTime` still advancing and
|
|
414
|
+
* nobody having called `pause()`.
|
|
415
|
+
*
|
|
416
|
+
* Rendering `player={null}` for one commit runs expo-video's own `[props.player]`
|
|
417
|
+
* effect while the node is STILL in the DOM, and the operative part of that is
|
|
418
|
+
* NOT the unbind. `unmountVideoView` takes the element out of `_mountedVideos`
|
|
419
|
+
* and leaves the `onpause` handler `_addListeners` installed, so unbinding
|
|
420
|
+
* alone would still mirror. What saves it is the other half of the same
|
|
421
|
+
* branch: `removeAttribute('src'); load()` (`VideoView.web.js:206`). The media
|
|
422
|
+
* element load algorithm sets `paused` to true WITHOUT firing `pause`, so the
|
|
423
|
+
* element goes quiet, and its later removal fires nothing either because it is
|
|
424
|
+
* already paused.
|
|
425
|
+
*
|
|
426
|
+
* Measured in real Chrome, with controls: a playing element put through
|
|
427
|
+
* `removeAttribute('src') + load()` reports events `["play"]` and nothing more,
|
|
428
|
+
* while the same element paused by hand, or removed from the DOM while
|
|
429
|
+
* playing, reports `["play","pause"]`. Against real builds: without this commit
|
|
430
|
+
* the destination ends STOPPED (5/5), with it PLAYING (6/6) and the position
|
|
431
|
+
* carried.
|
|
432
|
+
*
|
|
433
|
+
* DO NOT replace this with a direct `unmountVideoView` call. It looks
|
|
434
|
+
* equivalent, it is the half that does not matter, and the fix would disappear
|
|
435
|
+
* silently. `MediaFlight.test.tsx` makes that substitution its control.
|
|
436
|
+
*
|
|
437
|
+
* The cost is one frame in which the outgoing surface shows its poster instead
|
|
438
|
+
* of video. It is already on its way out and the destination is live by then —
|
|
439
|
+
* `handOff` is what got us here — so that frame is covered.
|
|
440
|
+
*/
|
|
398
441
|
export function releaseFlight(id: string): void {
|
|
399
442
|
const reg = registry();
|
|
443
|
+
const flight = reg.flights.get(id);
|
|
444
|
+
|
|
445
|
+
if (flight !== undefined && flight.content.kind === 'video' && !flight.unbinding) {
|
|
446
|
+
reg.flights.set(id, { ...flight, unbinding: true });
|
|
447
|
+
const unbinding = statusFor(id);
|
|
448
|
+
if (unbinding.unbindTimer === null) {
|
|
449
|
+
// After the commit AND expo-video's passive effect, which is the whole
|
|
450
|
+
// point — a microtask would run before either.
|
|
451
|
+
unbinding.unbindTimer = setTimeout(() => {
|
|
452
|
+
unbinding.unbindTimer = null;
|
|
453
|
+
releaseFlight(id);
|
|
454
|
+
}, 0);
|
|
455
|
+
}
|
|
456
|
+
publish(reg);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
|
|
400
460
|
const status = reg.status.get(id);
|
|
401
461
|
if (status) {
|
|
402
462
|
// A waiter still parked on `flyTo`'s promise would otherwise hang until its
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { StyleProp, ViewStyle } from 'react-native';
|
|
1
2
|
import type { SharedValue } from 'react-native-reanimated';
|
|
2
3
|
|
|
3
4
|
import type { VideoPlayerLike, VideoSurfaceType } from './expo-video-module';
|
|
@@ -94,6 +95,19 @@ export interface MediaFlight {
|
|
|
94
95
|
* settles, the surface is released instead of parked.
|
|
95
96
|
*/
|
|
96
97
|
landing: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* True for the ONE commit between "release requested" and the surface
|
|
100
|
+
* actually going away, during which a video surface renders with no player.
|
|
101
|
+
*
|
|
102
|
+
* expo-video's web player MIRRORS pause across every element bound to it
|
|
103
|
+
* (`VideoPlayer.web.js`: `video.onpause` pauses all the others), and a
|
|
104
|
+
* `<video>` removed from the DOM is auto-paused by the browser. Its
|
|
105
|
+
* `unmountVideoView` runs in a PASSIVE effect cleanup — after removal, after
|
|
106
|
+
* the auto-pause — so a dying element pauses the one the viewer is watching.
|
|
107
|
+
* Unbinding first takes this element out of `_mountedVideos` while it is
|
|
108
|
+
* still in the DOM, so its later pause reaches nobody.
|
|
109
|
+
*/
|
|
110
|
+
unbinding: boolean;
|
|
97
111
|
/** Bumped on every retarget, so React remounts nothing but the layer re-reads. */
|
|
98
112
|
generation: number;
|
|
99
113
|
}
|
|
@@ -208,3 +222,47 @@ export interface MediaFlightController {
|
|
|
208
222
|
*/
|
|
209
223
|
progress: SharedValue<number>;
|
|
210
224
|
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* A place a media surface may live: the feed row it starts in, the fullscreen
|
|
228
|
+
* player it lands in, and every other end of a flight.
|
|
229
|
+
*
|
|
230
|
+
* A host DECLARES a box; it does not own the media. On web that distinction is
|
|
231
|
+
* the whole feature — the media is ONE DOM node the layer paints once and moves
|
|
232
|
+
* between hosts, so it survives the origin route unmounting mid-flight without
|
|
233
|
+
* ever leaving the document. On native a host renders its media inline, which
|
|
234
|
+
* is what it has always done and what the platform makes correct there.
|
|
235
|
+
*/
|
|
236
|
+
export interface MediaFlightHostProps {
|
|
237
|
+
/** The id this host shares with the flight and with the other end. */
|
|
238
|
+
id: string;
|
|
239
|
+
/** What to paint: a still image, or a consumer-owned expo-video player. */
|
|
240
|
+
content: MediaSurfaceContent;
|
|
241
|
+
/** Style of the host BOX. Give it the radius and `overflow: 'hidden'` you want. */
|
|
242
|
+
style?: StyleProp<ViewStyle>;
|
|
243
|
+
/** How the media fills the box. Defaults to `'cover'`. */
|
|
244
|
+
contentFit?: 'contain' | 'cover';
|
|
245
|
+
/** Whether the video arm shows expo-video's own controls. Defaults to `false`. */
|
|
246
|
+
nativeControls?: boolean;
|
|
247
|
+
accessibilityLabel?: string;
|
|
248
|
+
/**
|
|
249
|
+
* Android's rendering surface for the video arm. See `MediaSurfaceProps`.
|
|
250
|
+
* Ignored on web, where there is no such choice.
|
|
251
|
+
*/
|
|
252
|
+
surfaceType?: VideoSurfaceType;
|
|
253
|
+
/**
|
|
254
|
+
* Passed through as a PROP, never as a style key — react-native-web resolves
|
|
255
|
+
* `box-none`/`box-only` from the prop path only.
|
|
256
|
+
*/
|
|
257
|
+
pointerEvents?: 'auto' | 'none' | 'box-none' | 'box-only';
|
|
258
|
+
/**
|
|
259
|
+
* Set on a DESTINATION host, to the id its flight was started with, when the
|
|
260
|
+
* destination paints its media ITSELF rather than through the shared node —
|
|
261
|
+
* which on native is always. The web host does not need it: a flight there
|
|
262
|
+
* ends when the destination host claims the node, because by then the node it
|
|
263
|
+
* is claiming has been showing the media all along.
|
|
264
|
+
*
|
|
265
|
+
* Leave it unset on an origin host, which would otherwise hand off to itself.
|
|
266
|
+
*/
|
|
267
|
+
flightId?: string;
|
|
268
|
+
}
|