-
Notifications
You must be signed in to change notification settings - Fork 24.9k
AnimatedWithChildren support for addListener #12620
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
Conversation
This would address the issue that manipulations with Animated.add, .multiply, etc. would not return an Animated.Value which meant it could not be used for attaching listeners.
|
Ping @janicduplessis . This is the PR I mentioned on Twitter. I don't know what the plans are for the native driver with listeners so forgive me if this is a naive solution. I expect we may need to talk it over, depending on what your plans are. |
| NativeAnimatedAPI.stopListeningToAnimatedNodeValue(this.__getNativeTag()); | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameter data Missing annotation
|
|
||
| /* Typically used internally */ | ||
| triggerListeners(eventPropagation?: bool = true): void { | ||
| for (var key in this._listeners) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keyword-spacing: Expected space(s) after "if".
| triggerListeners(eventPropagation?: bool = true): void { | ||
| for (var key in this._listeners) { | ||
| this._listeners[key]({value: this.__getValue()}); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keyword-spacing: Expected space(s) after "if".
|
Nice! Seems like a great solution, I like how you implemented the JS part with event propagation since every node is not a value node like in the native implementation. One thing that could be improved is make sure the value actually changed before triggering the change event and stop the propagation too. I'm pretty sure native does that too so it would make sure the behavior is exactly the same. |
|
|
||
| /* Typically used internally */ | ||
| triggerListeners(eventPropagation?: bool = true): void { | ||
| for (var key in this._listeners) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keyword-spacing: Expected space(s) after "if".
| NativeAnimatedAPI.stopListeningToAnimatedNodeValue(this.__getNativeTag()); | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameter data Missing annotation
| triggerListeners(eventPropagation?: bool = true): void { | ||
| for (var key in this._listeners) { | ||
| this._listeners[key]({value: this.__getValue()}); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keyword-spacing: Expected space(s) after "if".
|
LGTM, could you just fix lint and flow errors. |
|
Ofc! I fixed the lint issues but I can't reproduce the flow error "function type Callable signature not found in object type" locally. I'll wait for the eslint-bot to re-run and then see what comes out. |
|
Have you run flow with |
|
Forgot to add |
Flow (correctly) detected that listeners were not compatible in AnimatedXYValue, so this change should ensure that both work. It should also ensure that all listeners are correctly tracked and removed after use.
|
Tested this in an app and it works really well! Did you manage to fix the flow errors? |
|
Great! Yes, I did. The patch following your comment were to fix the flow issues :) |
|
Looks like there is still flow errors in other files caused by this change. https://github.com/facebook/react-native/blob/master/Libraries/CustomComponents/NavigationExperimental/NavigationCardStackPanResponder.js#L232 is the issue but it might be a bug in flow because the property does exist since it is defined in the parent class. Maybe you could just add a $FlowFixMe comment since it seems like a flow bug and this code is hacky and uses Animated internals. |
|
@ericvicenti Could you have a look at these flow errors to see if I'm missing something or this might be a bug in flow? |
|
If I type hint it to The error is: My best guess is that Flow doesn't recognize when setting instance properties through |
|
It looks like the issue is that CardStackPanResponder attempts to access a private |
|
@ericvicenti What is weird is that it used to work before moving the property to the parent class. |
|
A workaround would be to re-define the type in |
|
I'd rather just fix it in PanResponder. You could annotate the function with Object instead of Animated.Value and it should work. |
Relating to #12620 (comment) 83
| } | ||
|
|
||
| type ValueListenerCallback = (state: {value: number}) => void; | ||
| type AnimatedListenerCallback = (state: Object) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for this change? I'd rather keep the more precise flow type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the callbacks are now added by the parent class (AnimatedWithChildren), the arguments are now up to the extending classes, which means AnimatedValueXY takes {x: number, y:number} while AnimatedValue takes {value: number}.
I'd like to keep the types but I'm not familiar enough with Flow to know how that would work. Should I investigate further?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, does keeping (state: {value: number}) => void in AnimatedWithChildren and using a different type for AnimatedValueXY works? If not I don't really have a better idea :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, after the flow fixes your suggestion may work (listeners for AnimatedValue and ~XY were decoupled). I'll brb with a fix and change the name back as well, to keep the changes minimal.
| for (var key in this._listeners) { | ||
| this._listeners[key]({value: this.__getValue()}); | ||
| } | ||
| this.triggerListeners(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's prefix this method with an underscore since it's not really public API.
|
I've made it a union type and renamed it back to |
|
@janicduplessis Let me know if you need any more fixes to get it merged :) |
|
I tested this with flow and the issue is that the argument of the listener callback is a union type now so you will have to check the type before using it. I guess I can see 2 solutions:
|
|
If we move the implementation to all subclasses, then we fragment everything relating to listeners within |
|
@scarlac I tried to find reviewers for this pull request and wanted to ping them to take another look. However, based on the blame information for the files in this pull request I couldn't find any reviewers. This sometimes happens when the files in the pull request are new or don't exist on master anymore. Is this pull request still relevant? If yes could you please rebase? In case you know who has context on this code feel free to mention them in a comment (one person is fine). Thanks for reading and hope you will continue contributing to the project. |
|
Hey, @janicduplessis. Is there any progress here? |
|
@juangl Not at the moment, I'll try to find some time soon to revisit this. |
|
Hi @janicduplessis , Any idea when this PR will be merged? |
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions. |
|
this useful PR has been closed :( Yet I think we really need this to land in RN to make some animations easier. What should we do to make progress on this? make a new PR or ask to reopen this one? |
|
Someone would need to rebase this off of master and test it thoroughly.
Flow has also been updated quite a bit since then so maybe there are
options to make it work better.
…On Thu, 2 Aug 2018 at 00:49, Sébastien Lorber ***@***.***> wrote:
this useful PR has been closed :(
Yet I think we really need this to land in RN to make some animations
easier.
What should we do to make progress on this? make a new PR or ask to reopen
this one?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#12620 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA2piQbeNQuIlowCdvqb4q6fh3vL2nCAks5uMq8hgaJpZM4MOhEk>
.
|
|
Should it work in recent versions? (I'm using |
|
@cglacet I don't know but I recommend reanimated 2 |
|
@scarlac Yeah maybe, but switching to reanimated is a very heavy change that involves a lot of risks (app crashes). If I could avoid this I would really love to. |
Motivation
This PR is born out of trying to move to
useNativeDriver. It addresses the issue that manipulations with Animated.add, .multiply, etc. would not return an Animated.Value which meant it could not be used for attaching listeners.Animated.eventwith native driver also doesn't support listeners either.This PR mostly moved code from
Animated.ValueintoAnimatedWithChildren. There are some edge cases which may need to be addressed but I feel like this PR can get some discussion started on where to take the changes. Not being able to add listeners to animation child nodes currently limits the usability of native animations.Test plan (required)
I have a local project that has been transitioned to useNativeDriver. This is how I am currently testing my code. I think we should discuss if this is the right way to go forward before writing extensive tests.