Public Vars

Back

chime-ch (clj)

(source)

function

(chime-ch times & [{:keys [ch], :or {ch (a/chan)}}])
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

Examples

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))))