mobx.dart icon indicating copy to clipboard operation
mobx.dart copied to clipboard

why @action is not batching notifications? Is my implementation of @action correct?

Open danteCarvalho opened this issue 2 years ago • 3 comments

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);"

testef.zip

danteCarvalho avatar Apr 07 '22 15:04 danteCarvalho

I also have the same question

thphuccoder avatar Apr 21 '22 03:04 thphuccoder

@danteCarvalho @thphuc https://mobx.js.org/actions.html#asynchronous-actions

Yes, that documentation needs to be updated.

amondnet avatar Jan 30 '23 17:01 amondnet

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.

leon-volq avatar Mar 23 '23 19:03 leon-volq