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

Add listener for non-value animated node #22883

Closed
wants to merge 8 commits into from
Closed

Add listener for non-value animated node #22883

wants to merge 8 commits into from

Conversation

osdnk
Copy link
Contributor

@osdnk osdnk commented Jan 6, 2019

Changelog:

[Changed][General] Move callback-related logic to AnimatedNode class in order to make it possible to add the listener for other animated nodes than AnimatedValue.

I observed that native code appears to be fully prepared for listening not only to animated value but animated nodes generally. Therefore I managed to modify js code for exposing addListener method from AnimatedNode class instead of AnimatedValue. It called for some minor changes, which are not breaking.

If you're fine with these changes, I could add proper docs if needed.

Test Plan:

The most simple example to verify whether it works is to use code like this.

export default class App extends Component<Props> {
  constructor(props) {
    super(props)
    this.A = new Animated.Value(0);
    this.B = Animated.add(this.A, 10)
    this.B.addListener(({ value }) => console.log(value))

    Animated.timing(
      // Animate value over time
      this.A, // The value to drive
      {
        useNativeDriver: false,
        duration: 5000,
        toValue: 100, // Animate to final value of 1
      },
    ).start();
  }
  render() {
    return (
      <View style={styles.container}>
        <Animated.View style={{ width: 100, height: 100, backgroundColor: 'red', transform: [{ translateX: this.B }] }}/>
      </View>
    );
  }
}

You might switch useNativeDriver in order to verify my solution with and without native driver support.

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Tests This PR adds or edits a test case. labels Jan 6, 2019
Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

  • flow found some issues.

@@ -259,6 +176,10 @@ class AnimatedValue extends AnimatedWithChildren {

Choose a reason for hiding this comment

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

Missing type annotation for value.

@@ -321,9 +242,7 @@ class AnimatedValue extends AnimatedWithChildren {
if (flush) {
_flush(this);

Choose a reason for hiding this comment

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

Cannot call this._callListeners because property _callListeners is missing in AnimatedValue [1].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But it's in superclass

@pull-bot
Copy link

pull-bot commented Jan 6, 2019

Messages
📖 📋 Missing Summary - Can you add a Summary? To do so, add a "## Summary" section to your PR description. This is a good place to explain the motivation for making this change.
📖 📋 Missing Test Plan - Can you add a Test Plan? To do so, add a "## Test Plan" section to your PR description. A Test Plan lets us know how these changes were tested.
📖

📋 Missing Changelog - Can you add a Changelog? To do so, add a "## Changelog" section to your PR description. A changelog entry has the following format: [CATEGORY] [TYPE] - Message.

Generated by 🚫 dangerJS against 196859c

Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

  • flow found some issues.

@@ -321,9 +242,7 @@ class AnimatedValue extends AnimatedWithChildren {
if (flush) {
_flush(this);

Choose a reason for hiding this comment

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

Cannot call this._callListeners because property _callListeners is missing in AnimatedValue [1].

@osdnk
Copy link
Contributor Author

osdnk commented Jan 6, 2019

iOS tests appear to fail in not-related places

@osdnk osdnk changed the title Add listener for non value animated node Add listener for non-value animated node Jan 6, 2019
@matthargett
Copy link
Contributor

can you rebase against master to see if the latest CI fixes there make this PR go green?

Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

  • flow found some issues.

@@ -321,9 +242,7 @@ class AnimatedValue extends AnimatedWithChildren {
if (flush) {
_flush(this);

Choose a reason for hiding this comment

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

Cannot call this._callListeners because property _callListeners is missing in AnimatedValue [1].

@hramos hramos removed the 🔶APIs label Jan 24, 2019
Copy link
Contributor

@matthargett matthargett left a comment

Choose a reason for hiding this comment

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

CI failures are unrelated, probably another merge from master would work now, but I'd like to see if any issues are found in FB's CI.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@cpojer
Copy link
Contributor

cpojer commented Feb 12, 2019

CI is failing because of a Flow error, so this definitely doesn't seem to be ready yet. At FB, it's failing because we access _listeners somewhere where we shouldn't, but I can fix that once this PR is actually ready. It also seems to break about 20 tests at this time but I'm unsure whether that is because of mocking or if the Flow error on this PR is pointing at the same issue. Either way, I can try again once this is actually working.

*
* See http://facebook.github.io/react-native/docs/animatedvalue.html#addlistener
*/
addListener(callback: any): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know it's not perfect to use any, but we need to override this type in AnimatedValueXY with another type which is related to another logic. 😭

Copy link
Contributor

Choose a reason for hiding this comment

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

@osdnk maybe we can use callback: (value: any) => mixed instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

callback: (value: any) => void

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, well, I didn't want to do it previously, cause it changes existing code

@cpojer
Copy link
Contributor

cpojer commented Mar 22, 2019

cc @mattharget let me know when this is ready for shipping

@osdnk
Copy link
Contributor Author

osdnk commented Mar 22, 2019

@cpojer @matthargett forgot second "t" 😃

@cpojer
Copy link
Contributor

cpojer commented Apr 8, 2019

Still waiting for a review on this PR.

Copy link
Contributor

@matthargett matthargett left a comment

Choose a reason for hiding this comment

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

Thanks for adding tests and fixing the flow issues!

Copy link
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

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

Let's give this a try.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @osdnk in 68a5cee.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Apr 9, 2019
dsyang pushed a commit to dsyang/react-native that referenced this pull request Apr 12, 2019
Summary:
Changelog:
----------
[Changed][General] Move callback-related logic to `AnimatedNode` class in order to make it possible to add the listener for other animated nodes than `AnimatedValue`.

I observed that native code appears to be fully prepared for listening not only to animated value but animated nodes generally. Therefore I managed to modify js code for exposing `addListener` method from `AnimatedNode` class instead of `AnimatedValue`. It called for some minor changes, which are not breaking.

If you're fine with these changes, I could add proper docs if needed.
Pull Request resolved: facebook#22883

Differential Revision: D14041747

Pulled By: cpojer

fbshipit-source-id: 94c68024ceaa259d9bb145bf4b3107af0b15db88
grabbou pushed a commit that referenced this pull request May 6, 2019
Summary:
Changelog:
----------
[Changed][General] Move callback-related logic to `AnimatedNode` class in order to make it possible to add the listener for other animated nodes than `AnimatedValue`.

I observed that native code appears to be fully prepared for listening not only to animated value but animated nodes generally. Therefore I managed to modify js code for exposing `addListener` method from `AnimatedNode` class instead of `AnimatedValue`. It called for some minor changes, which are not breaking.

If you're fine with these changes, I could add proper docs if needed.
Pull Request resolved: #22883

Differential Revision: D14041747

Pulled By: cpojer

fbshipit-source-id: 94c68024ceaa259d9bb145bf4b3107af0b15db88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: Animated CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Tests This PR adds or edits a test case.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants