diff --git a/scripts/figwheel.clj b/scripts/figwheel.clj index 599c6fa73..2ed5f7fd7 100644 --- a/scripts/figwheel.clj +++ b/scripts/figwheel.clj @@ -7,7 +7,7 @@ (ra/start-figwheel! {:figwheel-options {:css-dirs ["resources/public/css"]} - :build-ids ["dev"] + :build-ids ["dev" "worker"] :all-builds [{:id "dev" :figwheel {:on-jsload "uxbox.ui/init"} @@ -20,10 +20,24 @@ "https://test.uxbox.io/api"} :warnings {:ns-var-clash false} :pretty-print true - :language-in :ecmascript5 + :language-in :ecmascript6 :language-out :ecmascript5 :output-to "resources/public/js/main.js" :output-dir "resources/public/js" + :verbose true}} + + {:id "worker" + :source-paths ["src" "vendor"] + :compiler {:main 'uxbox.worker + :asset-path "js" + :parallel-build false + :optimizations :simple + :warnings {:ns-var-clash false} + :pretty-print true + :static-fns true + :language-in :ecmascript6 + :language-out :ecmascript5 + :output-to "resources/public/js/worker.js" :verbose true}}]}) -(ra/cljs-repl) +(ra/cljs-repl "dev") diff --git a/src/uxbox/worker.cljs b/src/uxbox/worker.cljs new file mode 100644 index 000000000..4db0d390f --- /dev/null +++ b/src/uxbox/worker.cljs @@ -0,0 +1,21 @@ +;; 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) 2016 Andrey Antukh + +(ns uxbox.worker + (:require [beicon.core :as rx] + [uxbox.util.transit :as t] + [uxbox.worker.core :as wrk] + [uxbox.worker.align])) + +(enable-console-print!) + +(defn- on-message + [event] + (let [message (t/decode (.-data event))] + (wrk/handler message))) + +(defonce _ + (.addEventListener js/self "message" on-message)) diff --git a/src/uxbox/worker/core.cljs b/src/uxbox/worker/core.cljs new file mode 100644 index 000000000..43988fb93 --- /dev/null +++ b/src/uxbox/worker/core.cljs @@ -0,0 +1,31 @@ +;; 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) 2016 Andrey Antukh + +(ns uxbox.worker.core + (:require [uxbox.util.transit :as t])) + +(enable-console-print!) + +;; --- Handler + +(defmulti handler :cmd) + +(defmethod handler :default + [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)] + (println "replying " message) + (.postMessage js/self (t/encode message))))