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

The reason runInAction does not support asynchronous #905

Closed
ascoders opened this issue Mar 26, 2017 · 5 comments
Closed

The reason runInAction does not support asynchronous #905

ascoders opened this issue Mar 26, 2017 · 5 comments

Comments

@ascoders
Copy link

On the process of collecting, which is setter, if it is triggered by the asynchronous runInAction callback, setter can be added to the execution queue corresponding to the runInAction queue to solve the problem, but in fact don't know, unless the runInAction callback is synchronous, we can set the global variable to identify.

So can i do someThing like following?

const fn = () => {
  dynamicObject.someProperty = 666
}

// runInAction1
runInAction(async () => {
  await Promise.resolve()
  fn()
})

// runInAction2
runInAction(async () => {
  await Promise.resolve()
  fn()
})

In the pre bound setter of dynamicObject.someProperty, know which runInAction1 or runInAction2 trigger I am?

@andykog
Copy link
Member

andykog commented Mar 26, 2017

@ascoders, no, runInAction only applied to what runs synchronously. There is no mechanism in JS to do what you want reliably (if runInAction will wait until your promise resolved, how to know, if some was invoked in your promise or somewhere else?). You can make something like:

runInAction(async () => {
  fn() // this part runs sync, so everything ok
  await Promise.resolve()
  runInAction(fn) // this part needs another runInAction call
})

@ascoders
Copy link
Author

Thanks @andykog, This is the most regrettable, only to see the way Babel pretreatment, but this is contrary to the code running mode, maintainability is not good.

But I think the principle is feasible, if the callback is async, all triggered setter corresponding triggered autoRun push to the isolated queue, when .then run this queue.

The focus is on how to isolate.

@andykog
Copy link
Member

andykog commented Mar 26, 2017

@ascoders, do you have some specific propositions on how to tell whether the “setter” was called as a result of some async operation that relates to action without reinventing javascript (event loop, promises…)?

@ascoders
Copy link
Author

Not yet, as long as the problem solved, runInAction can support asynchronous callback. 😍

@andykog
Copy link
Member

andykog commented Mar 26, 2017

@ascoders, cool, then, closing the issue

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

2 participants