purescript-apparch icon indicating copy to clipboard operation
purescript-apparch copied to clipboard

Passing channel to update function?

Open alexmingoia opened this issue 9 years ago • 5 comments

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.

alexmingoia avatar Jan 06 '16 04:01 alexmingoia

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".

agrafix avatar Jan 06 '16 14:01 agrafix

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?

jasonzoladz avatar Mar 11 '16 21:03 jasonzoladz

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

agrafix avatar Mar 12 '16 09:03 agrafix

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

jasonzoladz avatar Mar 12 '16 10:03 jasonzoladz

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

jasonzoladz avatar Mar 12 '16 13:03 jasonzoladz