cleanup send callback support. add new on-error callback to send-data

This commit is contained in:
Gered 2016-05-15 20:06:00 -04:00
parent 5fd9e7e244
commit 890ef4596a

View file

@ -125,13 +125,15 @@
(.addHandler (or f #(js/console.log %))))) (.addHandler (or f #(js/console.log %)))))
(defn send-data (defn send-data
"sends data to the server over the forward channel. when the data has "sends data to the server over the forward channel. context can
been sent and the server sends an acknowledgement, the optional contain optional callback functions:
on-success callback is invoked."
[data & [{:keys [on-success]}]] on-success - called when the data has been sent to the server
on-error - called if there is an error sending the data to
the server"
[data & [context]]
(if data (if data
(let [m (encode-map data) (let [m (encode-map data)]
context {:on-success on-success}]
(if (get-channel) (if (get-channel)
(.sendMap (get-channel) m context) (.sendMap (get-channel) m context)
(swap! state update-in [:queued-buffer] conj [m context]))))) (swap! state update-in [:queued-buffer] conj [m context])))))
@ -188,6 +190,13 @@
(apply-options! options) (apply-options! options)
(connect-channel! options)) (connect-channel! options))
(defn- raise-context-callbacks!
[array callback-k]
(doseq [m array]
(let [context (aget m "context")
callback (get context callback-k)]
(if callback (callback)))))
(defn- ->handler (defn- ->handler
[{:keys [on-open on-close on-receive on-sent on-error]} options] [{:keys [on-open on-close on-receive on-sent on-error]} options]
(let [handler (goog.net.BrowserChannel.Handler.)] (let [handler (goog.net.BrowserChannel.Handler.)]
@ -195,8 +204,7 @@
(fn [_] (fn [_]
(clear-last-error!) (clear-last-error!)
(reset-reconnect-attempts-counter!) (reset-reconnect-attempts-counter!)
(if on-open (if on-open (on-open))))
(on-open))))
(set! (.-channelClosed handler) (set! (.-channelClosed handler)
(fn [_ pending undelivered] (fn [_ pending undelivered]
(let [last-error (:last-error @state) (let [last-error (:last-error @state)
@ -212,6 +220,9 @@
(if (= last-error :unknown-session-id) (if (= last-error :unknown-session-id)
0 0
(:reconnect-time options)))) (:reconnect-time options))))
(when due-to-error?
(raise-context-callbacks! pending :on-error)
(raise-context-callbacks! undelivered :on-error))
(if on-close (if on-close
(on-close due-to-error? (on-close due-to-error?
(decode-queued-map-array pending) (decode-queued-map-array pending)
@ -225,18 +236,13 @@
(fn [_ delivered] (fn [_ delivered]
(if on-sent (if on-sent
(let [decoded (decode-queued-map-array delivered)] (let [decoded (decode-queued-map-array delivered)]
(if (seq decoded) (if (seq decoded) (on-sent decoded))))
(on-sent decoded)))) (raise-context-callbacks! delivered :on-success)))
(doseq [m delivered]
(let [{:keys [on-success] :as context} (aget m "context")]
(if on-success
(on-success))))))
(set! (.-channelError handler) (set! (.-channelError handler)
(fn [_ error-code] (fn [_ error-code]
(let [error-code (get bch-error-enum-to-keyword error-code :unknown)] (let [error-code (get bch-error-enum-to-keyword error-code :unknown)]
(set-last-error! error-code) (set-last-error! error-code)
(if on-error (if on-error (on-error error-code)))))
(on-error error-code)))))
handler)) handler))
(def default-options (def default-options