Skip to content

Commit 5033860

Browse files
committed
Test and bugfixes
1 parent 94e811d commit 5033860

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

project.clj

+33-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,36 @@
33
:url "https://github.com/jtkDvlp/cljs-workers"
44
:license {:name "MIT"}
55
:dependencies [[org.clojure/clojure "1.8.0"]
6-
[org.clojure/clojurescript "1.9.229"]])
6+
[org.clojure/clojurescript "1.9.229"]]
7+
8+
:min-lein-version "2.5.3"
9+
10+
:source-paths ["src"]
11+
:test-paths ["test"]
12+
13+
:clean-targets ^{:protect false} ["resources/public/js" "target"]
14+
15+
:plugins [[lein-cljsbuild "1.1.4" :exclusions [[org.clojure/clojure]]]
16+
[lein-figwheel "0.5.0-1"]]
17+
18+
:profiles {:dev {:dependencies [[figwheel-sidecar "0.5.8"]
19+
[com.cemerick/piggieback "0.2.1"]]}}
20+
21+
:repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}
22+
23+
:cljsbuild
24+
{:builds
25+
[{:id "test"
26+
:source-paths ["src" "test"]
27+
:figwheel true
28+
:compiler {:main cljs-workers.test
29+
:asset-path "js/test/out"
30+
:output-to "resources/public/js/test/test.js"
31+
:output-dir "resources/public/js/test/out" }}
32+
{:id "worker"
33+
:source-paths ["src" "test"]
34+
:compiler {:main cljs-workers.test
35+
:asset-path "js/worker/out"
36+
:output-to "resources/public/js/worker/worker.js"
37+
:output-dir "resources/public/js/worker/out"
38+
:optimizations :simple}}]})

resources/public/test.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script src="js/test/test.js"></script>

src/cljs_workers/core.cljs

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44

55
(defn supported?
66
[]
7-
(-> js/Worker
7+
(-> js/self
8+
.-Worker
89
undefined?
910
not))
1011

12+
(defn worker?
13+
[]
14+
(-> js/self
15+
.-document
16+
undefined?))
17+
18+
(def main?
19+
(complement worker?))
20+
1121
(defn create-one
1222
[script]
1323
(js/Worker. script))

src/cljs_workers/worker.cljs

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
(defn- handle-request!
4040
[event]
41+
4142
(let [data
4243
(.-data event)
4344

@@ -49,4 +50,6 @@
4950

5051
(do-respond! handler arguments)))
5152

52-
(aset js/self "onmessage" handle-request!)
53+
(defn bootstrap
54+
[]
55+
(aset js/self "onmessage" handle-request!))

test/cljs_workers/test.cljs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(ns cljs-workers.test
2+
(:require [cljs-workers.core :as main]
3+
[cljs-workers.worker :as worker]))
4+
5+
(defn app
6+
[]
7+
(let [worker-pool (main/create-pool 2 "js/worker/worker.js")]
8+
(main/do-with-pool! worker-pool {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10}} #(.debug js/console %))
9+
(main/do-with-pool! worker-pool {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10 :d (js/ArrayBuffer. 10) :transfer [:d]} :transfer [:d]} #(.debug js/console %))
10+
(main/do-with-pool! worker-pool {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10 :d (js/ArrayBuffer. 10) :transfer [:d]} :transfer [:c]} #(.debug js/console %))
11+
(main/do-with-pool! worker-pool {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10 :d (js/ArrayBuffer. 10) :transfer [:c]} :transfer [:d]} #(.debug js/console %))))
12+
13+
(defn worker
14+
[]
15+
(worker/register
16+
:mirror
17+
(fn [arguments]
18+
arguments))
19+
20+
(worker/bootstrap))
21+
22+
(if (and (main/supported?) (main/main?))
23+
(app)
24+
(worker))

0 commit comments

Comments
 (0)