Add id and type to wasm shape-proxy static attrs

This commit is contained in:
Andrey Antukh 2024-11-20 09:23:31 +01:00 committed by AzazelN28
parent 82104dd823
commit 30d7ba7136

View file

@ -16,7 +16,11 @@
(declare ^:private impl-conj) (declare ^:private impl-conj)
(declare ^:private impl-dissoc) (declare ^:private impl-dissoc)
(deftype ShapeProxy [delegate] (defn map-entry
[k v]
(cljs.core/MapEntry. k v nil))
(deftype ShapeProxy [id type delegate]
Object Object
(toString [coll] (toString [coll]
(str "{" (str/join ", " (for [[k v] coll] (str k " " v))) "}")) (str "{" (str/join ", " (for [[k v] coll] (str k " " v))) "}"))
@ -29,7 +33,7 @@
IWithMeta IWithMeta
(-with-meta [_ meta] (-with-meta [_ meta]
(ShapeProxy. (with-meta delegate meta))) (ShapeProxy. id type (with-meta delegate meta)))
IMeta IMeta
(-meta [_] (meta delegate)) (-meta [_] (meta delegate))
@ -49,7 +53,9 @@
ISeqable ISeqable
(-seq [_] (-seq [_]
(c/-seq delegate)) (cons (map-entry :id id)
(cons (map-entry :type type)
(c/-seq delegate))))
ICounted ICounted
(-count [_] (-count [_]
@ -60,18 +66,28 @@
(-lookup coll k nil)) (-lookup coll k nil))
(-lookup [_ k not-found] (-lookup [_ k not-found]
(c/-lookup delegate k not-found)) (case k
:id id
:type type
(c/-lookup delegate k not-found)))
IFind IFind
(-find [_ k] (-find [_ k]
(c/-find delegate k)) (case k
:id
(map-entry :id id)
:type
(map-entry :type type)
(c/-find delegate k)))
IAssociative IAssociative
(-assoc [coll k v] (-assoc [coll k v]
(impl-assoc coll k v)) (impl-assoc coll k v))
(-contains-key? [_ k] (-contains-key? [_ k]
(contains? delegate k)) (or (= k :id)
(= k :type)
(contains? delegate k)))
IMap IMap
(-dissoc [coll k] (-dissoc [coll k]
@ -79,7 +95,7 @@
IFn IFn
(-invoke [coll k] (-invoke [coll k]
(-lookup coll k)) (-lookup coll k nil))
(-invoke [coll k not-found] (-invoke [coll k not-found]
(-lookup coll k not-found)) (-lookup coll k not-found))
@ -107,19 +123,44 @@
;; is modified, we need to request ;; is modified, we need to request
;; a new render. ;; a new render.
(api/request-render)) (api/request-render))
(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) (let [delegate (.-delegate ^ShapeProxy self)
delegate' (assoc delegate k v)] delegate' (assoc delegate k v)]
(if (identical? delegate' delegate) (if (identical? delegate' delegate)
self self
(ShapeProxy. delegate')))) (ShapeProxy. (.-id ^ShapeProxy self)
(.-type ^ShapeProxy self)
delegate')))))
(defn- impl-dissoc (defn- impl-dissoc
[self k] [self k]
(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) (let [delegate (.-delegate ^ShapeProxy self)
delegate' (dissoc delegate k)] delegate' (dissoc delegate k)]
(if (identical? delegate delegate') (if (identical? delegate delegate')
self self
(ShapeProxy. delegate')))) (ShapeProxy. (.-id ^ShapeProxy self)
(.-type ^ShapeProxy self)
delegate')))))
(defn- impl-conj (defn- impl-conj
[self entry] [self entry]
@ -137,7 +178,9 @@
(defn create-shape (defn create-shape
"Instanciate a shape from a map" "Instanciate a shape from a map"
[attrs] [attrs]
(ShapeProxy. attrs)) (ShapeProxy. (:id attrs)
(:type attrs)
(dissoc attrs :id :type)))
(t/add-handlers! (t/add-handlers!
;; We only add a write handler, read handler uses the dynamic dispatch ;; We only add a write handler, read handler uses the dynamic dispatch