Fork me on GitHub
#core-async
<
2020-05-21
>
jjttjj16:05:52

If I have a task that needs a result and then to perform some "teardown" step, I could use a promise chan and a go block that takes from it then performs the teardown, and then return the promise-chan. Is this hacky, or is this idiomatic and one of the reasons for promise-chan?

(defn mount-prom [parent child]
  (let [ch (a/promise-chan
             (comp (map #(array-seq (.-addedNodes %))) cat (keep #{child})))
        mut (js/MutationObserver. (fn [muts] (go (a/onto-chan! ch muts))))]
    
    (.observe mut parent #js {:childList true})
    
    (go (<! ch) (.disconnect mut)) ;;once we have a value in the promise,
                                   ;;disconnection observer
    ch))

noisesmith16:05:13

I think that's one of the primary reasons promise-chans exist

👍 4
hiredman16:05:12

because you are filtering things out of the promise-chan your clean up may never fire

hiredman16:05:23

onto-chan! has its own go-loop internally, so there is no need to wrap it in a go block

hiredman16:05:30

onto-chan! is also a super weird thing to use with a promise-chan

hiredman16:05:05

onto-chan! implies putting multiple values on to a channel, a promise-chan only ever has one

jjttjj17:05:06

good points. I was depending on the filtering (via keep) in the transducer to just ignore things until I get one I cared about, at which point it would be done

jjttjj17:05:23

I think I saw the recent changes with onto-chan! and it got in my head, but I didn't actually realzied it closed the chan once the items are copied, so I see that a doseq+put! would be better

dpsutton17:05:27

i think the question was more what are you putting many values into a promise chan

dpsutton17:05:53

disconnect between multiple values into a channel that ignores subsequent values

jjttjj17:05:20

Ohhh so I should probably just do the filtering in the callback before putting it in the channel?

jjttjj17:05:34

and get rid of the transducer

dpsutton17:05:49

and i think that relates to @hiredman's point that if you are filtering its possible you never get an acceptable value

jjttjj17:05:18

yeah that's a good point to, I think I'll add a timeout option