0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-09 08:20:45 -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 ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [wsession (get-in state [:ws file-id])] (let [wsession (get-in state [:ws file-id])
(->> (rx/merge stoper (rx/filter #(= ::finalize-ws %) stream)]
(rx/of (fetch-users file-id)) (->> (ws/-stream wsession)
(->> (ws/-stream wsession) (rx/filter #(= :message (:type %)))
(rx/filter #(= :message (:type %))) (rx/map (comp t/decode :payload))
(rx/map (comp t/decode :payload)) (rx/filter #(s/valid? ::message %))
(rx/filter #(s/valid? ::message %)) (rx/map (fn [{:keys [type] :as msg}]
(rx/map (fn [{:keys [type] :as msg}] (case type
(case type :who (handle-who msg)
:who (handle-who msg) :pointer-update (handle-pointer-update msg)
:pointer-update (handle-pointer-update msg) :page-snapshot (handle-page-snapshot msg)
:page-snapshot (handle-page-snapshot msg) ::unknown)))
::unknown))))) (rx/take-until stoper))))))
(rx/take-until ;; #_(->> stream
(rx/filter #(= ::finalize %) 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 ;; --- Finalize Websocket
(defn finalize-ws (defn finalize-ws
[file-id] [file-id]
(ptk/reify ::finalize (ptk/reify ::finalize-ws
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(ws/-close (get-in state [:ws file-id])) (ws/-close (get-in state [:ws file-id]))
(rx/of ::finalize)))) (rx/of ::finalize-ws))))
;; --- Handle: Who ;; --- Handle: Who
@ -232,67 +243,31 @@
:tooltip nil}) :tooltip nil})
(declare initialized) (declare initialized)
(declare initialize-page)
(defn initialize (defn initialize
"Initialize the workspace state." "Initialize the workspace state."
[file-id] [file-id page-id]
(s/assert ::us/uuid file-id) (s/assert ::us/uuid file-id)
(s/assert ::us/uuid page-id)
(ptk/reify ::initialize (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 ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [wsession (get-in state [:ws file-id])] (let [local (:workspace-local state)]
(rx/merge (if (not= (:file-id local) file-id)
;; Stop possible previous watchers and re-fetch the main page (rx/merge
;; and all project related pages. (rx/of (dp/fetch-file file-id)
(rx/of ::stop-watcher (dp/fetch-pages file-id)
(dp/fetch-file file-id) (fetch-users file-id))
(dp/fetch-pages file-id) (->> (rx/zip (rx/filter (ptk/type? ::dp/pages-fetched) stream)
(fetch-users file-id)) (rx/filter (ptk/type? ::dp/files-fetched) stream))
(rx/take 1)
;; When main page is fetched, schedule the main initialization. (rx/do #(reset! st/loader false))
(->> (rx/zip (rx/filter (ptk/type? ::dp/pages-fetched) stream) (rx/mapcat #(rx/of (initialized file-id)
(rx/filter (ptk/type? ::dp/files-fetched) stream)) (initialize-page page-id)
(rx/take 1) #_(initialize-alignment page-id)))))
(rx/do #(reset! st/loader false)) (rx/merge
(rx/mapcat #(rx/of (initialized file-id) (rx/of (initialize-page page-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))))))))
(defn- initialized (defn- initialized
[file-id] [file-id]
@ -300,18 +275,12 @@
(ptk/reify ::initialized (ptk/reify ::initialized
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [file (get-in state [:files file-id])] (let [file (get-in state [:files file-id])
(assoc state :workspace-file file))))) local (assoc workspace-default :file-id file-id)]
(-> state
(defn finalize (assoc :workspace-file file)
[file-id] (assoc :workspace-layout default-layout)
(ptk/reify ::finalize (assoc :workspace-local local))))))
cljs.core/IDeref
(-deref [_] file-id)
ptk/EffectEvent
(effect [_ state stream]
(ws/-close (get-in state [:ws file-id])))))
(defn initialize-page (defn initialize-page
[page-id] [page-id]
@ -325,7 +294,9 @@
:workspace-page page))) :workspace-page page)))
ptk/EffectEvent ptk/EffectEvent
(effect [_ state stream]))) (effect [_ state stream]
;; TODO: emit join page event
)))
;; --- Fetch Workspace Users ;; --- Fetch Workspace Users

View file

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