mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Animated: Lower onAnimatedValueUpdate to AnimatedValue (#48514)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48514 Currently, `AnimatedNode` implements logic to start listening to updates on the current native node if a listener is added. However, the `startListeningToAnimatedNodeValue` native module method only supports native tags for instances of `AnimatedValue`, which is a subclass of `AnimatedNode`. In fact… * On Android, [`startListeningToAnimatedNodeValue`](https://fburl.com/code/bdsl4sro) throws if the node is not an instance of `ValueAnimatedNode`. * On iOS, [`startListeningToAnimatedNodeValue`](https://fburl.com/code/hlpk1rzk) does nothing if node is not an instance of `RCTValueAnimatedNode`. As such, this refactors `AnimatedNode` to never manage this subscription for native nodes. Instead, the logic is moved into the `AnimatedValue` subclass, ensuring that we never accidentally try to `startListeningToAnimatedNodeValue` with non-`AnimatedValue` native tags. Changelog: [Internal] Reviewed By: javache Differential Revision: D67884973 fbshipit-source-id: 5601efd8c29104a991301eabd97ddb88fd03c4a3
This commit is contained in:
1 parent
d3c5f6d1df
commit
38c46fe865
4 files changed
+80
-64
No files matched your search
+10
-16
@@ -8,21 +8,15 @@
|
|||||||
* @oncall react_native
|
* @oncall react_native
|
||||||
*/
|
*/
|
||||||
|
|
||||||
describe('AnimatedNode', () => {
|
describe('AnimatedValue', () => {
|
||||||
let NativeAnimatedHelper;
|
let NativeAnimatedHelper;
|
||||||
let AnimatedNode;
|
let AnimatedValue;
|
||||||
|
|
||||||
function createNativeAnimatedNode(): AnimatedNode {
|
function createNativeAnimatedValue(): AnimatedValue {
|
||||||
class NativeAnimatedNode extends AnimatedNode {
|
return new AnimatedValue(0, {useNativeDriver: true});
|
||||||
__isNative = true;
|
|
||||||
__getNativeConfig(): {} {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new NativeAnimatedNode();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function emitMockUpdate(node: AnimatedNode, mockValue: number): void {
|
function emitMockUpdate(node: AnimatedValue, mockValue: number): void {
|
||||||
const nativeTag = node.__nativeTag;
|
const nativeTag = node.__nativeTag;
|
||||||
expect(nativeTag).not.toBe(undefined);
|
expect(nativeTag).not.toBe(undefined);
|
||||||
|
|
||||||
@@ -50,7 +44,7 @@ describe('AnimatedNode', () => {
|
|||||||
|
|
||||||
NativeAnimatedHelper =
|
NativeAnimatedHelper =
|
||||||
require('../../../src/private/animated/NativeAnimatedHelper').default;
|
require('../../../src/private/animated/NativeAnimatedHelper').default;
|
||||||
AnimatedNode = require('../nodes/AnimatedNode').default;
|
AnimatedValue = require('../nodes/AnimatedValue').default;
|
||||||
|
|
||||||
jest.spyOn(NativeAnimatedHelper.API, 'createAnimatedNode');
|
jest.spyOn(NativeAnimatedHelper.API, 'createAnimatedNode');
|
||||||
jest.spyOn(NativeAnimatedHelper.API, 'dropAnimatedNode');
|
jest.spyOn(NativeAnimatedHelper.API, 'dropAnimatedNode');
|
||||||
@@ -58,7 +52,7 @@ describe('AnimatedNode', () => {
|
|||||||
|
|
||||||
it('emits update events for listeners added', () => {
|
it('emits update events for listeners added', () => {
|
||||||
const callback = jest.fn();
|
const callback = jest.fn();
|
||||||
const node = createNativeAnimatedNode();
|
const node = createNativeAnimatedValue();
|
||||||
node.__attach();
|
node.__attach();
|
||||||
const id = node.addListener(callback);
|
const id = node.addListener(callback);
|
||||||
|
|
||||||
@@ -75,7 +69,7 @@ describe('AnimatedNode', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('creates a native node when adding a listener', () => {
|
it('creates a native node when adding a listener', () => {
|
||||||
const node = createNativeAnimatedNode();
|
const node = createNativeAnimatedValue();
|
||||||
node.__attach();
|
node.__attach();
|
||||||
expect(NativeAnimatedHelper.API.createAnimatedNode).not.toBeCalled();
|
expect(NativeAnimatedHelper.API.createAnimatedNode).not.toBeCalled();
|
||||||
|
|
||||||
@@ -85,7 +79,7 @@ describe('AnimatedNode', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('drops a created native node on detach', () => {
|
it('drops a created native node on detach', () => {
|
||||||
const node = createNativeAnimatedNode();
|
const node = createNativeAnimatedValue();
|
||||||
node.__attach();
|
node.__attach();
|
||||||
expect(NativeAnimatedHelper.API.createAnimatedNode).toBeCalledTimes(0);
|
expect(NativeAnimatedHelper.API.createAnimatedNode).toBeCalledTimes(0);
|
||||||
|
|
||||||
@@ -100,7 +94,7 @@ describe('AnimatedNode', () => {
|
|||||||
|
|
||||||
it('emits update events for listeners added after re-attach', () => {
|
it('emits update events for listeners added after re-attach', () => {
|
||||||
const callbackA = jest.fn();
|
const callbackA = jest.fn();
|
||||||
const node = createNativeAnimatedNode();
|
const node = createNativeAnimatedValue();
|
||||||
node.__attach();
|
node.__attach();
|
||||||
|
|
||||||
node.addListener(callbackA);
|
node.addListener(callbackA);
|
||||||
@@ -8,15 +8,11 @@
|
|||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
|
|
||||||
import type {PlatformConfig} from '../AnimatedPlatformConfig';
|
import type {PlatformConfig} from '../AnimatedPlatformConfig';
|
||||||
|
|
||||||
import NativeAnimatedHelper from '../../../src/private/animated/NativeAnimatedHelper';
|
import NativeAnimatedHelper from '../../../src/private/animated/NativeAnimatedHelper';
|
||||||
import invariant from 'invariant';
|
import invariant from 'invariant';
|
||||||
|
|
||||||
const {startListeningToAnimatedNodeValue, stopListeningToAnimatedNodeValue} =
|
|
||||||
NativeAnimatedHelper.API;
|
|
||||||
|
|
||||||
type ValueListenerCallback = (state: {value: number, ...}) => mixed;
|
type ValueListenerCallback = (state: {value: number, ...}) => mixed;
|
||||||
|
|
||||||
export type AnimatedNodeConfig = $ReadOnly<{
|
export type AnimatedNodeConfig = $ReadOnly<{
|
||||||
@@ -33,7 +29,6 @@ let _assertNativeAnimatedModule: ?() => void = () => {
|
|||||||
|
|
||||||
export default class AnimatedNode {
|
export default class AnimatedNode {
|
||||||
#listeners: Map<string, ValueListenerCallback> = new Map();
|
#listeners: Map<string, ValueListenerCallback> = new Map();
|
||||||
#updateSubscription: ?EventSubscription = null;
|
|
||||||
|
|
||||||
_platformConfig: ?PlatformConfig = undefined;
|
_platformConfig: ?PlatformConfig = undefined;
|
||||||
|
|
||||||
@@ -78,9 +73,6 @@ export default class AnimatedNode {
|
|||||||
);
|
);
|
||||||
|
|
||||||
this._platformConfig = platformConfig;
|
this._platformConfig = platformConfig;
|
||||||
if (this.#listeners.size > 0) {
|
|
||||||
this.#ensureUpdateSubscriptionExists();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,9 +85,6 @@ export default class AnimatedNode {
|
|||||||
addListener(callback: (value: any) => mixed): string {
|
addListener(callback: (value: any) => mixed): string {
|
||||||
const id = String(_uniqueId++);
|
const id = String(_uniqueId++);
|
||||||
this.#listeners.set(id, callback);
|
this.#listeners.set(id, callback);
|
||||||
if (this.__isNative) {
|
|
||||||
this.#ensureUpdateSubscriptionExists();
|
|
||||||
}
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,9 +96,6 @@ export default class AnimatedNode {
|
|||||||
*/
|
*/
|
||||||
removeListener(id: string): void {
|
removeListener(id: string): void {
|
||||||
this.#listeners.delete(id);
|
this.#listeners.delete(id);
|
||||||
if (this.__isNative && this.#listeners.size === 0) {
|
|
||||||
this.#updateSubscription?.remove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,44 +105,12 @@ export default class AnimatedNode {
|
|||||||
*/
|
*/
|
||||||
removeAllListeners(): void {
|
removeAllListeners(): void {
|
||||||
this.#listeners.clear();
|
this.#listeners.clear();
|
||||||
if (this.__isNative) {
|
|
||||||
this.#updateSubscription?.remove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hasListeners(): boolean {
|
hasListeners(): boolean {
|
||||||
return this.#listeners.size > 0;
|
return this.#listeners.size > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ensureUpdateSubscriptionExists(): void {
|
|
||||||
if (this.#updateSubscription != null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const nativeTag = this.__getNativeTag();
|
|
||||||
startListeningToAnimatedNodeValue(nativeTag);
|
|
||||||
const subscription: EventSubscription =
|
|
||||||
NativeAnimatedHelper.nativeEventEmitter.addListener(
|
|
||||||
'onAnimatedValueUpdate',
|
|
||||||
data => {
|
|
||||||
if (data.tag === nativeTag) {
|
|
||||||
this.__onAnimatedValueUpdateReceived(data.value);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
this.#updateSubscription = {
|
|
||||||
remove: () => {
|
|
||||||
// Only this function assigns to `this.#updateSubscription`.
|
|
||||||
if (this.#updateSubscription == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.#updateSubscription = null;
|
|
||||||
subscription.remove();
|
|
||||||
stopListeningToAnimatedNodeValue(nativeTag);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
__onAnimatedValueUpdateReceived(value: number): void {
|
__onAnimatedValueUpdateReceived(value: number): void {
|
||||||
this.__callListeners(value);
|
this.__callListeners(value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
|
||||||
|
import type {PlatformConfig} from '../AnimatedPlatformConfig';
|
||||||
import type Animation, {EndCallback} from '../animations/Animation';
|
import type Animation, {EndCallback} from '../animations/Animation';
|
||||||
import type {InterpolationConfigType} from './AnimatedInterpolation';
|
import type {InterpolationConfigType} from './AnimatedInterpolation';
|
||||||
import type AnimatedNode from './AnimatedNode';
|
import type AnimatedNode from './AnimatedNode';
|
||||||
@@ -85,6 +85,9 @@ function _executeAsAnimatedBatch(id: string, operation: () => void) {
|
|||||||
* See https://reactnative.dev/docs/animatedvalue
|
* See https://reactnative.dev/docs/animatedvalue
|
||||||
*/
|
*/
|
||||||
export default class AnimatedValue extends AnimatedWithChildren {
|
export default class AnimatedValue extends AnimatedWithChildren {
|
||||||
|
#listenerCount: number = 0;
|
||||||
|
#updateSubscription: ?EventSubscription = null;
|
||||||
|
|
||||||
_value: number;
|
_value: number;
|
||||||
_startingValue: number;
|
_startingValue: number;
|
||||||
_offset: number;
|
_offset: number;
|
||||||
@@ -118,6 +121,67 @@ export default class AnimatedValue extends AnimatedWithChildren {
|
|||||||
return this._value + this._offset;
|
return this._value + this._offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__makeNative(platformConfig: ?PlatformConfig): void {
|
||||||
|
super.__makeNative(platformConfig);
|
||||||
|
if (this.#listenerCount > 0) {
|
||||||
|
this.#ensureUpdateSubscriptionExists();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addListener(callback: (value: any) => mixed): string {
|
||||||
|
const id = super.addListener(callback);
|
||||||
|
this.#listenerCount++;
|
||||||
|
if (this.__isNative) {
|
||||||
|
this.#ensureUpdateSubscriptionExists();
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
removeListener(id: string): void {
|
||||||
|
super.removeListener(id);
|
||||||
|
this.#listenerCount--;
|
||||||
|
if (this.__isNative && this.#listenerCount === 0) {
|
||||||
|
this.#updateSubscription?.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
removeAllListeners(): void {
|
||||||
|
super.removeAllListeners();
|
||||||
|
this.#listenerCount = 0;
|
||||||
|
if (this.__isNative) {
|
||||||
|
this.#updateSubscription?.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ensureUpdateSubscriptionExists(): void {
|
||||||
|
if (this.#updateSubscription != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const nativeTag = this.__getNativeTag();
|
||||||
|
NativeAnimatedAPI.startListeningToAnimatedNodeValue(nativeTag);
|
||||||
|
const subscription: EventSubscription =
|
||||||
|
NativeAnimatedHelper.nativeEventEmitter.addListener(
|
||||||
|
'onAnimatedValueUpdate',
|
||||||
|
data => {
|
||||||
|
if (data.tag === nativeTag) {
|
||||||
|
this.__onAnimatedValueUpdateReceived(data.value);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
this.#updateSubscription = {
|
||||||
|
remove: () => {
|
||||||
|
// Only this function assigns to `this.#updateSubscription`.
|
||||||
|
if (this.#updateSubscription == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.#updateSubscription = null;
|
||||||
|
subscription.remove();
|
||||||
|
NativeAnimatedAPI.stopListeningToAnimatedNodeValue(nativeTag);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Directly set the value. This will stop any animations running on the value
|
* Directly set the value. This will stop any animations running on the value
|
||||||
* and update all the bound properties.
|
* and update all the bound properties.
|
||||||
|
|||||||
@@ -1168,6 +1168,10 @@ declare export default class AnimatedValue extends AnimatedWithChildren {
|
|||||||
constructor(value: number, config?: ?AnimatedValueConfig): void;
|
constructor(value: number, config?: ?AnimatedValueConfig): void;
|
||||||
__detach(): void;
|
__detach(): void;
|
||||||
__getValue(): number;
|
__getValue(): number;
|
||||||
|
__makeNative(platformConfig: ?PlatformConfig): void;
|
||||||
|
addListener(callback: (value: any) => mixed): string;
|
||||||
|
removeListener(id: string): void;
|
||||||
|
removeAllListeners(): void;
|
||||||
setValue(value: number): void;
|
setValue(value: number): void;
|
||||||
setOffset(offset: number): void;
|
setOffset(offset: number): void;
|
||||||
flattenOffset(): void;
|
flattenOffset(): void;
|
||||||
|
|||||||
Reference in new issue
Block a user