Skip to content

Commit

Permalink
Pass the eventbus to on-open callbacks [fixes #53]
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias committed Oct 2, 2013
1 parent 9ced38a commit 2cc94b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## v0.3.0 - unreleased

* BREAKING CHANGE: Pass the eventbus to on-open callbacks in the ClojureScript client [#53](/../../issues/53)

## [v0.2.0](/../../tree/0.2.0) - 2013-09-17

* add `with-vertx` convenience macro to embed ns
Expand Down
17 changes: 9 additions & 8 deletions api/src/main/clojure/vertx/client/eventbus.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,25 @@
[eb]
(= js/vertx.EventBus.CLOSED (ready-state eb)))


(defn- extend-callback [old-f new-f]
(fn []
(and old-f (old-f))
(new-f)))

(defn on-open
"Registers a fn to be called when the eventbus is opened.
If the eventbus is currently open, the fn is called immediately.
If the eventbus is closing or is closed, and error is thrown.
Can be called multiple times to register multiple fns."
[eb f]
The function should take one argument, and will be passed the
eventbus. If the eventbus is currently open, the fn is called
immediately. If the eventbus is closing or is closed, and error is
thrown. Can be called multiple times to register multiple fns."
[eb f]
(if (or (closing? eb)
(closed? eb))
(throw (js/Error. "EventBus is closing or has closed.")))
(if (open? eb)
(f)
(set! (.-onopen eb) (extend-callback (.-onopen eb) f))))
(let [with-eb #(f eb)]
(if (open? eb)
(with-eb)
(set! (.-onopen eb) (extend-callback (.-onopen eb) with-eb)))))

(defn on-close
"Registers a fn to be called when the eventbus is closed.
Expand Down

0 comments on commit 2cc94b9

Please sign in to comment.