Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awaiting response #26

Closed
4 tasks done
emiln opened this issue May 22, 2015 · 1 comment · Fixed by #29
Closed
4 tasks done

Awaiting response #26

emiln opened this issue May 22, 2015 · 1 comment · Fixed by #29

Comments

@emiln
Copy link
Owner

emiln commented May 22, 2015

At times it would be great to be able to await a response to some emitted event.

(handle :add +)
(println (<!! (emit! :add 1 2 3)))

The idea is that any handler for :do-some-io can return a non-nil value, and this will be put onto the channel awaited by the function that called emit! :do-some-io. This raises a few questions, however, and answering these will be the first task of this story:

  • If multiple functions are awaiting feedback on the same topic, should they all be notified separately by all handlers of the topic?

Yes

  • When should these feedback channels close?

By the caller of emit!; warrants function split

This is actually a fairly difficult question to answer. It seems unlikely that any one handler will be able to ascertain that no future puts from other handlers could occur to the channel. This leaves the question of whether emit! should be split into two functions: one returning an open channel that handlers may communicate over, and one returning nil, opening no channels. I think this might be preferable as it does not risk leaving open channels everywhere, and does not require the caller of emit! to diligently close all open, unused feedback channels.

  • Do emit! and handle at this point provide a genuinely simpler and easier interface to the user, or are they really just reinventing core.async poorly?

They are warranted

If you wanted to do this without handle and emit!, you'd declare the following once:

(def publisher (chan))
(def publication (pub publisher first))

And the following for every pair of handle and emit! calls:

(let [handler (chan)]
  (sub publication :add handler)
  (go (let [[_ return-chan & args] (<! handler)]
        (>! return-chan (apply + args)))))

(let [return-chan (chan)]
  (go (>! publisher [:add return-chan 1 2 3])
  (println (<!! return-chan))))

While this is certainly not a big task, I do think handle and emit! provide an easy as well as simple way to deal with this very topic-centric flow.

  • What should happen when a handler returns nil? It is not legal to put on a channel.

Nothing is put

It is not safe to assume you can close the channel as more puts from other handlers may happen in the future. It is not legal to put nil on the channel either, so the sensible and most simple solution is most likely to just don't put anything on at all in this case.

@emiln emiln modified the milestone: Release 1.3 May 27, 2015
emiln added a commit that referenced this issue May 28, 2015
First shot at implementing awaiting. The last unit test currently fails,
but I need help solving it. Hence the broken push.
@emiln emiln added medium and removed small labels May 29, 2015
@emiln
Copy link
Owner Author

emiln commented May 31, 2015

One corner case: What to do when the the handler returns nil?

emiln added a commit that referenced this issue Jun 1, 2015
This new function emit-with-feedback! is introduced alongside emit!
to be explicit about the creation of a feedback channel. This should
help newcomers avoid creating a plethora of unused channels that clog
up the JVM.
@emiln emiln closed this as completed in #29 Jun 1, 2015
emiln added a commit that referenced this issue Jun 1, 2015
[#26] Implement emit-with-feedback! to handle awaiting responses from handlers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant