Set page objects once on wasm render(#6994)

This commit is contained in:
Elena Torró 2025-07-29 13:00:40 +02:00 committed by GitHub
parent 3f657a0c04
commit a16f40cb73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 39 deletions

View File

@ -51,6 +51,7 @@
[app.main.ui.workspace.viewport.viewport-ref :refer [create-viewport-ref]]
[app.main.ui.workspace.viewport.widgets :as widgets]
[app.render-wasm.api :as wasm.api]
[app.render-wasm.shape :as wasm.shape]
[app.util.debug :as dbg]
[app.util.text-editor :as ted]
[beicon.v2.core :as rx]
@ -280,7 +281,6 @@
(:x first-shape)
(:x selected-frame))
offset-y (if selecting-first-level-frame?
(:y first-shape)
(:y selected-frame))
@ -292,7 +292,6 @@
;; canvas, even though we are not using `page-id` inside the hook.
;; We think moving this out to a handler will make the render code
;; harder to follow through.
(mf/with-effect [page-id]
(when-let [canvas (mf/ref-val canvas-ref)]
(->> wasm.api/module
@ -349,6 +348,9 @@
(wasm.api/show-grid @hover-top-frame-id)
(wasm.api/clear-grid))))
(mf/with-effect [objects]
(wasm.shape/set-current-page-objects! objects))
(hooks/setup-dom-events zoom disable-paste in-viewport? read-only? drawing-tool path-drawing?)
(hooks/setup-viewport-size vport viewport-ref)
(hooks/setup-cursor cursor alt? mod? space? panning drawing-tool path-drawing? path-editing? z? read-only?)

View File

@ -1068,9 +1068,3 @@
(js/console.error cause)
(p/resolved false)))))
(p/resolved false))))
(defn shape-in-current-page?
"Check if a shape is in the current page by looking up the current page objects"
[shape-id]
(let [objects (deref refs/workspace-page-objects)]
(contains? objects shape-id)))

View File

@ -19,6 +19,15 @@
(declare ^:private impl-assoc)
(declare ^:private impl-conj)
(declare ^:private impl-dissoc)
(defonce ^:private current-page-objects {})
(defn set-current-page-objects!
[objects]
(set! current-page-objects objects))
(defn shape-in-current-page?
[shape-id]
(contains? current-page-objects shape-id))
(defn map-entry
[k v]
@ -211,35 +220,13 @@
(defn set-wasm-multi-attrs!
[shape properties]
;; Only call WASM API if the shape is in the current page
(when (api/shape-in-current-page? (:id shape))
(api/use-shape (:id shape))
(let [result
(->> properties
(mapcat #(set-wasm-single-attr! shape %)))
pending (-> (d/index-by :key :callback result) vals)]
(if (and pending (seq pending))
(->> (rx/from pending)
(rx/mapcat (fn [callback] (callback)))
(rx/reduce conj [])
(rx/subs!
(fn [_]
(api/update-shape-tiles)
(api/clear-drawing-cache)
(api/request-render "set-wasm-attrs-pending"))))
(do
(api/update-shape-tiles)
(api/request-render "set-wasm-attrs"))))))
(defn set-wasm-attrs!
[shape k v]
(let [shape (assoc shape k v)]
;; Only call WASM API if the shape is in the current page
(when (api/shape-in-current-page? (:id shape))
(api/use-shape (:id shape))
(let [result (set-wasm-single-attr! shape k)
(let [shape-id (dm/get-prop shape :id)]
(when (shape-in-current-page? shape-id)
(api/use-shape shape-id)
(let [result
(->> properties
(mapcat #(set-wasm-single-attr! shape %)))
pending (-> (d/index-by :key :callback result) vals)]
;; TODO: set-wasm-attrs is called twice with every set
(if (and pending (seq pending))
(->> (rx/from pending)
(rx/mapcat (fn [callback] (callback)))
@ -253,6 +240,27 @@
(api/update-shape-tiles)
(api/request-render "set-wasm-attrs")))))))
(defn set-wasm-attrs!
[shape k v]
(let [shape-id (dm/get-prop shape :id)]
(when (shape-in-current-page? shape-id)
(let [shape (assoc shape k v)]
(api/use-shape shape-id)
(let [result (set-wasm-single-attr! shape k)
pending (-> (d/index-by :key :callback result) vals)]
(if (and pending (seq pending))
(->> (rx/from pending)
(rx/mapcat (fn [callback] (callback)))
(rx/reduce conj [])
(rx/subs!
(fn [_]
(api/update-shape-tiles)
(api/clear-drawing-cache)
(api/request-render "set-wasm-attrs-pending"))))
(do
(api/update-shape-tiles)
(api/request-render "set-wasm-attrs"))))))))
(defn- impl-assoc
[self k v]
(when ^boolean shape/*wasm-sync*
@ -281,10 +289,8 @@
[self k]
(when ^boolean shape/*wasm-sync*
(binding [shape/*wasm-sync* false]
;; Only call WASM API if the shape is in the current page
(when (api/shape-in-current-page? (.-id ^ShapeProxy self))
(when (shape-in-current-page? (.-id ^ShapeProxy self))
(set-wasm-attrs! self k nil))))
(case k
:id
(ShapeProxy. nil