-
Notifications
You must be signed in to change notification settings - Fork 38
/
core_async.clj
41 lines (33 loc) · 1.38 KB
/
core_async.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(ns chime.core-async
(:require [chime.core :as chime]
[clojure.core.async :as a :refer [<! >! go-loop]]
[clojure.core.async.impl.protocols :as p]))
(defn chime-ch
"Returns a core.async channel that 'chimes' at every time in the times list.
Arguments:
times - (required) Sequence of java.util.Dates, java.time.Instant,
java.time.ZonedDateTime or msecs since epoch
ch - (optional) Channel to chime on - defaults to a new unbuffered channel
Closing this channel stops the schedule.
Usage:
(let [chimes (chime-ch [(.plusSeconds (Instant/now) -2)
(.plusSeconds (Instant/now) 2)
(.plusSeconds (Instant/now) 2)])]
(a/<!! (go-loop []
(when-let [msg (<! chimes)]
(prn \"Chiming at:\" msg)
(recur)))))
There are extensive usage examples in the README"
[times & [{:keys [ch], :or {ch (a/chan)}}]]
(let [sched (chime/chime-at times
(fn [time]
(a/>!! ch time))
{:on-finished (fn []
(a/close! ch))})]
(reify
p/ReadPort
(take! [_ handler]
(p/take! ch handler))
p/Channel
(close! [_] (.close sched))
(closed? [_] (p/closed? ch)))))