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

Make InteractionManager.runAfterInteractions() return a Promise #3788

Closed
wants to merge 1 commit into from

Conversation

philikon
Copy link
Contributor

In accordance with the unwritten rule that any API that takes a callback should also return a promise, I've changed InteractionManager.runAfterInteractions() to do just that.

  InteractionManager.runAfterInteractions(() => {
    ...
  });

can become

  InteractionManager.runAfterInteractions().then(() => {
    ...
  });

(but doesn't have to). Most importantly, though, this change enables code like

  doSomeUIStuff();
  await InteractionManager.runAfterInteractions();
  doSomeNonUIStuff();

which is nice.

Note: Because returning a Promise means the callback argument is now optional, the behaviour of the API is slightly changed, though not in a backwards-incompatible way (unless a consumer is in some way relying on the exception, but that would be insane).

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @vjeux, @bhosmer and @sahrens to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Oct 30, 2015
@vjeux
Copy link
Contributor

vjeux commented Oct 30, 2015

cc @joesavona

@philikon
Copy link
Contributor Author

(Hi! 👋)

@brentvatne
Copy link
Collaborator

This is pretty useful, we do something similar in the Exponent fork of react-timer-mixin, called react-native-timer-mixin: expo/react-native-timer-mixin@8064622

This allows us to do:

await this.waitForInteractionsAsync()

And it protects us from the component potentially being unmounted. One potential issue with using the approach from this PR is that it is possible for the component to be unmounted between when the function yields and regains control, so you'd have to always wrap it in try / catch. But it's still useful outside of components. 👍

@philikon
Copy link
Contributor Author

Are those CI failures for real, btw? Seems unlikely that this affected layout...

_queue.push(callback);
}
_queue.push(resolve);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a promise seems fine, but the callback has to be executed after interactions, not when the promise is created. How about:

runAfterInteractions(callback: ?Function): Promise {
  return new Promise(resolve => {
    scheduleUpdate();
    _queue.push(() => {
      callback && callback();
      resolve();
    });
  });
}

Also, both of these implementations will cause the promise to never be resolved if executing callback throws an error. Is that intended?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback is pushed onto the queue i.e. invoked when the interaction completes. That's my reading of @philikon's code at least. The InteractionManager wraps each callback in an ErrorUtils guard so even if the callback fails the InteractionManager will run the promise's resolve function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah what @ide says. I didn't change the semantics around how callback gets executed. If you read the red portions of the diff, you'll see that it currently gets pushed onto the queue just like it does after my change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I read this too fast.

👍

@philikon
Copy link
Contributor Author

philikon commented Nov 3, 2015

Pingsies?

@philikon
Copy link
Contributor Author

philikon commented Nov 9, 2015

Meow.

@josephsavona
Copy link
Contributor

@facebook-github-bot import

@josephsavona
Copy link
Contributor

Sorry about the wait!

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/1637419186517901/int_phab to review.

@ghost ghost closed this in d7c8b44 Nov 10, 2015
ide pushed a commit to expo/react-native that referenced this pull request Nov 13, 2015
Summary: In accordance with the unwritten rule that any API that takes a callback should also return a promise, I've changed `InteractionManager.runAfterInteractions()` to do just that.

```js
  InteractionManager.runAfterInteractions(() => {
    ...
  });
```
can become
```js
  InteractionManager.runAfterInteractions().then(() => {
    ...
  });
```
(but doesn't have to). Most importantly, though, this change enables code like
```js
  doSomeUIStuff();
  await InteractionManager.runAfterInteractions();
  doSomeNonUIStuff();
```
which is nice.

Note: Because returning a `Promise` means the callback argument is now optional, the behaviour of the API is slightly changed, though not in a backwards-incompatible way (unless a consumer is in some way relying on the exception, but that would be insane).
Closes facebook#3788

Reviewed By: vjeux

Differential Revision: D2634693

Pulled By: josephsavona

fb-gh-sync-id: 7315120963be23cf69d777e940b2750d32ae47a8
sunnylqm pushed a commit to sunnylqm/react-native that referenced this pull request Dec 2, 2015
Summary: In accordance with the unwritten rule that any API that takes a callback should also return a promise, I've changed `InteractionManager.runAfterInteractions()` to do just that.

```js
  InteractionManager.runAfterInteractions(() => {
    ...
  });
```
can become
```js
  InteractionManager.runAfterInteractions().then(() => {
    ...
  });
```
(but doesn't have to). Most importantly, though, this change enables code like
```js
  doSomeUIStuff();
  await InteractionManager.runAfterInteractions();
  doSomeNonUIStuff();
```
which is nice.

Note: Because returning a `Promise` means the callback argument is now optional, the behaviour of the API is slightly changed, though not in a backwards-incompatible way (unless a consumer is in some way relying on the exception, but that would be insane).
Closes facebook#3788

Reviewed By: vjeux

Differential Revision: D2634693

Pulled By: josephsavona

fb-gh-sync-id: 7315120963be23cf69d777e940b2750d32ae47a8
Crash-- pushed a commit to Crash--/react-native that referenced this pull request Dec 24, 2015
Summary: In accordance with the unwritten rule that any API that takes a callback should also return a promise, I've changed `InteractionManager.runAfterInteractions()` to do just that.

```js
  InteractionManager.runAfterInteractions(() => {
    ...
  });
```
can become
```js
  InteractionManager.runAfterInteractions().then(() => {
    ...
  });
```
(but doesn't have to). Most importantly, though, this change enables code like
```js
  doSomeUIStuff();
  await InteractionManager.runAfterInteractions();
  doSomeNonUIStuff();
```
which is nice.

Note: Because returning a `Promise` means the callback argument is now optional, the behaviour of the API is slightly changed, though not in a backwards-incompatible way (unless a consumer is in some way relying on the exception, but that would be insane).
Closes facebook#3788

Reviewed By: vjeux

Differential Revision: D2634693

Pulled By: josephsavona

fb-gh-sync-id: 7315120963be23cf69d777e940b2750d32ae47a8
@philikon philikon deleted the interaction_promise branch January 5, 2016 01:57
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants