Add the ability to deref internal state on library file instance

This commit is contained in:
Andrey Antukh 2025-05-08 14:07:46 +02:00
parent dc7e53881a
commit 4c487834f0

View file

@ -58,193 +58,198 @@
(json/->js params) (json/->js params)
params)) params))
(defn- create-file* (defn- create-file-api
[file] [file]
(let [state* (volatile! file)] (let [state* (volatile! file)
(obj/reify {:name "File"} api (obj/reify {:name "File"}
:id :id
{:get #(dm/str (:id @state*))} {:get #(dm/str (:id @state*))}
:currentFrameId :currentFrameId
{:get #(dm/str (::fb/current-frame-id @state*))} {:get #(dm/str (::fb/current-frame-id @state*))}
:currentPageId :currentPageId
{:get #(dm/str (::fb/current-page-id @state*))} {:get #(dm/str (::fb/current-page-id @state*))}
:lastId :lastId
{:get #(dm/str (::fb/last-id @state*))} {:get #(dm/str (::fb/last-id @state*))}
:addPage :addPage
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(decode-params) (decode-params)
(fb/decode-page))] (fb/decode-page))]
(vswap! state* fb/add-page params) (vswap! state* fb/add-page params)
(dm/str (::fb/current-page-id @state*))) (dm/str (::fb/current-page-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:closePage :closePage
(fn [] (fn []
(vswap! state* fb/close-page)) (vswap! state* fb/close-page))
:addArtboard :addArtboard
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(assoc :type :frame) (assoc :type :frame)
(fb/decode-shape))] (fb/decode-shape))]
(vswap! state* fb/add-artboard params) (vswap! state* fb/add-artboard params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:closeArtboard :closeArtboard
(fn [] (fn []
(vswap! state* fb/close-artboard)) (vswap! state* fb/close-artboard))
:addGroup :addGroup
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(assoc :type :group) (assoc :type :group)
(fb/decode-shape))] (fb/decode-shape))]
(vswap! state* fb/add-group params) (vswap! state* fb/add-group params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:closeGroup :closeGroup
(fn [] (fn []
(vswap! state* fb/close-group)) (vswap! state* fb/close-group))
:addBool :addBool
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(fb/decode-add-bool))] (fb/decode-add-bool))]
(vswap! state* fb/add-bool params) (vswap! state* fb/add-bool params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:addRect :addRect
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(assoc :type :rect) (assoc :type :rect)
(fb/decode-shape))] (fb/decode-shape))]
(vswap! state* fb/add-shape params) (vswap! state* fb/add-shape params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:addCircle :addCircle
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(assoc :type :circle) (assoc :type :circle)
(fb/decode-shape))] (fb/decode-shape))]
(vswap! state* fb/add-shape params) (vswap! state* fb/add-shape params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:addPath :addPath
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(assoc :type :path) (assoc :type :path)
(fb/decode-shape))] (fb/decode-shape))]
(vswap! state* fb/add-shape params) (vswap! state* fb/add-shape params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:addText :addText
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(assoc :type :text) (assoc :type :text)
(fb/decode-shape))] (fb/decode-shape))]
(vswap! state* fb/add-shape params) (vswap! state* fb/add-shape params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:addLibraryColor :addLibraryColor
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(fb/decode-library-color) (fb/decode-library-color)
(d/without-nils))] (d/without-nils))]
(vswap! state* fb/add-library-color params) (vswap! state* fb/add-library-color params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:addLibraryTypography :addLibraryTypography
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(fb/decode-library-typography) (fb/decode-library-typography)
(d/without-nils))] (d/without-nils))]
(vswap! state* fb/add-library-typography params) (vswap! state* fb/add-library-typography params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:addComponent :addComponent
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(fb/decode-component) (fb/decode-component)
(d/without-nils))] (d/without-nils))]
(vswap! state* fb/add-component params) (vswap! state* fb/add-component params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:addComponentInstance :addComponentInstance
(fn [params] (fn [params]
(try (try
(let [params (-> params (let [params (-> params
(json/->clj) (json/->clj)
(fb/decode-add-component-instance) (fb/decode-add-component-instance)
(d/without-nils))] (d/without-nils))]
(vswap! state* fb/add-component-instance params) (vswap! state* fb/add-component-instance params)
(dm/str (::fb/last-id @state*))) (dm/str (::fb/last-id @state*)))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))
:getShape :getShape
(fn [shape-id] (fn [shape-id]
(let [shape-id (uuid/parse shape-id)] (let [shape-id (uuid/parse shape-id)]
(some-> (fb/lookup-shape @state* shape-id) (some-> (fb/lookup-shape @state* shape-id)
(json/->js)))) (json/->js))))
:toMap :toMap
(fn [] (fn []
(-> @state* (-> @state*
(d/without-qualified) (d/without-qualified)
(json/->js)))))) (json/->js))))]
(specify! api
cljs.core/IDeref
(-deref [_]
(d/without-qualified @state*)))))
(defn create-file (defn create-file
[params] [params]
(try (try
(let [params (-> params json/->clj fb/decode-file) (let [params (-> params json/->clj fb/decode-file)
file (fb/create-file params)] file (fb/create-file params)]
(create-file* file)) (create-file-api file))
(catch :default cause (catch :default cause
(handle-exception cause)))) (handle-exception cause))))