Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
danteCarvalho opened this issue Apr 7, 2022 Discussed in #789 · 3 comments

Comments

@danteCarvalho
Copy link

danteCarvalho commented Apr 7, 2022

Discussed in #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

@tran-huy-phuc
Copy link

I also have the same question

@amondnet
Copy link
Collaborator

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

Yes, that documentation needs to be updated.

@leon-volq
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants