core-async

2020-05-21T16:05:52.272900Z

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))

2020-05-21T16:18:13.273300Z

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

👍 1
2020-05-21T16:54:12.273900Z

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

2020-05-21T16:56:23.274500Z

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

2020-05-21T16:57:30.275Z

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

2020-05-21T16:58:05.275600Z

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

2020-05-21T17:34:06.277Z

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

2020-05-21T17:36:23.278100Z

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

dpsutton 2020-05-21T17:40:27.278600Z

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

dpsutton 2020-05-21T17:40:53.279Z

disconnect between multiple values into a channel that ignores subsequent values

2020-05-21T17:42:20.279500Z

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

2020-05-21T17:42:34.279900Z

and get rid of the transducer

dpsutton 2020-05-21T17:42:49.280200Z

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

2020-05-21T17:43:18.280800Z

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