diff --git a/packages/react-native/Libraries/Animated/__tests__/AnimatedValue-test.js b/packages/react-native/Libraries/Animated/__tests__/AnimatedValue-test.js index b78c893e2c1ff1..8e08a3ccd8bc2a 100644 --- a/packages/react-native/Libraries/Animated/__tests__/AnimatedValue-test.js +++ b/packages/react-native/Libraries/Animated/__tests__/AnimatedValue-test.js @@ -38,6 +38,7 @@ describe('AnimatedValue', () => { removeListeners: jest.fn(), startListeningToAnimatedNodeValue: jest.fn(), stopListeningToAnimatedNodeValue: jest.fn(), + extractAnimatedNodeOffset: jest.fn(), // ... }, })); @@ -49,6 +50,8 @@ describe('AnimatedValue', () => { jest.spyOn(NativeAnimatedHelper.API, 'createAnimatedNode'); jest.spyOn(NativeAnimatedHelper.API, 'dropAnimatedNode'); jest.spyOn(NativeAnimatedHelper.API, 'startListeningToAnimatedNodeValue'); + jest.spyOn(NativeAnimatedHelper.API, 'setWaitingForIdentifier'); + jest.spyOn(NativeAnimatedHelper.API, 'unsetWaitingForIdentifier'); }); it('emits update events for listeners added', () => { @@ -161,4 +164,39 @@ describe('AnimatedValue', () => { ).toBeCalledTimes(0); }); }); + + describe('when extractOffset is called', () => { + it('flushes changes to native immediately when native', () => { + const node = new AnimatedValue(0, {useNativeDriver: true}); + + expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes( + 0, + ); + expect( + NativeAnimatedHelper.API.unsetWaitingForIdentifier, + ).toBeCalledTimes(0); + + node.extractOffset(); + + expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes( + 1, + ); + expect( + NativeAnimatedHelper.API.unsetWaitingForIdentifier, + ).toBeCalledTimes(1); + }); + + it('does not flush changes when not native', () => { + const node = new AnimatedValue(0, {useNativeDriver: false}); + + node.extractOffset(); + + expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes( + 0, + ); + expect( + NativeAnimatedHelper.API.unsetWaitingForIdentifier, + ).toBeCalledTimes(0); + }); + }); }); diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js b/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js index 1727b9f463bcb5..56ee801799fd78 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedValue.js @@ -247,7 +247,9 @@ export default class AnimatedValue extends AnimatedWithChildren { this._offset += this._value; this._value = 0; if (this.__isNative) { - NativeAnimatedAPI.extractAnimatedNodeOffset(this.__getNativeTag()); + _executeAsAnimatedBatch(this.__getNativeTag().toString(), () => + NativeAnimatedAPI.extractAnimatedNodeOffset(this.__getNativeTag()), + ); } }