Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns timer
- (:import [java.util Timer TimerTask]))
- (def the-timer (Timer.))
- (defn timer
- "Schedules a callback for delayed execution.
- Returns a function, that accepts a parameter:
- :bump to restart the timer, returning true only if timer was active, hence was extended by that call
- :cancel to abort the timer, returning true only if timer was active, hence was canceled b.t.c."
- [delay callback]
- (let [current (atom 0) ; bump counter, nil is special for a disabled timer
- create-task (fn [rev]
- (proxy [TimerTask] []
- ; only run, when this handler is the latest
- (run [] (when (compare-and-set! current rev nil)
- (callback)))))
- ctl (fn [action]
- (condp = action
- :bump (when-let [in-current @current]
- (when (compare-and-set! current in-current (inc in-current))
- (.schedule the-timer
- (create-task (inc in-current))
- (long delay))
- true))
- :cancel (loop [] (when-let [in-cur @current] ; :cancel
- (if (compare-and-set! current in-cur nil)
- true (recur))))))]
- (ctl :bump)
- ctl))
Add Comment
Please, Sign In to add comment