Repository navigation
feat(core): the preview can stream an element's geometry to the editor every frame - #8052
Conversation
f13a914 to
db4a0e5
Compare
vanceingalls
left a comment
There was a problem hiding this comment.
Approved at b09a92d.
- One watch per port: each new request replaces the port's watch, and
parseWatchGeometrycaps it at 32 handles. - Teardown:
dispose()sends an empty list before closing the port.pagehideand agent dispose clear every watch, and the rAF loop cancels when none remain. - Rate: pushes are coalesced to one per frame by that single rAF loop.
- Exposure: pushes reuse
elementGeometryon handles that passEDITING_ELEMENT_HANDLE, so they expose nothing beyond ageometryreply. - Test: the
fails without the fix:line is present.
Should-fix:
- Port count:
installEditingAgentaccepts any number ofediting-connectports, each with up to 32 handles. Cap the total number of watchers or ports. - Lost ports: a port lost without
dispose()keeps its watch and is measured every frame untilpagehide. Drop a watch whosepostMessagethrows. - Loop stops on error:
push()callsschedule()last, so an exception fromelementGeometrystops every watch. Usetry/finally. - Client bounds:
geometryEventonly checks that values are finite, with no MAX bounds and no watch id, so pushes still in flight after a stop reach listeners. The consumer (the #8010 bench) must clamp and drop stale pushes before anything is persisted, as on #8002 and #8005.
|
Done in
Each new test fails with its guard removed. Core's editing-port tests pass 173 of 173, three runs in a row; typecheck, oxfmt, oxlint and fallow are clean. |
vanceingalls
left a comment
There was a problem hiding this comment.
Re-approved at b729f73. This covers three of my four should-fixes:
- Watcher cap:
MAX_GEOMETRY_WATCHERS = 8ports per page. A new port past the cap is refused, and an existing one can still re-watch. - Lost ports:
pushTodrops a watch whosepostMessagethrows. - Errors:
schedule()now runs infinally, so one failed measure doesn't stop every watch.
Still open: the client-side bounds, for the #8010 consumer.
b729f73 to
813c845
Compare
…ers, not inside rAF
vanceingalls
left a comment
There was a problem hiding this comment.
Re-approved at 83708b1. Watched geometry is now measured in a MessageChannel task posted from rAF, so after the frame renders rather than inside rAF, where a later callback could still change what paints. A throwing push is caught with swallow, and schedule() stays in finally, so the loop survives.
…r step, just before paint
vanceingalls
left a comment
There was a problem hiding this comment.
Re-approved at c856c25. The measurement now runs in the frame's ResizeObserver step, after every rAF callback and layout and just before paint, rather than in a task after the frame.
- Loop: rAF observes
documentElement. A freshobserve()reports a non-zero root once,pushunobserves, andscheduleinfinallyre-arms for the next frame. - Stopping: cancels the rAF and unobserves. A new test covers a stop between the re-arm and the observer step.
Note: in a zero-size frame (a hidden iframe) the observer has nothing to report, so pushes pause until the frame gets a size, and then resume on their own. That's fine for a frame with no geometry, but worth a comment.
jrusso1020
left a comment
There was a problem hiding this comment.
Approving at 5047d4f8. I found no blocker.
Delta since the last approved head (c856c25c). It is two commits, and both change comments only. watchGeometry.ts:105 gains the hidden-frame note, and the fake ResizeObserver comment is reworded. Since this was my first review of the PR, I also reviewed all of it.
Timing. The rAF callback calls observe(), and the measure runs in the ResizeObserver step, after all rAF callbacks and layout and before paint. Because observe() resets the last reported size, the root reports once per frame. push reschedules in finally, and the last stop cancels the pending rAF and detaches the observer, including when a stop lands between the rAF and the ResizeObserver step. A 0x0 hidden frame pauses and resumes on its own once it has a size.
Bounds. Pushes reuse elementGeometry, the same code the geometry reply uses, so no new data leaves the frame. The caps hold: at most 32 handles per watch and 8 watching ports. geometryEvent checks the exact key set at every level, requires finite numbers and caps list lengths. Dispose sends the empty list before it closes the port.
Fails without the fix. The named tests pass, 174/174, three runs in a row. Each mutation fails the number of tests the body predicts:
- Measuring back inside rAF fails 2.
- Leaving the observer attached on stop fails 1.
- Sending no stop message on dispose fails 1.
- Removing the watcher cap fails 1.
- Using
isRecordinstead ofkeysArefails 1.
Nit (non-blocking). Removing the unobserve at the top of push passes every test. In a real browser that's harmless, because the next observe() resets the size anyway.
CI. Every check-run at this head is green or skipped.
— Rames
What
Adds a new editing-port request,
watch-geometry. The editor names up to 32 element handles. On every animation frame, the preview then pushes those elements' geometry to the editor, with the same fields thegeometryreply already gives, stamped withperformance.timeOrigin + performance.now().The push stops when:
The editor's client parses every push against a field allowlist and drops anything else.
Why
The edit-accuracy bench checks that a dragged element stays under the pointer. To do that it needs the element's position and the pointer's position at the same moment.
With a pushed, timestamped stream, the bench can pair each frame's geometry with the pointer's position at that same time. That is the same pairing the direct read gets.
Related work
Refs #8010. That bench change will use this request, and will land after this PR.
How
packages/core/src/runtime/editing/watchGeometry.tsholds the new kind:MAX_WATCHED_GEOMETRY(32) handles, and an empty list as the stop.ResizeObserverstep, which runs after every rAF callback and layout, just before paint. A private observer watchesdocument.documentElement. Each frame, a rAF callback callsobserve(), which resets the last reported size, so the root reports once whether or not anything resized. The callback then measures, pushes and callsunobserve(). No node is added to the page. A measure taken inside rAF, or in a task after it, could report a state that a later callback or task changed before paint. It measures withelementGeometry, the function that answersgeometry, so a push carries exactly the fields a reply does, and nothing new.MAX_GEOMETRY_WATCHERS(8) ports may watch at once. A further port's request fails, and a port that is already watching can still change its list.postMessagethrows loses its watch. A measure that throws is logged throughswallow, and the next frame is still scheduled (finally).geometryEventis the editor-side parser. It checks every key at every level (rect, corners, source, clip path, members, visible) and drops a push with any unknown field, wrong type or over-cap list. It works the waykeyEventdoes forwatch-keys.editingPort.tshas the kind's entry in the request list, its types and its parser. The client gainsonGeometry(listener). If the client ever asked forwatch-geometry,dispose()first sends the empty list, so the frame stops pushing to a released port.editingAgent.tsadds the handler, and itsdispose()stops every geometry watch.performance.timeOrigin + performance.now()on each side. I measured them in Chromium 152 headless, with the frame on another origin in its own process (--site-per-process), over 3 runs of 300 round trips each:Test plan
editing/watchGeometry.test.ts:geometryEventtakes what the frame measures. It drops a push with an extra field at the top level, inside an element, inside a rect or member rect, a wrong version or event name, a non-finite stamp, a shortcornersorsource, and a too-long clip path.nullfor a gone handle. It stops on an empty list, onpagehide, and on the agent's stop.editingPort.test.ts: the client hands valid pushes to its listeners, drops an off-shape one, and sends the empty list when disposed.editingAgent.test.ts:watch-geometryrefuses bad params like every other kind, and is listed inhello.editing/watchGeometry.test.tsandeditingPort.test.ts. Each test is shown to fail with its guard removed:keysAredropped from the element check: 1 test fails.pagehidelistener removed: 1 test fails.finallyremoved: 3 tests fail.