Skip to content

A re-frame "effects handler" for performing async tasks via cljs-workers

License

Notifications You must be signed in to change notification settings

jtkDvlp/re-frame-worker-fx

Repository files navigation

Clojars Project License: MIT paypal

Web workers effect handler for re-frame

This re-frame library contains an web worker effect handler. The handler can be addressed by :worker and wraps the API of cljs-workers.

Getting started

Get it / add dependency

Add the following dependency to your project.cljs:
Clojars Project

Usage

See the following minimal code example or the test.cljs. For general usage of workers see cljs-workers.

The following example presupposes that there is already a worker pool and registered worker-handler.

(ns your.project
  (:require [re-frame.core :as re-frame]
            [re-frame-worker-fx.core]))

(re-frame/reg-event-fx
 :some-event
 (fn [coeffects _]
   (let [worker-pool
         (-> coeffects :db :worker-pool)]
     {:worker {:pool worker-pool
               ;; handler that will be called
               :handler :your-worker-handler
               ;; arguments applied to the handler
               :arguments {:a "Hallo Welt!" :b 10 :c (js/ArrayBuffer. 10)}
               ;; which arguments will be transfered instead of copied
               :transfer [:c]
               ;; dispatched on success conjoined with the result
               :on-success [:your-success-event]
               ;; dispatched on error conjoined with the result
               :on-error [:your-error-event]}})))

Appendix

I´d be thankful to receive patches, comments and constructive criticism.

Hope the package is useful :-)

About

A re-frame "effects handler" for performing async tasks via cljs-workers

Topics

Resources

License

Stars

Watchers

Forks