From 968ea56197aba2914dcfb848e86d8c76c42662ab Mon Sep 17 00:00:00 2001 From: Miguel de Benito Delgado Date: Thu, 15 May 2025 09:46:49 +0200 Subject: [PATCH] :recycle: Reorganize index management on worker code (#6477) * :recycle: Factor index management out of app.worker.impl * :lipstick: Fix silly spacing * :lipstick: Lint --- frontend/src/app/main/data/changes.cljs | 4 +-- frontend/src/app/main/data/workspace.cljs | 4 +-- frontend/src/app/worker.cljs | 1 + frontend/src/app/worker/impl.cljs | 27 ++---------------- frontend/src/app/worker/index.cljs | 34 +++++++++++++++++++++++ 5 files changed, 41 insertions(+), 29 deletions(-) create mode 100644 frontend/src/app/worker/index.cljs diff --git a/frontend/src/app/main/data/changes.cljs b/frontend/src/app/main/data/changes.cljs index c63832693..077a6418e 100644 --- a/frontend/src/app/main/data/changes.cljs +++ b/frontend/src/app/main/data/changes.cljs @@ -13,7 +13,7 @@ [app.common.types.shape-tree :as ctst] [app.common.uuid :as uuid] [app.main.data.helpers :as dsh] - [app.main.worker :as uw] + [app.main.worker :as mw] [app.util.time :as dt] [beicon.v2.core :as rx] [potok.v2.core :as ptk])) @@ -53,7 +53,7 @@ (->> (rx/from changes) (rx/merge-map (fn [[page-id changes]] (log/debug :hint "update-indexes" :page-id page-id :changes (count changes)) - (uw/ask! {:cmd :update-page-index + (mw/ask! {:cmd :index/update-page-index :page-id page-id :changes changes}))) (rx/catch (fn [cause] diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 48b67fe39..93cebac95 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -85,7 +85,7 @@ [app.main.repo :as rp] [app.main.router :as rt] [app.main.streams :as ms] - [app.main.worker :as uw] + [app.main.worker :as mw] [app.render-wasm :as wasm] [app.render-wasm.api :as api] [app.util.code-gen.style-css :as css] @@ -165,7 +165,7 @@ (rx/mapcat (fn [[id page]] (let [page (update page :objects ctst/start-page-index)] - (->> (uw/ask! {:cmd :initialize-page-index :page page}) + (->> (mw/ask! {:cmd :index/initialize-page-index :page page}) (rx/map (fn [_] [id page])))))) (rx/reduce conj {}) (rx/map (fn [pages-index] diff --git a/frontend/src/app/worker.cljs b/frontend/src/app/worker.cljs index b07cd1fdc..dce2f952d 100644 --- a/frontend/src/app/worker.cljs +++ b/frontend/src/app/worker.cljs @@ -13,6 +13,7 @@ [app.worker.export] [app.worker.impl :as impl] [app.worker.import] + [app.worker.index] [app.worker.messages :as wm] [app.worker.selection] [app.worker.snaps] diff --git a/frontend/src/app/worker/impl.cljs b/frontend/src/app/worker/impl.cljs index 207073f7b..0dae81fa0 100644 --- a/frontend/src/app/worker/impl.cljs +++ b/frontend/src/app/worker/impl.cljs @@ -5,19 +5,15 @@ ;; Copyright (c) KALEIDOS INC (ns app.worker.impl + "Dispatcher for messages received from the main thread." (:require - [app.common.data.macros :as dm] - [app.common.files.changes :as ch] [app.common.logging :as log] - [app.config :as cf] - [okulary.core :as l])) + [app.config :as cf])) (log/set-level! :info) (enable-console-print!) -(defonce state (l/atom {:pages-index {}})) - ;; --- Handler (defmulti handler :cmd) @@ -30,25 +26,6 @@ [message] message) -(defmethod handler :initialize-page-index - [{:keys [page] :as message}] - (swap! state update :pages-index assoc (:id page) page) - (handler (assoc message :cmd :selection/initialize-page-index)) - (handler (assoc message :cmd :snaps/initialize-page-index))) - -(defmethod handler :update-page-index - [{:keys [page-id changes] :as message}] - - (let [old-page (dm/get-in @state [:pages-index page-id]) - new-page (-> state - (swap! ch/process-changes changes false) - (dm/get-in [:pages-index page-id])) - message (assoc message - :old-page old-page - :new-page new-page)] - (handler (assoc message :cmd :selection/update-page-index)) - (handler (assoc message :cmd :snaps/update-page-index)))) - (defmethod handler :configure [{:keys [config]}] (log/info :hint "configure worker" :keys (keys config)) diff --git a/frontend/src/app/worker/index.cljs b/frontend/src/app/worker/index.cljs new file mode 100644 index 000000000..77c2b31f3 --- /dev/null +++ b/frontend/src/app/worker/index.cljs @@ -0,0 +1,34 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS INC + +(ns app.worker.index + "Page index management within the worker." + (:require + [app.common.data.macros :as dm] + [app.common.files.changes :as ch] + [app.worker.impl :as impl] + [okulary.core :as l])) + +(defonce state (l/atom {:pages-index {}})) + +(defmethod impl/handler :index/initialize-page-index + [{:keys [page] :as message}] + (swap! state update :pages-index assoc (:id page) page) + (impl/handler (assoc message :cmd :selection/initialize-page-index)) + (impl/handler (assoc message :cmd :snaps/initialize-page-index))) + +(defmethod impl/handler :index/update-page-index + [{:keys [page-id changes] :as message}] + + (let [old-page (dm/get-in @state [:pages-index page-id]) + new-page (-> state + (swap! ch/process-changes changes false) + (dm/get-in [:pages-index page-id])) + message (assoc message + :old-page old-page + :new-page new-page)] + (impl/handler (assoc message :cmd :selection/update-page-index)) + (impl/handler (assoc message :cmd :snaps/update-page-index))))