0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-09 00:10:11 -05:00

♻️ An other rework of workspace initialization.

This commit is contained in:
Andrey Antukh 2020-01-07 10:48:37 +01:00
parent 188872d712
commit dc9fc5e5a9
2 changed files with 79 additions and 98 deletions

View file

@ -142,33 +142,44 @@
ptk/WatchEvent
(watch [_ state stream]
(let [wsession (get-in state [:ws file-id])]
(->> (rx/merge
(rx/of (fetch-users file-id))
(->> (ws/-stream wsession)
(rx/filter #(= :message (:type %)))
(rx/map (comp t/decode :payload))
(rx/filter #(s/valid? ::message %))
(rx/map (fn [{:keys [type] :as msg}]
(case type
:who (handle-who msg)
:pointer-update (handle-pointer-update msg)
:page-snapshot (handle-page-snapshot msg)
::unknown)))))
(let [wsession (get-in state [:ws file-id])
stoper (rx/filter #(= ::finalize-ws %) stream)]
(->> (ws/-stream wsession)
(rx/filter #(= :message (:type %)))
(rx/map (comp t/decode :payload))
(rx/filter #(s/valid? ::message %))
(rx/map (fn [{:keys [type] :as msg}]
(case type
:who (handle-who msg)
:pointer-update (handle-pointer-update msg)
:page-snapshot (handle-page-snapshot msg)
::unknown)))
(rx/take-until stoper))))))
(rx/take-until
(rx/filter #(= ::finalize %) stream)))))))
;; #_(->> stream
;; ;; TODO: this need to be rethinked
;; (rx/filter uxbox.main.ui.workspace.streams/pointer-event?)
;; (rx/sample 150)
;; (rx/tap (fn [{:keys [pt] :as event}]
;; (let [msg {:type :pointer-update
;; :page-id page-id
;; :x (:x pt)
;; :y (:y pt)}]
;; (ws/-send (get-in state [:ws file-id]) (t/encode msg)))))
;; (rx/ignore)
;; (rx/take-until (rx/filter #(= ::finalize %) stream))))))))
;; --- Finalize Websocket
(defn finalize-ws
[file-id]
(ptk/reify ::finalize
(ptk/reify ::finalize-ws
ptk/WatchEvent
(watch [_ state stream]
(ws/-close (get-in state [:ws file-id]))
(rx/of ::finalize))))
(rx/of ::finalize-ws))))
;; --- Handle: Who
@ -232,67 +243,31 @@
:tooltip nil})
(declare initialized)
(declare initialize-page)
(defn initialize
"Initialize the workspace state."
[file-id]
[file-id page-id]
(s/assert ::us/uuid file-id)
(s/assert ::us/uuid page-id)
(ptk/reify ::initialize
ptk/UpdateEvent
(update [_ state]
(let [local (assoc workspace-default
:file-id file-id)
;; :page-id page-id)
;; TODO: this need to be parametrized
uri (str "ws://localhost:6060/sub/" file-id)]
(-> state
(assoc :workspace-layout default-layout)
(assoc :workspace-local local)
(assoc-in [:ws file-id] (ws/open uri)))))
ptk/WatchEvent
(watch [_ state stream]
(let [wsession (get-in state [:ws file-id])]
(rx/merge
;; Stop possible previous watchers and re-fetch the main page
;; and all project related pages.
(rx/of ::stop-watcher
(dp/fetch-file file-id)
(dp/fetch-pages file-id)
(fetch-users file-id))
;; When main page is fetched, schedule the main initialization.
(->> (rx/zip (rx/filter (ptk/type? ::dp/pages-fetched) stream)
(rx/filter (ptk/type? ::dp/files-fetched) stream))
(rx/take 1)
(rx/do #(reset! st/loader false))
(rx/mapcat #(rx/of (initialized file-id)
#_(initialize-alignment page-id))))
;; WebSocket Incoming Messages Handling
(->> (ws/-stream wsession)
(rx/filter #(= :message (:type %)))
(rx/map (comp t/decode :payload))
(rx/filter #(s/valid? ::message %))
(rx/map (fn [{:keys [type] :as msg}]
(case type
:who (handle-who msg)
:pointer-update (handle-pointer-update msg)
:page-snapshot (handle-page-snapshot msg)
::unknown))))
#_(->> stream
;; TODO: this need to be rethinked
(rx/filter uxbox.main.ui.workspace.streams/pointer-event?)
(rx/sample 150)
(rx/tap (fn [{:keys [pt] :as event}]
(let [msg {:type :pointer-update
:page-id page-id
:x (:x pt)
:y (:y pt)}]
(ws/-send (get-in state [:ws file-id]) (t/encode msg)))))
(rx/ignore)
(rx/take-until (rx/filter #(= ::finalize %) stream))))))))
(let [local (:workspace-local state)]
(if (not= (:file-id local) file-id)
(rx/merge
(rx/of (dp/fetch-file file-id)
(dp/fetch-pages file-id)
(fetch-users file-id))
(->> (rx/zip (rx/filter (ptk/type? ::dp/pages-fetched) stream)
(rx/filter (ptk/type? ::dp/files-fetched) stream))
(rx/take 1)
(rx/do #(reset! st/loader false))
(rx/mapcat #(rx/of (initialized file-id)
(initialize-page page-id)
#_(initialize-alignment page-id)))))
(rx/merge
(rx/of (initialize-page page-id))))))))
(defn- initialized
[file-id]
@ -300,18 +275,12 @@
(ptk/reify ::initialized
ptk/UpdateEvent
(update [_ state]
(let [file (get-in state [:files file-id])]
(assoc state :workspace-file file)))))
(defn finalize
[file-id]
(ptk/reify ::finalize
cljs.core/IDeref
(-deref [_] file-id)
ptk/EffectEvent
(effect [_ state stream]
(ws/-close (get-in state [:ws file-id])))))
(let [file (get-in state [:files file-id])
local (assoc workspace-default :file-id file-id)]
(-> state
(assoc :workspace-file file)
(assoc :workspace-layout default-layout)
(assoc :workspace-local local))))))
(defn initialize-page
[page-id]
@ -325,7 +294,9 @@
:workspace-page page)))
ptk/EffectEvent
(effect [_ state stream])))
(effect [_ state stream]
;; TODO: emit join page event
)))
;; --- Fetch Workspace Users

View file

@ -95,10 +95,6 @@
(mf/defc workspace-page
[{:keys [file-id page-id layout file flags] :as props}]
(mf/use-effect
{:deps (mf/deps file-id page-id)
:fn #(st/emit! (dw/initialize-page page-id))})
(let [page (mf/deref refs/workspace-page)]
[:> rdnd/provider {:backend rdnd/html5}
[:& messages-widget]
@ -117,11 +113,19 @@
(mf/defc workspace
[{:keys [file-id page-id] :as props}]
(mf/use-effect
{:deps (mf/deps file-id page-id)
:fn #(st/emit! (dw/initialize file-id page-id))})
(mf/use-effect
{:deps (mf/deps file-id)
:fn (fn []
(st/emit! (dw/initialize file-id))
#(st/emit! (dw/finalize file-id)))})
(st/emit! (dw/initialize-ws file-id))
#(st/emit! (dw/finalize-ws file-id)))})
;; (mf/use-effect
;; {:deps (mf/deps file-id page-id)
;; :fn #(st/emit! (dw/initialize-page page-id))})
(mf/use-effect
{:deps (mf/deps file-id page-id)
@ -130,13 +134,19 @@
#(rx/cancel! sub)))})
(let [layout (mf/deref refs/workspace-layout)
flags (mf/deref refs/selected-flags)
file (mf/deref refs/workspace-file)
flags (mf/deref refs/selected-flags)]
page (mf/deref refs/workspace-page)]
;; TODO: maybe loading state?
(when file
[:& workspace-page {:layout layout
:file file
:flags flags
:page-id page-id
:file-id file-id}])))
[:> rdnd/provider {:backend rdnd/html5}
[:& messages-widget]
[:& header {:page page :layout layout :flags flags}]
(when (:colorpalette layout)
[:& colorpalette])
(when (and layout page)
[:& workspace-content {:layout layout
:flags flags
:file file
:page page}])]))