mobx.dart
mobx.dart copied to clipboard
why @action is not batching notifications? Is my implementation of @action correct?
Discussed in https://github.com/mobxjs/mobx.dart/discussions/789
Originally posted by danteCarvalho April 6, 2022
shouldn't @action make notifications be executed after the method?
here says https://pub.dev/packages/mobx#actions :
Besides, actions also batch up all the notifications and ensure the changes are notified only after they complete. Thus the observers are notified only upon the atomic completion of the action.
@observable
int a = 0;
@action
init() async {
await Future.delayed(Duration.zero);
a = await Future.delayed(Duration(seconds: 1), () {
return 1;
});
await Future.delayed(Duration.zero);
a = await Future.delayed(Duration(seconds: 1), () {
return 2;
});
await Future.delayed(Duration.zero);
a = await Future.delayed(Duration(seconds: 1), () {
return 3;
});
await Future.delayed(Duration.zero);
}
if the code has a 'await' the Observer is called even if the @action method is not finished, in this case the Observer is called 3 times after the "await Future.delayed(Duration.zero);"
I also have the same question
@danteCarvalho @thphuc https://mobx.js.org/actions.html#asynchronous-actions
Yes, that documentation needs to be updated.
Hello @amondnet, so are Mobx dart async actions exactly the same as mobx.js?
Do all examples apply in dart?
- wrap handlers in actions
- handle updates in separate actions
- async/await runInAction
- flow + generator
I've never used dart.js, so by reading the dart docs it made me think that the state will be updated at the end of the async function.
If you instruct me a little bit about what parts of the js docs apply in dart I can help updating the docs.