penpot/frontend/src/app/plugins/reflow.cljs
Pablo Alba 73e7815817
🐛 Make execute_code wait for save before success (#13027)
* 🐛 Make execute_code wait for save before success

* 🐛 Address review feedback on waitForSave

Reject with an Error carrying a `code`: save-timeout, rolled-back,
save-retrying, or the failure code (save-failed by default). Lower the
default timeout from 90s to 60s, leaving room for the 30s layout wait
and the code under the MCP tool timeout.

When persistence is already in error at the start of the wait, keep the
stored failure code, and tell a failure Penpot keeps retrying apart from
one that needs the file reopened.

Share timeout validation with waitForLayoutUpdate through
app.plugins.utils, and make reject-not-valid reject with an Error, which
also fixes the string rejections of connectLibrary, restore, remove,
pin, export, findVersions and saveVersion.

The MCP execute_code handler now waits for the save only when the
workspace is ready, like the layout wait, so dashboard calls no longer
fail on a save error from an earlier file.

Move the waitForSave tests to frontend-tests.plugins, add tests for the
error codes, the retrying state and a failure during the wait, and add
plugin API suite and MCP handler tests. Update the docs, CHANGELOG and
the generated API files.

AI-assisted-by: claude-opus-5-5
2026-11-17 22:35:37 +01:00

59 lines
2.3 KiB
Clojure

;; 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 app.plugins.reflow
"Promise adapter for the plugin `waitForLayoutUpdate` methods. Owns the
argument validation, the default deadline and the rejection shape; the
workspace only reports when its pending work has drained."
(:require
[app.common.files.helpers :as cfh]
[app.common.uuid :as uuid]
[app.main.data.workspace.reflow :as wrf]
[app.plugins.utils :as u]
[beicon.v2.core :as rx]))
;; Ceiling for callers that pass no timeout, so a pipeline that never drains
;; its marks rejects the promise rather than leaving it unsettled.
(def ^:private default-timeout 30000)
(defn shape-wait-ids
"Ids a per-shape wait covers: the shape subtree, its ancestors, and the file
its components sync from."
[objects file-id id]
(-> (into #{} (cfh/get-children-ids-with-self objects id))
(into (cfh/get-parent-ids objects id))
(conj file-id)
(disj uuid/zero)))
(defn wait-for-layout-update
"Returns a JS Promise that resolves once every id in `ids` has drained from
the workspace pending map. A nil `ids` waits for every pending id; an empty
one has nothing to wait for and resolves right away.
The promise is rejected when `timeout` (ms) is not a valid timer value, or
when it elapses first; a nil `timeout` uses `default-timeout`."
([timeout]
(wait-for-layout-update nil timeout))
([ids timeout]
(js/Promise.
(fn [resolve reject]
(if-not (u/valid-timeout? timeout)
(u/reject-not-valid reject :waitForLayoutUpdate timeout)
;; Race the settle signal against the deadline; the loser is
;; unsubscribed. `settled` replays on subscribe, so an already drained
;; map wins even against a 1ms deadline.
(->> (rx/race (->> (rx/of :timeout)
(rx/delay (or timeout default-timeout)))
(->> (wrf/settled ids)
(rx/map (constantly :ok))))
(rx/take 1)
(rx/subs!
(fn [value]
(if (= value :timeout)
(reject (js/Error. "waitForLayoutUpdate timeout"))
(resolve)))
reject)))))))