Back
chime-ch (clj)
(source)function
(chime-ch times & [{:keys [ch], :or {ch (a/chan)}}])
Deprecated: use `chime.core-async/chime-ch` - see the source of this fn for a migration.
Returns a core.async channel that 'chimes' at every time in the
times list. Times that have already passed are ignored.
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
Examples
chime
(ns chime-test
(:require
[chime :refer :all]
[clojure.core.async :as a :refer [<! go-loop]]
[clojure.test :refer :all]
[chime.core :as chime]
[chime.joda-time]
[clj-time.core :as t]
[clj-time.periodic])
(:import (java.time Instant)
(java.time.temporal ChronoUnit)))
(deftest test-chime-ch
(let [will-be-omitted (.minusSeconds (now) 2)
t1 (.plusSeconds (now) 2)
t2 (.plusSeconds (now) 3)
chimes (chime-ch [will-be-omitted
t1
t2])
proof (atom [])]
(a/<!! (go-loop []
(when-let [msg (<! chimes)]
(swap! proof conj [msg (chime-test/now)])
(recur))))
(is (= [t1 t2]
(mapv first @proof)))
(check-timeliness! proof)))
(deftest test-channel-closing
(let [omitted (.minusSeconds (now) 2)
expected (.plusSeconds (now) 2)
dropped (.plusSeconds (now) 3)
chimes (chime-ch [omitted expected dropped])
proof (atom [])]
(a/<!!
(a/go
(swap! proof conj (<! chimes))
(a/close! chimes)
(when-let [v (<! chimes)]
(swap! proof conj v))))
(is (= [expected]
@proof))))
(let [proof (atom [])
ch (chime-ch (->> (chime/periodic-seq (-> (.plusSeconds (chime-test/now) 2)
(.truncatedTo (ChronoUnit/SECONDS)))
(java.time.Duration/ofSeconds 1))
(take 3)))]
chime
(ns chime.core-async-test
(:require [chime.core-async :as sut]
[chime.core-test :refer [check-timeliness!]]
[clojure.test :as t]
[clojure.core.async :as a :refer [go-loop]])
(:import [java.time Instant]))
(t/deftest test-chime-ch
(let [now (Instant/now)
times [(.minusSeconds now 2)
(.plusSeconds now 1)
(.plusSeconds now 2)]
chimes (sut/chime-ch times)
proof (atom [])]
(t/deftest test-channel-closing
(let [now (Instant/now)
times [(.minusSeconds now 2)
(.plusSeconds now 1)
(.plusSeconds now 2)]
chimes (sut/chime-ch times)
proof (atom [])]
(a/<!! (a/go
(swap! proof conj (a/<! chimes))
(swap! proof conj (a/<! chimes))
(a/close! chimes)
(when-let [v (a/<! chimes)]
(swap! proof conj v))))
(t/is (= (butlast times) @proof))))
jarohen/chime
(ns chime.core-async-test
(:require [chime.core-async :as sut]
[chime.core-test :refer [check-timeliness!]]
[clojure.test :as t]
[clojure.core.async :as a :refer [go-loop]])
(:import [java.time Instant]))
(t/deftest test-chime-ch
(let [now (Instant/now)
times [(.minusSeconds now 2)
(.plusSeconds now 1)
(.plusSeconds now 2)]
chimes (sut/chime-ch times)
proof (atom [])]
(t/deftest test-channel-closing
(let [now (Instant/now)
times [(.minusSeconds now 2)
(.plusSeconds now 1)
(.plusSeconds now 2)]
chimes (sut/chime-ch times)
proof (atom [])]
(a/<!! (a/go
(swap! proof conj (a/<! chimes))
(swap! proof conj (a/<! chimes))
(a/close! chimes)
(when-let [v (a/<! chimes)]
(swap! proof conj v))))
(t/is (= (butlast times) @proof))))