- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)delegate
bundleURL:(NSURL *)bundleURL
moduleProvider:(RCTBridgeModuleListProvider)block
launchOptions:(NSDictionary *)launchOptions
{
if (self = [super init]) {
_delegate = delegate;
_bundleURL = bundleURL;
_moduleProvider = block;
_launchOptions = [launchOptions copy];
[self setUp];
}
return self;
}
其中 delegate 是 nil。主要的过程在 setUp
里。
setUp 过程
Class bridgeClass = self.bridgeClass;
self.batchedBridge = [[bridgeClass alloc] initWithParentBridge:self];
[self.batchedBridge start];
主要是找到 Bridge 对应的类,然后初始化,然后调用 Bridge 的 start
函数。
找到 Bridge 对应的类
- (Class)bridgeClass
{
// In order to facilitate switching between bridges with only build
// file changes, this uses reflection to check which bridges are
// available. This is a short-term hack until RCTBatchedBridge is
// removed.
Class batchedBridgeClass = objc_lookUpClass("RCTBatchedBridge");
Class cxxBridgeClass = objc_lookUpClass("RCTCxxBridge");
Class implClass = nil;
if ([self.delegate respondsToSelector:@selector(shouldBridgeUseCxxBridge:)]) {
if ([self.delegate shouldBridgeUseCxxBridge:self]) {
implClass = cxxBridgeClass;
} else {
implClass = batchedBridgeClass;
}
} else if (cxxBridgeClass != nil) {
implClass = cxxBridgeClass;
} else if (batchedBridgeClass != nil) {
implClass = batchedBridgeClass;
}
RCTAssert(implClass != nil, @"No bridge implementation is available, giving up.");
return implClass;
}
可以简化为看有没有 RCTCxxBridge
这个类,然后看下有没有 RCTBatchedBridge
这个类。最终结果是 RCTCxxBridge
这个类。
因为 RCTCxxBridge
是 C++ 文件,所以要加入 -lc++
的编译选项。
初始化 RCTCxxBridge
_pendingCalls = [NSMutableArray new];
_displayLink = [RCTDisplayLink new];
[RCTBridge setCurrentBridge:self];
初始化了一个数组,存储的是 dispatch_block_t
。
创建了一个 RCTDisplayLink
,即一个定时器,定时调用 _jsThreadUpdate
函数。
设置 RCTBridge
的 RCTCurrentBridgeInstance
是刚刚生成的实例。
调用 start
函数
开启一个 JS 线程,命名为 com.facebook.react.JavaScript
。
_jsThread = [[NSThread alloc] initWithTarget:self
selector:@selector(runJSRunLoop)
object:nil];
_jsThread.name = RCTJSThreadName;
_jsThread.qualityOfService = NSOperationQualityOfServiceUserInteractive;
[_jsThread start];
初始化不能被延迟初始化的 Native 模块。
[self _initModulesWithDispatchGroup:prepareBridge];
.....
加载 JS Bundle 中的代码
[self loadSource:^(NSError *error, NSData *source, int64_t sourceLength) {
执行代码
[strongSelf executeSourceCode:sourceCode sync:NO];
时间: 2024-10-16 07:33:15