com.7theta/re-frame-via-fx

A re-frame effects handler for via WebSocket based messaging


Keywords
clojure, clojurescript, effects-handler, re-frame, websockets
License
EPL-1.0

Documentation

com.7theta/re-frame-via-fx

Current Version GitHub license Circle CI Dependencies Status

A re-frame Effect Handler that relies on via to provide WebSocket based messaging to the server.

Registering the effects handler

The namespace where event handlers are registered, typically events.cljs, is the generally the place where the effects handler can be registered with re-frame.

In order to register the effects handler:

(ns app.events
  (:require
    ...
    [via-fx.fx :as via-fx]
    ...))

(via-fx/register <via-server-proxy>)

Using the effects handler

Once the effects handler is registered it can be used within an event handler as follows:

(reg-event-fx
  :some-event
  (fn [{:keys [db]} _]
    {:db   (assoc db :show-loading true)
     :via {:message [:api/fetch-items {:filter {:color "red"}}]
           :on-success [:api/fetch-items-succeeded]
           :on-failure [:api/fetch-items-failed]}}))

The :on-success and :on-failure keys can be omitted for one-way messages. Both keys must be provided or omitted, e.g., providing a success handler without providing a failure handler is not supported.

Multiple messages can be sent by passing a sequence of maps to the :via key.

Handlers for :on-success and :on-failure

Normal re-frame handlers are used for :on-success and :on-failure. The event handlers will receive the response as the second argument of the event vector.

(reg-event-db
  :api/fetch-items-succeeded
  (fn [db [_ result]]
    (assoc db :items result)}))

Copyright and License

Copyright © 2015, 2016, 2017 7theta

Distributed under the Eclipse Public License.