-
-
Notifications
You must be signed in to change notification settings - Fork 153
[atom] Async effects in events bus #14
Comments
Hey, sorry just briefly... I already noticed this too and the current lack of getting a handle on the bus itself from a handler is an oversight of my possibly too speedy refactoring last night. My original solution which I used in existing projects was to have an InterceptorContext, a kind of accumulator object into which handlers would not just add their side effects, but which also allows access to other bindings (like the bus, or other services). I got rid of this last night after reading about & liking redux's approach to just return values (instead of assigning them to the accumulator), but I think I might do a U-turn on this again... also still have to read about Citrus... so many links, so little time ;) Thanks, for these pointers though! You also correctly observed that the way the "addCounter" effect in the demo is defined from within the |
So I just had a quick go at this and was wondering what your thoughts are :) New demo: http://demo.thi.ng/umbrella/async-effect/ |
@allforabit i hope you don't mind, but since i've already got the demo and done some more manual testing and updating other smaller things, I'll cut another release with this promise based version of the |
Related, I also updated the |
That's absolutely perfect. It's much nicer to do the normal dispatch along with an async dispatch as it's not nearly as nested this way. I was thinking that it'd be good to pass in the dispatch function to the effect handlers but it's probably the most flexible way is to pass in the full event bus. I can envisage standard effect handlers for channels and the likes, that could do multiple dispatches as new values come in. This would work nicely for socket connections for instance. Brilliant stuff, thanks very much for that! |
Yeah, exactly & thanks to you, sir! Regarding multiple dispatches, I also added support for each individual interceptor to have the option to declare multiple side effects of same type: https://github.com/thi-ng/umbrella/blob/master/packages/atom/src/event-bus.ts#L269 |
I believe it's safe to close this issue for now... |
This is related to the previous issue but it's probably separate enough to warrant it's own issue.
How would you go about adding an async event such as an an ajax request? I can't see a way to do it via the event handler config because they don't get a handle on the event bus. They only get the state and event. I can see that you could do it after the event bus is instantiated but it would be nice to be able to be able to do it through the event config. It would probably need to be another built in effect similar to "dispatch" that might look like this:
[FX_DISPATCH_ASYNC]: {effect: [FETCH_JSON: {url: "http://thi.ng/feed.json"}], handler: "myHandler"}
. The effect function would then need to take a callback as an argument to callback into the event system.I can attempt a pull request for this!
The text was updated successfully, but these errors were encountered: