🐛 Keep text mounted until its measurement settles (#13097)

The text measurement in update-text-shape used ->> with p/catch. But
p/catch takes the promise first, so the handler was read as the promise.
The call then returned a promise that resolved to the handler function at
once. Measurement errors were never logged, and a failed measurement was
left as an unhandled rejection.

The caller ran p/finally right away, so the text left the pending set and
its :text-measure reflow task closed before it was measured. The text
could then unmount: the measurement read a detached node, got a zero rect
and skipped resize-text. waitForLayoutUpdate could also resolve before
the text was measured. This bug came in with #9898.

Use p/merr instead, which takes the function first. It logs the error and
resolves to nil, so the promise settles only when the measurement does.

Tests check that the promise stays pending until the measurement settles,
that the measurement then completes, and that a failed measurement
resolves to nil.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Pablo Alba 2026-11-17 12:48:19 +01:00 committed by GitHub
parent fa1249c1a5
commit e0832335c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 6 deletions

View File

@ -97,12 +97,14 @@
(st/emit! (dwt/resize-text id width height)))))
(st/emit! (dwt/clean-text-modifier id))))
;; Always clear the task and log measurement errors.
(p/catch (fn [cause]
(log/error :hint "Could not measure text shape"
:shape-id id
:cause cause)
nil))))
;; Log measurement errors and resolve to nil. The returned promise
;; stays pending until the measurement settles, so callers keep the
;; text mounted while it is measured.
(p/merr (fn [cause]
(log/error :hint "Could not measure text shape"
:shape-id id
:cause cause)
(p/resolved nil)))))
(defn- update-text-modifier
[{:keys [grow-type id] :as shape} node]

View File

@ -176,6 +176,7 @@
[frontend-tests.ui.subscription-test]
[frontend-tests.ui.team-container-test]
[frontend-tests.ui.text-attrs-multiple-test]
[frontend-tests.ui.viewport-texts-html-test]
[frontend-tests.ui.viewport-top-bar-test]
[frontend-tests.ui.workspace-libraries-test]
[frontend-tests.util-clipboard-test]
@ -384,6 +385,7 @@
'frontend-tests.ui.subscription-test
'frontend-tests.ui.team-container-test
'frontend-tests.ui.text-attrs-multiple-test
'frontend-tests.ui.viewport-texts-html-test
'frontend-tests.ui.viewport-top-bar-test
'frontend-tests.ui.workspace-libraries-test
'frontend-tests.util-clipboard-test

View File

@ -0,0 +1,71 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns frontend-tests.ui.viewport-texts-html-test
"How the SVG renderer measures text shapes and commits the result."
(:require
[app.common.uuid :as uuid]
[app.main.data.workspace.texts :as dwt]
[app.main.store :as st]
[app.main.ui.workspace.shapes.text.viewport-texts-html :as vth]
[app.util.text-svg-position :as tsp]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.async :as async]
[frontend-tests.helpers.mock :as mock]
[potok.v2.core :as ptk]
[promesa.core :as p]))
(def ^:private fixed-text
{:id (uuid/next) :type :text :grow-type :fixed})
(defn- measure-text
"Measure `shape` as the SVG renderer does and return the measurement
promise. Kept out of the `^:async` test bodies: there, a call through the
var compiles to an awaited wrapper that would wait for the measurement."
[shape]
(#'vth/update-text-shape shape nil))
(defn- with-measurement
"Run `f` with the text measurement mocked to `measurement`, a promise the
test settles. Events emitted to the store are recorded in `emitted`."
[measurement emitted f]
(mock/with-mocks*
{tsp/calc-position-data (mock/stub (constantly measurement))
st/emit! (mock/stub (fn [& events] (swap! emitted into events) nil))}
(await (f))))
(t/deftest ^:async measurement-promise-settles-with-the-measurement
(let [measurement (p/deferred)
emitted (atom [])
settled? (atom false)]
(await
(with-measurement
measurement
emitted
(^:async fn []
(let [result (p/finally (measure-text fixed-text)
#(reset! settled? true))]
(await (async/settle))
(t/is (false? @settled?)
"the promise stays pending while the text is measured")
(p/resolve measurement [])
(await result)
(t/is (true? @settled?)
"the promise settles once the measurement settles")
(t/is (some #(= ::dwt/clean-text-modifier (ptk/type %)) @emitted)
"the measurement completes and cleans the text modifier")))))))
(t/deftest ^:async failed-measurement-resolves-to-nil
(let [measurement (p/deferred)]
(await
(with-measurement
measurement
(atom [])
(^:async fn []
(let [result (measure-text fixed-text)]
(p/reject measurement (ex-info "measure failed" {}))
(t/is (nil? (await result))
"a failed measurement resolves to nil")))))))