-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Add listener for non-value animated node #22883
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
Changes from all commits
e73e81c
fb301f8
3537e56
446e668
9f3f01e
d7fa6ee
5f8931e
196859c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,10 +20,6 @@ import type AnimatedTracking from './AnimatedTracking'; | |
|
|
||
| const NativeAnimatedAPI = NativeAnimatedHelper.API; | ||
|
|
||
| type ValueListenerCallback = (state: {value: number}) => void; | ||
|
|
||
| let _uniqueId = 1; | ||
|
|
||
| /** | ||
| * Animated works by building a directed acyclic graph of dependencies | ||
| * transparently when you render your Animated components. | ||
|
|
@@ -77,15 +73,12 @@ class AnimatedValue extends AnimatedWithChildren { | |
| _offset: number; | ||
| _animation: ?Animation; | ||
| _tracking: ?AnimatedTracking; | ||
| _listeners: {[key: string]: ValueListenerCallback}; | ||
| __nativeAnimatedValueListener: ?any; | ||
|
|
||
| constructor(value: number) { | ||
| super(); | ||
| this._startingValue = this._value = value; | ||
| this._offset = 0; | ||
| this._animation = null; | ||
| this._listeners = {}; | ||
| } | ||
|
|
||
| __detach() { | ||
|
|
@@ -97,14 +90,6 @@ class AnimatedValue extends AnimatedWithChildren { | |
| return this._value + this._offset; | ||
| } | ||
|
|
||
| __makeNative() { | ||
| super.__makeNative(); | ||
|
|
||
| if (Object.keys(this._listeners).length) { | ||
| this._startListeningToNativeValueUpdates(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Directly set the value. This will stop any animations running on the value | ||
| * and update all the bound properties. | ||
|
|
@@ -167,74 +152,6 @@ class AnimatedValue extends AnimatedWithChildren { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adds an asynchronous listener to the value so you can observe updates from | ||
| * animations. This is useful because there is no way to | ||
| * synchronously read the value because it might be driven natively. | ||
| * | ||
| * See http://facebook.github.io/react-native/docs/animatedvalue.html#addlistener | ||
| */ | ||
| addListener(callback: ValueListenerCallback): string { | ||
| const id = String(_uniqueId++); | ||
| this._listeners[id] = callback; | ||
| if (this.__isNative) { | ||
| this._startListeningToNativeValueUpdates(); | ||
| } | ||
| return id; | ||
| } | ||
|
|
||
| /** | ||
| * Unregister a listener. The `id` param shall match the identifier | ||
| * previously returned by `addListener()`. | ||
| * | ||
| * See http://facebook.github.io/react-native/docs/animatedvalue.html#removelistener | ||
| */ | ||
| removeListener(id: string): void { | ||
| delete this._listeners[id]; | ||
| if (this.__isNative && Object.keys(this._listeners).length === 0) { | ||
| this._stopListeningForNativeValueUpdates(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Remove all registered listeners. | ||
| * | ||
| * See http://facebook.github.io/react-native/docs/animatedvalue.html#removealllisteners | ||
| */ | ||
| removeAllListeners(): void { | ||
| this._listeners = {}; | ||
| if (this.__isNative) { | ||
| this._stopListeningForNativeValueUpdates(); | ||
| } | ||
| } | ||
|
|
||
| _startListeningToNativeValueUpdates() { | ||
| if (this.__nativeAnimatedValueListener) { | ||
| return; | ||
| } | ||
|
|
||
| NativeAnimatedAPI.startListeningToAnimatedNodeValue(this.__getNativeTag()); | ||
| this.__nativeAnimatedValueListener = NativeAnimatedHelper.nativeEventEmitter.addListener( | ||
| 'onAnimatedValueUpdate', | ||
| data => { | ||
| if (data.tag !== this.__getNativeTag()) { | ||
| return; | ||
| } | ||
| this._updateValue(data.value, false /* flush */); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| _stopListeningForNativeValueUpdates() { | ||
| if (!this.__nativeAnimatedValueListener) { | ||
| return; | ||
| } | ||
|
|
||
| this.__nativeAnimatedValueListener.remove(); | ||
| this.__nativeAnimatedValueListener = null; | ||
| NativeAnimatedAPI.stopListeningToAnimatedNodeValue(this.__getNativeTag()); | ||
| } | ||
|
|
||
| /** | ||
| * Stops any running animation or tracking. `callback` is invoked with the | ||
| * final value after stopping the animation, which is useful for updating | ||
|
|
@@ -259,6 +176,10 @@ class AnimatedValue extends AnimatedWithChildren { | |
| this._value = this._startingValue; | ||
| } | ||
|
|
||
| _onAnimatedValueUpdateReceived(value: number): void { | ||
| this._updateValue(value, false /*flush*/); | ||
| } | ||
|
|
||
| /** | ||
| * Interpolates the value before updating the property, e.g. mapping 0-1 to | ||
| * 0-10. | ||
|
|
@@ -321,9 +242,7 @@ class AnimatedValue extends AnimatedWithChildren { | |
| if (flush) { | ||
| _flush(this); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cannot call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cannot call |
||
| } | ||
| for (const key in this._listeners) { | ||
| this._listeners[key]({value: this.__getValue()}); | ||
| } | ||
| super._callListeners(this.__getValue()); | ||
| } | ||
|
|
||
| __getNativeConfig(): Object { | ||
|
|
||
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.
Cannot call
this._callListenersbecause property_callListenersis missing inAnimatedValue[1].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.
But it's in superclass