@1agh/maude 2.0.4 → 2.0.8

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 (272) hide show
  1. package/apps/hub/src/project-transactions/kernel.mjs +10 -1
  2. package/apps/studio/access/closure.ts +156 -16
  3. package/apps/studio/access/filters.ts +39 -5
  4. package/apps/studio/access/known-canvases.ts +24 -1
  5. package/apps/studio/access/refused-inputs.ts +89 -0
  6. package/apps/studio/actions/agents.ts +59 -0
  7. package/apps/studio/actions/ctx.ts +3 -0
  8. package/apps/studio/actions/defs/app.ts +12 -0
  9. package/apps/studio/actions/defs/sync.ts +13 -0
  10. package/apps/studio/actions/defs/view.ts +59 -3
  11. package/apps/studio/actions/keymap.gen.ts +35 -0
  12. package/apps/studio/actions/menu.ts +2 -0
  13. package/apps/studio/actions/types.ts +3 -1
  14. package/apps/studio/actions.manifest.json +148 -1
  15. package/apps/studio/actions.parity.json +31 -1
  16. package/apps/studio/annotations/ui/ai-seen.tsx +4 -3
  17. package/apps/studio/annotations/ui/element-node.tsx +139 -54
  18. package/apps/studio/annotations/ui/lod.tsx +1 -1
  19. package/apps/studio/annotations/ui/ride.ts +2 -1
  20. package/apps/studio/annotations/ui/text-style.ts +1 -1
  21. package/apps/studio/annotations-context-toolbar.tsx +5 -4
  22. package/apps/studio/annotations-layer.tsx +4 -15
  23. package/apps/studio/api.ts +425 -128
  24. package/apps/studio/artboard-activity-overlay.tsx +4 -4
  25. package/apps/studio/artboard-ai-copies.tsx +4 -4
  26. package/apps/studio/artboard-freeze.ts +108 -0
  27. package/apps/studio/artboard-live-gate.tsx +636 -0
  28. package/apps/studio/artboard-live.ts +831 -0
  29. package/apps/studio/artboard-placeholder.ts +197 -0
  30. package/apps/studio/artboard-still-raster.ts +485 -0
  31. package/apps/studio/artboard-still-warmup.ts +221 -0
  32. package/apps/studio/artboard-still.ts +1188 -0
  33. package/apps/studio/artboard-webgl.ts +669 -0
  34. package/apps/studio/asset-placeholder.tsx +237 -132
  35. package/apps/studio/asset-upload-bars.tsx +4 -4
  36. package/apps/studio/bin/_agent-browser-bin.mjs +8 -0
  37. package/apps/studio/bin/_agent-browser-safe.mjs +2 -1
  38. package/apps/studio/bin/_canvas-reasons.mjs +34 -0
  39. package/apps/studio/bin/_ds-check-live.mjs +37 -0
  40. package/apps/studio/bin/_ds-check.mjs +4 -1
  41. package/apps/studio/bin/_ds-prove-judge.mjs +167 -0
  42. package/apps/studio/bin/_ds-prove-playwright.mjs +183 -105
  43. package/apps/studio/bin/_import-asset.mjs +2 -1
  44. package/apps/studio/bin/_perf-probe-open.mjs +364 -0
  45. package/apps/studio/bin/_perf-probe.mjs +2 -1
  46. package/apps/studio/bin/_perf-shared.mjs +12 -7
  47. package/apps/studio/bin/_pw-launch.mjs +26 -1
  48. package/apps/studio/bin/_studio-verb.mjs +24 -3
  49. package/apps/studio/bin/_thumb-blank.mjs +70 -0
  50. package/apps/studio/bin/_thumb-cover.mjs +35 -0
  51. package/apps/studio/bin/_thumbs-playwright.mjs +89 -12
  52. package/apps/studio/bin/ds-check.sh +5 -1
  53. package/apps/studio/bin/perf.sh +30 -1
  54. package/apps/studio/bin/server-up.sh +7 -2
  55. package/apps/studio/bin/smoke.sh +19 -0
  56. package/apps/studio/bridge/dgn-protocol.ts +35 -0
  57. package/apps/studio/canvas-build-sandbox.ts +69 -13
  58. package/apps/studio/canvas-camera.ts +90 -0
  59. package/apps/studio/canvas-comment-mount.tsx +9 -9
  60. package/apps/studio/canvas-lib.tsx +289 -44
  61. package/apps/studio/canvas-pages-view.tsx +465 -0
  62. package/apps/studio/canvas-pages.ts +305 -0
  63. package/apps/studio/canvas-rows.ts +62 -0
  64. package/apps/studio/canvas-shell.tsx +62 -16
  65. package/apps/studio/canvas-theme-icons.tsx +41 -0
  66. package/apps/studio/canvas-theme-pill.tsx +15 -6
  67. package/apps/studio/canvas-theme.ts +12 -11
  68. package/apps/studio/client/app.jsx +7 -0
  69. package/apps/studio/client/canvas-url.js +36 -1
  70. package/apps/studio/client/home/HomeAccounts.jsx +262 -0
  71. package/apps/studio/client/home/Shelves.jsx +4 -3
  72. package/apps/studio/client/home/home-accounts-model.js +121 -0
  73. package/apps/studio/client/home/home-api.js +6 -0
  74. package/apps/studio/client/home/home-app.jsx +141 -40
  75. package/apps/studio/client/home/home-model.js +172 -6
  76. package/apps/studio/client/home/home.css +169 -5
  77. package/apps/studio/client/home/shell-mode.js +8 -1
  78. package/apps/studio/client/hooks/canvas-origin-probe.js +34 -0
  79. package/apps/studio/client/hooks/use-action-keys.jsx +3 -1
  80. package/apps/studio/client/hooks/use-canvas-bridge.jsx +1 -1
  81. package/apps/studio/client/hooks/use-palette-and-panels.jsx +23 -6
  82. package/apps/studio/client/hooks/use-photo-and-timeline.jsx +16 -8
  83. package/apps/studio/client/hooks/use-project-data.jsx +13 -2
  84. package/apps/studio/client/hooks/use-shell-core.jsx +19 -9
  85. package/apps/studio/client/index-loader.ts +41 -21
  86. package/apps/studio/client/inspector/inspector-panel.jsx +35 -14
  87. package/apps/studio/client/panels/ChatPanel.jsx +27 -20
  88. package/apps/studio/client/panels/CreateProject.jsx +19 -5
  89. package/apps/studio/client/panels/caps-cache.js +46 -0
  90. package/apps/studio/client/shell/viewport.jsx +66 -22
  91. package/apps/studio/client/tree/tree.jsx +17 -0
  92. package/apps/studio/client/v2/access/access-client.js +2 -0
  93. package/apps/studio/client/v2/access/access-host.jsx +12 -1
  94. package/apps/studio/client/v2/access/trashed-banner.jsx +6 -3
  95. package/apps/studio/client/v2/ai/Advanced.jsx +12 -0
  96. package/apps/studio/client/v2/ai/Composer.jsx +27 -10
  97. package/apps/studio/client/v2/canvases/canvases-panel.jsx +24 -0
  98. package/apps/studio/client/v2/canvases/use-canvas-index.js +24 -1
  99. package/apps/studio/client/v2/code/canvas-error-card.jsx +14 -2
  100. package/apps/studio/client/v2/code/canvas-imports-card.jsx +19 -14
  101. package/apps/studio/client/v2/code/canvas-imports-words.js +60 -0
  102. package/apps/studio/client/v2/code/canvas-waiting-card.jsx +59 -5
  103. package/apps/studio/client/v2/code/canvas-waiting.js +18 -1
  104. package/apps/studio/client/v2/code/code-bus.js +5 -0
  105. package/apps/studio/client/v2/code/code-consent-sheet.jsx +87 -0
  106. package/apps/studio/client/v2/code/code-consent.js +67 -0
  107. package/apps/studio/client/v2/diagnostics/sync-details/sync-details-model.js +62 -10
  108. package/apps/studio/client/v2/diagnostics/sync-details/sync-details.jsx +314 -5
  109. package/apps/studio/client/v2/ds/advanced-host.jsx +11 -26
  110. package/apps/studio/client/v2/ds/ds-refresh.js +161 -0
  111. package/apps/studio/client/v2/ds/library-host.jsx +45 -45
  112. package/apps/studio/client/v2/ds/make-host.jsx +13 -15
  113. package/apps/studio/client/v2/ds/off-system-host.jsx +18 -19
  114. package/apps/studio/client/v2/ds/review-host.jsx +7 -1
  115. package/apps/studio/client/v2/ds/truth-host.jsx +15 -17
  116. package/apps/studio/client/v2/ds/upgrade-host.jsx +26 -10
  117. package/apps/studio/client/v2/history/branch-actions.js +13 -9
  118. package/apps/studio/client/v2/history/git-advanced.jsx +8 -1
  119. package/apps/studio/client/v2/history/git-model.js +3 -1
  120. package/apps/studio/client/v2/history/git-parts.jsx +16 -1
  121. package/apps/studio/client/v2/layers/artboard-layers.jsx +24 -5
  122. package/apps/studio/client/v2/layers/layers-request.js +49 -0
  123. package/apps/studio/client/v2/left-panel/canvases-pinned.jsx +93 -3
  124. package/apps/studio/client/v2/left-panel/ds-boards.js +39 -0
  125. package/apps/studio/client/v2/left-panel/left-panel-model.js +13 -0
  126. package/apps/studio/client/v2/left-panel/left-panel.css +24 -0
  127. package/apps/studio/client/v2/presence/faces.jsx +42 -14
  128. package/apps/studio/client/v2/shell/account-card.jsx +10 -4
  129. package/apps/studio/client/v2/shell/canvas-crash-guard.js +172 -0
  130. package/apps/studio/client/v2/shell/canvas-mode.js +17 -1
  131. package/apps/studio/client/v2/shell/floating-panels.css +37 -1
  132. package/apps/studio/client/v2/shell/github-signin-host.jsx +42 -0
  133. package/apps/studio/client/v2/shell/menu-runner.js +11 -0
  134. package/apps/studio/client/v2/shell/mode-switch.jsx +14 -3
  135. package/apps/studio/client/v2/shell/opening-skeleton.js +118 -0
  136. package/apps/studio/client/v2/shell/opening-skeleton.jsx +103 -0
  137. package/apps/studio/client/v2/shell/pages-pick.js +49 -0
  138. package/apps/studio/client/v2/shell/project-account-card.jsx +203 -0
  139. package/apps/studio/client/v2/shell/project-account-model.js +182 -0
  140. package/apps/studio/client/v2/shell/project-menu-model.js +3 -0
  141. package/apps/studio/client/v2/shell/share-cluster-host.jsx +140 -11
  142. package/apps/studio/client/v2/shell/share-cluster.css +58 -0
  143. package/apps/studio/client/v2/shell/share-cluster.jsx +26 -7
  144. package/apps/studio/client/v2/shell/shell-parity.json +8 -0
  145. package/apps/studio/client/v2/shell/team-switch.js +94 -0
  146. package/apps/studio/client/v2/shell/too-heavy-card.jsx +38 -0
  147. package/apps/studio/client/v2/shell/use-account.js +8 -1
  148. package/apps/studio/client/v2/shell/use-canvas-mode-lane.js +30 -0
  149. package/apps/studio/collab/persistence.ts +9 -1
  150. package/apps/studio/collab/registry.ts +32 -0
  151. package/apps/studio/collab/room.ts +8 -0
  152. package/apps/studio/comments-overlay.tsx +12 -11
  153. package/apps/studio/config-reload-wire.ts +56 -0
  154. package/apps/studio/config.schema.json +6 -0
  155. package/apps/studio/context.ts +37 -1
  156. package/apps/studio/dist/client.bundle.js +1198 -1198
  157. package/apps/studio/dist/comment-mount.js +2 -2
  158. package/apps/studio/dist/styles.css +1 -1
  159. package/apps/studio/ds/board/board.css +6 -0
  160. package/apps/studio/ds/board/ds-bar.tsx +21 -3
  161. package/apps/studio/ds/board/specimen.tsx +16 -2
  162. package/apps/studio/ds/board/template.ts +39 -19
  163. package/apps/studio/ds/canvas-cli.ts +58 -2
  164. package/apps/studio/ds/check.ts +26 -1
  165. package/apps/studio/ds/cli.ts +14 -4
  166. package/apps/studio/ds/component-list.ts +2 -1
  167. package/apps/studio/ds/convert.ts +34 -6
  168. package/apps/studio/ds/default.ts +6 -1
  169. package/apps/studio/ds/ds-resolver.ts +58 -8
  170. package/apps/studio/ds/ds-verb.ts +13 -0
  171. package/apps/studio/ds/engine.ts +3 -1
  172. package/apps/studio/ds/folder-systems.ts +199 -0
  173. package/apps/studio/ds/pairing-css.ts +2 -1
  174. package/apps/studio/ds/post-build.ts +38 -0
  175. package/apps/studio/ds/rename.ts +10 -3
  176. package/apps/studio/ds/shell-fallbacks.ts +2 -1
  177. package/apps/studio/ds/upgrade-apply.ts +27 -5
  178. package/apps/studio/ds/upgrade-cli.ts +22 -2
  179. package/apps/studio/ds/upgrade-prove.ts +198 -15
  180. package/apps/studio/ds/upgrade-stage.ts +10 -3
  181. package/apps/studio/editing/ai-point-ring.tsx +4 -4
  182. package/apps/studio/editing/ai-scope-outline.tsx +4 -4
  183. package/apps/studio/editing/align-bar.tsx +4 -4
  184. package/apps/studio/editing/anchor-report.ts +3 -2
  185. package/apps/studio/editing/artboard-handles.tsx +4 -4
  186. package/apps/studio/editing/corner-dots.tsx +4 -4
  187. package/apps/studio/editing/crop-mode.tsx +4 -4
  188. package/apps/studio/editing/drop-chip.tsx +4 -4
  189. package/apps/studio/editing/empty-report.ts +3 -2
  190. package/apps/studio/editing/focus-model.tsx +4 -4
  191. package/apps/studio/editing/gap-bands.tsx +3 -3
  192. package/apps/studio/editing/hold-rings.tsx +4 -4
  193. package/apps/studio/editing/lock-badge.tsx +3 -3
  194. package/apps/studio/editing/long-page.ts +23 -1
  195. package/apps/studio/editing/nudge.tsx +2 -2
  196. package/apps/studio/editing/page-activity.ts +4 -3
  197. package/apps/studio/editing/paste-bar.tsx +4 -4
  198. package/apps/studio/editing/pen/overlay.tsx +4 -3
  199. package/apps/studio/editing/place-freely.tsx +3 -3
  200. package/apps/studio/editing/reorder-slot.tsx +3 -3
  201. package/apps/studio/editing/selection.tsx +4 -4
  202. package/apps/studio/editing/size-ghost.tsx +4 -4
  203. package/apps/studio/editing/tab-order.tsx +4 -4
  204. package/apps/studio/editing/text-in-place.tsx +4 -4
  205. package/apps/studio/editing/undo-preview.tsx +3 -3
  206. package/apps/studio/equal-spacing-handles.tsx +3 -3
  207. package/apps/studio/frame-clock.ts +89 -0
  208. package/apps/studio/history/trash.ts +6 -0
  209. package/apps/studio/home/accounts.ts +87 -0
  210. package/apps/studio/home/index-reader.ts +24 -2
  211. package/apps/studio/home/routes.ts +19 -0
  212. package/apps/studio/http.ts +80 -34
  213. package/apps/studio/index/ds-usage.ts +4 -1
  214. package/apps/studio/index-cache.ts +272 -0
  215. package/apps/studio/inspect-overlay.tsx +4 -3
  216. package/apps/studio/inspect.ts +7 -2
  217. package/apps/studio/link-chip-overlay.tsx +4 -3
  218. package/apps/studio/measure-overlay.tsx +4 -3
  219. package/apps/studio/present/pointer-dot.tsx +3 -2
  220. package/apps/studio/present/present-media.ts +6 -2
  221. package/apps/studio/present/present-stage.tsx +3 -2
  222. package/apps/studio/preview/preview-camera.tsx +3 -2
  223. package/apps/studio/routes/ds-convert.ts +18 -0
  224. package/apps/studio/routes/sync.ts +176 -0
  225. package/apps/studio/routes/trash.ts +16 -1
  226. package/apps/studio/safe-zones-overlay.tsx +3 -2
  227. package/apps/studio/server.ts +8 -10
  228. package/apps/studio/skip-dirs.ts +103 -47
  229. package/apps/studio/studio-page-host.ts +70 -0
  230. package/apps/studio/sync/accepted-cold-start.ts +28 -13
  231. package/apps/studio/sync/asset-push.ts +146 -23
  232. package/apps/studio/sync/asset-state.ts +3 -2
  233. package/apps/studio/sync/code-module-consent.ts +86 -0
  234. package/apps/studio/sync/file-ledger.ts +24 -0
  235. package/apps/studio/sync/file-membership.ts +28 -3
  236. package/apps/studio/sync/file-plane.ts +497 -61
  237. package/apps/studio/sync/file-pull.ts +304 -33
  238. package/apps/studio/sync/held-deletes.ts +230 -0
  239. package/apps/studio/sync/hub-link.ts +1 -2
  240. package/apps/studio/sync/hubs-config.ts +59 -2
  241. package/apps/studio/sync/index.ts +405 -163
  242. package/apps/studio/sync/journal-client.ts +94 -5
  243. package/apps/studio/sync/pending-deletes.ts +100 -0
  244. package/apps/studio/sync/projection.ts +77 -12
  245. package/apps/studio/sync/seed-progress.ts +104 -3
  246. package/apps/studio/sync/settings.ts +36 -1
  247. package/apps/studio/sync/status.ts +17 -1
  248. package/apps/studio/sync/writer-registry.ts +5 -0
  249. package/apps/studio/text-caret.ts +5 -3
  250. package/apps/studio/thumbs/service.ts +12 -2
  251. package/apps/studio/thumbs/validate.ts +7 -0
  252. package/apps/studio/toolbars/toolbar-rect.ts +3 -2
  253. package/apps/studio/ui-prefs-migrate.ts +4 -0
  254. package/apps/studio/use-annotation-resize.tsx +4 -3
  255. package/apps/studio/use-artboard-drag.tsx +3 -2
  256. package/apps/studio/use-element-resize.tsx +7 -6
  257. package/apps/studio/use-grid-track-handles.tsx +5 -4
  258. package/apps/studio/use-selection-set.tsx +64 -4
  259. package/apps/studio/use-spacing-handles.tsx +7 -6
  260. package/apps/studio/video-comp.tsx +75 -39
  261. package/apps/studio/video-play-overlay.tsx +4 -3
  262. package/apps/studio/whats-new.json +99 -0
  263. package/cli/commands/design.mjs +5 -2
  264. package/cli/commands/hub.mjs +10 -1
  265. package/cli/lib/bash-guard.mjs +27 -1
  266. package/cli/lib/design-help.gen.mjs +1 -1
  267. package/cli/lib/design-hook.mjs +20 -0
  268. package/cli/lib/design-link.mjs +42 -17
  269. package/cli/lib/hub-purge.mjs +183 -0
  270. package/cli/lib/studio-locate.mjs +35 -10
  271. package/package.json +8 -8
  272. package/plugins/design/templates/_shell.html +164 -1
@@ -0,0 +1,831 @@
1
+ /**
2
+ * @file artboard-live.ts — 2.0.5: heavy canvases must not crash the tab
3
+ * @scope apps/studio/artboard-live.ts (React-free core; the React half is artboard-live-gate.tsx)
4
+ * @purpose Decides which artboards of an open canvas run LIVE. A canvas with 88 three.js scenes or 30
5
+ * artboards of decoding video ran every one of them at once, and WebKit killed the page.
6
+ *
7
+ * An artboard is LIVE when it is near the viewport AND the canvas is not in its overview
8
+ * (zoomed out below `LIVE_MIN_ZOOM`, the annotation mosaic's own cut-over, or more than
9
+ * `LIVE_MAX_VISIBLE` artboards on screen at once). A sleeping artboard keeps its DOM and its
10
+ * size; what stops is its heavy content: WebGL contexts (lost, with a snapshot of the last
11
+ * frame), `<video>` (paused), CSS animations (paused), `VideoComp` players (paused) and anything
12
+ * a canvas gates on `useArtboardActive()`.
13
+ *
14
+ * Artboards come back a few per frame (`WAKE_PER_FRAME`) and only once a gesture settles, so a
15
+ * pan never pays for re-starting a dozen scenes at once. The first mount of an artboard's
16
+ * children waits for the same queue in `light` mode (and for `useArtboardSeen()` consumers —
17
+ * the design-system board's specimens), so a canvas never builds what nobody looked at.
18
+ *
19
+ * Captures render EVERYTHING: the mode is `off` for every headless/export render (top-level
20
+ * page, `?hide-chrome=1`, `?thumb-only=`), and the in-frame export (`export-capture`) wakes
21
+ * every artboard for the length of the capture (`forceAll`).
22
+ *
23
+ * 2.0.7 — the mount RING (Michal: "what is in the viewport is right, what is far away is released").
24
+ * In `sleep` an artboard's children mount when it comes into the prefetch ring (`NEAR_MARGIN`) and
25
+ * are RELEASED again once it has been outside the larger keep ring (`KEEP_MARGIN` — the hysteresis)
26
+ * for `RELEASE_AFTER_MS`, so a long canvas never holds every artboard's DOM at once (a 45k-node
27
+ * canvas froze WebKit for seconds per frame even with three artboards on screen). Mounting is
28
+ * bounded by a DOM budget (`MOUNT_NODE_BUDGET`, measured per artboard): on-screen artboards first,
29
+ * nearest the centre first, then the prefetch ring; one that does not fit stays a same-size
30
+ * placeholder. Mounting goes on DURING a gesture (on-screen artboards only, one per frame; the React
31
+ * side renders it as an interruptible transition), so a pan never waits for the canvas to load.
32
+ * Live wakes (WebGL, video) still wait for the settle.
33
+ *
34
+ * 2.0.7 — a LIGHT canvas has no ring (decision `v2-207-light-canvas-mounts-all`): when the whole canvas's
35
+ * estimated element count fits the budget (`isLight`), every artboard mounts — far ones too, through the
36
+ * same queue, never mid-gesture — and none is released, as in 1.x: no churn and no stand-in blocks. The
37
+ * estimate follows the weights as artboards mount, so a canvas that grows past the budget falls back to
38
+ * the ring (its far artboards are released after their grace, at a settle). Live wakes and a video
39
+ * artboard's media still follow the ring, as on any canvas.
40
+ */
41
+
42
+ import { LOD_ZOOM } from './annotations/ui/lod.tsx';
43
+
44
+ /** Below this (published) zoom the canvas is in its overview: nothing runs live. Same cut-over as the annotation mosaic. */
45
+ export const LIVE_MIN_ZOOM = LOD_ZOOM;
46
+ /** More artboards than this on screen at once is an overview too, whatever the zoom. */
47
+ export const LIVE_MAX_VISIBLE = 16;
48
+ /** At most this many WebGL contexts per canvas stay live — well under the browsers' hard limit (~16). */
49
+ export const WEBGL_LIVE_CAP = 8;
50
+ /** How far past the viewport an artboard still counts as near (IntersectionObserver rootMargin). */
51
+ export const NEAR_MARGIN = '50%';
52
+ /** How many artboards wake (or first mount) per animation frame. */
53
+ export const WAKE_PER_FRAME = 3;
54
+ /** The keep ring: a mounted artboard stays mounted while it is this near (hysteresis over `NEAR_MARGIN`). */
55
+ export const KEEP_MARGIN = '150%';
56
+ /** How long an artboard must have been outside the keep ring before its children are released (ms). */
57
+ export const RELEASE_AFTER_MS = 2500;
58
+ /**
59
+ * How long a slot nobody subscribes to any more is kept (ms): an edit's soft reload remounts every gate, and the
60
+ * new one must find its artboard still mounted (2.0.7 — a placeholder in between lost the shell's selection).
61
+ */
62
+ export const RETIRE_AFTER_MS = 250;
63
+ /** The DOM budget of one canvas document: mounted artboards' element count stays about this (2.0.7). */
64
+ export const MOUNT_NODE_BUDGET = 12000;
65
+ /**
66
+ * On-screen artboards mount past `MOUNT_NODE_BUDGET` up to this many elements (2.0.7): the budget bounds what is
67
+ * off screen; this only stops a fit-all of a huge canvas, whose tiny artboards keep their still pictures.
68
+ */
69
+ export const ON_SCREEN_NODE_CEILING = 30000;
70
+ /** The weight (elements) of an artboard nobody has measured yet, until one of the canvas has been. */
71
+ export const DEFAULT_WEIGHT = 400;
72
+ /** After a frame longer than this (ms) the next frame mounts one artboard, not `WAKE_PER_FRAME`. */
73
+ export const SLOW_FRAME_MS = 34;
74
+ /** Mid-gesture, at most one first mount per this many ms (a WebKit mount lays out and paints at once). */
75
+ export const GESTURE_MOUNT_GAP_MS = 350;
76
+ /**
77
+ * More artboards than this on screen and WebKit drops their per-artboard compositing layers: it rasterizes each
78
+ * layer at full size whatever the world's scale (40 on screen at 8 % = seconds per frame). An 18-board
79
+ * moodboard keeps them (they are why its fit-all zoom is smooth — RCA issue-canvas-pan-zoom-jank-large-moodboard).
80
+ */
81
+ export const LAYER_MAX_VISIBLE = 24;
82
+ /**
83
+ * A light canvas mounts every artboard and keeps it (2.0.7, decision `v2-207-light-canvas-mounts-all`): when the
84
+ * canvas's estimated element count — the measured artboards plus their average for each one not measured yet — fits
85
+ * `MOUNT_NODE_BUDGET`. The estimate is trusted once this many artboards are measured (or all of a smaller canvas);
86
+ * before that the ring decides, and `DEFAULT_WEIGHT` is never an estimate.
87
+ */
88
+ export const LIGHT_MIN_MEASURED = 4;
89
+
90
+ /**
91
+ * `off` — every artboard live, every child mounted (captures, exports, headless, tests): the pre-2.0.5 behaviour.
92
+ * `sleep` — the studio default: offscreen / overview artboards sleep, their DOM stays mounted.
93
+ * `light` — "Show visible artboards only" after a crash: an artboard's children mount only while it is live
94
+ * (near the viewport and the canvas not in its overview).
95
+ */
96
+ export type LiveMode = 'off' | 'sleep' | 'light';
97
+
98
+ /** The frame parameter that picks a mode explicitly (`?artboard-live=off|sleep|light`). */
99
+ export const LIVE_PARAM = 'artboard-live';
100
+
101
+ export function resolveLiveMode(env: { search: string; embedded: boolean }): LiveMode {
102
+ const p = new URLSearchParams(env.search);
103
+ // A capture — whoever asks for it — renders every artboard live. This wins over an explicit mode.
104
+ if (p.get('hide-chrome') === '1' || p.has('thumb-only')) return 'off';
105
+ const explicit = p.get(LIVE_PARAM);
106
+ if (explicit === 'off' || explicit === 'sleep' || explicit === 'light') return explicit;
107
+ // Only the studio frames a canvas. A top-level canvas page is a headless render (screenshot,
108
+ // smoke, canvas-rects, perf without --studio) or someone opening the shell URL by hand: all live.
109
+ return env.embedded ? 'sleep' : 'off';
110
+ }
111
+
112
+ /** The overview: zoomed out past the mosaic cut-over, or too many artboards on screen at once. */
113
+ export function inOverview(zoom: number, visibleCount: number): boolean {
114
+ return (zoom > 0 && zoom < LIVE_MIN_ZOOM) || visibleCount > LIVE_MAX_VISIBLE;
115
+ }
116
+
117
+ /** Whether one artboard should be live. `forceAll` (a capture in progress) always wins. */
118
+ export function decideLive(s: {
119
+ near: boolean;
120
+ zoom: number;
121
+ visibleCount: number;
122
+ forceAll: boolean;
123
+ }): boolean {
124
+ if (s.forceAll) return true;
125
+ if (!s.near) return false;
126
+ return !inOverview(s.zoom, s.visibleCount);
127
+ }
128
+
129
+ export interface ArtboardLiveState {
130
+ /** Runs its heavy content. */
131
+ live: boolean;
132
+ /** Its children may be mounted (always true in `sleep`/`off` once seen; follows `live` in `light`). */
133
+ seen: boolean;
134
+ /**
135
+ * Its media may load (a `<video>`'s src, a Player). Equal to `seen`, except on an eager artboard: that one is
136
+ * mounted wherever it is so its comp registers, but its media follows the ring as a lazy artboard's children
137
+ * would — near or on screen, kept inside the keep ring, released after the grace (2.0.7).
138
+ */
139
+ media: boolean;
140
+ }
141
+
142
+ interface Slot {
143
+ near: boolean;
144
+ visible: boolean;
145
+ /** Inside the keep ring (starts true: an artboard nobody has reported on is not far). */
146
+ keep: boolean;
147
+ /** When it left the keep ring (ms), or 0. */
148
+ farSince: number;
149
+ /** Distance of its centre to the viewport centre (px, the near observer's last word). */
150
+ dist: number;
151
+ /** Measured element count of its mounted children, or 0. */
152
+ weight: number;
153
+ /** Never released (the active artboard — a selection or an edit lives in its DOM). */
154
+ pinned: boolean;
155
+ /**
156
+ * Always mounted in `sleep`, wherever it is (a video artboard: its comp registers itself only while mounted, and
157
+ * the link glyph, the Timeline's shared count and Present's format switch read those registrations).
158
+ */
159
+ eager: boolean;
160
+ /**
161
+ * Mounted for the stills' warm-up (2.0.7, artboard-still-warmup.ts): far away, asleep, within the DOM budget, until
162
+ * its still is drawn. Never a prefetch past the budget, never mid-gesture (the drain's own rules).
163
+ */
164
+ warm: boolean;
165
+ admitted: boolean;
166
+ live: boolean;
167
+ /** Its media may load: `admitted`, except on an eager artboard, where it is the ring's own answer. */
168
+ media: boolean;
169
+ /** The last state handed out — one object per change, so React's external store sees a stable snapshot. */
170
+ view: ArtboardLiveState;
171
+ subs: Set<(s: ArtboardLiveState) => void>;
172
+ }
173
+
174
+ const ASLEEP: ArtboardLiveState = Object.freeze({ live: false, seen: false, media: false });
175
+
176
+ const ALL_LIVE: ArtboardLiveState = Object.freeze({ live: true, seen: true, media: true });
177
+
178
+ /**
179
+ * One per canvas document. Fed by two IntersectionObservers (near / visible), the published zoom and the
180
+ * gesture flag; artboards subscribe by id. Sleeping is immediate; waking goes through a queue drained
181
+ * `WAKE_PER_FRAME` at a time on `schedule` (rAF in the browser), and holds while a gesture is in flight.
182
+ */
183
+ export class LiveRegistry {
184
+ readonly mode: LiveMode;
185
+ private slots = new Map<string, Slot>();
186
+ private zoom = 1;
187
+ private interacting = false;
188
+ private forceAll = 0;
189
+ private pending = new Set<string>();
190
+ private scheduled = false;
191
+ private readonly schedule: (fn: () => void) => void;
192
+ private listeners = new Set<() => void>();
193
+ private readonly now: () => number;
194
+ private readonly later: (fn: () => void, ms: number) => void;
195
+ private sweepAt = 0;
196
+ private lastDrainAt = 0;
197
+ private lastGestureMountAt = Number.NEGATIVE_INFINITY;
198
+ private lastAnyMountAt = Number.NEGATIVE_INFINITY;
199
+ /** First mounts made during the current gesture (2.0.7: the settle frees what they took past the budget). */
200
+ private gestureMounts = 0;
201
+ /** The canvas fits the budget whole (`isLight`): every artboard mounted and kept. Kept current by `recompute`. */
202
+ private light = false;
203
+ readonly budget: number;
204
+ readonly ceiling: number;
205
+
206
+ constructor(
207
+ mode: LiveMode,
208
+ schedule: (fn: () => void) => void,
209
+ opts: {
210
+ now?: () => number;
211
+ later?: (fn: () => void, ms: number) => void;
212
+ budget?: number;
213
+ ceiling?: number;
214
+ } = {}
215
+ ) {
216
+ this.mode = mode;
217
+ this.schedule = schedule;
218
+ this.now =
219
+ opts.now ?? (() => (typeof performance !== 'undefined' ? performance.now() : Date.now()));
220
+ this.later = opts.later ?? ((fn, ms) => void setTimeout(fn, ms));
221
+ this.budget = opts.budget ?? MOUNT_NODE_BUDGET;
222
+ this.ceiling = opts.ceiling ?? ON_SCREEN_NODE_CEILING;
223
+ }
224
+
225
+ private mayWait: (id: string) => boolean = () => false;
226
+
227
+ /**
228
+ * 2.0.7 — which on-screen artboards may wait for the settle to mount (the gate: under the overview cut, where
229
+ * the placeholder shows its colour or its kept still and a mount would show nothing more mid-gesture). The
230
+ * rest mount mid-gesture as before: a placeholder over the cut is a block the user sees.
231
+ */
232
+ setMayWait(fn: (id: string) => boolean): void {
233
+ this.mayWait = fn;
234
+ }
235
+
236
+ /** The active artboard and a video one never wait (neither is ever a still). */
237
+ private waits(id: string, s: Slot): boolean {
238
+ return !s.pinned && !s.eager && this.mayWait(id);
239
+ }
240
+
241
+ get(id: string): ArtboardLiveState {
242
+ if (this.mode === 'off') return ALL_LIVE;
243
+ return this.slots.get(id)?.view ?? ASLEEP;
244
+ }
245
+
246
+ isLive(id: string): boolean {
247
+ return this.get(id).live;
248
+ }
249
+
250
+ /** Any change at all (the WebGL budget re-plans on it). */
251
+ onChange(fn: () => void): () => void {
252
+ this.listeners.add(fn);
253
+ return () => this.listeners.delete(fn);
254
+ }
255
+
256
+ private settleListeners = new Set<() => void>();
257
+
258
+ /** A gesture settled. */
259
+ onSettle(fn: () => void): () => void {
260
+ this.settleListeners.add(fn);
261
+ return () => this.settleListeners.delete(fn);
262
+ }
263
+
264
+ subscribe(id: string, fn: (s: ArtboardLiveState) => void): () => void {
265
+ const s = this.slot(id);
266
+ s.subs.add(fn);
267
+ return () => {
268
+ s.subs.delete(fn);
269
+ if (s.subs.size > 0) return;
270
+ // Not dropped at once: an edit soft-reloads the canvas and remounts every gate in ONE commit (old cleanup,
271
+ // then the new subscribe). The new gate must find the slot mounted, or its artboard falls back to a
272
+ // placeholder just as the shell reselects — the 2.0.7 video regression. Nobody back within the grace: gone.
273
+ this.later(() => {
274
+ if (s.subs.size > 0 || this.slots.get(id) !== s) return;
275
+ this.slots.delete(id);
276
+ this.pending.delete(id);
277
+ this.recompute();
278
+ }, RETIRE_AFTER_MS);
279
+ };
280
+ }
281
+
282
+ /**
283
+ * The gate stopped observing its article (an unmount, or the old gate of a remount). Clears what the observers
284
+ * said — unless another gate holds the id now (the remount: its own observers report on its new article).
285
+ * Never creates a slot.
286
+ */
287
+ unobserve(id: string): void {
288
+ const s = this.slots.get(id);
289
+ if (!s || s.subs.size > 0) return;
290
+ if (!s.near && !s.visible) return;
291
+ s.near = false;
292
+ s.visible = false;
293
+ this.recompute();
294
+ }
295
+
296
+ setNear(id: string, near: boolean, dist?: number): void {
297
+ const s = this.slot(id);
298
+ if (dist !== undefined && Number.isFinite(dist)) s.dist = dist;
299
+ if (s.near === near) return;
300
+ s.near = near;
301
+ // near is inside the keep ring by definition (the two observers can report in either order)
302
+ if (near) this.setKeepState(s, true);
303
+ this.recompute();
304
+ }
305
+
306
+ /** The keep ring (`KEEP_MARGIN`): outside it for `RELEASE_AFTER_MS`, a mounted artboard is released. */
307
+ setKeep(id: string, keep: boolean): void {
308
+ const s = this.slot(id);
309
+ const k = keep || s.near;
310
+ if (s.keep === k) return;
311
+ this.setKeepState(s, k);
312
+ this.recompute();
313
+ }
314
+
315
+ /** The active artboard is never released nor pushed out of the budget (its selection lives in its DOM). */
316
+ setPinned(id: string, pinned: boolean): void {
317
+ const s = this.slot(id);
318
+ if (s.pinned === pinned) return;
319
+ s.pinned = pinned;
320
+ this.recompute();
321
+ }
322
+
323
+ /** A video artboard: mounted wherever it is, past the budget, never released nor pushed out (`sleep` only). */
324
+ setEager(id: string, eager: boolean): void {
325
+ const s = this.slot(id);
326
+ if (s.eager === eager) return;
327
+ s.eager = eager;
328
+ this.recompute();
329
+ }
330
+
331
+ /**
332
+ * The stills' warm-up mounts a far artboard (`on`) and releases it once its still is drawn. The release is at once
333
+ * (not the keep ring's grace) unless the artboard came near, on screen, or is pinned / eager meanwhile.
334
+ */
335
+ setWarm(id: string, on: boolean): void {
336
+ const s = this.slots.get(id);
337
+ if (!s || s.warm === on) return;
338
+ s.warm = on;
339
+ if (on) {
340
+ this.recompute();
341
+ return;
342
+ }
343
+ const stays = s.near || s.visible || s.pinned || s.eager || this.forceAll > 0;
344
+ if (s.admitted && !stays) {
345
+ s.admitted = false;
346
+ s.live = false;
347
+ s.media = false;
348
+ this.pending.delete(id);
349
+ this.notify(id, s);
350
+ this.emit();
351
+ } else if (!s.admitted) this.pending.delete(id);
352
+ this.kick();
353
+ }
354
+
355
+ /** Is this artboard's warm mount done (its children mounted)? */
356
+ isMounted(id: string): boolean {
357
+ return !!this.slots.get(id)?.admitted;
358
+ }
359
+
360
+ /**
361
+ * Artboards the warm-up may mount: a gate holds them, not mounted, not on screen nor near, not pinned nor eager (a
362
+ * video keeps its own media rule), and their first mount fits the DOM budget now. Nearest the viewport first.
363
+ */
364
+ warmCandidates(): Array<{ id: string; dist: number; fits: boolean }> {
365
+ if (this.mode !== 'sleep' || this.forceAll > 0) return [];
366
+ const mounted = this.mountedWeight();
367
+ const out: Array<{ id: string; dist: number; fits: boolean }> = [];
368
+ for (const [id, s] of this.slots) {
369
+ if (s.admitted || s.warm || s.visible || s.near || s.pinned || s.eager || s.subs.size === 0)
370
+ continue;
371
+ out.push({ id, dist: s.dist, fits: mounted + this.weightOf(s) <= this.budget });
372
+ }
373
+ return out;
374
+ }
375
+
376
+ /** When the queue last mounted or woke an artboard (ms) — the warm-up waits for the ring's own mounts. */
377
+ lastMountAt(): number {
378
+ return this.lastAnyMountAt;
379
+ }
380
+
381
+ /** The element count an artboard's children came to once mounted (the DOM budget's unit). */
382
+ setWeight(id: string, nodes: number): void {
383
+ const s = this.slots.get(id);
384
+ if (!s || !Number.isFinite(nodes) || nodes < 0) return;
385
+ s.weight = Math.max(1, Math.round(nodes));
386
+ if (this.computeLight() !== this.light) this.recompute();
387
+ }
388
+
389
+ /** The whole canvas fits the DOM budget: every artboard is mounted and kept (2.0.7, `LIGHT_MIN_MEASURED`). */
390
+ isLight(): boolean {
391
+ return this.light;
392
+ }
393
+
394
+ /** The estimated element count of the whole canvas (every artboard a gate holds), or -1 before it is known. */
395
+ estimatedWeight(): number {
396
+ let held = 0;
397
+ let measured = 0;
398
+ let sum = 0;
399
+ for (const s of this.slots.values()) {
400
+ if (s.subs.size === 0) continue;
401
+ held += 1;
402
+ if (s.weight) {
403
+ measured += 1;
404
+ sum += s.weight;
405
+ }
406
+ }
407
+ if (held === 0 || measured < Math.min(LIGHT_MIN_MEASURED, held)) return -1;
408
+ return sum + ((held - measured) * sum) / measured;
409
+ }
410
+
411
+ private computeLight(): boolean {
412
+ if (this.mode !== 'sleep') return false;
413
+ const est = this.estimatedWeight();
414
+ return est >= 0 && est <= this.budget;
415
+ }
416
+
417
+ /** Elements mounted now (measured, or estimated for one not measured yet). */
418
+ mountedWeight(): number {
419
+ let n = 0;
420
+ for (const s of this.slots.values()) if (s.admitted) n += this.weightOf(s);
421
+ return n;
422
+ }
423
+
424
+ /** How many artboards have their children mounted now. */
425
+ mountedCount(): number {
426
+ let n = 0;
427
+ for (const s of this.slots.values()) if (s.admitted) n += 1;
428
+ return n;
429
+ }
430
+
431
+ private weightOf(s: Slot): number {
432
+ if (s.weight) return s.weight;
433
+ let sum = 0;
434
+ let k = 0;
435
+ for (const o of this.slots.values())
436
+ if (o.weight) {
437
+ sum += o.weight;
438
+ k += 1;
439
+ }
440
+ return k ? sum / k : DEFAULT_WEIGHT;
441
+ }
442
+
443
+ private setKeepState(s: Slot, keep: boolean): void {
444
+ if (s.keep === keep) return;
445
+ s.keep = keep;
446
+ s.farSince = keep ? 0 : this.now();
447
+ if (!keep) this.sweepLater(RELEASE_AFTER_MS);
448
+ }
449
+
450
+ private sweepLater(ms: number): void {
451
+ const at = this.now() + ms;
452
+ if (this.sweepAt && this.sweepAt <= at) return;
453
+ this.sweepAt = at;
454
+ this.later(() => {
455
+ this.sweepAt = 0;
456
+ this.recompute();
457
+ // the ones whose grace has not run out yet (they left later) get their own sweep
458
+ let next = Number.POSITIVE_INFINITY;
459
+ // (one whose grace ran out and is still held waits for what holds it — a settle, an unpin, a light canvas
460
+ // growing — and each of those recomputes: rescheduling it here was a sweep every 16 ms meanwhile)
461
+ const now = this.now();
462
+ for (const s of this.slots.values())
463
+ if ((s.admitted || s.media) && !s.keep && s.farSince && s.farSince + RELEASE_AFTER_MS > now)
464
+ next = Math.min(next, s.farSince + RELEASE_AFTER_MS);
465
+ if (Number.isFinite(next)) this.sweepLater(Math.max(16, next - this.now()));
466
+ }, ms);
467
+ }
468
+
469
+ setVisible(id: string, visible: boolean): void {
470
+ const s = this.slot(id);
471
+ if (s.visible === visible) return;
472
+ s.visible = visible;
473
+ this.recompute();
474
+ }
475
+
476
+ setZoom(zoom: number): void {
477
+ if (!Number.isFinite(zoom) || zoom === this.zoom) return;
478
+ this.zoom = zoom;
479
+ this.recompute();
480
+ }
481
+
482
+ setInteracting(on: boolean): void {
483
+ if (on === this.interacting) return;
484
+ this.interacting = on;
485
+ // the settle wakes what waited, and lets the releases that waited for it happen
486
+ if (!on) {
487
+ this.recompute();
488
+ // 2.0.7 — a mount mid-gesture pushed nothing out (no unmount while the camera moves): what it took past
489
+ // the budget is freed now, from what is off screen, the farthest first
490
+ if (this.gestureMounts > 0 && this.mode === 'sleep' && this.forceAll === 0) {
491
+ const over = this.mountedWeight() - this.budget;
492
+ if (over > 0 && this.evictFor(over) > 0) this.emit();
493
+ }
494
+ this.gestureMounts = 0;
495
+ for (const fn of this.settleListeners) fn();
496
+ } else this.kick(); // on-screen artboards keep mounting during the gesture
497
+ }
498
+
499
+ /** A capture is running: everything live, NOW (no queue). Returns the release. */
500
+ holdAllLive(): () => void {
501
+ this.forceAll += 1;
502
+ this.recompute();
503
+ let released = false;
504
+ return () => {
505
+ if (released) return;
506
+ released = true;
507
+ this.forceAll -= 1;
508
+ this.recompute();
509
+ };
510
+ }
511
+
512
+ /** The canvas is in its overview right now (zoomed out, or too many artboards on screen). */
513
+ overview(): boolean {
514
+ return this.mode !== 'off' && inOverview(this.zoom, this.visibleCount());
515
+ }
516
+
517
+ /**
518
+ * So many artboards on screen that a compositing layer each costs more than it saves (WebKit, 2.0.7). Never on a
519
+ * light canvas (`isLight`): its layers are cheap, and they are what keeps its fit-all zoom smooth.
520
+ */
521
+ crowded(): boolean {
522
+ return this.mode !== 'off' && !this.light && this.visibleCount() > LAYER_MAX_VISIBLE;
523
+ }
524
+
525
+ /** A gesture is in flight (the gate defers its picture-taking past it). */
526
+ isInteracting(): boolean {
527
+ return this.interacting;
528
+ }
529
+
530
+ /** On screen, and the elements its children came to (measured, or the estimate) — the stills' heavy signal. */
531
+ slotInfo(id: string): { visible: boolean; weight: number } | null {
532
+ const s = this.slots.get(id);
533
+ if (!s) return null;
534
+ return { visible: s.visible, weight: s.admitted ? this.weightOf(s) : 0 };
535
+ }
536
+
537
+ visibleCount(): number {
538
+ let n = 0;
539
+ for (const s of this.slots.values()) if (s.visible) n += 1;
540
+ return n;
541
+ }
542
+
543
+ private slot(id: string): Slot {
544
+ let s = this.slots.get(id);
545
+ if (!s) {
546
+ s = {
547
+ near: false,
548
+ visible: false,
549
+ keep: true,
550
+ farSince: 0,
551
+ dist: Number.POSITIVE_INFINITY,
552
+ weight: 0,
553
+ pinned: false,
554
+ eager: false,
555
+ warm: false,
556
+ admitted: false,
557
+ live: false,
558
+ media: false,
559
+ view: ASLEEP,
560
+ subs: new Set(),
561
+ };
562
+ this.slots.set(id, s);
563
+ }
564
+ return s;
565
+ }
566
+
567
+ private target(s: Slot): { live: boolean; admitted: boolean; media: boolean } {
568
+ const force = this.forceAll > 0;
569
+ const live = decideLive({
570
+ near: s.near,
571
+ zoom: this.zoom,
572
+ visibleCount: this.visibleCount(),
573
+ forceAll: force,
574
+ });
575
+ // `sleep` mounts what came near and keeps it. `light` (after a crash) mounts only what is LIVE — near and
576
+ // not in the overview — so a zoomed-out view of 60 scenes mounts none of them, and unmounts what leaves.
577
+ // `sleep` (2.0.7): wanted while in the prefetch ring or on screen, KEPT while inside the keep ring, and
578
+ // released only after `RELEASE_AFTER_MS` outside it (the sweep). Whether a first mount fits the DOM budget
579
+ // is the queue's call (drain).
580
+ // The ring's answer is the same for an eager artboard (a video), but there it decides its MEDIA only: the
581
+ // artboard itself is mounted wherever it is (its comp registers), its Player and <video> load in the ring.
582
+ // On a light canvas (2.0.7) every artboard a gate holds is mounted and kept, like an eager one: the ring then
583
+ // decides its media only (a non-video artboard has none — its children are mounted, asleep).
584
+ const kept = this.mode === 'sleep' && (s.eager || (this.light && s.subs.size > 0));
585
+ const held = kept ? s.media : s.admitted;
586
+ const ring =
587
+ this.mode === 'light'
588
+ ? live
589
+ : force ||
590
+ s.near ||
591
+ s.visible ||
592
+ (this.mode === 'sleep' && s.warm) ||
593
+ (this.mode === 'sleep' && held && !this.releasable(s, held));
594
+ const admitted = ring || kept;
595
+ return { live, admitted, media: ring };
596
+ }
597
+
598
+ /** Mounted (`held`), outside the keep ring for long enough, not on screen, and no gesture in flight. */
599
+ private releasable(s: Slot, held: boolean): boolean {
600
+ return (
601
+ held &&
602
+ !s.pinned &&
603
+ !s.keep &&
604
+ !s.visible &&
605
+ !s.near &&
606
+ s.farSince > 0 &&
607
+ this.now() - s.farSince >= RELEASE_AFTER_MS &&
608
+ !this.interacting
609
+ );
610
+ }
611
+
612
+ private recompute(): void {
613
+ if (this.mode === 'off') return;
614
+ const force = this.forceAll > 0;
615
+ // 2.0.7 — nothing goes down mid-gesture (no sleep, no media release, no unmount): the frame freeze already
616
+ // stops the artboards' own work while the camera moves, and each change is a render. The settle applies them.
617
+ const hold = this.interacting && !force;
618
+ this.light = this.computeLight();
619
+ let changed = false;
620
+ for (const [id, s] of this.slots) {
621
+ const t = this.target(s);
622
+ const goesDown = (s.live && !t.live) || (s.admitted && !t.admitted) || (s.media && !t.media);
623
+ const goesUp = (!s.live && t.live) || (!s.admitted && t.admitted) || (!s.media && t.media);
624
+ if (hold && goesDown) {
625
+ if (goesUp) this.pending.add(id);
626
+ } else if (goesDown && !goesUp) {
627
+ s.live = t.live;
628
+ s.admitted = t.admitted;
629
+ s.media = t.media;
630
+ this.pending.delete(id);
631
+ this.notify(id, s);
632
+ changed = true;
633
+ } else if (goesUp) {
634
+ if (force) {
635
+ s.live = t.live;
636
+ s.admitted = t.admitted;
637
+ s.media = t.media;
638
+ this.pending.delete(id);
639
+ this.notify(id, s);
640
+ changed = true;
641
+ } else {
642
+ // The part that goes down applies now; the part that goes up waits for the queue.
643
+ if ((s.live && !t.live) || (s.media && !t.media)) {
644
+ if (!t.live) s.live = false;
645
+ if (!t.media) s.media = false;
646
+ this.notify(id, s);
647
+ changed = true;
648
+ }
649
+ this.pending.add(id);
650
+ }
651
+ } else {
652
+ this.pending.delete(id);
653
+ }
654
+ }
655
+ if (changed) this.emit();
656
+ this.kick();
657
+ }
658
+
659
+ private kick(): void {
660
+ if (this.scheduled || this.pending.size === 0) return;
661
+ if (this.interacting && !this.hasVisibleMount()) return;
662
+ this.scheduled = true;
663
+ this.schedule(() => {
664
+ this.scheduled = false;
665
+ this.drain();
666
+ });
667
+ }
668
+
669
+ private hasVisibleMount(): boolean {
670
+ for (const id of this.pending) {
671
+ const s = this.slots.get(id);
672
+ if (!s?.visible || this.waits(id, s)) continue;
673
+ const t = this.target(s);
674
+ if ((!s.admitted && t.admitted) || (!s.media && t.media)) return true;
675
+ }
676
+ return false;
677
+ }
678
+
679
+ /**
680
+ * One frame's worth of wakes and first mounts. Exposed for tests (the browser drains on rAF).
681
+ *
682
+ * Order: on screen before the prefetch ring, nearest the viewport centre first. A first mount must fit the
683
+ * DOM budget; an on-screen one may push out mounted artboards that are not on screen (outside the prefetch
684
+ * ring first, the farthest first); a prefetch never pushes anything out. During a gesture only on-screen
685
+ * artboards mount (asleep), one per frame, and nothing wakes. After a slow frame, one per frame.
686
+ */
687
+ drain(): void {
688
+ const t = this.now();
689
+ const slow = this.lastDrainAt > 0 && t - this.lastDrainAt > SLOW_FRAME_MS;
690
+ this.lastDrainAt = t;
691
+ // mid-gesture: one first mount, after a fast frame, and not sooner than GESTURE_MOUNT_GAP_MS after the last
692
+ const gestureHold =
693
+ this.interacting && (slow || t - this.lastGestureMountAt < GESTURE_MOUNT_GAP_MS);
694
+ const cap = gestureHold ? 0 : this.interacting || slow ? 1 : WAKE_PER_FRAME;
695
+ const order: Array<{ id: string; s: Slot }> = [];
696
+ for (const id of this.pending) {
697
+ const s = this.slots.get(id);
698
+ if (s) order.push({ id, s });
699
+ else this.pending.delete(id);
700
+ }
701
+ order.sort((a, b) => Number(b.s.visible) - Number(a.s.visible) || a.s.dist - b.s.dist);
702
+ let n = 0;
703
+ let changed = false;
704
+ let mounted = this.mountedWeight();
705
+ for (const { id, s } of order) {
706
+ if (n >= cap) break;
707
+ const tg = this.target(s);
708
+ if (tg.live === s.live && tg.admitted === s.admitted && tg.media === s.media) {
709
+ this.pending.delete(id);
710
+ continue;
711
+ }
712
+ // an eager artboard's media coming up is a mount too (its Player): paced the same, outside the budget
713
+ const mounts = (tg.admitted && !s.admitted) || (tg.media && !s.media);
714
+ // mid-gesture only what is on screen and would show a block mounts (2.0.7: under the cut it waits)
715
+ if (this.interacting && !(mounts && s.visible && !this.waits(id, s))) continue; // waits for the settle
716
+ if (mounts && this.forceAll === 0 && this.mode === 'sleep' && !s.eager) {
717
+ const w = this.weightOf(s);
718
+ if (mounted > 0 && mounted + w > this.budget) {
719
+ if (s.visible && !this.interacting) {
720
+ const freed = this.evictFor(mounted + w - this.budget);
721
+ if (freed) changed = true;
722
+ mounted -= freed;
723
+ }
724
+ // What is ON SCREEN mounts past the budget (up to the ceiling) once nothing off screen is left to push
725
+ // out: the budget bounds what you do not see. At a settled view of the mascot canvas 41 on-screen
726
+ // artboards weighed 17k and three of them stayed placeholders for good.
727
+ const limit = s.visible ? Math.max(this.budget, this.ceiling) : this.budget;
728
+ if (mounted > 0 && mounted + w > limit) continue; // a placeholder until there is room
729
+ }
730
+ mounted += w;
731
+ }
732
+ // mid-gesture a mount only raises (nothing goes down until the settle)
733
+ s.admitted = this.interacting ? s.admitted || tg.admitted : tg.admitted;
734
+ s.media = this.interacting ? s.media || tg.media : tg.media;
735
+ if (this.interacting) {
736
+ // a first mount mid-gesture comes up asleep (`live` stays what it was); its wake waits for the settle
737
+ this.lastGestureMountAt = t;
738
+ this.gestureMounts += 1;
739
+ } else {
740
+ s.live = tg.live;
741
+ this.pending.delete(id);
742
+ }
743
+ this.notify(id, s);
744
+ this.lastAnyMountAt = t;
745
+ changed = true;
746
+ n += 1;
747
+ }
748
+ if (changed) this.emit();
749
+ this.kick();
750
+ }
751
+
752
+ /** Release mounted artboards that are not on screen to free `need` elements; returns what it freed. */
753
+ private evictFor(need: number): number {
754
+ const victims = [...this.slots.values()]
755
+ .filter((v) => v.admitted && !v.visible && !v.pinned && !v.eager)
756
+ .sort((a, b) => Number(a.near) - Number(b.near) || b.dist - a.dist);
757
+ let freed = 0;
758
+ for (const v of victims) {
759
+ if (freed >= need) break;
760
+ freed += this.weightOf(v);
761
+ v.admitted = false;
762
+ v.live = false;
763
+ v.media = false;
764
+ this.notify('', v);
765
+ }
766
+ return freed;
767
+ }
768
+
769
+ private notify(_id: string, s: Slot): void {
770
+ if (s.view.live === s.live && s.view.seen === s.admitted && s.view.media === s.media) return;
771
+ const c = this.churn;
772
+ if (s.admitted !== s.view.seen) {
773
+ if (s.admitted) c.mounts += 1;
774
+ else c.releases += 1;
775
+ }
776
+ if (this.interacting) {
777
+ if (s.admitted && !s.view.seen) c.gestureMounts += 1;
778
+ if ((!s.live && s.view.live) || (!s.admitted && s.view.seen) || (!s.media && s.view.media))
779
+ c.gestureDowns += 1;
780
+ }
781
+ s.view = Object.freeze({ live: s.live, seen: s.admitted, media: s.media });
782
+ for (const fn of s.subs) fn(s.view);
783
+ }
784
+
785
+ private churn = { mounts: 0, releases: 0, gestureMounts: 0, gestureDowns: 0 };
786
+
787
+ /** Ring churn since the open: mounts, releases, and what changed mid-gesture (2.0.7 — the measurement). */
788
+ churnStats(): { mounts: number; releases: number; gestureMounts: number; gestureDowns: number } {
789
+ return { ...this.churn };
790
+ }
791
+
792
+ private emit(): void {
793
+ for (const fn of this.listeners) fn();
794
+ }
795
+ }
796
+
797
+ // ── WebGL budget ───────────────────────────────────────────────────────────────────────────────────
798
+
799
+ export interface GlCandidate {
800
+ key: number;
801
+ /** Its artboard is live (a canvas outside any artboard counts as live). */
802
+ live: boolean;
803
+ /** Distance of its centre to the viewport centre, in screen px. */
804
+ distance: number;
805
+ /** Last time it was kept live (ms) — the LRU tie-break. */
806
+ lastLive: number;
807
+ /** Currently lost by us. */
808
+ lost: boolean;
809
+ }
810
+
811
+ /**
812
+ * Which contexts to lose and which to restore: of the live artboards' canvases, the `cap` nearest the
813
+ * viewport centre (ties: most recently live) stay; every other context is released.
814
+ */
815
+ export function planWebgl(
816
+ entries: readonly GlCandidate[],
817
+ cap: number = WEBGL_LIVE_CAP
818
+ ): { lose: number[]; restore: number[] } {
819
+ const ranked = entries
820
+ .filter((e) => e.live)
821
+ .sort((a, b) => a.distance - b.distance || b.lastLive - a.lastLive);
822
+ const keep = new Set(ranked.slice(0, Math.max(0, cap)).map((e) => e.key));
823
+ const lose: number[] = [];
824
+ const restore: number[] = [];
825
+ for (const e of entries) {
826
+ if (keep.has(e.key)) {
827
+ if (e.lost) restore.push(e.key);
828
+ } else if (!e.lost) lose.push(e.key);
829
+ }
830
+ return { lose, restore };
831
+ }