core-async

souenzzo 2020-06-05T02:56:02.441700Z

There is a implementation of "broadcast" in core.async? I mean, when I put into a channel, all listeners will receive the message

(let [pub-port (async/chan)
      pub (async/pub pub-port (constantly ::all))
      sub-port (async/chan)
      sub (async/sub pub ::all sub-port)]
  (async/go
    (prn [1 (async/<! sub)]))
  (async/go
    (prn [2 (async/<! sub)]))
  (async/put! pub-port "hello"))
On this example, just 1 or 2 will be printed. I want both.

2020-06-05T02:59:52.441900Z

Mults

2020-06-05T03:00:37.442800Z

You can have a pub and a mult and tap a channel to both

souenzzo 2020-06-05T03:07:48.443700Z

There is no examples of use of mult @hiredman I tryied in this way

(let [pub-port (async/chan)
      mult (async/mult pub-port)
      sub-port (async/chan)]
  (async/tap mult sub-port)
  (async/go
    (prn [1 (async/<! sub-port)]))
  (async/go
    (prn [2 (async/<! sub-port)]))
  (async/put! pub-port "ok"))
With the same result of pub/sub

souenzzo 2020-06-05T03:10:05.444100Z

Looking for this: https://github.com/clojure/core.async/blob/master/src/test/cljs/cljs/core/async/tests.cljs#L263 My solution looks right.

souenzzo 2020-06-05T03:18:16.444600Z

I think I get it. I need to create a new channel and tap it.

souenzzo 2020-06-05T03:21:51.445200Z

(letfn [(listen [m]
          (let [c (async/chan)]
            (async/tap m c)
            c))]
  (let [pub-port (async/chan)
        mult (async/mult pub-port)]
    (async/go
      (let [c (listen mult)]
        (prn [1 (async/<! c)])))
    (async/go
      (let [c (listen mult)]
        (prn [2 (async/<! c)])))
    (async/go
      (async/>! pub-port "ok"))))
This listen function make sense? It's implemented somewhere?

raspasov 2020-06-05T04:20:14.445600Z

@souenzzo also look at (promise-chan)

raspasov 2020-06-05T04:20:34.446200Z

That is, if you need to put one, and only one value on the channel and have all potential consumers receive that value