├── resources
└── public
│ └── test.html
├── .gitignore
├── src
└── re_frame_worker_fx
│ └── core.cljs
├── LICENSE
├── project.clj
├── test
└── re_frame_worker_fx
│ └── test.cljs
└── README.md
/resources/public/test.html:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | /classes
3 | /checkouts
4 | pom.xml
5 | pom.xml.asc
6 | *.jar
7 | *.class
8 | /.lein-*
9 | /.nrepl-port
10 | .hgignore
11 | .hg/
12 |
--------------------------------------------------------------------------------
/src/re_frame_worker_fx/core.cljs:
--------------------------------------------------------------------------------
1 | (ns re-frame-worker-fx.core
2 | (:require [cljs.core.async :refer [ coeffects :db :worker-pool)
24 |
25 | task-with-pool
26 | (assoc task :pool worker-pool)]
27 |
28 | {:worker task-with-pool})))
29 |
30 | (re-frame/reg-event-fx
31 | :initialize
32 | (fn [_ _]
33 | {:db {:worker-pool (main/create-pool 2 "js/worker/worker.js")}
34 | :dispatch-n [[:test-worker-fx {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10} :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]
35 | [:test-worker-fx {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10 :d (js/ArrayBuffer. 10) :transfer [:d]} :transfer [:d] :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]
36 | [:test-worker-fx {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10 :d (js/ArrayBuffer. 10) :transfer [:d]} :transfer [:c] :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]
37 | [:test-worker-fx {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10 :d (js/ArrayBuffer. 10) :transfer [:c]} :transfer [:d] :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]]}))
38 |
39 | (re-frame/dispatch-sync [:initialize]))
40 |
41 | (defn worker
42 | []
43 | (worker/register
44 | :mirror
45 | (fn [arguments]
46 | arguments))
47 |
48 | (worker/bootstrap))
49 |
50 | (if (and (main/supported?) (main/main?))
51 | (app)
52 | (worker))
53 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://clojars.org/jtk-dvlp/re-frame-worker-fx)
2 | [](https://github.com/jtkDvlp/re-frame-worker-fx/blob/master/LICENSE)
3 | [](https://www.paypal.com/donate?hosted_button_id=2PDXQMHX56T6U)
4 |
5 | # Web workers effect handler for re-frame
6 |
7 | This [re-frame](https://github.com/Day8/re-frame) library contains an [web worker](https://developer.mozilla.org/docs/Web/API/Web_Workers_API/Using_web_workers) [effect handler](https://github.com/Day8/re-frame/tree/develop/docs). The handler can be addressed by `:worker` and wraps the API of [cljs-workers](https://github.com/jtkDvlp/cljs-workers).
8 |
9 | ## Getting started
10 |
11 | ### Get it / add dependency
12 |
13 | Add the following dependency to your `project.cljs`:
14 | [](https://clojars.org/jtk-dvlp/re-frame-worker-fx)
15 |
16 | ### Usage
17 |
18 | See the following minimal code example or the [test.cljs](https://github.com/jtkDvlp/re-frame-worker-fx/blob/master/test/re_frame_worker_fx/test.cljs). For general usage of workers see [cljs-workers](https://github.com/jtkDvlp/cljs-workers).
19 |
20 | The following example presupposes that there is already a worker pool and registered worker-handler.
21 |
22 | ```clojure
23 | (ns your.project
24 | (:require [re-frame.core :as re-frame]
25 | [re-frame-worker-fx.core]))
26 |
27 | (re-frame/reg-event-fx
28 | :some-event
29 | (fn [coeffects _]
30 | (let [worker-pool
31 | (-> coeffects :db :worker-pool)]
32 | {:worker {:pool worker-pool
33 | ;; handler that will be called
34 | :handler :your-worker-handler
35 | ;; arguments applied to the handler
36 | :arguments {:a "Hallo Welt!" :b 10 :c (js/ArrayBuffer. 10)}
37 | ;; which arguments will be transfered instead of copied
38 | :transfer [:c]
39 | ;; dispatched on success conjoined with the result
40 | :on-success [:your-success-event]
41 | ;; dispatched on error conjoined with the result
42 | :on-error [:your-error-event]}})))
43 | ```
44 |
45 | ## Appendix
46 |
47 | I´d be thankful to receive patches, comments and constructive criticism.
48 |
49 | Hope the package is useful :-)
50 |
--------------------------------------------------------------------------------