Fork me on GitHub
#core-async
<
2023-05-17
>
souenzzo02:05:48

How to <! a java Publisher interface?

souenzzo02:05:38

something like this:

(defn as-async
  [^Publisher p]
  (let [promise-chan (async/promise-chan)]
    (.subscribe p
      (reify Subscriber
        (onComplete [this]
          (async/close! promise-chan))
        (onNext [this t]
          (async/put! promise-chan t))
        (onSubscribe [this s]
          (.request s 1))))
    promise-chan))
There is a library for that?

Ben Sless02:05:15

Extend the ReadPort protocol?

hiredman03:05:53

You need to use >!!

hiredman03:05:50

(oh, didn't notice the single use promise chan)

hiredman03:05:17

Actually what you would do is use put!, But pass a callback that calls request 1 after the put! conpletes

hiredman03:05:37

And then don't use a promise chan

hiredman03:05:24

Then you'll have a pretty generic subscriber to channel adapter

hiredman03:05:11

You could extend readport to Publisher, but then you are extending a protocol you don't own to a type you don't own, which is generally a bad practice