Skip to content

Instantly share code, notes, and snippets.

@mattmoss
Created June 28, 2012 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmoss/3013845 to your computer and use it in GitHub Desktop.
Save mattmoss/3013845 to your computer and use it in GitHub Desktop.
How to handle Java Listeners (anon interfaces) of multiple functions from off the main thread...
(defn- some-listener
[event-1 event-2 event-3]
(reify SomeListener
(onEvent1 [this x] (send event-1 (constantly x)))
(onEvent2 [this x] (send event-2 (constantly x)))
(onEvent3 [this x y] (send event-3 (constantly [x y])))))
(defn make-thing
[]
(let [event-1 (agent nil)
event-2 (agent nil)
event-3 (agent nil)
instance (doto (Thing.)
(.addSomeListener (some-listener event-1 event-2 event-3)))]
{:event-1 event-1
:event-2 event-2
:event-3 event-3
:instance instance}))
(defn watch
[subj what key f]
(add-watch (what subj) key f)
(defn ignore
[subj what key]
(remove-watch (what subj) key)
; sample use
(def thing (make-thing))
(watch thing :event-1 :hullo (fn [k r o n] (clojure.pprint/pprint n)))
@mattmoss
Copy link
Author

This is just silly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment