some renaming for clarity. add client-side on-opening event

This commit is contained in:
Gered 2016-05-15 21:49:29 -04:00
parent 890ef4596a
commit eff6566ac5

View file

@ -174,21 +174,22 @@
(.disconnect (get-channel)))) (.disconnect (get-channel))))
(defn- connect-channel! (defn- connect-channel!
[{:keys [base] :as options}] [{:keys [on-opening] :as events} {:keys [base] :as options}]
(let [state (channel-state)] (let [state (channel-state)]
(if (or (= state :closed) (when (or (= state :closed)
(= state :init)) (= state :init))
(.connect (get-channel) (.connect (get-channel)
(str base "/test") (str base "/test")
(str base "/bind"))))) (str base "/bind"))
(if on-opening (on-opening)))))
(defn- reconnect! (defn- reconnect!
[handler options] [events handler options]
(set-new-channel!) (set-new-channel!)
(increase-reconnect-attempt-counter!) (increase-reconnect-attempt-counter!)
(.setHandler (get-channel) handler) (.setHandler (get-channel) handler)
(apply-options! options) (apply-options! options)
(connect-channel! options)) (connect-channel! events options))
(defn- raise-context-callbacks! (defn- raise-context-callbacks!
[array callback-k] [array callback-k]
@ -197,8 +198,8 @@
callback (get context callback-k)] callback (get context callback-k)]
(if callback (callback))))) (if callback (callback)))))
(defn- ->handler (defn- ->browserchannel-handler
[{:keys [on-open on-close on-receive on-sent on-error]} options] [{:keys [on-open on-close on-receive on-sent on-error] :as events} options]
(let [handler (goog.net.BrowserChannel.Handler.)] (let [handler (goog.net.BrowserChannel.Handler.)]
(set! (.-channelOpened handler) (set! (.-channelOpened handler)
(fn [_] (fn [_]
@ -216,7 +217,7 @@
(dec (:max-reconnect-attempts options)))) (dec (:max-reconnect-attempts options))))
(clear-reconnect-timer!) (clear-reconnect-timer!)
(js/setTimeout (js/setTimeout
#(reconnect! handler options) #(reconnect! events handler options)
(if (= last-error :unknown-session-id) (if (= last-error :unknown-session-id)
0 0
(:reconnect-time options)))) (:reconnect-time options))))
@ -246,7 +247,7 @@
handler)) handler))
(def default-options (def default-options
"default options that will be applied by init! unless "default options that will be applied by connect! unless
overridden." overridden."
{ {
;; base/root url on which to send browserchannel requests to ;; base/root url on which to send browserchannel requests to
@ -305,28 +306,22 @@
"initializes a browserchannel connection for use, registers your "initializes a browserchannel connection for use, registers your
application event handlers and setting any specified options. application event handlers and setting any specified options.
handler should be a map of event handler functions: events should be a map of event handler functions. you only need
to include handler functions for events you care about.
{:on-open (fn [] ...) {:on-opening (fn [] ...)
:on-open (fn [] ...)
:on-close (fn [due-to-error? pending undelivered] ...) :on-close (fn [due-to-error? pending undelivered] ...)
:on-receive (fn [data] ...) :on-receive (fn [data] ...)
:on-sent (fn [delivered] ...) :on-sent (fn [delivered] ...)
:on-error (fn [error-code] ...) :on-error (fn [error-code] ...)
:on-open is called when a connection is (re-)established. for the supported options, see
:on-close is called when a connection is closed.
:on-receive is called when data is received from the server.
:on-sent is called when data has been successfully sent to
the server ('delivered' is a list of what was sent).
:on-error is only invoked once just before the connection is
closed, and only if there was an error.
for other supported options, see
net.thegeez.browserchannel.client/default-options" net.thegeez.browserchannel.client/default-options"
[handler & [options]] [events & [options]]
(let [options (merge default-options options)] (let [options (merge default-options options)]
(events/listen js/window "unload" disconnect!) (events/listen js/window "unload" disconnect!)
(set-new-channel!) (set-new-channel!)
(.setHandler (get-channel) (->handler handler options)) (.setHandler (get-channel) (->browserchannel-handler events options))
(apply-options! options) (apply-options! options)
(connect-channel! options))) (connect-channel! events options)))