From c39a4c7f2ee2a81be2ea967a2eb84340f97ede84 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Tue, 3 Aug 2021 11:31:08 -0700 Subject: [PATCH] Flush operations queue when animation starts Summary: changelog: [internal] If nodesManager has the information if animated node is managed by Fabric, we can't decide if the operation queue should be flushed before it is flushed. Therefore, keep the information about animated nodes inside a set instead of nodesManager. For simplicity, I will refer to class `RCTNativeAnimatedTurboModule` as *NativeAnimated* and to `RCTNativeAnimatedNodesManager` as *NodesManager* Notice that each call to *NativeAnimated* is queued up in `_operations` or `_preOperations`. When the queues are flushed, only then methods are called on `RCTNativeAnimatedNodesManager`. There are two mechanisms that flush operations. One is triggered by `RCTMountingManager` before mounting operations are applied and after they are applied. This works fine but is important to paint the picture. The second mechanism is inside `[RCTNativeAnimatedTurboModule startAnimatingNode]`. It flushes the queues for Fabric nodes only (not sure why Fabric nodes only, I couldn't find any explanation in old diffs). It checks with *NativeAnimated* if a node is managed by Fabric. Keep in mind, *NodesManager* only knows about the nodes when the queues have been flushed. Exampe: JavaScript calls methods on *NativeAnimated*. For example: 1. `createNode` 2. `connectAnimatedNodeToView` 3. `startAnimatingNode`. (here, the queues should be flushed, since we are in Fabric) All of these operations are queued up and for as long as `RCTMountingManager` executes mounting, all proceeds as expected. But if those operations happen after mounting phase, `startAnimatingNode` will not flush the operations queues, because it can't tell if nodeTag is managed by fabric or it isn't. This is because *NodesManager* hasn't been notified about any new nodes. Reviewed By: JoshuaGross, p-sun Differential Revision: D30053890 fbshipit-source-id: b7fe24861d5300f9cfefa813a53df8330fa56d86 --- .../RCTNativeAnimatedTurboModule.mm | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.mm b/Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.mm index 53208c1e0b7..dfb0c96990d 100644 --- a/Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.mm +++ b/Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.mm @@ -26,6 +26,9 @@ typedef void (^AnimatedOperation)(RCTNativeAnimatedNodesManager *nodesManager); // Operations called before views have been updated. NSMutableArray *_preOperations; NSMutableDictionary *_animIdIsManagedByFabric; + // A set of nodeIDs managed by Fabric. + NSMutableSet *_nodeIDsManagedByFabric; + } RCT_EXPORT_MODULE(); @@ -41,6 +44,7 @@ RCT_EXPORT_MODULE(); _operations = [NSMutableArray new]; _preOperations = [NSMutableArray new]; _animIdIsManagedByFabric = [NSMutableDictionary new]; + _nodeIDsManagedByFabric = [NSMutableSet new]; } return self; } @@ -109,6 +113,9 @@ RCT_EXPORT_METHOD(createAnimatedNode:(double)tag RCT_EXPORT_METHOD(connectAnimatedNodes:(double)parentTag childTag:(double)childTag) { + if ([_nodeIDsManagedByFabric containsObject:@(childTag)]) { + [_nodeIDsManagedByFabric addObject:@(parentTag)]; + } [self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) { [nodesManager connectAnimatedNodes:[NSNumber numberWithDouble:parentTag] childTag:[NSNumber numberWithDouble:childTag]]; }]; @@ -131,16 +138,11 @@ RCT_EXPORT_METHOD(startAnimatingNode:(double)animationId [nodesManager startAnimatingNode:[NSNumber numberWithDouble:animationId] nodeTag:[NSNumber numberWithDouble:nodeTag] config:config endCallback:callBack]; }]; - RCTExecuteOnMainQueue(^{ - if (![self->_nodesManager isNodeManagedByFabric:[NSNumber numberWithDouble:nodeTag]]) { - return; - } - - RCTExecuteOnUIManagerQueue(^{ - self->_animIdIsManagedByFabric[[NSNumber numberWithDouble:animationId]] = @YES; - [self flushOperationQueues]; - }); - }); + BOOL isNodeManagedByFabric = [_nodeIDsManagedByFabric containsObject:@(nodeTag)]; + if (isNodeManagedByFabric) { + self->_animIdIsManagedByFabric[[NSNumber numberWithDouble:animationId]] = @YES; + [self flushOperationQueues]; + } } RCT_EXPORT_METHOD(stopAnimation:(double)animationId) @@ -186,6 +188,9 @@ RCT_EXPORT_METHOD(extractAnimatedNodeOffset:(double)nodeTag) RCT_EXPORT_METHOD(connectAnimatedNodeToView:(double)nodeTag viewTag:(double)viewTag) { + if (RCTUIManagerTypeForTagIsFabric(@(viewTag))) { + [_nodeIDsManagedByFabric addObject:@(nodeTag)]; + } NSString *viewName = [self.bridge.uiManager viewNameForReactTag:[NSNumber numberWithDouble:viewTag]]; [self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) { [nodesManager connectAnimatedNodeToView:[NSNumber numberWithDouble:nodeTag] viewTag:[NSNumber numberWithDouble:viewTag] viewName:viewName];