@oxyhq/bloom 1.0.6 → 1.2.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.
Files changed (220) hide show
  1. package/docs/level-picker.mdx +105 -0
  2. package/docs/media-flight.mdx +272 -0
  3. package/docs/migrating-the-media-gallery.mdx +103 -0
  4. package/docs/migrating-to-1.0.mdx +1 -1
  5. package/docs/slider.mdx +3 -1
  6. package/docs/zoomable-media-gallery.mdx +116 -0
  7. package/lib/commonjs/floating/index.js.map +1 -1
  8. package/lib/commonjs/image-aspect-ratio-cache/aspect-ratio-cache.js +1 -1
  9. package/lib/commonjs/index.js +8 -0
  10. package/lib/commonjs/index.js.map +1 -1
  11. package/lib/commonjs/index.web.js +33 -25
  12. package/lib/commonjs/index.web.js.map +1 -1
  13. package/lib/commonjs/level-picker/LevelPicker.js +349 -0
  14. package/lib/commonjs/level-picker/LevelPicker.js.map +1 -0
  15. package/lib/commonjs/level-picker/LevelPicker.web.js +346 -0
  16. package/lib/commonjs/level-picker/LevelPicker.web.js.map +1 -0
  17. package/lib/commonjs/level-picker/constants.js +137 -0
  18. package/lib/commonjs/level-picker/constants.js.map +1 -0
  19. package/lib/commonjs/level-picker/index.js +13 -0
  20. package/lib/commonjs/level-picker/index.js.map +1 -0
  21. package/lib/commonjs/level-picker/index.web.js +13 -0
  22. package/lib/commonjs/level-picker/index.web.js.map +1 -0
  23. package/lib/commonjs/level-picker/types.js +6 -0
  24. package/lib/commonjs/level-picker/types.js.map +1 -0
  25. package/lib/commonjs/media-flight/MediaFlightLayer.js +151 -0
  26. package/lib/commonjs/media-flight/MediaFlightLayer.js.map +1 -0
  27. package/lib/commonjs/media-flight/MediaSurface.js +188 -0
  28. package/lib/commonjs/media-flight/MediaSurface.js.map +1 -0
  29. package/lib/commonjs/media-flight/constants.js +39 -0
  30. package/lib/commonjs/media-flight/constants.js.map +1 -0
  31. package/lib/commonjs/media-flight/expo-video-module.js +148 -0
  32. package/lib/commonjs/media-flight/expo-video-module.js.map +1 -0
  33. package/lib/commonjs/media-flight/index.js +102 -0
  34. package/lib/commonjs/media-flight/index.js.map +1 -0
  35. package/lib/commonjs/media-flight/store.js +457 -0
  36. package/lib/commonjs/media-flight/store.js.map +1 -0
  37. package/lib/commonjs/media-flight/types.js +6 -0
  38. package/lib/commonjs/media-flight/types.js.map +1 -0
  39. package/lib/commonjs/media-flight/use-media-flight.js +41 -0
  40. package/lib/commonjs/media-flight/use-media-flight.js.map +1 -0
  41. package/lib/commonjs/tab-bar/TabBarBase.js +1 -1
  42. package/lib/commonjs/{zoomable-image-gallery/ZoomableImageGallery.js → zoomable-media-gallery/ZoomableMediaGallery.js} +185 -131
  43. package/lib/commonjs/zoomable-media-gallery/ZoomableMediaGallery.js.map +1 -0
  44. package/lib/commonjs/{zoomable-image-gallery → zoomable-media-gallery}/constants.js.map +1 -1
  45. package/lib/commonjs/{zoomable-image-gallery → zoomable-media-gallery}/index.js +5 -5
  46. package/lib/commonjs/{zoomable-image-gallery → zoomable-media-gallery}/index.js.map +1 -1
  47. package/lib/commonjs/zoomable-media-gallery/types.js +6 -0
  48. package/lib/commonjs/{zoomable-image-gallery → zoomable-media-gallery}/types.js.map +1 -1
  49. package/lib/module/floating/index.js +5 -0
  50. package/lib/module/floating/index.js.map +1 -1
  51. package/lib/module/image-aspect-ratio-cache/aspect-ratio-cache.js +1 -1
  52. package/lib/module/index.js +3 -1
  53. package/lib/module/index.js.map +1 -1
  54. package/lib/module/index.web.js +3 -1
  55. package/lib/module/index.web.js.map +1 -1
  56. package/lib/module/level-picker/LevelPicker.js +343 -0
  57. package/lib/module/level-picker/LevelPicker.js.map +1 -0
  58. package/lib/module/level-picker/LevelPicker.web.js +340 -0
  59. package/lib/module/level-picker/LevelPicker.web.js.map +1 -0
  60. package/lib/module/level-picker/constants.js +131 -0
  61. package/lib/module/level-picker/constants.js.map +1 -0
  62. package/lib/module/level-picker/index.js +4 -0
  63. package/lib/module/level-picker/index.js.map +1 -0
  64. package/lib/module/level-picker/index.web.js +4 -0
  65. package/lib/module/level-picker/index.web.js.map +1 -0
  66. package/lib/module/level-picker/types.js +4 -0
  67. package/lib/module/level-picker/types.js.map +1 -0
  68. package/lib/module/media-flight/MediaFlightLayer.js +146 -0
  69. package/lib/module/media-flight/MediaFlightLayer.js.map +1 -0
  70. package/lib/module/media-flight/MediaSurface.js +180 -0
  71. package/lib/module/media-flight/MediaSurface.js.map +1 -0
  72. package/lib/module/media-flight/constants.js +35 -0
  73. package/lib/module/media-flight/constants.js.map +1 -0
  74. package/lib/module/media-flight/expo-video-module.js +142 -0
  75. package/lib/module/media-flight/expo-video-module.js.map +1 -0
  76. package/lib/module/media-flight/index.js +9 -0
  77. package/lib/module/media-flight/index.js.map +1 -0
  78. package/lib/module/media-flight/store.js +439 -0
  79. package/lib/module/media-flight/store.js.map +1 -0
  80. package/lib/module/media-flight/types.js +4 -0
  81. package/lib/module/media-flight/types.js.map +1 -0
  82. package/lib/module/media-flight/use-media-flight.js +36 -0
  83. package/lib/module/media-flight/use-media-flight.js.map +1 -0
  84. package/lib/module/tab-bar/TabBarBase.js +1 -1
  85. package/lib/module/{zoomable-image-gallery/ZoomableImageGallery.js → zoomable-media-gallery/ZoomableMediaGallery.js} +184 -130
  86. package/lib/module/zoomable-media-gallery/ZoomableMediaGallery.js.map +1 -0
  87. package/lib/module/{zoomable-image-gallery → zoomable-media-gallery}/constants.js.map +1 -1
  88. package/lib/module/zoomable-media-gallery/index.js +5 -0
  89. package/lib/module/zoomable-media-gallery/index.js.map +1 -0
  90. package/lib/module/zoomable-media-gallery/types.js +4 -0
  91. package/lib/module/{zoomable-image-gallery → zoomable-media-gallery}/types.js.map +1 -1
  92. package/lib/typescript/commonjs/floating/index.d.ts +5 -0
  93. package/lib/typescript/commonjs/floating/index.d.ts.map +1 -1
  94. package/lib/typescript/commonjs/index.d.ts +2 -0
  95. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  96. package/lib/typescript/commonjs/index.web.d.ts +2 -0
  97. package/lib/typescript/commonjs/index.web.d.ts.map +1 -1
  98. package/lib/typescript/commonjs/level-picker/LevelPicker.d.ts +6 -0
  99. package/lib/typescript/commonjs/level-picker/LevelPicker.d.ts.map +1 -0
  100. package/lib/typescript/commonjs/level-picker/LevelPicker.web.d.ts +6 -0
  101. package/lib/typescript/commonjs/level-picker/LevelPicker.web.d.ts.map +1 -0
  102. package/lib/typescript/commonjs/level-picker/constants.d.ts +93 -0
  103. package/lib/typescript/commonjs/level-picker/constants.d.ts.map +1 -0
  104. package/lib/typescript/commonjs/level-picker/index.d.ts +3 -0
  105. package/lib/typescript/commonjs/level-picker/index.d.ts.map +1 -0
  106. package/lib/typescript/commonjs/level-picker/index.web.d.ts +3 -0
  107. package/lib/typescript/commonjs/level-picker/index.web.d.ts.map +1 -0
  108. package/lib/typescript/commonjs/level-picker/types.d.ts +63 -0
  109. package/lib/typescript/commonjs/level-picker/types.d.ts.map +1 -0
  110. package/lib/typescript/commonjs/media-flight/MediaFlightLayer.d.ts +11 -0
  111. package/lib/typescript/commonjs/media-flight/MediaFlightLayer.d.ts.map +1 -0
  112. package/lib/typescript/commonjs/media-flight/MediaSurface.d.ts +106 -0
  113. package/lib/typescript/commonjs/media-flight/MediaSurface.d.ts.map +1 -0
  114. package/lib/typescript/commonjs/media-flight/constants.d.ts +18 -0
  115. package/lib/typescript/commonjs/media-flight/constants.d.ts.map +1 -0
  116. package/lib/typescript/commonjs/media-flight/expo-video-module.d.ts +103 -0
  117. package/lib/typescript/commonjs/media-flight/expo-video-module.d.ts.map +1 -0
  118. package/lib/typescript/commonjs/media-flight/index.d.ts +9 -0
  119. package/lib/typescript/commonjs/media-flight/index.d.ts.map +1 -0
  120. package/lib/typescript/commonjs/media-flight/store.d.ts +132 -0
  121. package/lib/typescript/commonjs/media-flight/store.d.ts.map +1 -0
  122. package/lib/typescript/commonjs/media-flight/types.d.ts +195 -0
  123. package/lib/typescript/commonjs/media-flight/types.d.ts.map +1 -0
  124. package/lib/typescript/commonjs/media-flight/use-media-flight.d.ts +4 -0
  125. package/lib/typescript/commonjs/media-flight/use-media-flight.d.ts.map +1 -0
  126. package/lib/typescript/commonjs/zoomable-media-gallery/ZoomableMediaGallery.d.ts +5 -0
  127. package/lib/typescript/commonjs/zoomable-media-gallery/ZoomableMediaGallery.d.ts.map +1 -0
  128. package/lib/typescript/commonjs/{zoomable-image-gallery → zoomable-media-gallery}/constants.d.ts.map +1 -1
  129. package/lib/typescript/commonjs/zoomable-media-gallery/index.d.ts +4 -0
  130. package/lib/typescript/commonjs/{zoomable-image-gallery → zoomable-media-gallery}/index.d.ts.map +1 -1
  131. package/lib/typescript/commonjs/zoomable-media-gallery/types.d.ts +90 -0
  132. package/lib/typescript/commonjs/zoomable-media-gallery/types.d.ts.map +1 -0
  133. package/lib/typescript/module/floating/index.d.ts +5 -0
  134. package/lib/typescript/module/floating/index.d.ts.map +1 -1
  135. package/lib/typescript/module/index.d.ts +2 -0
  136. package/lib/typescript/module/index.d.ts.map +1 -1
  137. package/lib/typescript/module/index.web.d.ts +2 -0
  138. package/lib/typescript/module/index.web.d.ts.map +1 -1
  139. package/lib/typescript/module/level-picker/LevelPicker.d.ts +6 -0
  140. package/lib/typescript/module/level-picker/LevelPicker.d.ts.map +1 -0
  141. package/lib/typescript/module/level-picker/LevelPicker.web.d.ts +6 -0
  142. package/lib/typescript/module/level-picker/LevelPicker.web.d.ts.map +1 -0
  143. package/lib/typescript/module/level-picker/constants.d.ts +93 -0
  144. package/lib/typescript/module/level-picker/constants.d.ts.map +1 -0
  145. package/lib/typescript/module/level-picker/index.d.ts +3 -0
  146. package/lib/typescript/module/level-picker/index.d.ts.map +1 -0
  147. package/lib/typescript/module/level-picker/index.web.d.ts +3 -0
  148. package/lib/typescript/module/level-picker/index.web.d.ts.map +1 -0
  149. package/lib/typescript/module/level-picker/types.d.ts +63 -0
  150. package/lib/typescript/module/level-picker/types.d.ts.map +1 -0
  151. package/lib/typescript/module/media-flight/MediaFlightLayer.d.ts +11 -0
  152. package/lib/typescript/module/media-flight/MediaFlightLayer.d.ts.map +1 -0
  153. package/lib/typescript/module/media-flight/MediaSurface.d.ts +106 -0
  154. package/lib/typescript/module/media-flight/MediaSurface.d.ts.map +1 -0
  155. package/lib/typescript/module/media-flight/constants.d.ts +18 -0
  156. package/lib/typescript/module/media-flight/constants.d.ts.map +1 -0
  157. package/lib/typescript/module/media-flight/expo-video-module.d.ts +103 -0
  158. package/lib/typescript/module/media-flight/expo-video-module.d.ts.map +1 -0
  159. package/lib/typescript/module/media-flight/index.d.ts +9 -0
  160. package/lib/typescript/module/media-flight/index.d.ts.map +1 -0
  161. package/lib/typescript/module/media-flight/store.d.ts +132 -0
  162. package/lib/typescript/module/media-flight/store.d.ts.map +1 -0
  163. package/lib/typescript/module/media-flight/types.d.ts +195 -0
  164. package/lib/typescript/module/media-flight/types.d.ts.map +1 -0
  165. package/lib/typescript/module/media-flight/use-media-flight.d.ts +4 -0
  166. package/lib/typescript/module/media-flight/use-media-flight.d.ts.map +1 -0
  167. package/lib/typescript/module/zoomable-media-gallery/ZoomableMediaGallery.d.ts +5 -0
  168. package/lib/typescript/module/zoomable-media-gallery/ZoomableMediaGallery.d.ts.map +1 -0
  169. package/lib/typescript/module/{zoomable-image-gallery → zoomable-media-gallery}/constants.d.ts.map +1 -1
  170. package/lib/typescript/module/zoomable-media-gallery/index.d.ts +4 -0
  171. package/lib/typescript/module/{zoomable-image-gallery → zoomable-media-gallery}/index.d.ts.map +1 -1
  172. package/lib/typescript/module/zoomable-media-gallery/types.d.ts +90 -0
  173. package/lib/typescript/module/zoomable-media-gallery/types.d.ts.map +1 -0
  174. package/package.json +61 -8
  175. package/src/floating/index.ts +5 -0
  176. package/src/image-aspect-ratio-cache/aspect-ratio-cache.ts +1 -1
  177. package/src/index.ts +4 -1
  178. package/src/index.web.ts +4 -1
  179. package/src/level-picker/LevelPicker.tsx +365 -0
  180. package/src/level-picker/LevelPicker.web.tsx +374 -0
  181. package/src/level-picker/constants.ts +131 -0
  182. package/src/level-picker/index.ts +2 -0
  183. package/src/level-picker/index.web.ts +2 -0
  184. package/src/level-picker/types.ts +63 -0
  185. package/src/media-flight/MediaFlightLayer.tsx +144 -0
  186. package/src/media-flight/MediaSurface.tsx +233 -0
  187. package/src/media-flight/constants.ts +32 -0
  188. package/src/media-flight/expo-video-module.ts +181 -0
  189. package/src/media-flight/index.ts +33 -0
  190. package/src/media-flight/store.ts +497 -0
  191. package/src/media-flight/types.ts +210 -0
  192. package/src/media-flight/use-media-flight.ts +42 -0
  193. package/src/tab-bar/TabBarBase.tsx +1 -1
  194. package/src/{zoomable-image-gallery/ZoomableImageGallery.tsx → zoomable-media-gallery/ZoomableMediaGallery.tsx} +192 -117
  195. package/src/zoomable-media-gallery/index.ts +11 -0
  196. package/src/zoomable-media-gallery/types.ts +100 -0
  197. package/docs/zoomable-image-gallery.mdx +0 -66
  198. package/lib/commonjs/zoomable-image-gallery/ZoomableImageGallery.js.map +0 -1
  199. package/lib/commonjs/zoomable-image-gallery/types.js +0 -2
  200. package/lib/module/zoomable-image-gallery/ZoomableImageGallery.js.map +0 -1
  201. package/lib/module/zoomable-image-gallery/index.js +0 -5
  202. package/lib/module/zoomable-image-gallery/index.js.map +0 -1
  203. package/lib/module/zoomable-image-gallery/types.js +0 -2
  204. package/lib/typescript/commonjs/zoomable-image-gallery/ZoomableImageGallery.d.ts +0 -5
  205. package/lib/typescript/commonjs/zoomable-image-gallery/ZoomableImageGallery.d.ts.map +0 -1
  206. package/lib/typescript/commonjs/zoomable-image-gallery/index.d.ts +0 -4
  207. package/lib/typescript/commonjs/zoomable-image-gallery/types.d.ts +0 -56
  208. package/lib/typescript/commonjs/zoomable-image-gallery/types.d.ts.map +0 -1
  209. package/lib/typescript/module/zoomable-image-gallery/ZoomableImageGallery.d.ts +0 -5
  210. package/lib/typescript/module/zoomable-image-gallery/ZoomableImageGallery.d.ts.map +0 -1
  211. package/lib/typescript/module/zoomable-image-gallery/index.d.ts +0 -4
  212. package/lib/typescript/module/zoomable-image-gallery/types.d.ts +0 -56
  213. package/lib/typescript/module/zoomable-image-gallery/types.d.ts.map +0 -1
  214. package/src/zoomable-image-gallery/index.ts +0 -9
  215. package/src/zoomable-image-gallery/types.ts +0 -59
  216. /package/lib/commonjs/{zoomable-image-gallery → zoomable-media-gallery}/constants.js +0 -0
  217. /package/lib/module/{zoomable-image-gallery → zoomable-media-gallery}/constants.js +0 -0
  218. /package/lib/typescript/commonjs/{zoomable-image-gallery → zoomable-media-gallery}/constants.d.ts +0 -0
  219. /package/lib/typescript/module/{zoomable-image-gallery → zoomable-media-gallery}/constants.d.ts +0 -0
  220. /package/src/{zoomable-image-gallery → zoomable-media-gallery}/constants.ts +0 -0
@@ -0,0 +1,497 @@
1
+ /**
2
+ * The media-flight registry: anchors, live surfaces, and the animation that
3
+ * moves them.
4
+ *
5
+ * ## Why the animation lives HERE and not in the layer
6
+ *
7
+ * A flight is started by an imperative call (`flyTo`) from a screen that may be
8
+ * about to unmount. If the layer drove the animation from an effect keyed on the
9
+ * flight record, the first frame would be one commit late — and on web, where
10
+ * the origin route is genuinely gone by the next commit, that frame is the one
11
+ * the user sees. Driving the shared value at the call site makes the leg start
12
+ * exactly when the caller says it does and leaves the layer a pure painter.
13
+ *
14
+ * The shared values are created with `makeMutable` rather than `useSharedValue`
15
+ * for the same reason: they belong to a flight, not to a component, and the
16
+ * component that paints a flight is not the one that starts it.
17
+ *
18
+ * ## Why the registry is anchored on `globalThis`
19
+ *
20
+ * Same reason as the portal group and the overlay stack: `exports` ships a
21
+ * `react-native` → `src` condition beside the `lib/module` and `lib/commonjs`
22
+ * forks, and a bundler can resolve `@oxyhq/bloom/media-flight` through different
23
+ * conditions from different call sites. Two physical copies of this module would
24
+ * each keep their own registry, and the `<MediaFlightLayer>` mounted at the app
25
+ * root would never see the flight a screen started.
26
+ */
27
+ import { Platform } from 'react-native';
28
+ import {
29
+ Easing,
30
+ makeMutable,
31
+ runOnJS,
32
+ withSpring,
33
+ withTiming,
34
+ type SharedValue,
35
+ } from 'react-native-reanimated';
36
+
37
+ import { RADIUS } from '../design-tokens';
38
+ import { SURFACE_MOUNT_TIMEOUT_MS } from './constants';
39
+ import {
40
+ CLOSE_DURATION_WEB,
41
+ CLOSE_SPRING,
42
+ OPEN_DURATION_WEB,
43
+ OPEN_SPRING,
44
+ } from '../zoomable-media-gallery/constants';
45
+ import type {
46
+ MediaFlight,
47
+ MediaFlightAnchorNode,
48
+ MediaFlightOptions,
49
+ MediaSurfaceContent,
50
+ MeasuredRect,
51
+ } from './types';
52
+
53
+ /**
54
+ * Default corner radius of a flying box — Bloom's `radius-12`, the same value
55
+ * the media gallery fits its media at, so a flight that lands in the gallery
56
+ * lands on an identical shape.
57
+ */
58
+ export const DEFAULT_FLIGHT_CORNER_RADIUS = RADIUS['radius-12'];
59
+
60
+ /**
61
+ * Bookkeeping the layer does not RENDER, kept out of the `MediaFlight` record
62
+ * on purpose.
63
+ *
64
+ * The record is replaced on every visual change so its identity tells React the
65
+ * leg moved; folding these flags into it would churn that identity on events
66
+ * nobody paints, and folding the record into these would put mutable state back
67
+ * in a rendered position. They are separate because they answer to different
68
+ * consumers.
69
+ */
70
+ interface FlightStatus {
71
+ /** The layer has committed a surface for this id. */
72
+ mounted: boolean;
73
+ /** The current leg's animation has finished. */
74
+ settled: boolean;
75
+ /** A destination surface has reported that it is live. */
76
+ handedOff: boolean;
77
+ /** Waiters on `flyTo`'s promise, resolved once `mounted` turns true. */
78
+ mountWaiters: Array<() => void>;
79
+ mountTimer: ReturnType<typeof setTimeout> | null;
80
+ }
81
+
82
+ interface Registry {
83
+ anchors: Map<string, MediaFlightAnchorNode>;
84
+ flights: Map<string, MediaFlight>;
85
+ status: Map<string, FlightStatus>;
86
+ listeners: Set<() => void>;
87
+ /**
88
+ * The array handed to `useSyncExternalStore`. Cached because that hook
89
+ * compares snapshots by IDENTITY: rebuilding it per call renders the layer
90
+ * forever (and React throws "getSnapshot should be cached" in development).
91
+ */
92
+ snapshot: readonly MediaFlight[];
93
+ progress: SharedValue<number>;
94
+ }
95
+
96
+ declare global {
97
+ // eslint-disable-next-line no-var
98
+ var __oxyhq_bloom_media_flight__: Registry | undefined;
99
+ }
100
+
101
+ function emptyRegistry(): Registry {
102
+ return {
103
+ anchors: new Map(),
104
+ flights: new Map(),
105
+ status: new Map(),
106
+ listeners: new Set(),
107
+ snapshot: [],
108
+ progress: makeMutable(0),
109
+ };
110
+ }
111
+
112
+ function statusFor(id: string): FlightStatus {
113
+ const reg = registry();
114
+ let status = reg.status.get(id);
115
+ if (!status) {
116
+ status = {
117
+ mounted: false,
118
+ settled: false,
119
+ handedOff: false,
120
+ mountWaiters: [],
121
+ mountTimer: null,
122
+ };
123
+ reg.status.set(id, status);
124
+ }
125
+ return status;
126
+ }
127
+
128
+ function clearTimers(status: FlightStatus): void {
129
+ if (status.mountTimer !== null) clearTimeout(status.mountTimer);
130
+ status.mountTimer = null;
131
+ }
132
+
133
+ function resolveMountWaiters(status: FlightStatus): void {
134
+ const waiters = status.mountWaiters;
135
+ status.mountWaiters = [];
136
+ if (status.mountTimer !== null) {
137
+ clearTimeout(status.mountTimer);
138
+ status.mountTimer = null;
139
+ }
140
+ for (const waiter of waiters) waiter();
141
+ }
142
+
143
+ function registry(): Registry {
144
+ globalThis.__oxyhq_bloom_media_flight__ ??= emptyRegistry();
145
+ return globalThis.__oxyhq_bloom_media_flight__;
146
+ }
147
+
148
+ function publish(reg: Registry): void {
149
+ reg.snapshot = [...reg.flights.values()];
150
+ for (const listener of reg.listeners) listener();
151
+ }
152
+
153
+ /** Subscribe to the live-surface set. The layer's `useSyncExternalStore` half. */
154
+ export function subscribeToFlights(listener: () => void): () => void {
155
+ const reg = registry();
156
+ reg.listeners.add(listener);
157
+ return () => {
158
+ reg.listeners.delete(listener);
159
+ };
160
+ }
161
+
162
+ /** The live surfaces, oldest first. Stable identity between changes. */
163
+ export function getFlights(): readonly MediaFlight[] {
164
+ return registry().snapshot;
165
+ }
166
+
167
+ /** The shared progress value of the most recent leg. */
168
+ export function flightProgress(): SharedValue<number> {
169
+ return registry().progress;
170
+ }
171
+
172
+ /**
173
+ * Register (or clear) the node that anchors `id`.
174
+ *
175
+ * A `Map` re-insertion moves the key to the end, which is what makes "the most
176
+ * recently registered node is the anchor" true even when an origin screen's
177
+ * callback ref fires again after the destination has registered.
178
+ */
179
+ export function registerAnchor(id: string, node: MediaFlightAnchorNode | null): void {
180
+ const reg = registry();
181
+ reg.anchors.delete(id);
182
+ if (node !== null) reg.anchors.set(id, node);
183
+ }
184
+
185
+ export function measureAnchor(id: string): Promise<MeasuredRect | null> {
186
+ const node = registry().anchors.get(id);
187
+ if (!node) return Promise.resolve(null);
188
+ return new Promise<MeasuredRect | null>((resolve) => {
189
+ node.measureInWindow((x, y, width, height) => {
190
+ // A zero-area measurement is what a node reports while it is laid out but
191
+ // not yet on screen. Flying to it would collapse the surface to a point,
192
+ // so it reads as "no anchor" and the caller skips the transition.
193
+ resolve(width > 0 && height > 0 ? { x, y, width, height } : null);
194
+ });
195
+ });
196
+ }
197
+
198
+ /** Whether a surface is currently live for `id`. */
199
+ export function hasFlight(id: string): boolean {
200
+ return registry().flights.has(id);
201
+ }
202
+
203
+ /**
204
+ * Take the surface for `id` live (if it is not already) and move it to `rect`.
205
+ *
206
+ * Retargeting mid-flight keeps the SAME `MediaFlight` record — and therefore the
207
+ * same mounted `VideoView` — which is the whole point. A second record would be
208
+ * a second surface for one id, and swapping between them is exactly the remount
209
+ * that restarts playback.
210
+ *
211
+ * ## Why it returns a promise, and what the promise means
212
+ *
213
+ * It resolves once the layer has COMMITTED a surface for this id — not once
214
+ * that surface has painted.
215
+ *
216
+ * The distinction is the whole ordering contract. expo-video's web player keeps
217
+ * a `Set` of mounted `<video>` elements and, on each mount, copies
218
+ * `currentTime`, play state, volume, muted and rate from `[...set][0]`. If the
219
+ * set is EMPTY when the new element mounts, it returns early and the element
220
+ * starts at zero — a restart. So the layer's surface has to enter that set while
221
+ * the origin's is still in it, which means the caller must not tear the origin
222
+ * down (on web: must not navigate) until this promise resolves.
223
+ *
224
+ * That knowledge lives here rather than in the caller, which is the point: a
225
+ * consumer that has to know the mount order is a consumer that will get it wrong
226
+ * once and produce an intermittent restart nobody can reproduce locally.
227
+ *
228
+ * Painting is deliberately NOT waited on. `MediaSurface` renders the poster
229
+ * BEHIND the video on both platforms, and neither an unpainted `<video>` (no
230
+ * `poster` attribute is set) nor an Android `TextureView` with the ExoPlayer
231
+ * shutter off draws anything opaque — so the still shows through until the first
232
+ * frame lands, and there is no black box to hide. Waiting for a decode would
233
+ * trade an invisible problem for a laggy tap.
234
+ */
235
+ export function flyTo(
236
+ id: string,
237
+ rect: MeasuredRect,
238
+ content: MediaSurfaceContent,
239
+ options?: MediaFlightOptions,
240
+ ): Promise<void> {
241
+ const reg = registry();
242
+ const existing = reg.flights.get(id);
243
+
244
+ if (existing) {
245
+ // A retarget REPLACES the record rather than mutating it. Identity is what
246
+ // tells React (and the React Compiler) that the leg changed: a mutated
247
+ // record has the same identity, so a memoized read of `flight.from` would
248
+ // keep returning the previous leg's origin forever — the exact
249
+ // external-mutable-state stale read the compiler is documented to produce.
250
+ // The shared value is carried over untouched, so the surface resumes rather
251
+ // than restarting.
252
+ const next: MediaFlight = {
253
+ ...existing,
254
+ // The leg that was running is abandoned where it stands: `from` becomes
255
+ // the rect the surface is CURRENTLY at, so a retarget never snaps.
256
+ from: interpolateRect(existing.from, existing.to, existing.progress.value),
257
+ to: rect,
258
+ content,
259
+ cornerRadius: options?.cornerRadius ?? existing.cornerRadius,
260
+ contentFit: options?.contentFit ?? existing.contentFit,
261
+ // NOT retargetable: `MediaSurface` captures it on first render because
262
+ // expo-video forbids changing it at runtime, so a new value here would be
263
+ // silently ignored by the view it is meant to configure.
264
+ surfaceType: existing.surfaceType,
265
+ landing: false,
266
+ generation: existing.generation + 1,
267
+ };
268
+ reg.flights.set(id, next);
269
+ const retargeted = statusFor(id);
270
+ retargeted.settled = false;
271
+ animate(next, false);
272
+ publish(reg);
273
+ return awaitSurfaceMount(id);
274
+ }
275
+
276
+ // Nothing live yet. `from` is the origin rect the caller measured; without one
277
+ // the surface simply appears at `rect` rather than flying in from a corner.
278
+ const flight: MediaFlight = {
279
+ id,
280
+ content,
281
+ from: options?.from ?? rect,
282
+ to: rect,
283
+ cornerRadius: options?.cornerRadius ?? DEFAULT_FLIGHT_CORNER_RADIUS,
284
+ contentFit: options?.contentFit ?? 'cover',
285
+ surfaceType: options?.surfaceType ?? 'textureView',
286
+ progress: makeMutable(0),
287
+ landing: false,
288
+ generation: 0,
289
+ };
290
+ reg.flights.set(id, flight);
291
+ const status = statusFor(id);
292
+ status.mounted = false;
293
+ status.settled = false;
294
+ status.handedOff = false;
295
+ animate(flight, false);
296
+ publish(reg);
297
+ return awaitSurfaceMount(id);
298
+ }
299
+
300
+ /**
301
+ * Resolve once `<MediaFlightLayer>` reports it has committed the surface, or
302
+ * after {@link SURFACE_MOUNT_TIMEOUT_MS} — which is what happens when no layer
303
+ * is mounted at all. Degrading to "carry on without a transition" is right;
304
+ * never resolving would mean an app that forgot one line at its root has a feed
305
+ * whose videos cannot be opened.
306
+ */
307
+ function awaitSurfaceMount(id: string): Promise<void> {
308
+ const status = statusFor(id);
309
+ if (status.mounted) return Promise.resolve();
310
+ return new Promise<void>((resolve) => {
311
+ status.mountWaiters.push(resolve);
312
+ if (status.mountTimer !== null) return;
313
+ status.mountTimer = setTimeout(() => {
314
+ status.mountTimer = null;
315
+ warnNoLayerMounted();
316
+ resolveMountWaiters(status);
317
+ }, SURFACE_MOUNT_TIMEOUT_MS);
318
+ });
319
+ }
320
+
321
+ /**
322
+ * The layer calls this from the surface's mount effect. It is the commit signal
323
+ * `flyTo` waits on, and the only thing that makes the ordering contract above
324
+ * true rather than hopeful.
325
+ */
326
+ export function notifySurfaceMounted(id: string): void {
327
+ const status = statusFor(id);
328
+ status.mounted = true;
329
+ resolveMountWaiters(status);
330
+ }
331
+
332
+ /** The layer calls this when a leg's animation finishes. */
333
+ export function notifySurfaceSettled(id: string): void {
334
+ const status = statusFor(id);
335
+ status.settled = true;
336
+ if (status.handedOff) releaseFlight(id);
337
+ }
338
+
339
+ /**
340
+ * A destination surface reporting that it is LIVE — it has presented a frame of
341
+ * its own, so the flying copy is now redundant and can go.
342
+ *
343
+ * This is the release condition, and it is deliberately not the animation's
344
+ * clock. Measured against production, a fullscreen video route needs ~1 s from
345
+ * tap to first presented frame; releasing when a ~300 ms animation ends would
346
+ * leave ~700 ms with nothing live on screen — the hole the transition exists to
347
+ * cover, made longer.
348
+ *
349
+ * Wire it by giving the destination's `<MediaSurface>` a `flightId`; it calls
350
+ * this itself on its first frame. A destination that renders something other
351
+ * than a Bloom surface can call it directly.
352
+ */
353
+ export function handOffFlight(id: string): void {
354
+ const reg = registry();
355
+ if (!reg.flights.has(id)) return;
356
+ const status = statusFor(id);
357
+ status.handedOff = true;
358
+ // Mid-flight the surface is still travelling: releasing now would make it
359
+ // vanish somewhere between the two rects. `notifySurfaceSettled` picks it up.
360
+ if (status.settled) releaseFlight(id);
361
+ }
362
+
363
+ /**
364
+ * Aim the live surface at its anchor and release it once it lands.
365
+ *
366
+ * With no measurable anchor there is nothing to fly to, so the surface is
367
+ * released immediately — whoever owns the media renders its own copy again at
368
+ * that point, and holding a flying copy over it would double the video.
369
+ */
370
+ export function flyBack(id: string): void {
371
+ const flight = registry().flights.get(id);
372
+ if (!flight) return;
373
+
374
+ void measureAnchor(id).then((rect) => {
375
+ // Re-read: the flight may have been released or retargeted while the
376
+ // measurement was outstanding (`measureInWindow` is asynchronous on both
377
+ // platforms), and landing a stale record would fly the wrong surface.
378
+ const reg = registry();
379
+ if (reg.flights.get(id) !== flight) return;
380
+ if (rect === null) {
381
+ releaseFlight(id);
382
+ return;
383
+ }
384
+ const landing: MediaFlight = {
385
+ ...flight,
386
+ from: interpolateRect(flight.from, flight.to, flight.progress.value),
387
+ to: rect,
388
+ landing: true,
389
+ generation: flight.generation + 1,
390
+ };
391
+ reg.flights.set(id, landing);
392
+ animate(landing, true);
393
+ publish(reg);
394
+ });
395
+ }
396
+
397
+ /** Drop the surface for `id`. Called when a landing leg settles, and on teardown. */
398
+ export function releaseFlight(id: string): void {
399
+ const reg = registry();
400
+ const status = reg.status.get(id);
401
+ if (status) {
402
+ // A waiter still parked on `flyTo`'s promise would otherwise hang until its
403
+ // own timeout, holding a caller that has nothing left to wait for.
404
+ clearTimers(status);
405
+ resolveMountWaiters(status);
406
+ reg.status.delete(id);
407
+ }
408
+ if (!reg.flights.delete(id)) return;
409
+ publish(reg);
410
+ }
411
+
412
+ /** Test seam — drops every anchor and surface. */
413
+ export function resetMediaFlight(): void {
414
+ const existing = globalThis.__oxyhq_bloom_media_flight__;
415
+ // Timers outlive a registry swap: an unfired mount timeout from the previous
416
+ // suite would warn — and resolve a waiter nobody is holding — inside the next.
417
+ if (existing) for (const status of existing.status.values()) clearTimers(status);
418
+ globalThis.__oxyhq_bloom_media_flight__ = emptyRegistry();
419
+ hasWarnedNoLayer = false;
420
+ }
421
+
422
+ let hasWarnedNoLayer = false;
423
+
424
+ /**
425
+ * One warning per module lifetime, and none in production — the same mechanism
426
+ * as the optional-peer boundaries. A missing `<MediaFlightLayer>` is otherwise
427
+ * completely silent: every `flyTo` resolves on its timeout and the app simply
428
+ * never shows a transition, which looks like the feature not being wired rather
429
+ * than the root not being.
430
+ */
431
+ function warnNoLayerMounted(): void {
432
+ if (process.env.NODE_ENV === 'production' || hasWarnedNoLayer) return;
433
+ hasWarnedNoLayer = true;
434
+ // eslint-disable-next-line no-console
435
+ console.warn(
436
+ '[Bloom] flyTo() timed out waiting for a media surface to mount. Nothing is ' +
437
+ 'rendering <MediaFlightLayer>, so no media transition will ever run and a ' +
438
+ 'video opened from a feed will restart. Mount it ONCE at the app root ' +
439
+ '(app/_layout.tsx), above the router.',
440
+ );
441
+ }
442
+
443
+ /** Rect at `t` along `from` → `to`, so a retarget resumes rather than snapping. */
444
+ function interpolateRect(from: MeasuredRect, to: MeasuredRect, t: number): MeasuredRect {
445
+ const clamped = t < 0 ? 0 : t > 1 ? 1 : t;
446
+ return {
447
+ x: from.x + (to.x - from.x) * clamped,
448
+ y: from.y + (to.y - from.y) * clamped,
449
+ width: from.width + (to.width - from.width) * clamped,
450
+ height: from.height + (to.height - from.height) * clamped,
451
+ };
452
+ }
453
+
454
+ /**
455
+ * Run one leg: 0 → 1 on the flight's own value, and on the shared `progress` a
456
+ * consumer reads.
457
+ *
458
+ * Both are driven with the SAME curve rather than one deriving from the other,
459
+ * because deriving would need a `useAnimatedReaction` — a hook, in a module with
460
+ * no component to hang it on. Two animations on two shared values cost a few
461
+ * bytes on the UI thread and keep the driver at the call site.
462
+ *
463
+ * The springs and the web timings are the media gallery's, imported rather than
464
+ * re-tuned: a flight that lands in the gallery and the gallery's own open
465
+ * transition are the same motion, and two copies of a spring config drift.
466
+ */
467
+ function animate(flight: MediaFlight, landing: boolean): void {
468
+ const reg = registry();
469
+ const id = flight.id;
470
+ // EVERY leg reports its settlement, because the release condition is now
471
+ // "settled AND the destination is live" and either can arrive first. A
472
+ // `finished === false` callback means the leg was interrupted by a retarget,
473
+ // which owns the surface from then on.
474
+ //
475
+ // A LANDING leg is the one case that releases on its own: it has flown back to
476
+ // its anchor and there is nothing left to cover.
477
+ const onSettled = (finished?: boolean) => {
478
+ 'worklet';
479
+ if (finished === false) return;
480
+ runOnJS(landing ? releaseFlight : notifySurfaceSettled)(id);
481
+ };
482
+
483
+ flight.progress.value = 0;
484
+ reg.progress.value = 0;
485
+
486
+ if (Platform.OS === 'web') {
487
+ const duration = landing ? CLOSE_DURATION_WEB : OPEN_DURATION_WEB;
488
+ const easing = landing ? Easing.in(Easing.cubic) : Easing.out(Easing.cubic);
489
+ flight.progress.value = withTiming(1, { duration, easing }, onSettled);
490
+ reg.progress.value = withTiming(1, { duration, easing });
491
+ return;
492
+ }
493
+
494
+ const spring = landing ? CLOSE_SPRING : OPEN_SPRING;
495
+ flight.progress.value = withSpring(1, spring, onSettled);
496
+ reg.progress.value = withSpring(1, spring);
497
+ }