purescript-apparch
purescript-apparch copied to clipboard
Passing channel to update function?
Let's say I need to send an action to the channel from another action using async callbacks? The use case is trigging an ajax call using affjax from an action, then attempting to trigger another action to update state. This doesn't seem possible because Aff monad does not wrap the value of the callback, but unit instead.
Another use case is triggering multiple actions from another action.
What are you thoughts on passing the channel to the update function? That way actions could send actions.
You're right, it currently does not handle async effects well. I'm still trying to figure out how to do this effectively to keep the data flow simple and "traceable".
So I think we can solve this trivially using RxJS. Here's an implementation: https://github.com/jasonzoladz/purescript-react-rxState
Basically, there are distinct queues for Actions and Effects. Effects dispatched to the Effects-queue are interpreted and the results are dispatched as Actions.
I'm planning to package this all up "StartApp"-style. Thoughts?
Yes, but that would mean passing an action channel (or worse: a callback) to the update function. It's probably best to simply run in the Aff monad...
The action channel is a global (like a Mailbox in Elm). The update function is pure. There is simply a separate "effects" function, which runs your code in Aff and then dispatches an Action. The effects have to happen somewhere, right? :)
So here it is packaged as startApp in (small) library: https://github.com/jasonzoladz/purescript-rx-state
And an example is here: https://github.com/jasonzoladz/purescript-rx-state-react-example