Files
react-native/React/CoreModules/RCTLogBox.mm
T
Paige Sun 37e5fa3a6c Refactor: Migrate Logbox surface initialization to Fabric when available, in Bridge and Bridgeless modes
Summary:
Changelog: [iOS][Internal] Refactor: Migrate Logbox surface initialization to Fabric when available, in Bridge and Bridgeless modes

# Why
This diff main purpose is to add `RCTErrorNewArchitectureValidation(RCTNotAllowedInAppWideFabric)` in `RCTSurface`, to ensure Paper surfaces are never created in FBiOS.

# The Situation
Before this diff, in Bridged Fabric, `[RCTLogbox show]` initializes a Paper `RCTSurface`, [using `[RCTLogBoxView initWithWindow]`](https://github.com/facebook/react-native/blob/main/React/CoreModules/RCTLogBoxView.mm#L46))
In this diff,  in Bridged and Bridgeless Fabric, `[RCTLogbox show]` initializes a Fabric `RCTFabricSurface`.

Before this diff, in Bridgeless Fabric,  RCTLogBox posts a "CreateLogBoxSurface" notification to RCTInstance.
In this diff, the notification hack is replaced by the same `RCTFabricSurface` initialization above.
Behavior is the same.

Reviewed By: RSNara

Differential Revision: D35177311

fbshipit-source-id: 6de418af8a01f914c9a806bb8d74915015f9087a
2022-03-29 18:40:19 -07:00

137 lines
3.0 KiB
Plaintext

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTLogBox.h"
#import <FBReactNativeSpec/FBReactNativeSpec.h>
#import <React/RCTBridge.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTLog.h>
#import <React/RCTRedBoxSetEnabled.h>
#import <React/RCTSurface.h>
#import "CoreModulesPlugins.h"
#if RCT_DEV_MENU
@interface RCTLogBox () <NativeLogBoxSpec, RCTBridgeModule>
@end
@implementation RCTLogBox {
RCTLogBoxView *_view;
__weak id<RCTSurfacePresenterStub> _bridgelessSurfacePresenter;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
- (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
{
_bridgelessSurfacePresenter = surfacePresenter;
}
RCT_EXPORT_METHOD(show)
{
if (RCTRedBoxGetEnabled()) {
__weak RCTLogBox *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong RCTLogBox *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
if (strongSelf->_view) {
[strongSelf->_view show];
return;
}
if (strongSelf->_bridgelessSurfacePresenter) {
strongSelf->_view = [[RCTLogBoxView alloc] initWithFrame:RCTKeyWindow().frame
surfacePresenter:strongSelf->_bridgelessSurfacePresenter];
} else if (strongSelf->_bridge && strongSelf->_bridge.valid) {
if (strongSelf->_bridge.surfacePresenter) {
strongSelf->_view = [[RCTLogBoxView alloc] initWithFrame:RCTKeyWindow().frame
surfacePresenter:strongSelf->_bridge.surfacePresenter];
} else {
strongSelf->_view = [[RCTLogBoxView alloc] initWithWindow:RCTKeyWindow() bridge:strongSelf->_bridge];
}
}
[strongSelf->_view show];
});
}
}
RCT_EXPORT_METHOD(hide)
{
if (RCTRedBoxGetEnabled()) {
__weak RCTLogBox *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong RCTLogBox *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
[strongSelf->_view setHidden:YES];
strongSelf->_view = nil;
});
}
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeLogBoxSpecJSI>(params);
}
- (void)setRCTLogBoxView:(RCTLogBoxView *)view
{
self->_view = view;
}
@end
#else // Disabled
@interface RCTLogBox () <NativeLogBoxSpec>
@end
@implementation RCTLogBox
+ (NSString *)moduleName
{
return nil;
}
- (void)show
{
// noop
}
- (void)hide
{
// noop
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeLogBoxSpecJSI>(params);
}
@end
#endif
Class RCTLogBoxCls(void)
{
return RCTLogBox.class;
}