From e0832335c7e611a832e3fbe608334fb08f7bfb42 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Tue, 17 Nov 2026 12:48:19 +0100 Subject: [PATCH] :bug: 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 --- .../shapes/text/viewport_texts_html.cljs | 14 ++-- frontend/test/frontend_tests/runner.cljs | 2 + .../ui/viewport_texts_html_test.cljs | 71 +++++++++++++++++++ 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs diff --git a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs index 3869f62a03..87b65864a3 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs @@ -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] diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 07f30d46ce..61de81d853 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -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 diff --git a/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs b/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs new file mode 100644 index 0000000000..d6369e5882 --- /dev/null +++ b/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs @@ -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")))))))