Fork me on GitHub
#core-async
<
2018-05-07
>
noisesmith02:05:28

@twashing here's pub and sub doing exactly what you want

Clojure 1.9.0
:user=> (require '[clojure.core.async :as >])
nil
:user=> (def c (>/chan))
#'user/c
:user=> (def p (>/pub c :id))
#'user/p
:user=> (def c' (>/chan))
#'user/c'
:user=> (def s (>/sub p 1 c'))
#'user/s
:user=> (def c'' (>/chan))
#'user/c''
:user=> (def s' (>/sub p 2 c''))
#'user/s'
:user=> (>/take! c'' #(println % 'has 'id 2))
nil
:user=> (>/take! c' #(println % 'has 'id 1))
nil
:user=> (>/put! c {:id 1 :foo 2})
true
user=> {:id 1, :foo 2} has id 1

twashing03:05:48

@noisesmith Thanks for putting this together. But the line(s) where we subscribe with a specific id… (>/sub p 1 c')

twashing03:05:03

I won’t know the ids ahead of time.

twashing03:05:04

I suppose subscriber channels are cheap, so could set up some extra bookkeeping to remember ids until everything’s collected.

twashing03:05:14

So yeah, that could be a strategy.