mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-26 05:15:49 +08:00
Refactored module access to allow for lazy loading
Summary: public The `bridge.modules` dictionary provides access to all native modules, but this API requires that every module is initialized in advance so that any module can be accessed. This diff introduces a better API that will allow modules to be initialized lazily as they are needed, and deprecates `bridge.modules` (modules that use it will still work, but should be rewritten to use `bridge.moduleClasses` or `-[bridge moduleForName/Class:` instead. The rules are now as follows: * Any module that overrides `init` or `setBridge:` will be initialized on the main thread when the bridge is created * Any module that implements `constantsToExport:` will be initialized later when the config is exported (the module itself will be initialized on a background queue, but `constantsToExport:` will still be called on the main thread. * All other modules will be initialized lazily when a method is first called on them. These rules may seem slightly arcane, but they have the advantage of not violating any assumptions that may have been made by existing code - any module written under the original assumption that it would be initialized synchronously on the main thread when the bridge is created should still function exactly the same, but modules that avoid overriding `init` or `setBridge:` will now be loaded lazily. I've rewritten most of the standard modules to take advantage of this new lazy loading, with the following results: Out of the 65 modules included in UIExplorer: * 16 are initialized on the main thread when the bridge is created * A further 8 are initialized when the config is exported to JS * The remaining 41 will be initialized lazily on-demand Reviewed By: jspahrsummers Differential Revision: D2677695 fb-gh-sync-id: 507ae7e9fd6b563e89292c7371767c978e928f33
This commit is contained in:
committed by
facebook-github-bot-7
parent
bba71f146d
commit
060664fd3d
@@ -18,7 +18,6 @@
|
||||
#import "RCTJavaScriptLoader.h"
|
||||
#import "RCTLog.h"
|
||||
#import "RCTModuleData.h"
|
||||
#import "RCTModuleMap.h"
|
||||
#import "RCTPerformanceLogger.h"
|
||||
#import "RCTProfile.h"
|
||||
#import "RCTSourceCode.h"
|
||||
@@ -48,6 +47,8 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
+ (instancetype)currentBridge;
|
||||
+ (void)setCurrentBridge:(RCTBridge *)bridge;
|
||||
|
||||
@property (nonatomic, copy, readonly) RCTBridgeModuleProviderBlock moduleProvider;
|
||||
|
||||
@end
|
||||
|
||||
@interface RCTBatchedBridge : RCTBridge
|
||||
@@ -63,10 +64,16 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
BOOL _wasBatchActive;
|
||||
__weak id<RCTJavaScriptExecutor> _javaScriptExecutor;
|
||||
NSMutableArray<NSArray *> *_pendingCalls;
|
||||
NSMutableArray<RCTModuleData *> *_moduleDataByID;
|
||||
RCTModuleMap *_modulesByName;
|
||||
NSMutableDictionary<NSString *, RCTModuleData *> *_moduleDataByName;
|
||||
NSArray<RCTModuleData *> *_moduleDataByID;
|
||||
NSDictionary<NSString *, id<RCTBridgeModule>> *_modulesByName_DEPRECATED;
|
||||
NSArray<Class> *_moduleClassesByID;
|
||||
CADisplayLink *_jsDisplayLink;
|
||||
NSMutableSet<RCTModuleData *> *_frameUpdateObservers;
|
||||
|
||||
// Bridge startup stats (TODO: capture in perf logger)
|
||||
NSUInteger _syncInitializedModules;
|
||||
NSUInteger _asyncInitializedModules;
|
||||
}
|
||||
|
||||
- (instancetype)initWithParentBridge:(RCTBridge *)bridge
|
||||
@@ -86,7 +93,6 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
_valid = YES;
|
||||
_loading = YES;
|
||||
_pendingCalls = [NSMutableArray new];
|
||||
_moduleDataByID = [NSMutableArray new];
|
||||
_frameUpdateObservers = [NSMutableSet new];
|
||||
_jsDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_jsThreadUpdate:)];
|
||||
|
||||
@@ -106,6 +112,8 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
dispatch_queue_t bridgeQueue = dispatch_queue_create("com.facebook.react.RCTBridgeQueue", DISPATCH_QUEUE_CONCURRENT);
|
||||
|
||||
dispatch_group_t initModulesAndLoadSource = dispatch_group_create();
|
||||
|
||||
// Asynchronously load source code
|
||||
dispatch_group_enter(initModulesAndLoadSource);
|
||||
__weak RCTBatchedBridge *weakSelf = self;
|
||||
__block NSData *sourceCode;
|
||||
@@ -120,9 +128,13 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
dispatch_group_leave(initModulesAndLoadSource);
|
||||
}];
|
||||
|
||||
// Synchronously initialize all native modules
|
||||
// Synchronously initialize all native modules that cannot be deferred
|
||||
[self initModules];
|
||||
|
||||
#if RCT_DEBUG
|
||||
_syncInitializedModules = [[_moduleDataByID valueForKeyPath:@"@sum.hasInstance"] integerValue];
|
||||
#endif
|
||||
|
||||
if (RCTProfileIsProfiling()) {
|
||||
// Depends on moduleDataByID being loaded
|
||||
RCTProfileHookModules(self);
|
||||
@@ -132,22 +144,32 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
dispatch_group_enter(initModulesAndLoadSource);
|
||||
dispatch_async(bridgeQueue, ^{
|
||||
dispatch_group_t setupJSExecutorAndModuleConfig = dispatch_group_create();
|
||||
|
||||
// Asynchronously initialize the JS executor
|
||||
dispatch_group_async(setupJSExecutorAndModuleConfig, bridgeQueue, ^{
|
||||
[weakSelf setupExecutor];
|
||||
[weakSelf setUpExecutor];
|
||||
});
|
||||
|
||||
// Asynchronously gather the module config
|
||||
dispatch_group_async(setupJSExecutorAndModuleConfig, bridgeQueue, ^{
|
||||
if (weakSelf.isValid) {
|
||||
|
||||
RCTPerformanceLoggerStart(RCTPLNativeModulePrepareConfig);
|
||||
config = [weakSelf moduleConfig];
|
||||
RCTPerformanceLoggerEnd(RCTPLNativeModulePrepareConfig);
|
||||
|
||||
#if RCT_DEBUG
|
||||
NSInteger total = [[_moduleDataByID valueForKeyPath:@"@sum.hasInstance"] integerValue];
|
||||
_asyncInitializedModules = total - _syncInitializedModules;
|
||||
#endif
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
dispatch_group_notify(setupJSExecutorAndModuleConfig, bridgeQueue, ^{
|
||||
// We're not waiting for this complete to leave the dispatch group, since
|
||||
// injectJSONConfiguration and executeSourceCode will schedule operations on the
|
||||
// same queue anyway.
|
||||
// We're not waiting for this to complete to leave dispatch group, since
|
||||
// injectJSONConfiguration and executeSourceCode will schedule operations
|
||||
// on the same queue anyway.
|
||||
RCTPerformanceLoggerStart(RCTPLNativeModuleInjectConfig);
|
||||
[weakSelf injectJSONConfiguration:config onComplete:^(NSError *error) {
|
||||
RCTPerformanceLoggerEnd(RCTPLNativeModuleInjectConfig);
|
||||
@@ -199,6 +221,28 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
}
|
||||
}
|
||||
|
||||
- (NSArray<Class> *)moduleClasses
|
||||
{
|
||||
if (RCT_DEBUG && self.isValid && _moduleClassesByID == nil) {
|
||||
RCTLogError(@"Bridge modules have not yet been initialized. You may be "
|
||||
"trying to access a module too early in the startup procedure.");
|
||||
}
|
||||
return _moduleClassesByID;
|
||||
}
|
||||
|
||||
- (id)moduleForName:(NSString *)moduleName
|
||||
{
|
||||
RCTModuleData *moduleData = _moduleDataByName[moduleName];
|
||||
if (RCT_DEBUG) {
|
||||
Class moduleClass = moduleData.moduleClass;
|
||||
if (!RCTBridgeModuleClassIsRegistered(moduleClass)) {
|
||||
RCTLogError(@"Class %@ was not exported. Did you forget to use "
|
||||
"RCT_EXPORT_MODULE()?", moduleClass);
|
||||
}
|
||||
}
|
||||
return moduleData.instance;
|
||||
}
|
||||
|
||||
- (void)initModules
|
||||
{
|
||||
RCTAssertMainThread();
|
||||
@@ -220,54 +264,71 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
preregisteredModules[RCTBridgeModuleNameForClass([module class])] = module;
|
||||
}
|
||||
|
||||
// Instantiate modules
|
||||
_moduleDataByID = [NSMutableArray new];
|
||||
NSMutableDictionary *modulesByName = [preregisteredModules mutableCopy];
|
||||
SEL setBridgeSelector = NSSelectorFromString(@"setBridge:");
|
||||
IMP objectInitMethod = [NSObject instanceMethodForSelector:@selector(init)];
|
||||
|
||||
// Set up moduleData and pre-initialize module instances
|
||||
NSMutableArray<RCTModuleData *> *moduleDataByID = [NSMutableArray new];
|
||||
NSMutableDictionary<NSString *, RCTModuleData *> *moduleDataByName = [NSMutableDictionary new];
|
||||
for (Class moduleClass in RCTGetModuleClasses()) {
|
||||
NSString *moduleName = RCTBridgeModuleNameForClass(moduleClass);
|
||||
id module = preregisteredModules[moduleName];
|
||||
if (!module) {
|
||||
// Check if the module class, or any of its superclasses override init
|
||||
// or setBridge:. If they do, we assume that they are expecting to be
|
||||
// initialized when the bridge first loads.
|
||||
if ([moduleClass instanceMethodForSelector:@selector(init)] != objectInitMethod ||
|
||||
[moduleClass instancesRespondToSelector:setBridgeSelector]) {
|
||||
module = [moduleClass new];
|
||||
if (!module) {
|
||||
module = [NSNull null];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if module instance has already been registered for this name
|
||||
id<RCTBridgeModule> module = modulesByName[moduleName];
|
||||
// Check for module name collisions.
|
||||
// It's OK to have a name collision as long as the second instance is null.
|
||||
if (module != [NSNull class] && _moduleDataByName[moduleName]) {
|
||||
RCTLogError(@"Attempted to register RCTBridgeModule class %@ for the name "
|
||||
"'%@', but name was already registered by class %@", moduleClass,
|
||||
moduleName, _moduleDataByName[moduleName]);
|
||||
}
|
||||
|
||||
if (module) {
|
||||
// Preregistered instances takes precedence, no questions asked
|
||||
if (!preregisteredModules[moduleName]) {
|
||||
// It's OK to have a name collision as long as the second instance is nil
|
||||
RCTAssert([moduleClass new] == nil,
|
||||
@"Attempted to register RCTBridgeModule class %@ for the name "
|
||||
"'%@', but name was already registered by class %@", moduleClass,
|
||||
moduleName, [modulesByName[moduleName] class]);
|
||||
}
|
||||
} else {
|
||||
// Module name hasn't been used before, so go ahead and instantiate
|
||||
module = [moduleClass new];
|
||||
}
|
||||
if (module) {
|
||||
modulesByName[moduleName] = module;
|
||||
}
|
||||
// Instantiate moduleData (TODO: defer this until config generation)
|
||||
RCTModuleData *moduleData;
|
||||
if (module) {
|
||||
if (module != [NSNull null]) {
|
||||
moduleData = [[RCTModuleData alloc] initWithModuleInstance:module];
|
||||
}
|
||||
} else {
|
||||
moduleData = [[RCTModuleData alloc] initWithModuleClass:moduleClass
|
||||
bridge:self];
|
||||
}
|
||||
if (moduleData) {
|
||||
moduleDataByName[moduleName] = moduleData;
|
||||
[moduleDataByID addObject:moduleData];
|
||||
}
|
||||
}
|
||||
|
||||
// Store modules
|
||||
_modulesByName = [[RCTModuleMap alloc] initWithDictionary:modulesByName];
|
||||
_moduleDataByID = [moduleDataByID copy];
|
||||
_moduleDataByName = [moduleDataByName copy];
|
||||
_moduleClassesByID = [moduleDataByID valueForKey:@"moduleClass"];
|
||||
|
||||
/**
|
||||
* The executor is a bridge module, wait for it to be created and set it before
|
||||
* any other module has access to the bridge
|
||||
*/
|
||||
_javaScriptExecutor = _modulesByName[RCTBridgeModuleNameForClass(self.executorClass)];
|
||||
_javaScriptExecutor = [self moduleForClass:self.executorClass];
|
||||
|
||||
for (id<RCTBridgeModule> module in _modulesByName.allValues) {
|
||||
// Bridge must be set before moduleData is set up, as methodQueue
|
||||
// initialization requires it (View Managers get their queue by calling
|
||||
// self.bridge.uiManager.methodQueue)
|
||||
if ([module respondsToSelector:@selector(setBridge:)]) {
|
||||
module.bridge = self;
|
||||
for (RCTModuleData *moduleData in _moduleDataByID) {
|
||||
[moduleData setBridgeForInstance:self];
|
||||
}
|
||||
|
||||
for (RCTModuleData *moduleData in _moduleDataByID) {
|
||||
if (moduleData.hasInstance) {
|
||||
[moduleData methodQueue]; // initialize the queue
|
||||
}
|
||||
|
||||
RCTModuleData *moduleData = [[RCTModuleData alloc] initWithExecutor:_javaScriptExecutor
|
||||
moduleID:@(_moduleDataByID.count)
|
||||
instance:module];
|
||||
[_moduleDataByID addObject:moduleData];
|
||||
}
|
||||
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTDidCreateNativeModules
|
||||
@@ -275,7 +336,7 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
RCTPerformanceLoggerEnd(RCTPLNativeModuleInit);
|
||||
}
|
||||
|
||||
- (void)setupExecutor
|
||||
- (void)setUpExecutor
|
||||
{
|
||||
[_javaScriptExecutor setUp];
|
||||
}
|
||||
@@ -284,8 +345,8 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
{
|
||||
NSMutableArray<NSArray *> *config = [NSMutableArray new];
|
||||
for (RCTModuleData *moduleData in _moduleDataByID) {
|
||||
[config addObject:moduleData.config];
|
||||
if ([moduleData.instance conformsToProtocol:@protocol(RCTFrameUpdateObserver)]) {
|
||||
[config addObject:RCTNullIfNil(moduleData.config)];
|
||||
if ([moduleData.moduleClass conformsToProtocol:@protocol(RCTFrameUpdateObserver)]) {
|
||||
[_frameUpdateObservers addObject:moduleData];
|
||||
id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)moduleData.instance;
|
||||
__weak typeof(self) weakSelf = self;
|
||||
@@ -336,7 +397,7 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
|
||||
return;
|
||||
}
|
||||
|
||||
RCTSourceCode *sourceCodeModule = self.modules[RCTBridgeModuleNameForClass([RCTSourceCode class])];
|
||||
RCTSourceCode *sourceCodeModule = [self moduleForClass:[RCTSourceCode class]];
|
||||
sourceCodeModule.scriptURL = self.bundleURL;
|
||||
sourceCodeModule.scriptData = sourceCode;
|
||||
|
||||
@@ -448,13 +509,14 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
|
||||
return _valid;
|
||||
}
|
||||
|
||||
- (NSDictionary *)modules
|
||||
- (void)dispatchBlock:(dispatch_block_t)block
|
||||
queue:(dispatch_queue_t)queue
|
||||
{
|
||||
if (RCT_DEBUG && self.isValid && _modulesByName == nil) {
|
||||
RCTLogError(@"Bridge modules have not yet been initialized. You may be "
|
||||
"trying to access a module too early in the startup procedure.");
|
||||
if (queue == RCTJSThread) {
|
||||
[_javaScriptExecutor executeBlockOnJavaScriptQueue:block];
|
||||
} else if (queue) {
|
||||
dispatch_async(queue, block);
|
||||
}
|
||||
return _modulesByName;
|
||||
}
|
||||
|
||||
#pragma mark - RCTInvalidating
|
||||
@@ -475,17 +537,19 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
|
||||
|
||||
// Invalidate modules
|
||||
dispatch_group_t group = dispatch_group_create();
|
||||
for (RCTModuleData *moduleData in _moduleDataByID) {
|
||||
for (RCTModuleData *moduleData in _moduleDataByName.allValues) {
|
||||
if (moduleData.instance == _javaScriptExecutor) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ([moduleData.instance respondsToSelector:@selector(invalidate)]) {
|
||||
[moduleData dispatchBlock:^{
|
||||
dispatch_group_enter(group);
|
||||
[self dispatchBlock:^{
|
||||
[(id<RCTInvalidating>)moduleData.instance invalidate];
|
||||
} dispatchGroup:group];
|
||||
dispatch_group_leave(group);
|
||||
} queue:moduleData.methodQueue];
|
||||
}
|
||||
moduleData.queue = nil;
|
||||
[moduleData invalidate];
|
||||
}
|
||||
|
||||
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
|
||||
@@ -499,8 +563,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
|
||||
if (RCTProfileIsProfiling()) {
|
||||
RCTProfileUnhookModules(self);
|
||||
}
|
||||
_moduleDataByName = nil;
|
||||
_moduleDataByID = nil;
|
||||
_modulesByName = nil;
|
||||
_moduleClassesByID = nil;
|
||||
_modulesByName_DEPRECATED = nil;
|
||||
_frameUpdateObservers = nil;
|
||||
|
||||
}];
|
||||
@@ -683,15 +749,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
|
||||
|
||||
NSMapTable *buckets = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory
|
||||
valueOptions:NSPointerFunctionsStrongMemory
|
||||
capacity:_moduleDataByID.count];
|
||||
capacity:_moduleDataByName.count];
|
||||
|
||||
[moduleIDs enumerateObjectsUsingBlock:^(NSNumber *moduleID, NSUInteger i, __unused BOOL *stop) {
|
||||
RCTModuleData *moduleData = _moduleDataByID[moduleID.integerValue];
|
||||
if (RCT_DEBUG) {
|
||||
// verify that class has been registered
|
||||
(void)_modulesByName[moduleData.name];
|
||||
}
|
||||
dispatch_queue_t queue = moduleData.queue;
|
||||
dispatch_queue_t queue = moduleData.methodQueue;
|
||||
NSMutableOrderedSet<NSNumber *> *set = [buckets objectForKey:queue];
|
||||
if (!set) {
|
||||
set = [NSMutableOrderedSet new];
|
||||
@@ -739,10 +801,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
|
||||
{
|
||||
// TODO: batchDidComplete is only used by RCTUIManager - can we eliminate this special case?
|
||||
for (RCTModuleData *moduleData in _moduleDataByID) {
|
||||
if ([moduleData.instance respondsToSelector:@selector(batchDidComplete)]) {
|
||||
[moduleData dispatchBlock:^{
|
||||
if (moduleData.hasInstance && [moduleData.instance respondsToSelector:@selector(batchDidComplete)]) {
|
||||
[self dispatchBlock:^{
|
||||
[moduleData.instance batchDidComplete];
|
||||
}];
|
||||
} queue:moduleData.methodQueue];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -812,12 +874,12 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
|
||||
RCT_IF_DEV(NSString *name = [NSString stringWithFormat:@"[%@ didUpdateFrame:%f]", observer, displayLink.timestamp];)
|
||||
RCTProfileBeginFlowEvent();
|
||||
|
||||
[moduleData dispatchBlock:^{
|
||||
[self dispatchBlock:^{
|
||||
RCTProfileEndFlowEvent();
|
||||
RCT_PROFILE_BEGIN_EVENT(0, name, nil);
|
||||
[observer didUpdateFrame:frameUpdate];
|
||||
RCT_PROFILE_END_EVENT(0, @"objc_call,fps", nil);
|
||||
}];
|
||||
} queue:moduleData.methodQueue];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -851,3 +913,24 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation RCTBatchedBridge(Deprecated)
|
||||
|
||||
- (NSDictionary *)modules
|
||||
{
|
||||
if (!_modulesByName_DEPRECATED) {
|
||||
// Check classes are set up
|
||||
[self moduleClasses];
|
||||
NSMutableDictionary *modulesByName = [NSMutableDictionary new];
|
||||
for (NSString *moduleName in _moduleDataByName) {
|
||||
id module = [self moduleForName:moduleName];
|
||||
if (module) {
|
||||
modulesByName[moduleName] = module;
|
||||
}
|
||||
};
|
||||
_modulesByName_DEPRECATED = [modulesByName copy];
|
||||
}
|
||||
return _modulesByName_DEPRECATED;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -59,6 +59,11 @@ typedef NSArray<id<RCTBridgeModule>> *(^RCTBridgeModuleProviderBlock)(void);
|
||||
*/
|
||||
RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
|
||||
|
||||
/**
|
||||
* This function checks if a class has been registered
|
||||
*/
|
||||
RCT_EXTERN BOOL RCTBridgeModuleClassIsRegistered(Class);
|
||||
|
||||
/**
|
||||
* Async batched bridge used to communicate with the JavaScript application.
|
||||
*/
|
||||
@@ -98,10 +103,18 @@ RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
|
||||
- (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args;
|
||||
|
||||
/**
|
||||
* DEPRECATED: Do not use.
|
||||
* Retrieve a bridge module instance by name or class. Note that modules are
|
||||
* lazily instantiated, so calling these methods for the first time with a given
|
||||
* module name/class may cause the class to be sychronously instantiated,
|
||||
* blocking both the calling thread and main thread for a short time.
|
||||
*/
|
||||
#define RCT_IMPORT_METHOD(module, method) \
|
||||
_Pragma("message(\"This macro is no longer required\")")
|
||||
- (id)moduleForName:(NSString *)moduleName;
|
||||
- (id)moduleForClass:(Class)moduleClass;
|
||||
|
||||
/**
|
||||
* All registered bridge module classes.
|
||||
*/
|
||||
@property (nonatomic, copy, readonly) NSArray<Class> *moduleClasses;
|
||||
|
||||
/**
|
||||
* URL of the script that was loaded into the bridge.
|
||||
@@ -130,11 +143,6 @@ RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
|
||||
*/
|
||||
@property (nonatomic, readonly) RCTEventDispatcher *eventDispatcher;
|
||||
|
||||
/**
|
||||
* A dictionary of all registered RCTBridgeModule instances, keyed by moduleName.
|
||||
*/
|
||||
@property (nonatomic, copy, readonly) NSDictionary *modules;
|
||||
|
||||
/**
|
||||
* The launch options that were used to initialize the bridge.
|
||||
*/
|
||||
@@ -150,14 +158,19 @@ RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
|
||||
*/
|
||||
@property (nonatomic, readonly, getter=isValid) BOOL valid;
|
||||
|
||||
/**
|
||||
* The block passed in the constructor with pre-initialized modules
|
||||
*/
|
||||
@property (nonatomic, copy, readonly) RCTBridgeModuleProviderBlock moduleProvider;
|
||||
|
||||
/**
|
||||
* Reload the bundle and reset executor & modules. Safe to call from any thread.
|
||||
*/
|
||||
- (void)reload;
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* These properties and methods are deprecated and should not be used
|
||||
*/
|
||||
@interface RCTBridge (Deprecated)
|
||||
|
||||
@property (nonatomic, copy, readonly) NSDictionary *modules
|
||||
__deprecated_msg("Use moduleClasses and/or moduleForName: instead");
|
||||
|
||||
@end
|
||||
|
||||
@@ -37,6 +37,7 @@ NSString *const RCTDidCreateNativeModules = @"RCTDidCreateNativeModules";
|
||||
@interface RCTBridge ()
|
||||
|
||||
@property (nonatomic, strong) RCTBatchedBridge *batchedBridge;
|
||||
@property (nonatomic, copy, readonly) RCTBridgeModuleProviderBlock moduleProvider;
|
||||
|
||||
@end
|
||||
|
||||
@@ -87,9 +88,8 @@ NSString *RCTBridgeModuleNameForClass(Class cls)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if class has been registered
|
||||
* This function checks if a class has been registered
|
||||
*/
|
||||
BOOL RCTBridgeModuleClassIsRegistered(Class);
|
||||
BOOL RCTBridgeModuleClassIsRegistered(Class cls)
|
||||
{
|
||||
return [objc_getAssociatedObject(cls, &RCTBridgeModuleClassIsRegistered) ?: @YES boolValue];
|
||||
@@ -230,9 +230,24 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
#endif
|
||||
}
|
||||
|
||||
- (NSArray<Class> *)moduleClasses
|
||||
{
|
||||
return _batchedBridge.moduleClasses;
|
||||
}
|
||||
|
||||
- (id)moduleForName:(NSString *)moduleName
|
||||
{
|
||||
return [_batchedBridge moduleForName:moduleName];
|
||||
}
|
||||
|
||||
- (id)moduleForClass:(Class)moduleClass
|
||||
{
|
||||
return [self moduleForName:RCTBridgeModuleNameForClass(moduleClass)];
|
||||
}
|
||||
|
||||
- (RCTEventDispatcher *)eventDispatcher
|
||||
{
|
||||
return self.modules[RCTBridgeModuleNameForClass([RCTEventDispatcher class])];
|
||||
return [self moduleForClass:[RCTEventDispatcher class]];
|
||||
}
|
||||
|
||||
- (void)reload
|
||||
@@ -281,10 +296,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
[_batchedBridge logMessage:message level:level];
|
||||
}
|
||||
|
||||
- (NSDictionary *)modules
|
||||
{
|
||||
return _batchedBridge.modules;
|
||||
}
|
||||
|
||||
#define RCT_INNER_BRIDGE_ONLY(...) \
|
||||
- (void)__VA_ARGS__ \
|
||||
@@ -303,3 +314,12 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(__unused NSString *)module
|
||||
method:(__unused NSString *)method
|
||||
arguments:(__unused NSArray *)args);
|
||||
@end
|
||||
|
||||
@implementation RCTBridge(Deprecated)
|
||||
|
||||
- (NSDictionary *)modules
|
||||
{
|
||||
return _batchedBridge.modules;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -76,7 +76,7 @@ RCT_EXTERN void RCTRegisterModule(Class); \
|
||||
* will be set automatically by the bridge when it initializes the module.
|
||||
* To implement this in your module, just add `@synthesize bridge = _bridge;`
|
||||
*/
|
||||
@property (nonatomic, weak) RCTBridge *bridge;
|
||||
@property (nonatomic, weak, readonly) RCTBridge *bridge;
|
||||
|
||||
/**
|
||||
* The queue that will be used to call all exported methods. If omitted, this
|
||||
|
||||
@@ -97,14 +97,12 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
- (instancetype)init
|
||||
- (void)setBridge:(RCTBridge *)bridge
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_paused = YES;
|
||||
_eventQueue = [NSMutableDictionary new];
|
||||
_eventQueueLock = [NSLock new];
|
||||
}
|
||||
return self;
|
||||
_bridge = bridge;
|
||||
_paused = YES;
|
||||
_eventQueue = [NSMutableDictionary new];
|
||||
_eventQueueLock = [NSLock new];
|
||||
}
|
||||
|
||||
- (void)setPaused:(BOOL)paused
|
||||
|
||||
@@ -19,25 +19,24 @@ static NSDictionary *RCTParseKeyboardNotification(NSNotification *notification);
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
- (instancetype)init
|
||||
- (void)setBridge:(RCTBridge *)bridge
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
||||
_bridge = bridge;
|
||||
|
||||
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
||||
|
||||
#define ADD_KEYBOARD_HANDLER(NAME, SELECTOR) \
|
||||
[nc addObserver:self selector:@selector(SELECTOR:) name:NAME object:nil]
|
||||
[nc addObserver:self selector:@selector(SELECTOR:) name:NAME object:nil]
|
||||
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardWillShowNotification, keyboardWillShow);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardDidShowNotification, keyboardDidShow);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardWillHideNotification, keyboardWillHide);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardDidHideNotification, keyboardDidHide);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardWillChangeFrameNotification, keyboardWillChangeFrame);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardDidChangeFrameNotification, keyboardDidChangeFrame);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardWillShowNotification, keyboardWillShow);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardDidShowNotification, keyboardDidShow);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardWillHideNotification, keyboardWillHide);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardDidHideNotification, keyboardDidHide);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardWillChangeFrameNotification, keyboardWillChangeFrame);
|
||||
ADD_KEYBOARD_HANDLER(UIKeyboardDidChangeFrameNotification, keyboardDidChangeFrame);
|
||||
|
||||
#undef ADD_KEYBOARD_HANDLER
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
|
||||
@@ -9,28 +9,58 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "RCTJavaScriptExecutor.h"
|
||||
#import "RCTInvalidating.h"
|
||||
|
||||
@protocol RCTBridgeMethod;
|
||||
@protocol RCTBridgeModule;
|
||||
@class RCTBridge;
|
||||
|
||||
@interface RCTModuleData : NSObject
|
||||
@interface RCTModuleData : NSObject <RCTInvalidating>
|
||||
|
||||
@property (nonatomic, weak, readonly) id<RCTJavaScriptExecutor> javaScriptExecutor;
|
||||
@property (nonatomic, strong, readonly) NSNumber *moduleID;
|
||||
@property (nonatomic, strong, readonly) id<RCTBridgeModule> instance;
|
||||
- (instancetype)initWithModuleClass:(Class)moduleClass
|
||||
bridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
- (instancetype)initWithModuleInstance:(id<RCTBridgeModule>)instance NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
/**
|
||||
* Sets the bridge for the module instance. This is only needed when using the
|
||||
* `initWithModuleID:instance:` constructor. Otherwise, the bridge will be set
|
||||
* automatically when the module is first accessed.
|
||||
*/
|
||||
- (void)setBridgeForInstance:(RCTBridge *)bridge;
|
||||
|
||||
@property (nonatomic, strong, readonly) Class moduleClass;
|
||||
@property (nonatomic, copy, readonly) NSString *name;
|
||||
|
||||
/**
|
||||
* Returns the module methods. Note that this will gather the methods the first
|
||||
* time it is called and then memoize the results.
|
||||
*/
|
||||
@property (nonatomic, copy, readonly) NSArray<id<RCTBridgeMethod>> *methods;
|
||||
|
||||
/**
|
||||
* Returns YES if module instance has already been initialized; NO otherwise.
|
||||
*/
|
||||
@property (nonatomic, assign, readonly) BOOL hasInstance;
|
||||
|
||||
/**
|
||||
* Returns the current module instance. Note that this will init the instance
|
||||
* if it has not already been created. To check if the module instance exists
|
||||
* without causing it to be created, use `hasInstance` instead.
|
||||
*/
|
||||
@property (nonatomic, strong, readonly) id<RCTBridgeModule> instance;
|
||||
|
||||
/**
|
||||
* Returns the module method dispatch queue. Note that this will init both the
|
||||
* queue and the module itself if they have not already been created.
|
||||
*/
|
||||
@property (nonatomic, strong, readonly) dispatch_queue_t methodQueue;
|
||||
|
||||
/**
|
||||
* Returns the module config. Note that this will init the module if it has
|
||||
* not already been created. This method can be called on any thread, but will
|
||||
* block the main thread briefly if the module implements `constantsToExport`.
|
||||
*/
|
||||
@property (nonatomic, copy, readonly) NSArray *config;
|
||||
|
||||
@property (nonatomic, strong) dispatch_queue_t queue;
|
||||
|
||||
- (instancetype)initWithExecutor:(id<RCTJavaScriptExecutor>)javaScriptExecutor
|
||||
moduleID:(NSNumber *)moduleID
|
||||
instance:(id<RCTBridgeModule>)instance NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
- (void)dispatchBlock:(dispatch_block_t)block;
|
||||
- (void)dispatchBlock:(dispatch_block_t)block dispatchGroup:(dispatch_group_t)group;
|
||||
|
||||
@end
|
||||
|
||||
@@ -16,37 +16,75 @@
|
||||
|
||||
@implementation RCTModuleData
|
||||
{
|
||||
NSDictionary *_constants;
|
||||
NSString *_queueName;
|
||||
__weak RCTBridge *_bridge;
|
||||
}
|
||||
|
||||
@synthesize methods = _methods;
|
||||
@synthesize instance = _instance;
|
||||
@synthesize methodQueue = _methodQueue;
|
||||
|
||||
- (instancetype)initWithExecutor:(id<RCTJavaScriptExecutor>)javaScriptExecutor
|
||||
moduleID:(NSNumber *)moduleID
|
||||
instance:(id<RCTBridgeModule>)instance
|
||||
- (instancetype)initWithModuleClass:(Class)moduleClass
|
||||
bridge:(RCTBridge *)bridge
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_moduleClass = moduleClass;
|
||||
_bridge = bridge;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithModuleInstance:(id<RCTBridgeModule>)instance
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_javaScriptExecutor = javaScriptExecutor;
|
||||
_moduleID = moduleID;
|
||||
_instance = instance;
|
||||
_moduleClass = [instance class];
|
||||
_name = RCTBridgeModuleNameForClass(_moduleClass);
|
||||
|
||||
// Must be done at init time to ensure it's called on main thread
|
||||
RCTAssertMainThread();
|
||||
if ([_instance respondsToSelector:@selector(constantsToExport)]) {
|
||||
_constants = [_instance constantsToExport];
|
||||
}
|
||||
|
||||
// Must be done at init time due to race conditions
|
||||
(void)self.queue;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
RCT_NOT_IMPLEMENTED(- (instancetype)init);
|
||||
|
||||
- (BOOL)hasInstance
|
||||
{
|
||||
return _instance != nil;
|
||||
}
|
||||
|
||||
- (id<RCTBridgeModule>)instance
|
||||
{
|
||||
if (!_instance) {
|
||||
_instance = [_moduleClass new];
|
||||
|
||||
// Bridge must be set before methodQueue is set up, as methodQueue
|
||||
// initialization requires it (View Managers get their queue by calling
|
||||
// self.bridge.uiManager.methodQueue)
|
||||
[self setBridgeForInstance:_bridge];
|
||||
|
||||
// Initialize queue
|
||||
[self methodQueue];
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
- (void)setBridgeForInstance:(RCTBridge *)bridge
|
||||
{
|
||||
if ([_instance respondsToSelector:@selector(bridge)]) {
|
||||
@try {
|
||||
[(id)_instance setValue:bridge forKey:@"bridge"];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
RCTLogError(@"%@ has no setter or ivar for its bridge, which is not "
|
||||
"permitted. You must either @synthesize the bridge property, "
|
||||
"or provide your own setter method.", self.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)name
|
||||
{
|
||||
return RCTBridgeModuleNameForClass(_moduleClass);
|
||||
}
|
||||
|
||||
- (NSArray<id<RCTBridgeMethod>> *)methods
|
||||
{
|
||||
if (!_methods) {
|
||||
@@ -84,7 +122,15 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
|
||||
|
||||
- (NSArray *)config
|
||||
{
|
||||
if (_constants.count == 0 && self.methods.count == 0) {
|
||||
__block NSDictionary<NSString *, id> *constants;
|
||||
if (RCTClassOverridesInstanceMethod(_moduleClass, @selector(constantsToExport))) {
|
||||
[self instance]; // Initialize instance
|
||||
RCTExecuteOnMainThread(^{
|
||||
constants = [_instance constantsToExport];
|
||||
}, YES);
|
||||
}
|
||||
|
||||
if (constants.count == 0 && self.methods.count == 0) {
|
||||
return (id)kCFNull; // Nothing to export
|
||||
}
|
||||
|
||||
@@ -101,9 +147,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
|
||||
}
|
||||
|
||||
NSMutableArray *config = [NSMutableArray new];
|
||||
[config addObject:_name];
|
||||
if (_constants.count) {
|
||||
[config addObject:_constants];
|
||||
[config addObject:self.name];
|
||||
if (constants.count) {
|
||||
[config addObject:constants];
|
||||
}
|
||||
if (methods) {
|
||||
[config addObject:methods];
|
||||
@@ -114,53 +160,39 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init);
|
||||
return config;
|
||||
}
|
||||
|
||||
- (dispatch_queue_t)queue
|
||||
- (dispatch_queue_t)methodQueue
|
||||
{
|
||||
if (!_queue) {
|
||||
BOOL implementsMethodQueue = [_instance respondsToSelector:@selector(methodQueue)];
|
||||
if (!_methodQueue) {
|
||||
BOOL implementsMethodQueue = [self.instance respondsToSelector:@selector(methodQueue)];
|
||||
if (implementsMethodQueue) {
|
||||
_queue = _instance.methodQueue;
|
||||
_methodQueue = _instance.methodQueue;
|
||||
}
|
||||
if (!_queue) {
|
||||
if (!_methodQueue) {
|
||||
|
||||
// Create new queue (store queueName, as it isn't retained by dispatch_queue)
|
||||
_queueName = [NSString stringWithFormat:@"com.facebook.React.%@Queue", _name];
|
||||
_queue = dispatch_queue_create(_queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
|
||||
_queueName = [NSString stringWithFormat:@"com.facebook.React.%@Queue", self.name];
|
||||
_methodQueue = dispatch_queue_create(_queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
|
||||
|
||||
// assign it to the module
|
||||
if (implementsMethodQueue) {
|
||||
@try {
|
||||
[(id)_instance setValue:_queue forKey:@"methodQueue"];
|
||||
[(id)_instance setValue:_methodQueue forKey:@"methodQueue"];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
RCTLogError(@"%@ is returning nil for it's methodQueue, which is not "
|
||||
"permitted. You must either return a pre-initialized "
|
||||
"queue, or @synthesize the methodQueue to let the bridge "
|
||||
"create a queue for you.", _name);
|
||||
"create a queue for you.", self.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return _queue;
|
||||
return _methodQueue;
|
||||
}
|
||||
|
||||
- (void)dispatchBlock:(dispatch_block_t)block
|
||||
- (void)invalidate
|
||||
{
|
||||
[self dispatchBlock:block dispatchGroup:NULL];
|
||||
}
|
||||
|
||||
- (void)dispatchBlock:(dispatch_block_t)block
|
||||
dispatchGroup:(dispatch_group_t)group
|
||||
{
|
||||
if (self.queue == RCTJSThread) {
|
||||
[_javaScriptExecutor executeBlockOnJavaScriptQueue:block];
|
||||
} else if (self.queue) {
|
||||
if (group != NULL) {
|
||||
dispatch_group_async(group, self.queue, block);
|
||||
} else {
|
||||
dispatch_async(self.queue, block);
|
||||
}
|
||||
}
|
||||
_methodQueue = nil;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface RCTModuleMap : NSDictionary
|
||||
|
||||
- (instancetype)initWithDictionary:(NSDictionary *)modulesByName NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
@end
|
||||
@@ -1,79 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import "RCTModuleMap.h"
|
||||
|
||||
#import "RCTBridge.h"
|
||||
#import "RCTBridgeModule.h"
|
||||
#import "RCTDefines.h"
|
||||
#import "RCTLog.h"
|
||||
|
||||
@implementation RCTModuleMap
|
||||
{
|
||||
NSDictionary *_modulesByName;
|
||||
}
|
||||
|
||||
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:aDecoder)
|
||||
RCT_NOT_IMPLEMENTED(- (instancetype)initWithObjects:(const id [])objects
|
||||
forKeys:(const id<NSCopying> [])keys
|
||||
count:(NSUInteger)cnt)
|
||||
|
||||
- (instancetype)initWithDictionary:(NSDictionary *)modulesByName
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_modulesByName = [modulesByName copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSUInteger)count
|
||||
{
|
||||
return _modulesByName.count;
|
||||
}
|
||||
|
||||
//declared in RCTBridge.m
|
||||
extern BOOL RCTBridgeModuleClassIsRegistered(Class cls);
|
||||
|
||||
- (id)objectForKey:(NSString *)moduleName
|
||||
{
|
||||
id<RCTBridgeModule> module = _modulesByName[moduleName];
|
||||
if (RCT_DEBUG) {
|
||||
if (module) {
|
||||
Class moduleClass = [module class];
|
||||
if (!RCTBridgeModuleClassIsRegistered(moduleClass)) {
|
||||
RCTLogError(@"Class %@ was not exported. Did you forget to use "
|
||||
"RCT_EXPORT_MODULE()?", moduleClass);
|
||||
}
|
||||
} else {
|
||||
Class moduleClass = NSClassFromString(moduleName);
|
||||
module = _modulesByName[moduleName];
|
||||
if (module) {
|
||||
RCTLogError(@"bridge.modules[name] expects a module name, not a class "
|
||||
"name. Did you mean to pass '%@' instead?",
|
||||
RCTBridgeModuleNameForClass(moduleClass));
|
||||
}
|
||||
}
|
||||
}
|
||||
return module;
|
||||
}
|
||||
|
||||
- (NSEnumerator *)keyEnumerator
|
||||
{
|
||||
return [_modulesByName keyEnumerator];
|
||||
}
|
||||
|
||||
- (NSArray<id<RCTBridgeModule>> *)allValues
|
||||
{
|
||||
// don't perform validation in this case because we only want to error when
|
||||
// an invalid module is specifically requested
|
||||
return _modulesByName.allValues;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -79,15 +79,14 @@ RCT_EXPORT_MODULE()
|
||||
|
||||
@synthesize bridge = _bridge;
|
||||
|
||||
- (instancetype)init
|
||||
- (void)setBridge:(RCTBridge *)bridge
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(sendTimespans)
|
||||
name:RCTContentDidAppearNotification
|
||||
object:nil];
|
||||
}
|
||||
return self;
|
||||
_bridge = bridge;
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(sendTimespans)
|
||||
name:RCTContentDidAppearNotification
|
||||
object:nil];
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
|
||||
@@ -27,6 +27,10 @@ RCT_EXTERN id RCTJSONClean(id object);
|
||||
// Get MD5 hash of a string
|
||||
RCT_EXTERN NSString *RCTMD5Hash(NSString *string);
|
||||
|
||||
// Execute the specified block on the main thread. Unlike dispatch_sync/async
|
||||
// this will not context-switch if we're already running on the main thread.
|
||||
RCT_EXTERN void RCTExecuteOnMainThread(dispatch_block_t block, BOOL sync);
|
||||
|
||||
// Get screen metrics in a thread-safe way
|
||||
RCT_EXTERN CGFloat RCTScreenScale(void);
|
||||
RCT_EXTERN CGSize RCTScreenSize(void);
|
||||
|
||||
@@ -183,18 +183,29 @@ NSString *RCTMD5Hash(NSString *string)
|
||||
];
|
||||
}
|
||||
|
||||
void RCTExecuteOnMainThread(dispatch_block_t block, BOOL sync)
|
||||
{
|
||||
if ([NSThread isMainThread]) {
|
||||
block();
|
||||
} else if (sync) {
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
block();
|
||||
});
|
||||
} else {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
block();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
CGFloat RCTScreenScale()
|
||||
{
|
||||
static CGFloat scale;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
if (![NSThread isMainThread]) {
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
scale = [UIScreen mainScreen].scale;
|
||||
});
|
||||
} else {
|
||||
RCTExecuteOnMainThread(^{
|
||||
scale = [UIScreen mainScreen].scale;
|
||||
}
|
||||
}, YES);
|
||||
});
|
||||
|
||||
return scale;
|
||||
@@ -205,13 +216,9 @@ CGSize RCTScreenSize()
|
||||
static CGSize size;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
if (![NSThread isMainThread]) {
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
size = [UIScreen mainScreen].bounds.size;
|
||||
});
|
||||
} else {
|
||||
RCTExecuteOnMainThread(^{
|
||||
size = [UIScreen mainScreen].bounds.size;
|
||||
}
|
||||
}, YES);
|
||||
});
|
||||
|
||||
return size;
|
||||
|
||||
Reference in New Issue
Block a user