Why is timeout in CLJS not just,
(defn timeout
[ms]
(let [ch (chan)]
(js/setTimeout #(close! ch) ms)
ch))
I see that there’s a lot of stuff happening in the implementation, so I assume the above is naive, but I don’t understand why.The reason I’m asking is because I’m running into weird behaviour with timeout, where closing one timeout seems to close another, unrelated one (no pipes etc. involved, or other things that might propagate), and the naive version seems to work as expected.
don't close timeouts
They are designed to be closed by The System™
I’m not closing them, The System is.
But I’m also curious why they require so much infrastructure.
at least on the JVM side, timeouts are quantized at a resolution of 10ms
If you were going to change how timeouts are implemented in cljs I wouldn't go with exactly that, it perpetuates a big issue with the current design of timeouts, a strong reference to the timeout channel is kept by the global system that closes the timeout channels
https://clojure.atlassian.net/browse/ASYNC-109 I forgot about this, which has a cljs patch as well
It changes timeouts to return a custom ReadPort instead of a channel, so you get an error if close! Is called on them