From 30d7ba71369ccccc755a13b5a3a837ef8f0343d4 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 20 Nov 2024 09:23:31 +0100 Subject: [PATCH] :sparkles: Add id and type to wasm shape-proxy static attrs --- frontend/src/app/render_wasm/shape.cljs | 79 +++++++++++++++++++------ 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/frontend/src/app/render_wasm/shape.cljs b/frontend/src/app/render_wasm/shape.cljs index daaaedb635..39d5557a9f 100644 --- a/frontend/src/app/render_wasm/shape.cljs +++ b/frontend/src/app/render_wasm/shape.cljs @@ -16,7 +16,11 @@ (declare ^:private impl-conj) (declare ^:private impl-dissoc) -(deftype ShapeProxy [delegate] +(defn map-entry + [k v] + (cljs.core/MapEntry. k v nil)) + +(deftype ShapeProxy [id type delegate] Object (toString [coll] (str "{" (str/join ", " (for [[k v] coll] (str k " " v))) "}")) @@ -29,7 +33,7 @@ IWithMeta (-with-meta [_ meta] - (ShapeProxy. (with-meta delegate meta))) + (ShapeProxy. id type (with-meta delegate meta))) IMeta (-meta [_] (meta delegate)) @@ -49,7 +53,9 @@ ISeqable (-seq [_] - (c/-seq delegate)) + (cons (map-entry :id id) + (cons (map-entry :type type) + (c/-seq delegate)))) ICounted (-count [_] @@ -60,18 +66,28 @@ (-lookup coll k nil)) (-lookup [_ k not-found] - (c/-lookup delegate k not-found)) + (case k + :id id + :type type + (c/-lookup delegate k not-found))) IFind (-find [_ k] - (c/-find delegate k)) + (case k + :id + (map-entry :id id) + :type + (map-entry :type type) + (c/-find delegate k))) IAssociative (-assoc [coll k v] (impl-assoc coll k v)) (-contains-key? [_ k] - (contains? delegate k)) + (or (= k :id) + (= k :type) + (contains? delegate k))) IMap (-dissoc [coll k] @@ -79,7 +95,7 @@ IFn (-invoke [coll k] - (-lookup coll k)) + (-lookup coll k nil)) (-invoke [coll k not-found] (-lookup coll k not-found)) @@ -107,19 +123,44 @@ ;; is modified, we need to request ;; a new render. (api/request-render)) - (let [delegate (.-delegate ^ShapeProxy self) - delegate' (assoc delegate k v)] - (if (identical? delegate' delegate) - self - (ShapeProxy. delegate')))) + (case k + :id + (ShapeProxy. v + (.-type ^ShapeProxy self) + (.-delegate ^ShapeProxy self)) + :type + (ShapeProxy. (.-id ^ShapeProxy self) + v + (.-delegate ^ShapeProxy self)) + + (let [delegate (.-delegate ^ShapeProxy self) + delegate' (assoc delegate k v)] + (if (identical? delegate' delegate) + self + (ShapeProxy. (.-id ^ShapeProxy self) + (.-type ^ShapeProxy self) + delegate'))))) (defn- impl-dissoc [self k] - (let [delegate (.-delegate ^ShapeProxy self) - delegate' (dissoc delegate k)] - (if (identical? delegate delegate') - self - (ShapeProxy. delegate')))) + (case k + :id + (ShapeProxy. nil + (.-type ^ShapeProxy self) + (.-delegate ^ShapeProxy self)) + :type + (ShapeProxy. (.-id ^ShapeProxy self) + nil + (.-delegate ^ShapeProxy self)) + + :else + (let [delegate (.-delegate ^ShapeProxy self) + delegate' (dissoc delegate k)] + (if (identical? delegate delegate') + self + (ShapeProxy. (.-id ^ShapeProxy self) + (.-type ^ShapeProxy self) + delegate'))))) (defn- impl-conj [self entry] @@ -137,7 +178,9 @@ (defn create-shape "Instanciate a shape from a map" [attrs] - (ShapeProxy. attrs)) + (ShapeProxy. (:id attrs) + (:type attrs) + (dissoc attrs :id :type))) (t/add-handlers! ;; We only add a write handler, read handler uses the dynamic dispatch