by arstin · 9y ago · 0 comments
For example, this is nice:
// On login, spin off the auth process. If an error or a logout happens, clean up.
// Be sure to cancel any pending auth request!
// Then get ready for a new login.
function* loginFlow() {
while (true) {
const {user, password} = yield take('LOGIN_REQUEST')
const task = yield fork(authorize, user, password)
const action = yield take(['LOGOUT', 'LOGIN_ERROR'])
if (action.type === 'LOGOUT')
yield cancel(task)
yield call(Api.clearItem, 'token')
}
}
(Genuine question...idk!)https://github.com/jnorth/megalith/blob/master/docs/examples...
https://github.com/mobxjs/mobx-state-tree#creating-async-pro...
A conceptual appeal of mobx to me is that this distinction seems baked in. The original mobx didn't click with me like redux did ¯\_(ツ)_/¯, but if MST gives transactional updates and support for redux dev tools (right?) could be worth trying.
One thing I tried to do with megalith is mapping ideas from Redux to built-in language features as much as possible. An action object can trivially be represented as a function call for example—action name and payload is mapped to function name and arguments.
I think Mobx gets into trouble there by building off of observables—you have to litter your async code with different types of action markers to keep state consistent. MST tries to corral all those variations down into a smaller set, where the Redux ecosystem is layering ideas on top of an small initial core. Megalith and MST are starting from two completely opposite sides, but end up looking very similar somewhere in the middle :)
Edit: In MST it would look more like plain js:
class AuthStore() {
....
{
login() {
do_async_thing()
.then(this.postLoginStuff)
.catch(this.handleFailedLogin)
},
logout() {
// do logout stuff
}
}
}