From ba9f053fc4af83b43f54a31abe2ad343b3b8cad4 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Fri, 28 Aug 2026 13:14:30 +0200 Subject: [PATCH] :zap: Defer stale text selrect sync until viewport is ready Coalesce stale-selrect ids and emit after the first tiles-complete pass, scheduled on idle, so the page can paint before resize work. --- frontend/src/app/render_wasm/api.cljs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 12f072ee35..e9ee887e33 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -1610,6 +1610,29 @@ :auto-height (not (mth/close? height (:height selrect) 0.1)) false))) +(defonce ^:private pending-stale-selrect-ids (atom #{})) +(defonce ^:private stale-selrect-sync-token (atom 0)) + +(defn- flush-stale-selrect-sync! + [] + (when-let [ids (seq (first (reset-vals! pending-stale-selrect-ids #{})))] + (st/emit! (ptk/data-event ::stale-text-selrects {:ids (vec ids)})))) + +(defn- schedule-stale-selrect-sync! + "Coalesce stale-selrect emissions and defer until the first viewport tile + pass completes, then run on idle so page load can paint first." + [stale-ids] + (swap! pending-stale-selrect-ids into stale-ids) + (let [token (swap! stale-selrect-sync-token inc) + flush-on-idle! + (fn [] + (when (= token @stale-selrect-sync-token) + (timers/schedule-on-idle + (fn [] + (when (= token @stale-selrect-sync-token) + (flush-stale-selrect-sync!))))))] + (listen-tiles-render-complete-once! flush-on-idle!))) + (defn- sync-stale-text-selrects! "Emit the ids of auto-grow text shapes whose selrect no longer matches the measured layout, so the workspace resizes them (data-event instead of a @@ -1623,7 +1646,7 @@ (map :id)) shapes)] (when (seq stale-ids) - (st/emit! (ptk/data-event ::stale-text-selrects {:ids stale-ids}))))) + (schedule-stale-selrect-sync! stale-ids)))) (defn- relayout-after-fonts! "Relayout text shapes once their pending fonts have resolved. Font fetches