From 484702527efbdbc29d412d681e988a3756deca2e Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 16 Apr 2020 13:49:59 +0200 Subject: [PATCH] :tada: Add worker base implementation. --- frontend/resources/templates/index.mustache | 1 + frontend/shadow-cljs.edn | 10 ++- frontend/src/uxbox/main.cljs | 1 + frontend/src/uxbox/main/data/workspace.cljs | 1 - frontend/src/uxbox/main/streams.cljs | 1 - frontend/src/uxbox/main/ui/shapes/common.cljs | 1 - .../src/uxbox/main/ui/workspace/drawarea.cljs | 1 - .../uxbox/main/ui/workspace/selection.cljs | 9 +-- frontend/src/uxbox/main/workers.cljs | 41 ----------- frontend/src/uxbox/util/webapi.cljs | 5 +- frontend/src/uxbox/util/workers.cljs | 7 +- frontend/src/uxbox/worker.cljs | 73 +++++++++++++++---- frontend/src/uxbox/worker/impl.cljs | 14 +--- 13 files changed, 86 insertions(+), 79 deletions(-) delete mode 100644 frontend/src/uxbox/main/workers.cljs diff --git a/frontend/resources/templates/index.mustache b/frontend/resources/templates/index.mustache index 468d6629b6..4ddad27437 100644 --- a/frontend/resources/templates/index.mustache +++ b/frontend/resources/templates/index.mustache @@ -18,6 +18,7 @@ window.uxboxTranslations = JSON.parse({{& translations }}); window.uxboxThemes = {{& themes }}; + diff --git a/frontend/shadow-cljs.edn b/frontend/shadow-cljs.edn index 57a73dba92..4c6ca282e1 100644 --- a/frontend/shadow-cljs.edn +++ b/frontend/shadow-cljs.edn @@ -8,7 +8,15 @@ {:target :browser :output-dir "resources/public/js/" :asset-path "/js" - :modules {:main {:entries [uxbox.main]}} + :devtools {:browser-inject :main} + + :modules + {:shared {:entries []} + :main {:entries [uxbox.main] + :depends-on #{:shared}} + :worker {:entries [uxbox.worker] + :web-worker true + :depends-on #{:shared}}} :compiler-options {:output-feature-set :es8 :output-wrapper false} diff --git a/frontend/src/uxbox/main.cljs b/frontend/src/uxbox/main.cljs index 0cbf73715a..75f4f74136 100644 --- a/frontend/src/uxbox/main.cljs +++ b/frontend/src/uxbox/main.cljs @@ -19,6 +19,7 @@ [uxbox.main.ui :as ui] [uxbox.main.ui.modal :refer [modal]] [uxbox.main.ui.loader :refer [loader]] + [uxbox.main.worker] [uxbox.util.dom :as dom] [uxbox.util.html.history :as html-history] [uxbox.util.i18n :as i18n] diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index 6ada617f2d..54dcfdc4a9 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -31,7 +31,6 @@ [uxbox.main.store :as st] [uxbox.main.streams :as ms] [uxbox.main.websockets :as ws] - [uxbox.main.workers :as uwrk] [uxbox.util.geom.matrix :as gmt] [uxbox.util.geom.point :as gpt] [uxbox.util.math :as mth] diff --git a/frontend/src/uxbox/main/streams.cljs b/frontend/src/uxbox/main/streams.cljs index 27e5cbe95d..a8bb6d41c3 100644 --- a/frontend/src/uxbox/main/streams.cljs +++ b/frontend/src/uxbox/main/streams.cljs @@ -10,7 +10,6 @@ [beicon.core :as rx] [uxbox.main.store :as st] [uxbox.main.refs :as refs] - [uxbox.main.workers :as uwrk] [uxbox.util.geom.point :as gpt])) ;; --- User Events diff --git a/frontend/src/uxbox/main/ui/shapes/common.cljs b/frontend/src/uxbox/main/ui/shapes/common.cljs index b4a4965d23..409bd76020 100644 --- a/frontend/src/uxbox/main/ui/shapes/common.cljs +++ b/frontend/src/uxbox/main/ui/shapes/common.cljs @@ -20,7 +20,6 @@ [uxbox.main.store :as st] [uxbox.main.ui.keyboard :as kbd] [uxbox.main.streams :as uws] - [uxbox.main.workers :as uwrk] [uxbox.main.geom :as geom] [uxbox.util.geom.matrix :as gmt] [uxbox.util.geom.point :as gpt] diff --git a/frontend/src/uxbox/main/ui/workspace/drawarea.cljs b/frontend/src/uxbox/main/ui/workspace/drawarea.cljs index 300c39ed71..d7f93779f7 100644 --- a/frontend/src/uxbox/main/ui/workspace/drawarea.cljs +++ b/frontend/src/uxbox/main/ui/workspace/drawarea.cljs @@ -17,7 +17,6 @@ [uxbox.main.store :as st] [uxbox.main.streams :as ms] [uxbox.main.ui.shapes :as shapes] - [uxbox.main.workers :as uwrk] [uxbox.util.math :as mth] [uxbox.util.dom :as dom] [uxbox.util.data :refer [seek]] diff --git a/frontend/src/uxbox/main/ui/workspace/selection.cljs b/frontend/src/uxbox/main/ui/workspace/selection.cljs index b4db826c39..c1a6fff067 100644 --- a/frontend/src/uxbox/main/ui/workspace/selection.cljs +++ b/frontend/src/uxbox/main/ui/workspace/selection.cljs @@ -18,7 +18,6 @@ [uxbox.main.refs :as refs] [uxbox.main.store :as st] [uxbox.main.streams :as ms] - [uxbox.main.workers :as uwrk] [uxbox.util.dom :as dom] [uxbox.util.geom.point :as gpt] [uxbox.util.geom.matrix :as gmt])) @@ -96,8 +95,8 @@ (rx/map (fn [[pos ctrl?]] (let [delta-angle (calculate-angle pos ctrl?)] (dw/apply-rotation delta-angle shapes)))) - - + + (rx/take-until stoper)) (rx/of (dw/materialize-rotation shapes)) ))))) @@ -151,7 +150,7 @@ (let [{:keys [x y width height rotation] :as shape} (geom/shape->rect-shape shape) radius (if (> (max width height) handler-size-threshold) 6.0 4.0) transform (geom/rotation-matrix shape) - + resize-handlers {:top [(+ x (/ width 2 )) (- y 2)] :right [(+ x width 1) (+ y (/ height 2))] :bottom [(+ x (/ width 2)) (+ y height 2)] @@ -160,7 +159,7 @@ :top-right [(+ x width) y] :bottom-left [x (+ y height)] :bottom-right [(+ x width) (+ y height)]}] - + [:g.controls {:transform transform} [:rect.main {:x x :y y :width width diff --git a/frontend/src/uxbox/main/workers.cljs b/frontend/src/uxbox/main/workers.cljs deleted file mode 100644 index 9b7d3ce2ed..0000000000 --- a/frontend/src/uxbox/main/workers.cljs +++ /dev/null @@ -1,41 +0,0 @@ -;; 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) 2015-2017 Andrey Antukh - -(ns uxbox.main.workers - "A interface to webworkers exposed functionality." - (:require [cljs.spec.alpha :as s] - [beicon.core :as rx] - [potok.core :as ptk] - [uxbox.common.spec :as us] - [uxbox.util.workers :as uw])) - -;; (s/def ::width number?) -;; (s/def ::height number?) -;; (s/def ::x-axis number?) -;; (s/def ::y-axis number?) - -;; (s/def ::initialize-alignment-params -;; (s/keys :req-un [::width -;; ::height -;; ::x-axis -;; ::y-axis])) - -;; ;; This excludes webworker instantiation on nodejs where -;; ;; the tests are run. -;; (when (not= *target* "nodejs") -;; (defonce worker (uw/init "js/worker.js"))) - -;; (defn align-point -;; [point] -;; (let [message {:cmd :grid-align :point point}] -;; (->> (uw/ask! worker message) -;; (rx/map :point)))) - -;; (defn initialize-alignment -;; [params] -;; (us/verify ::initialize-alignment-params params) -;; (let [message (assoc params :cmd :grid-init)] -;; (uw/send! worker message))) diff --git a/frontend/src/uxbox/util/webapi.cljs b/frontend/src/uxbox/util/webapi.cljs index 841ac49bdb..825a68e79d 100644 --- a/frontend/src/uxbox/util/webapi.cljs +++ b/frontend/src/uxbox/util/webapi.cljs @@ -2,7 +2,10 @@ ;; 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) 2020 Andrey Antukh +;; This Source Code Form is "Incompatible With Secondary Licenses", as +;; defined by the Mozilla Public License, v. 2.0. +;; +;; Copyright (c) 2020 UXBOX Labs SL (ns uxbox.util.webapi "HTML5 web api helpers." diff --git a/frontend/src/uxbox/util/workers.cljs b/frontend/src/uxbox/util/workers.cljs index 4391d9b9e0..22815d743b 100644 --- a/frontend/src/uxbox/util/workers.cljs +++ b/frontend/src/uxbox/util/workers.cljs @@ -2,10 +2,13 @@ ;; 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) 2016 Andrey Antukh +;; This Source Code Form is "Incompatible With Secondary Licenses", as +;; defined by the Mozilla Public License, v. 2.0. +;; +;; Copyright (c) 2020 UXBOX Labs SL (ns uxbox.util.workers - "A lightweight layer on top of webworkers api." + "A lightweight layer on top of Web Workers API." (:require [beicon.core :as rx] [uxbox.common.uuid :as uuid] [uxbox.util.transit :as t])) diff --git a/frontend/src/uxbox/worker.cljs b/frontend/src/uxbox/worker.cljs index 3efca58e2a..d2fbee3ca7 100644 --- a/frontend/src/uxbox/worker.cljs +++ b/frontend/src/uxbox/worker.cljs @@ -2,27 +2,72 @@ ;; 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) 2016 Andrey Antukh +;; This Source Code Form is "Incompatible With Secondary Licenses", as +;; defined by the Mozilla Public License, v. 2.0. +;; +;; Copyright (c) 2020 UXBOX Labs SL (ns uxbox.worker - (:require [beicon.core :as rx] - [cuerdas.core :as str] - [uxbox.util.transit :as t] - [uxbox.common.uuid :as uuid] - [uxbox.worker.impl :as impl] - [uxbox.worker.align])) + (:require + [cljs.spec.alpha :as s] + [promesa.core :as p] + [beicon.core :as rx] + [cuerdas.core :as str] + [uxbox.common.exceptions :as ex] + [uxbox.common.spec :as us] + [uxbox.common.uuid :as uuid] + [uxbox.worker.impl :as impl] + [uxbox.util.transit :as t] + [uxbox.util.worker :as w])) -(enable-console-print!) +(s/def ::cmd keyword?) +(s/def ::payload + (s/keys :req-un [::cmd])) -(defonce id (uuid/next)) +(s/def ::sender-id uuid?) +(s/def ::message + (s/keys :req-un [::payload ::sender-id])) + +(defn- handle-message + [{:keys [sender-id payload] :as message}] + (us/assert ::message message) + (try + (let [result (impl/handler payload)] + (cond + (p/thenable? result) + (p/handle result + (fn [msg] + (.postMessage js/self (t/encode + {:reply-to sender-id + :payload msg}))) + (fn [err] + (.postMessage js/self (t/encode + {:reply-to sender-id + :error {:data (ex-data err) + :message (ex-message err)}})))) + + (or (rx/observable? result) + (rx/subject? result)) + (throw (ex-info "not implemented" {})) + + :else + (.postMessage js/self (t/encode + {:reply-to sender-id + :payload result})))) + (catch :default e + (let [message {:reply-to sender-id + :error {:data (ex-data e) + :message (ex-message e)}}] + (.postMessage js/self (t/encode message)))))) (defn- on-message [event] (when (nil? (.-source event)) - (let [message (t/decode (.-data event))] - (impl/handler message)))) + (let [message (.-data event) + message (t/decode message)] + (handle-message message)))) -(defonce _ - (.addEventListener js/self "message" on-message)) +(.addEventListener js/self "message" on-message) -(println (str/format "Worker with id '%s' is initialized." id)) +(defn ^:dev/before-load stop [] + (.removeEventListener js/self "message" on-message)) diff --git a/frontend/src/uxbox/worker/impl.cljs b/frontend/src/uxbox/worker/impl.cljs index 1e21ea6f82..8b44e74c65 100644 --- a/frontend/src/uxbox/worker/impl.cljs +++ b/frontend/src/uxbox/worker/impl.cljs @@ -17,14 +17,6 @@ [message] (println "Unexpected message:" message)) -;; --- Helpers - -(defn worker? - "Check if the code is executed in webworker context." - [] - (undefined? (.-document js/self))) - -(defn reply! - [sender message] - (let [message (assoc message :reply-to sender)] - (.postMessage js/self (t/encode message)))) +(defmethod handler :echo + [message] + message)