mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary:
If a NativeModule requires main queue setup, its `getConstants()` method must be executed on the main thead. The legacy NativeModule infra takes care of this for us. With TurboModules, however, all synchronous methods, including `getConstants()`, execute on the JS thread. Therefore, if a TurboModule requires main queue setup, and exports constants, we must execute its `getConstants()` method body on the main queue explicitly.
**Notes:**
- The changes in this diff should be a noop when TurboModules is off, because `RCTUnsafeExecuteOnMainQueueSync` synchronously execute its block on the current thread if the current thread is the main thread.
- If a NativeModule doens't have the `requiresMainQueueSetup` method, but has the `constantsToExport` method, both NativeModules and TurboModules assume that it requires main queue setup.
## Script
```
const exec = require("../lib/exec");
const abspath = require("../lib/abspath");
const relpath = require("../lib/relpath");
const readFile = (filename) => require("fs").readFileSync(filename, "utf8");
const writeFile = (filename, content) =>
require("fs").writeFileSync(filename, content);
function main() {
const tmFiles = exec("cd ~/fbsource && xbgs -n 10000 -l constantsToExport")
.split("\n")
.filter(Boolean);
const filesWithoutConstantsToExport = [];
const filesWithConstantsToExportButNotGetConstants = [];
const filesExplicitlyNotRequiringMainQueueSetup = [];
tmFiles
.filter((filename) => {
if (filename.includes("microsoft-fork-of-react-native")) {
return false;
}
return /\.mm?$/.test(filename);
})
.map(abspath)
.forEach((filename) => {
const code = readFile(filename);
const relFilename = relpath(filename);
if (!/constantsToExport\s*{/.test(code)) {
filesWithoutConstantsToExport.push(relFilename);
return;
}
if (!/getConstants\s*{/.test(code)) {
filesWithConstantsToExportButNotGetConstants.push(relFilename);
return;
}
if (/requiresMainQueueSetup\s*{/.test(code)) {
const requiresMainQueueSetupRegex = /requiresMainQueueSetup\s*{\s*return\s+(?<requiresMainQueueSetup>YES|NO)/;
const requiresMainQueueSetupRegexMatch = requiresMainQueueSetupRegex.exec(
code
);
if (!requiresMainQueueSetupRegexMatch) {
throw new Error(
"Detected requiresMainQueueSetup method in file " +
relFilename +
" but was unable to parse the method return value"
);
}
const {
requiresMainQueueSetup,
} = requiresMainQueueSetupRegexMatch.groups;
if (requiresMainQueueSetup == "NO") {
filesExplicitlyNotRequiringMainQueueSetup.push(relFilename);
return;
}
}
const getConstantsTypeRegex = () => /-\s*\((?<type>.*)\)getConstants\s*{/;
const getConstantsTypeRegexMatch = getConstantsTypeRegex().exec(code);
if (!getConstantsTypeRegexMatch) {
throw new Error(
`Failed to parse return type of getConstants method in file ${relFilename}`
);
}
const getConstantsType = getConstantsTypeRegexMatch.groups.type;
const getConstantsBody = code
.split(getConstantsTypeRegex())[2]
.split("\n}")[0];
const newGetConstantsBody = `
__block ${getConstantsType} constants;
RCTUnsafeExecuteOnMainQueueSync(^{${getConstantsBody
.replace(/\n/g, "\n ")
.replace(/_bridge/g, "self->_bridge")
.replace(/return /g, "constants = ")}
});
return constants;
`;
writeFile(
filename,
code
.replace(getConstantsBody, newGetConstantsBody)
.replace("#import", "#import <React/RCTUtils.h>\n#import")
);
});
console.log("Files without constantsToExport: ");
filesWithoutConstantsToExport.forEach((file) => console.log(file));
console.log();
console.log("Files with constantsToExport but no getConstants: ");
filesWithConstantsToExportButNotGetConstants.forEach((file) =>
console.log(file)
);
console.log();
console.log("Files with requiresMainQueueSetup = NO: ");
filesExplicitlyNotRequiringMainQueueSetup.forEach((file) =>
console.log(file)
);
}
if (!module.parent) {
main();
}
```
Changelog: [Internal]
Reviewed By: fkgozali
Differential Revision: D21797048
fbshipit-source-id: a822a858fecdbe976e6197f8339e509dc7cd917f
142 lines
2.9 KiB
Plaintext
142 lines
2.9 KiB
Plaintext
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import "RCTSampleTurboModule.h"
|
|
|
|
#import <React/RCTUtils.h>
|
|
#import <UIKit/UIKit.h>
|
|
|
|
using namespace facebook::react;
|
|
|
|
@implementation RCTSampleTurboModule
|
|
|
|
// Backward-compatible export
|
|
RCT_EXPORT_MODULE()
|
|
|
|
@synthesize bridge = _bridge;
|
|
@synthesize turboModuleLookupDelegate = _turboModuleLookupDelegate;
|
|
|
|
// Backward-compatible queue configuration
|
|
+ (BOOL)requiresMainQueueSetup
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (dispatch_queue_t)methodQueue
|
|
{
|
|
return dispatch_get_main_queue();
|
|
}
|
|
|
|
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
{
|
|
return std::make_shared<NativeSampleTurboModuleSpecJSI>(params);
|
|
}
|
|
|
|
// Backward compatible invalidation
|
|
- (void)invalidate
|
|
{
|
|
// Actually do nothing here.
|
|
NSLog(@"Invalidating RCTSampleTurboModule...");
|
|
}
|
|
|
|
- (NSDictionary *)getConstants
|
|
{
|
|
__block NSDictionary *constants;
|
|
RCTUnsafeExecuteOnMainQueueSync(^{
|
|
UIScreen *mainScreen = UIScreen.mainScreen;
|
|
CGSize screenSize = mainScreen.bounds.size;
|
|
|
|
constants = @{
|
|
@"const1" : @YES,
|
|
@"const2" : @(screenSize.width),
|
|
@"const3" : @"something",
|
|
};
|
|
});
|
|
|
|
return constants;
|
|
}
|
|
|
|
// TODO: Remove once fully migrated to TurboModule.
|
|
- (NSDictionary *)constantsToExport
|
|
{
|
|
return [self getConstants];
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(voidFunc)
|
|
{
|
|
// Nothing to do
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getBool : (BOOL)arg)
|
|
{
|
|
return @(arg);
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNumber : (double)arg)
|
|
{
|
|
return @(arg);
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getString : (NSString *)arg)
|
|
{
|
|
return arg;
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSArray<id<NSObject>> *, getArray : (NSArray *)arg)
|
|
{
|
|
return arg;
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObject : (NSDictionary *)arg)
|
|
{
|
|
return arg;
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getRootTag : (double)arg)
|
|
{
|
|
return @(arg);
|
|
}
|
|
|
|
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getValue : (double)x y : (NSString *)y z : (NSDictionary *)z)
|
|
{
|
|
return @{
|
|
@"x" : @(x),
|
|
@"y" : y ?: [NSNull null],
|
|
@"z" : z ?: [NSNull null],
|
|
};
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(getValueWithCallback : (RCTResponseSenderBlock)callback)
|
|
{
|
|
if (!callback) {
|
|
return;
|
|
}
|
|
callback(@[ @"value from callback!" ]);
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(getValueWithPromise
|
|
: (BOOL)error resolve
|
|
: (RCTPromiseResolveBlock)resolve reject
|
|
: (RCTPromiseRejectBlock)reject)
|
|
{
|
|
if (!resolve || !reject) {
|
|
return;
|
|
}
|
|
|
|
if (error) {
|
|
reject(
|
|
@"code_1",
|
|
@"intentional promise rejection",
|
|
[NSError errorWithDomain:@"RCTSampleTurboModule" code:1 userInfo:nil]);
|
|
} else {
|
|
resolve(@"result!");
|
|
}
|
|
}
|
|
|
|
@end
|