Skip to content

Commit 39fa276

Browse files
Martin Boothfacebook-github-bot
Martin Booth
authored andcommitted
Sync offset value to native immediately (facebook#50941)
Summary: Pull Request resolved: facebook#50941 Without doing this, using Animated.event to update a value with an offset causes the value to revert to not having an offset because the native side doesn't even know about the offset if it hasn't been synced. Don't think there's a better place to sync this for the cases where an animation is kicked off entirely from the native side Changelog: [Android][Fixed] - Ensure latest offset value is synced to native Reviewed By: javache Differential Revision: D73622302
1 parent 319ba0a commit 39fa276

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/react-native/Libraries/Animated/__tests__/AnimatedValue-test.js

+38
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('AnimatedValue', () => {
3838
removeListeners: jest.fn(),
3939
startListeningToAnimatedNodeValue: jest.fn(),
4040
stopListeningToAnimatedNodeValue: jest.fn(),
41+
extractAnimatedNodeOffset: jest.fn(),
4142
// ...
4243
},
4344
}));
@@ -49,6 +50,8 @@ describe('AnimatedValue', () => {
4950
jest.spyOn(NativeAnimatedHelper.API, 'createAnimatedNode');
5051
jest.spyOn(NativeAnimatedHelper.API, 'dropAnimatedNode');
5152
jest.spyOn(NativeAnimatedHelper.API, 'startListeningToAnimatedNodeValue');
53+
jest.spyOn(NativeAnimatedHelper.API, 'setWaitingForIdentifier');
54+
jest.spyOn(NativeAnimatedHelper.API, 'unsetWaitingForIdentifier');
5255
});
5356

5457
it('emits update events for listeners added', () => {
@@ -161,4 +164,39 @@ describe('AnimatedValue', () => {
161164
).toBeCalledTimes(0);
162165
});
163166
});
167+
168+
describe('when extractOffset is called', () => {
169+
it('flushes changes to native immediately when native', () => {
170+
const node = new AnimatedValue(0, {useNativeDriver: true});
171+
172+
expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes(
173+
0,
174+
);
175+
expect(
176+
NativeAnimatedHelper.API.unsetWaitingForIdentifier,
177+
).toBeCalledTimes(0);
178+
179+
node.extractOffset();
180+
181+
expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes(
182+
1,
183+
);
184+
expect(
185+
NativeAnimatedHelper.API.unsetWaitingForIdentifier,
186+
).toBeCalledTimes(1);
187+
});
188+
189+
it('does not flush changes when not native', () => {
190+
const node = new AnimatedValue(0, {useNativeDriver: false});
191+
192+
node.extractOffset();
193+
194+
expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes(
195+
0,
196+
);
197+
expect(
198+
NativeAnimatedHelper.API.unsetWaitingForIdentifier,
199+
).toBeCalledTimes(0);
200+
});
201+
});
164202
});

packages/react-native/Libraries/Animated/nodes/AnimatedValue.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ export default class AnimatedValue extends AnimatedWithChildren {
247247
this._offset += this._value;
248248
this._value = 0;
249249
if (this.__isNative) {
250-
NativeAnimatedAPI.extractAnimatedNodeOffset(this.__getNativeTag());
250+
_executeAsAnimatedBatch(this.__getNativeTag().toString(), () =>
251+
NativeAnimatedAPI.extractAnimatedNodeOffset(this.__getNativeTag()),
252+
);
251253
}
252254
}
253255

0 commit comments

Comments
 (0)