(defn
timeout
"returns a channel that will close after msecs"
[msecs]
(let
[timeout
(+ (.valueOf (js/Date.)) msecs)
me
(.ceilingEntry timeouts-map timeout)]
(or
(when
(and me (< (.-key me) (+ timeout TIMEOUT_RESOLUTION_MS)))
(.-val me))
(let
[timeout-channel (channels/chan nil)]
(.put timeouts-map timeout timeout-channel)
(dispatch/queue-delay
(fn
[]
(.remove timeouts-map timeout)
(impl/close! timeout-channel))
msecs)
timeout-channel))))