0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-13 08:11:30 -05:00

🎉 Add worker base implementation.

This commit is contained in:
Andrey Antukh 2020-04-16 13:49:59 +02:00 committed by Alonso Torres
parent 517b50238e
commit 484702527e
13 changed files with 86 additions and 79 deletions

View file

@ -18,6 +18,7 @@
window.uxboxTranslations = JSON.parse({{& translations }});
window.uxboxThemes = {{& themes }};
</script>
<script src="/js/shared.js?ts={{& ts}}"></script>
<script src="/js/main.js?ts={{& ts}}"></script>
<script>uxbox.main.init()</script>
</body>

View file

@ -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}

View file

@ -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]

View file

@ -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]

View file

@ -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

View file

@ -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]

View file

@ -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]]

View file

@ -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

View file

@ -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 <niwi@niwi.nz>
(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)))

View file

@ -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 <niwi@niwi.nz>
;; 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."

View file

@ -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 <niwi@niwi.nz>
;; 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]))

View file

@ -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 <niwi@niwi.nz>
;; 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))

View file

@ -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)