robinweser / alveron

Tiny (0.8kb) Elm-inspired state management for React
https://alveron.js.org
MIT License
73 stars 2 forks source link

Adds support for async actions #1

Closed farskid closed 6 years ago

farskid commented 6 years ago

Adds support for asynchronous actions which return a promise (then-able object).

robinweser commented 6 years ago

Thanks for that PR! Love it :) Going to merge it as soon as possible, but my left hand is damaged atm, so I should use the computer at all :/

farskid commented 6 years ago

@rofrischmann Thanks for the kind words :) Take care with the hand ;)

robinweser commented 6 years ago

I just came across an issue as you transformed the setState call from (simplyfied):

this.setState(prevState => ({ state: actions[name](prevState, ...) }))

to:

this.setState({ state: actions[name](this.state.state, ...) })

because you have to precalculate the value in order to check for async functions, but here's the issue: Without the functional setState (that passes the prevState to the callback), we might miss state updates if setState is called simultaniously.

A possible fix would be to separate async stuff from actions, e.g. calling em effects that get the setState handler passed.

farskid commented 6 years ago

Is it the same issue with how React batches state updates asynchronously?

Do you have a test case for me to test something as a fix?

robinweser commented 6 years ago

Yes, sadly it is. Apparently, there's no easy fix for this by returning Promises. I just added the effects option and released under 3.0.0 as I had to move forward quickly to finish some private stuff. Feel free to enhance, comment or improve that one though. I appreciate your efforts!

farskid commented 6 years ago

The change to support effects sounds cool! thanks for doing this 👍

robinweser commented 6 years ago

Yeah it's a more general approach to side effects I guess. Doesn't necessarily require Promises at all.