Skip to content

Commit 63a5a70

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
refactor native animated to pass folly::dynamic to fabric
Differential Revision: D74738548
1 parent 249a24a commit 63a5a70

File tree

12 files changed

+68
-22
lines changed

12 files changed

+68
-22
lines changed

packages/react-native/Libraries/NativeAnimation/Nodes/RCTPropsAnimatedNode.mm

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import <React/RCTStyleAnimatedNode.h>
1414
#import <React/RCTUIManager.h>
1515
#import <React/RCTValueAnimatedNode.h>
16+
#import <react/utils/FollyConvert.h>
1617

1718
@implementation RCTPropsAnimatedNode {
1819
NSNumber *_connectedViewTag;
@@ -61,9 +62,12 @@ - (void)updateView
6162
{
6263
if (_managedByFabric) {
6364
if (_bridge.surfacePresenter) {
64-
[_bridge.surfacePresenter synchronouslyUpdateViewOnUIThread:_connectedViewTag props:_propsDictionary];
65+
[_bridge.surfacePresenter
66+
synchronouslyUpdateViewOnUIThread:_connectedViewTag.integerValue
67+
props:facebook::react::convertIdToFollyDynamic(_propsDictionary)];
6568
} else {
66-
[_surfacePresenter synchronouslyUpdateViewOnUIThread:_connectedViewTag props:_propsDictionary];
69+
[_surfacePresenter synchronouslyUpdateViewOnUIThread:_connectedViewTag.integerValue
70+
props:facebook::react::convertIdToFollyDynamic(_propsDictionary)];
6771
}
6872
} else {
6973
[_bridge.uiManager synchronouslyUpdateViewOnUIThread:_connectedViewTag

packages/react-native/React/Base/RCTViewRegistry.m

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#import <React/RCTSurfacePresenterStub.h>
98
#import <React/RCTUIManager.h>
109

1110
#import "RCTBridge.h"

packages/react-native/React/CxxUtils/RCTFollyConvert.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace facebook::react {
1616
folly::dynamic convertIdToFollyDynamic(id json);
1717
[[deprecated(
1818
"This function is deprecated, please use /ReactCommon/react/utils/platform/ios/react/utils/FollyConvert.h instead")]]
19-
id convertFollyDynamicToId(const folly::dynamic& dyn);
19+
id convertFollyDynamicToId(const folly::dynamic &dyn);
20+
NSArray<NSString *> *extractKeysFromFollyDynamic(const folly::dynamic &dyn);
2021

2122
} // namespace facebook::react

packages/react-native/React/CxxUtils/RCTFollyConvert.mm

+16
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,20 @@ id convertFollyDynamicToId(const folly::dynamic &dyn)
113113
return nil;
114114
}
115115

116+
NSArray<NSString *> *extractKeysFromFollyDynamic(const folly::dynamic &dyn)
117+
{
118+
NSMutableArray<NSString *> *result = [NSMutableArray new];
119+
120+
if (dyn.type() == folly::dynamic::OBJECT) {
121+
for (const auto &elem : dyn.items()) {
122+
NSString *key = [[NSString alloc] initWithBytes:elem.first.c_str()
123+
length:elem.first.size()
124+
encoding:NSUTF8StringEncoding];
125+
[result addObject:key];
126+
}
127+
}
128+
129+
return result;
130+
}
131+
116132
} // namespace facebook::react

packages/react-native/React/Fabric/Mounting/RCTMountingManager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ NS_ASSUME_NONNULL_BEGIN
6767
forShadowView:(const facebook::react::ShadowView &)shadowView;
6868

6969
- (void)synchronouslyUpdateViewOnUIThread:(ReactTag)reactTag
70-
changedProps:(NSDictionary *)props
70+
changedProps:(folly::dynamic)props
7171
componentDescriptor:(const facebook::react::ComponentDescriptor &)componentDescriptor;
7272
@end
7373

packages/react-native/React/Fabric/Mounting/RCTMountingManager.mm

+14-5
Original file line numberDiff line numberDiff line change
@@ -282,25 +282,34 @@ - (void)setIsJSResponder:(BOOL)isJSResponder
282282
}
283283

284284
- (void)synchronouslyUpdateViewOnUIThread:(ReactTag)reactTag
285-
changedProps:(NSDictionary *)props
285+
changedProps:(folly::dynamic)props
286286
componentDescriptor:(const ComponentDescriptor &)componentDescriptor
287287
{
288288
RCTAssertMainQueue();
289+
NSArray<NSString *> *propsKeysToBeUpdated = extractKeysFromFollyDynamic(props);
290+
bool updatesTransform = props.find("transform") != props.items().end();
291+
bool updatesOpacity = props.find("opacity") != props.items().end();
292+
289293
UIView<RCTComponentViewProtocol> *componentView = [_componentViewRegistry findComponentViewWithTag:reactTag];
294+
if (!componentView) {
295+
RCTLogWarn(@"Attempted to update view with tag %ld, but it no longer exists", (long)reactTag);
296+
return;
297+
}
298+
290299
SurfaceId surfaceId = RCTSurfaceIdForView(componentView);
291300
Props::Shared oldProps = [componentView props];
292301
Props::Shared newProps = componentDescriptor.cloneProps(
293-
PropsParserContext{surfaceId, *_contextContainer.get()}, oldProps, RawProps(convertIdToFollyDynamic(props)));
302+
PropsParserContext{surfaceId, *_contextContainer.get()}, oldProps, RawProps(std::move(props)));
294303

295304
NSSet<NSString *> *propKeys = componentView.propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN ?: [NSSet new];
296-
propKeys = [propKeys setByAddingObjectsFromArray:props.allKeys];
305+
propKeys = [propKeys setByAddingObjectsFromArray:propsKeysToBeUpdated];
297306
componentView.propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = nil;
298307
[componentView updateProps:newProps oldProps:oldProps];
299308
componentView.propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = propKeys;
300309

301310
const auto &newViewProps = static_cast<const ViewProps &>(*newProps);
302311

303-
if (props[@"transform"]) {
312+
if (updatesTransform) {
304313
auto layoutMetrics = LayoutMetrics();
305314
layoutMetrics.frame.size.width = componentView.layer.bounds.size.width;
306315
layoutMetrics.frame.size.height = componentView.layer.bounds.size.height;
@@ -309,7 +318,7 @@ - (void)synchronouslyUpdateViewOnUIThread:(ReactTag)reactTag
309318
componentView.layer.transform = newTransform;
310319
}
311320
}
312-
if (props[@"opacity"] && componentView.layer.opacity != (float)newViewProps.opacity) {
321+
if (updatesOpacity && componentView.layer.opacity != (float)newViewProps.opacity) {
313322
componentView.layer.opacity = newViewProps.opacity;
314323
}
315324

packages/react-native/React/Fabric/RCTSurfacePresenter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ NS_ASSUME_NONNULL_BEGIN
6565

6666
- (nullable RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag;
6767

68-
- (BOOL)synchronouslyUpdateViewOnUIThread:(NSNumber *)reactTag props:(NSDictionary *)props;
68+
- (void)synchronouslyUpdateViewOnUIThread:(ReactTag)reactTag props:(folly::dynamic)props;
6969

7070
- (void)setupAnimationDriverWithSurfaceHandler:(const facebook::react::SurfaceHandler &)surfaceHandler;
7171

packages/react-native/React/Fabric/RCTSurfacePresenter.mm

+8-9
Original file line numberDiff line numberDiff line change
@@ -148,28 +148,27 @@ - (UIView *)findComponentViewWithTag_DO_NOT_USE_DEPRECATED:(NSInteger)tag
148148
return componentView;
149149
}
150150

151-
- (BOOL)synchronouslyUpdateViewOnUIThread:(NSNumber *)reactTag props:(NSDictionary *)props
151+
- (void)synchronouslyUpdateViewOnUIThread:(ReactTag)reactTag props:(folly::dynamic)props
152152
{
153153
RCTScheduler *scheduler = [self scheduler];
154154
if (!scheduler) {
155-
return NO;
155+
return;
156156
}
157-
158-
ReactTag tag = [reactTag integerValue];
159157
UIView<RCTComponentViewProtocol> *componentView =
160-
[_mountingManager.componentViewRegistry findComponentViewWithTag:tag];
158+
[_mountingManager.componentViewRegistry findComponentViewWithTag:reactTag];
161159
if (componentView == nil) {
162-
return NO; // This view probably isn't managed by Fabric
160+
return; // This view probably isn't managed by Fabric
163161
}
164162
ComponentHandle handle = [[componentView class] componentDescriptorProvider].handle;
165163
auto *componentDescriptor = [scheduler findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN:handle];
166164

167165
if (!componentDescriptor) {
168-
return YES;
166+
return;
169167
}
170168

171-
[_mountingManager synchronouslyUpdateViewOnUIThread:tag changedProps:props componentDescriptor:*componentDescriptor];
172-
return YES;
169+
[_mountingManager synchronouslyUpdateViewOnUIThread:reactTag
170+
changedProps:std::move(props)
171+
componentDescriptor:*componentDescriptor];
173172
}
174173

175174
- (void)setupAnimationDriverWithSurfaceHandler:(const facebook::react::SurfaceHandler &)surfaceHandler

packages/react-native/React/Modules/RCTSurfacePresenterStub.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#import <React/RCTBridge.h>
1111
#import <React/RCTBridgeProxy.h>
12+
#import <folly/dynamic.h>
1213

1314
@protocol RCTSurfaceProtocol;
1415

@@ -32,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN
3233
- (id<RCTSurfaceProtocol>)createFabricSurfaceForModuleName:(NSString *)moduleName
3334
initialProperties:(NSDictionary *)initialProperties;
3435
- (nullable UIView *)findComponentViewWithTag_DO_NOT_USE_DEPRECATED:(NSInteger)tag;
35-
- (BOOL)synchronouslyUpdateViewOnUIThread:(NSNumber *)reactTag props:(NSDictionary *)props;
36+
- (void)synchronouslyUpdateViewOnUIThread:(NSInteger)reactTag props:(folly::dynamic)props;
3637
- (void)addObserver:(id<RCTSurfacePresenterObserver>)observer;
3738
- (void)removeObserver:(id<RCTSurfacePresenterObserver>)observer;
3839

packages/react-native/ReactCommon/react/utils/platform/ios/react/utils/FollyConvert.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace facebook::react {
1313

1414
folly::dynamic convertIdToFollyDynamic(id json);
15-
id convertFollyDynamicToId(const folly::dynamic& dyn);
15+
id convertFollyDynamicToId(const folly::dynamic &dyn);
16+
NSArray<NSString *> *extractKeysFromFollyDynamic(const folly::dynamic &dyn);
1617

1718
} // namespace facebook::react

packages/react-native/ReactCommon/react/utils/platform/ios/react/utils/FollyConvert.mm

+16
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,20 @@ id convertFollyDynamicToId(const folly::dynamic &dyn)
113113
return nil;
114114
}
115115

116+
NSArray<NSString *> *extractKeysFromFollyDynamic(const folly::dynamic &dyn)
117+
{
118+
NSMutableArray<NSString *> *result = [NSMutableArray new];
119+
120+
if (dyn.type() == folly::dynamic::OBJECT) {
121+
for (const auto &elem : dyn.items()) {
122+
NSString *key = [[NSString alloc] initWithBytes:elem.first.c_str()
123+
length:elem.first.size()
124+
encoding:NSUTF8StringEncoding];
125+
[result addObject:key];
126+
}
127+
}
128+
129+
return result;
130+
}
131+
116132
} // namespace facebook::react

0 commit comments

Comments
 (0)