Created
September 18, 2017 17:09
-
-
Save mike706574/dcbd6610aea2f5ecd1c0f7d0e2cc8938 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn websocket-effect | |
[{:keys [uri on-message on-error on-success on-failure websocket]}] | |
(if websocket | |
(rf/dispatch (conj on-success websocket)) | |
(let [websocket (js/WebSocket. uri)] | |
(log/info "Starting new websocket.") | |
(set! (.-onmessage websocket) #(rf/dispatch (conj on-message %))) | |
(set! (.-onerror websocket) #(rf/dispatch (conj on-failure %))) | |
(set! (.-onopen websocket) (fn on-open [] | |
(set! (.-onerror websocket) #(rf/dispatch (conj on-error %))) | |
(rf/dispatch (conj on-success websocket))))))) | |
(defn websocket-message-effect | |
[{:keys [websocket message]}] | |
(if websocket | |
(.send websocket message) | |
(throw (js/Error. "No websocket provided.")))) | |
(rf/reg-fx :websocket websocket-effect) | |
(rf/reg-fx :websocket-message websocket-effect) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment