2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
|
|
|
|
#import <objc/runtime.h>
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
2015-04-02 14:33:21 +00:00
|
|
|
#import "RCTKeyCommands.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTLog.h"
|
2015-06-19 21:59:42 +00:00
|
|
|
#import "RCTPerformanceLogger.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2015-04-11 22:08:00 +00:00
|
|
|
NSString *const RCTReloadNotification = @"RCTReloadNotification";
|
2015-07-28 18:07:45 +00:00
|
|
|
NSString *const RCTJavaScriptWillStartLoadingNotification = @"RCTJavaScriptWillStartLoadingNotification";
|
2015-04-11 22:08:00 +00:00
|
|
|
NSString *const RCTJavaScriptDidLoadNotification = @"RCTJavaScriptDidLoadNotification";
|
2015-05-07 11:29:31 +00:00
|
|
|
NSString *const RCTJavaScriptDidFailToLoadNotification = @"RCTJavaScriptDidFailToLoadNotification";
|
2015-07-01 00:08:28 +00:00
|
|
|
NSString *const RCTDidCreateNativeModules = @"RCTDidCreateNativeModules";
|
2015-04-11 22:08:00 +00:00
|
|
|
|
[ReactNative] Move module info from bridge to RCTModuleData
Summary:
@public
The info about bridge modules (such as id, name, queue, methods...) was spread
across arrays & dictionaries on the bridge, move it into a specific class.
It also removes a lot of information that was statically cached, and now have
the same lifecycle of the bridge.
Also moved RCTModuleMethod, RCTFrameUpdate and RCTBatchedBridge into it's own
files, for organization sake.
NOTE: This diff seems huge, but most of it was just moving code :)
Test Plan:
Tested UIExplorer & UIExplorer tests, Catalyst, MAdMan and Groups. Everything
looks fine.
2015-06-24 23:34:56 +00:00
|
|
|
@class RCTBatchedBridge;
|
|
|
|
|
|
|
|
@interface RCTBatchedBridge : RCTBridge <RCTInvalidating>
|
|
|
|
|
|
|
|
@property (nonatomic, weak) RCTBridge *parentBridge;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
[ReactNative] Move module info from bridge to RCTModuleData
Summary:
@public
The info about bridge modules (such as id, name, queue, methods...) was spread
across arrays & dictionaries on the bridge, move it into a specific class.
It also removes a lot of information that was statically cached, and now have
the same lifecycle of the bridge.
Also moved RCTModuleMethod, RCTFrameUpdate and RCTBatchedBridge into it's own
files, for organization sake.
NOTE: This diff seems huge, but most of it was just moving code :)
Test Plan:
Tested UIExplorer & UIExplorer tests, Catalyst, MAdMan and Groups. Everything
looks fine.
2015-06-24 23:34:56 +00:00
|
|
|
- (instancetype)initWithParentBridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER;
|
2015-06-09 21:26:40 +00:00
|
|
|
|
[ReactNative] Move module info from bridge to RCTModuleData
Summary:
@public
The info about bridge modules (such as id, name, queue, methods...) was spread
across arrays & dictionaries on the bridge, move it into a specific class.
It also removes a lot of information that was statically cached, and now have
the same lifecycle of the bridge.
Also moved RCTModuleMethod, RCTFrameUpdate and RCTBatchedBridge into it's own
files, for organization sake.
NOTE: This diff seems huge, but most of it was just moving code :)
Test Plan:
Tested UIExplorer & UIExplorer tests, Catalyst, MAdMan and Groups. Everything
looks fine.
2015-06-24 23:34:56 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@interface RCTBridge ()
|
|
|
|
|
|
|
|
@property (nonatomic, strong) RCTBatchedBridge *batchedBridge;
|
|
|
|
|
|
|
|
@end
|
2015-05-04 17:35:49 +00:00
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
static NSMutableArray<Class> *RCTModuleClasses;
|
|
|
|
NSArray<Class> *RCTGetModuleClasses(void);
|
|
|
|
NSArray<Class> *RCTGetModuleClasses(void)
|
[ReactNative] Move module info from bridge to RCTModuleData
Summary:
@public
The info about bridge modules (such as id, name, queue, methods...) was spread
across arrays & dictionaries on the bridge, move it into a specific class.
It also removes a lot of information that was statically cached, and now have
the same lifecycle of the bridge.
Also moved RCTModuleMethod, RCTFrameUpdate and RCTBatchedBridge into it's own
files, for organization sake.
NOTE: This diff seems huge, but most of it was just moving code :)
Test Plan:
Tested UIExplorer & UIExplorer tests, Catalyst, MAdMan and Groups. Everything
looks fine.
2015-06-24 23:34:56 +00:00
|
|
|
{
|
|
|
|
return RCTModuleClasses;
|
|
|
|
}
|
2015-04-10 00:11:13 +00:00
|
|
|
|
2015-08-06 22:44:15 +00:00
|
|
|
/**
|
|
|
|
* Register the given class as a bridge module. All modules must be registered
|
|
|
|
* prior to the first bridge initialization.
|
|
|
|
*/
|
|
|
|
void RCTRegisterModule(Class);
|
2015-06-10 10:43:55 +00:00
|
|
|
void RCTRegisterModule(Class moduleClass)
|
|
|
|
{
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2015-08-17 14:35:34 +00:00
|
|
|
RCTModuleClasses = [NSMutableArray new];
|
2015-06-10 10:43:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
RCTAssert([moduleClass conformsToProtocol:@protocol(RCTBridgeModule)],
|
|
|
|
@"%@ does not conform to the RCTBridgeModule protocol",
|
2015-08-06 22:44:15 +00:00
|
|
|
moduleClass);
|
2015-06-10 10:43:55 +00:00
|
|
|
|
|
|
|
// Register module
|
[ReactNative] Move module info from bridge to RCTModuleData
Summary:
@public
The info about bridge modules (such as id, name, queue, methods...) was spread
across arrays & dictionaries on the bridge, move it into a specific class.
It also removes a lot of information that was statically cached, and now have
the same lifecycle of the bridge.
Also moved RCTModuleMethod, RCTFrameUpdate and RCTBatchedBridge into it's own
files, for organization sake.
NOTE: This diff seems huge, but most of it was just moving code :)
Test Plan:
Tested UIExplorer & UIExplorer tests, Catalyst, MAdMan and Groups. Everything
looks fine.
2015-06-24 23:34:56 +00:00
|
|
|
[RCTModuleClasses addObject:moduleClass];
|
2015-06-10 10:43:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
/**
|
|
|
|
* This function returns the module name for a given class.
|
|
|
|
*/
|
2015-04-07 14:36:26 +00:00
|
|
|
NSString *RCTBridgeModuleNameForClass(Class cls)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-08-25 11:27:09 +00:00
|
|
|
#if RCT_DEV
|
|
|
|
RCTAssert([cls conformsToProtocol:@protocol(RCTBridgeModule)], @"Bridge module classes must conform to RCTBridgeModule");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NSString *name = [cls moduleName];
|
2015-08-24 10:14:33 +00:00
|
|
|
if (name.length == 0) {
|
2015-04-08 12:42:43 +00:00
|
|
|
name = NSStringFromClass(cls);
|
|
|
|
}
|
|
|
|
if ([name hasPrefix:@"RK"]) {
|
|
|
|
name = [name stringByReplacingCharactersInRange:(NSRange){0,@"RK".length} withString:@"RCT"];
|
|
|
|
}
|
|
|
|
return name;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 15:51:28 +00:00
|
|
|
/**
|
|
|
|
* Check if class has been registered
|
|
|
|
*/
|
|
|
|
BOOL RCTBridgeModuleClassIsRegistered(Class);
|
|
|
|
BOOL RCTBridgeModuleClassIsRegistered(Class cls)
|
|
|
|
{
|
|
|
|
return [objc_getAssociatedObject(cls, &RCTBridgeModuleClassIsRegistered) ?: @YES boolValue];
|
|
|
|
}
|
2015-04-15 14:07:19 +00:00
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
@implementation RCTBridge
|
2015-04-15 14:07:19 +00:00
|
|
|
|
2015-06-19 11:18:54 +00:00
|
|
|
dispatch_queue_t RCTJSThread;
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-06-10 10:43:55 +00:00
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
2015-06-19 11:18:54 +00:00
|
|
|
// Set up JS thread
|
|
|
|
RCTJSThread = (id)kCFNull;
|
|
|
|
|
|
|
|
#if RCT_DEBUG
|
|
|
|
|
|
|
|
// Set up module classes
|
2015-06-10 10:43:55 +00:00
|
|
|
static unsigned int classCount;
|
|
|
|
Class *classes = objc_copyClassList(&classCount);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < classCount; i++)
|
|
|
|
{
|
|
|
|
Class cls = classes[i];
|
|
|
|
Class superclass = cls;
|
|
|
|
while (superclass)
|
|
|
|
{
|
|
|
|
if (class_conformsToProtocol(superclass, @protocol(RCTBridgeModule)))
|
|
|
|
{
|
[ReactNative] Move module info from bridge to RCTModuleData
Summary:
@public
The info about bridge modules (such as id, name, queue, methods...) was spread
across arrays & dictionaries on the bridge, move it into a specific class.
It also removes a lot of information that was statically cached, and now have
the same lifecycle of the bridge.
Also moved RCTModuleMethod, RCTFrameUpdate and RCTBatchedBridge into it's own
files, for organization sake.
NOTE: This diff seems huge, but most of it was just moving code :)
Test Plan:
Tested UIExplorer & UIExplorer tests, Catalyst, MAdMan and Groups. Everything
looks fine.
2015-06-24 23:34:56 +00:00
|
|
|
if (![RCTModuleClasses containsObject:cls]) {
|
2015-07-27 15:51:28 +00:00
|
|
|
RCTLogWarn(@"Class %@ was not exported. Did you forget to use "
|
|
|
|
"RCT_EXPORT_MODULE()?", cls);
|
|
|
|
|
|
|
|
RCTRegisterModule(cls);
|
|
|
|
objc_setAssociatedObject(cls, &RCTBridgeModuleClassIsRegistered,
|
|
|
|
@NO, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
2015-06-10 10:43:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
superclass = class_getSuperclass(superclass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(classes);
|
|
|
|
|
2015-06-19 11:18:54 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-10 10:43:55 +00:00
|
|
|
});
|
|
|
|
}
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-08-19 12:27:43 +00:00
|
|
|
static RCTBridge *RCTCurrentBridgeInstance = nil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The last current active bridge instance. This is set automatically whenever
|
|
|
|
* the bridge is accessed. It can be useful for static functions or singletons
|
|
|
|
* that need to access the bridge for purposes such as logging, but should not
|
|
|
|
* be relied upon to return any particular instance, due to race conditions.
|
|
|
|
*/
|
|
|
|
+ (instancetype)currentBridge
|
|
|
|
{
|
|
|
|
return RCTCurrentBridgeInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)setCurrentBridge:(RCTBridge *)currentBridge
|
|
|
|
{
|
|
|
|
RCTCurrentBridgeInstance = currentBridge;
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:48:46 +00:00
|
|
|
- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)delegate
|
|
|
|
launchOptions:(NSDictionary *)launchOptions
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
|
|
|
|
|
|
|
if ((self = [super init])) {
|
|
|
|
RCTPerformanceLoggerStart(RCTPLTTI);
|
|
|
|
|
|
|
|
_delegate = delegate;
|
|
|
|
_launchOptions = [launchOptions copy];
|
|
|
|
[self setUp];
|
|
|
|
[self bindKeys];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
|
|
|
|
moduleProvider:(RCTBridgeModuleProviderBlock)block
|
|
|
|
launchOptions:(NSDictionary *)launchOptions
|
2015-04-15 14:07:19 +00:00
|
|
|
{
|
2015-05-04 17:35:49 +00:00
|
|
|
RCTAssertMainThread();
|
|
|
|
|
2015-04-15 14:07:19 +00:00
|
|
|
if ((self = [super init])) {
|
2015-06-19 21:59:42 +00:00
|
|
|
RCTPerformanceLoggerStart(RCTPLTTI);
|
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
_bundleURL = bundleURL;
|
|
|
|
_moduleProvider = block;
|
|
|
|
_launchOptions = [launchOptions copy];
|
|
|
|
[self setUp];
|
2015-08-21 18:33:04 +00:00
|
|
|
[self bindKeys];
|
2015-04-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
- (void)dealloc
|
2015-04-15 14:07:19 +00:00
|
|
|
{
|
2015-05-04 17:35:49 +00:00
|
|
|
/**
|
|
|
|
* This runs only on the main thread, but crashes the subclass
|
|
|
|
* RCTAssertMainThread();
|
|
|
|
*/
|
2015-05-13 15:25:00 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-05-04 17:35:49 +00:00
|
|
|
[self invalidate];
|
2015-04-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
- (void)bindKeys
|
2015-04-15 14:07:19 +00:00
|
|
|
{
|
2015-05-04 17:35:49 +00:00
|
|
|
RCTAssertMainThread();
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(reload)
|
|
|
|
name:RCTReloadNotification
|
|
|
|
object:nil];
|
|
|
|
|
|
|
|
#if TARGET_IPHONE_SIMULATOR
|
|
|
|
RCTKeyCommands *commands = [RCTKeyCommands sharedInstance];
|
|
|
|
|
|
|
|
// reload in current mode
|
|
|
|
[commands registerKeyCommandWithInput:@"r"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
2015-08-21 18:33:04 +00:00
|
|
|
action:^(__unused UIKeyCommand *command) {
|
2015-08-07 11:36:10 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification
|
|
|
|
object:nil
|
|
|
|
userInfo:nil];
|
2015-05-04 17:35:49 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
#endif
|
2015-04-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (RCTEventDispatcher *)eventDispatcher
|
|
|
|
{
|
|
|
|
return self.modules[RCTBridgeModuleNameForClass([RCTEventDispatcher class])];
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
- (void)reload
|
2015-04-15 14:07:19 +00:00
|
|
|
{
|
2015-05-12 14:44:46 +00:00
|
|
|
/**
|
|
|
|
* AnyThread
|
|
|
|
*/
|
2015-05-04 17:35:49 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self invalidate];
|
|
|
|
[self setUp];
|
|
|
|
});
|
2015-04-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
2015-04-15 14:07:19 +00:00
|
|
|
|
2015-08-08 09:58:30 +00:00
|
|
|
_bundleURL = [self.delegate sourceURLForBridge:self] ?: _bundleURL;
|
2015-05-04 17:35:49 +00:00
|
|
|
_batchedBridge = [[RCTBatchedBridge alloc] initWithParentBridge:self];
|
|
|
|
}
|
2015-04-15 14:07:19 +00:00
|
|
|
|
2015-05-14 22:56:48 +00:00
|
|
|
- (BOOL)isLoading
|
|
|
|
{
|
|
|
|
return _batchedBridge.loading;
|
|
|
|
}
|
|
|
|
|
2015-08-21 18:33:04 +00:00
|
|
|
- (BOOL)isValid
|
|
|
|
{
|
|
|
|
return _batchedBridge.valid;
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
2015-04-15 14:07:19 +00:00
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
[_batchedBridge invalidate];
|
|
|
|
_batchedBridge = nil;
|
|
|
|
}
|
2015-04-15 14:07:19 +00:00
|
|
|
|
2015-08-19 12:27:43 +00:00
|
|
|
- (void)logMessage:(NSString *)message level:(NSString *)level
|
2015-04-15 14:07:19 +00:00
|
|
|
{
|
2015-08-19 12:27:43 +00:00
|
|
|
[_batchedBridge logMessage:message level:level];
|
2015-05-04 17:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)modules
|
|
|
|
{
|
|
|
|
return _batchedBridge.modules;
|
2015-04-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 14:44:38 +00:00
|
|
|
#define RCT_INNER_BRIDGE_ONLY(...) \
|
2015-05-04 17:35:49 +00:00
|
|
|
- (void)__VA_ARGS__ \
|
|
|
|
{ \
|
|
|
|
RCTLogMustFix(@"Called method \"%@\" on top level bridge. This method should \
|
|
|
|
only be called from bridge instance in a bridge module", @(__func__)); \
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:44:38 +00:00
|
|
|
- (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args
|
|
|
|
{
|
2015-05-28 17:30:51 +00:00
|
|
|
[self.batchedBridge enqueueJSCall:moduleDotMethod args:args];
|
2015-05-12 14:44:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(__unused NSString *)module
|
|
|
|
method:(__unused NSString *)method
|
2015-07-14 23:16:21 +00:00
|
|
|
arguments:(__unused NSArray *)args);
|
2015-04-15 14:07:19 +00:00
|
|
|
@end
|