[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
|
|
|
/**
|
|
|
|
* 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 "RCTModuleData.h"
|
|
|
|
|
2016-07-18 14:12:19 +00:00
|
|
|
#import <objc/runtime.h>
|
2016-09-02 02:55:29 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2015-12-15 13:39:30 +00:00
|
|
|
#import "RCTBridge+Private.h"
|
2017-04-07 18:11:03 +00:00
|
|
|
#import "RCTBridge.h"
|
[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
|
|
|
#import "RCTLog.h"
|
2017-04-07 18:11:03 +00:00
|
|
|
#import "RCTModuleMethod.h"
|
2016-03-15 12:40:09 +00:00
|
|
|
#import "RCTProfile.h"
|
2015-10-29 01:41:49 +00:00
|
|
|
#import "RCTUtils.h"
|
[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
|
|
|
|
|
|
|
@implementation RCTModuleData
|
2015-08-07 13:42:34 +00:00
|
|
|
{
|
2016-02-09 11:29:18 +00:00
|
|
|
NSDictionary<NSString *, id> *_constantsToExport;
|
2015-08-07 23:07:15 +00:00
|
|
|
NSString *_queueName;
|
2015-11-25 11:09:00 +00:00
|
|
|
__weak RCTBridge *_bridge;
|
2017-04-07 18:11:03 +00:00
|
|
|
RCTBridgeModuleProvider _moduleProvider;
|
2016-09-02 02:55:29 +00:00
|
|
|
std::mutex _instanceLock;
|
2016-01-05 17:04:08 +00:00
|
|
|
BOOL _setupComplete;
|
2015-08-07 13:42:34 +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
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
@synthesize methods = _methods;
|
2015-11-25 11:09:00 +00:00
|
|
|
@synthesize instance = _instance;
|
|
|
|
@synthesize methodQueue = _methodQueue;
|
2015-11-03 22:45:46 +00:00
|
|
|
|
2016-03-03 10:20:20 +00:00
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
_implementsBatchDidComplete = [_moduleClass instancesRespondToSelector:@selector(batchDidComplete)];
|
|
|
|
_implementsPartialBatchDidFlush = [_moduleClass instancesRespondToSelector:@selector(partialBatchDidFlush)];
|
|
|
|
|
|
|
|
static IMP objectInitMethod;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
objectInitMethod = [NSObject instanceMethodForSelector:@selector(init)];
|
|
|
|
});
|
|
|
|
|
2016-05-03 16:08:03 +00:00
|
|
|
// If a module overrides `init` then we must assume that it expects to be
|
|
|
|
// initialized on the main thread, because it may need to access UIKit.
|
2016-06-06 14:57:55 +00:00
|
|
|
_requiresMainQueueSetup = !_instance &&
|
2016-05-03 16:08:03 +00:00
|
|
|
[_moduleClass instanceMethodForSelector:@selector(init)] != objectInitMethod;
|
2016-03-07 17:30:20 +00:00
|
|
|
|
|
|
|
// If a module overrides `constantsToExport` then we must assume that it
|
|
|
|
// must be called on the main thread, because it may need to access UIKit.
|
2016-10-31 11:52:58 +00:00
|
|
|
_hasConstantsToExport = [_moduleClass instancesRespondToSelector:@selector(constantsToExport)];
|
2016-03-03 10:20:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
- (instancetype)initWithModuleClass:(Class)moduleClass
|
|
|
|
bridge:(RCTBridge *)bridge
|
|
|
|
{
|
2017-04-07 18:11:03 +00:00
|
|
|
return [self initWithModuleClass:moduleClass
|
|
|
|
moduleProvider:^id<RCTBridgeModule>{ return [moduleClass new]; }
|
|
|
|
bridge:bridge];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithModuleClass:(Class)moduleClass
|
|
|
|
moduleProvider:(RCTBridgeModuleProvider)moduleProvider
|
|
|
|
bridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
2015-11-25 11:09:00 +00:00
|
|
|
_bridge = bridge;
|
2016-03-03 10:20:20 +00:00
|
|
|
_moduleClass = moduleClass;
|
2017-04-07 18:11:03 +00:00
|
|
|
_moduleProvider = [moduleProvider copy];
|
2016-03-03 10:20:20 +00:00
|
|
|
[self setUp];
|
2015-11-25 11:09:00 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithModuleInstance:(id<RCTBridgeModule>)instance
|
2016-01-05 17:04:08 +00:00
|
|
|
bridge:(RCTBridge *)bridge
|
[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
|
|
|
{
|
2017-04-07 18:11:03 +00:00
|
|
|
if (self = [super init]) {
|
2016-03-03 10:20:20 +00:00
|
|
|
_bridge = bridge;
|
[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
|
|
|
_instance = instance;
|
2016-03-03 10:20:20 +00:00
|
|
|
_moduleClass = [instance class];
|
|
|
|
[self setUp];
|
[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 self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init);
|
[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
|
|
|
|
2016-01-05 17:04:08 +00:00
|
|
|
#pragma mark - private setup methods
|
|
|
|
|
2016-03-03 10:20:20 +00:00
|
|
|
- (void)setUpInstanceAndBridge
|
|
|
|
{
|
2017-04-07 18:11:03 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setUpInstanceAndBridge]", @{
|
|
|
|
@"moduleClass": NSStringFromClass(_moduleClass)
|
|
|
|
});
|
2016-09-02 02:55:29 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_instanceLock);
|
|
|
|
|
|
|
|
if (!_setupComplete && _bridge.valid) {
|
2016-03-03 10:20:20 +00:00
|
|
|
if (!_instance) {
|
2016-09-02 02:55:29 +00:00
|
|
|
if (RCT_DEBUG && _requiresMainQueueSetup) {
|
|
|
|
RCTAssertMainQueue();
|
|
|
|
}
|
2017-04-07 18:11:03 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setUpInstanceAndBridge] Create module", nil);
|
|
|
|
_instance = _moduleProvider ? _moduleProvider() : nil;
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
2016-09-02 02:55:29 +00:00
|
|
|
if (!_instance) {
|
|
|
|
// Module init returned nil, probably because automatic instantatiation
|
|
|
|
// of the module is not supported, and it is supposed to be passed in to
|
|
|
|
// the bridge constructor. Mark setup complete to avoid doing more work.
|
|
|
|
_setupComplete = YES;
|
|
|
|
RCTLogWarn(@"The module %@ is returning nil from its constructor. You "
|
|
|
|
"may need to instantiate it yourself and pass it into the "
|
|
|
|
"bridge.", _moduleClass);
|
|
|
|
}
|
2016-03-03 10:20:20 +00:00
|
|
|
}
|
2016-05-04 13:54:12 +00:00
|
|
|
|
2016-09-02 02:55:29 +00:00
|
|
|
if (_instance && RCTProfileIsProfiling()) {
|
|
|
|
RCTProfileHookInstance(_instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
2016-05-04 13:54:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 02:55:29 +00:00
|
|
|
[self setUpMethodQueue];
|
2016-03-03 10:20:20 +00:00
|
|
|
}
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
2016-03-03 10:20:20 +00:00
|
|
|
|
|
|
|
// This is called outside of the lock in order to prevent deadlock issues
|
|
|
|
// because the logic in `finishSetupForInstance` can cause
|
|
|
|
// `moduleData.instance` to be accessed re-entrantly.
|
|
|
|
if (_bridge.moduleSetupComplete) {
|
|
|
|
[self finishSetupForInstance];
|
2016-05-14 00:15:07 +00:00
|
|
|
} else {
|
|
|
|
// If we're here, then the module is completely initialized,
|
|
|
|
// except for what finishSetupForInstance does. When the instance
|
|
|
|
// method is called after moduleSetupComplete,
|
2016-06-06 14:57:55 +00:00
|
|
|
// finishSetupForInstance will run. If _requiresMainQueueSetup
|
2016-05-14 00:15:07 +00:00
|
|
|
// is true, getting the instance will block waiting for the main
|
|
|
|
// thread, which could take a while if the main thread is busy
|
|
|
|
// (I've seen 50ms in testing). So we clear that flag, since
|
|
|
|
// nothing in finishSetupForInstance needs to be run on the main
|
|
|
|
// thread.
|
2016-06-06 14:57:55 +00:00
|
|
|
_requiresMainQueueSetup = NO;
|
2016-03-03 10:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 17:04:08 +00:00
|
|
|
- (void)setBridgeForInstance
|
2015-11-25 11:09:00 +00:00
|
|
|
{
|
2016-01-07 18:15:45 +00:00
|
|
|
if ([_instance respondsToSelector:@selector(bridge)] && _instance.bridge != _bridge) {
|
2016-05-14 00:15:05 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setBridgeForInstance]", nil);
|
2016-01-05 17:04:08 +00:00
|
|
|
@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);
|
|
|
|
}
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
2016-01-05 17:04:08 +00:00
|
|
|
}
|
2015-11-25 11:09:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 09:29:43 +00:00
|
|
|
- (void)finishSetupForInstance
|
|
|
|
{
|
2016-03-03 10:20:20 +00:00
|
|
|
if (!_setupComplete && _instance) {
|
2016-05-14 00:15:05 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData finishSetupForInstance]", nil);
|
2016-01-07 09:29:43 +00:00
|
|
|
_setupComplete = YES;
|
|
|
|
[_bridge registerModuleForFrameUpdates:_instance withModuleData:self];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTDidInitializeModuleNotification
|
|
|
|
object:_bridge
|
2017-04-06 07:40:27 +00:00
|
|
|
userInfo:@{@"module": _instance, @"bridge": RCTNullIfNil(_bridge.parentBridge)}];
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
2016-01-07 09:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 14:23:51 +00:00
|
|
|
- (void)setUpMethodQueue
|
2015-11-25 11:09:00 +00:00
|
|
|
{
|
2016-03-07 17:30:20 +00:00
|
|
|
if (_instance && !_methodQueue && _bridge.valid) {
|
2016-05-14 00:15:05 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData setUpMethodQueue]", nil);
|
2016-01-05 17:04:08 +00:00
|
|
|
BOOL implementsMethodQueue = [_instance respondsToSelector:@selector(methodQueue)];
|
2016-03-07 17:30:20 +00:00
|
|
|
if (implementsMethodQueue && _bridge.valid) {
|
2016-01-05 17:04:08 +00:00
|
|
|
_methodQueue = _instance.methodQueue;
|
|
|
|
}
|
2016-03-07 17:30:20 +00:00
|
|
|
if (!_methodQueue && _bridge.valid) {
|
2016-01-05 17:04:08 +00:00
|
|
|
// Create new queue (store queueName, as it isn't retained by dispatch_queue)
|
2016-04-01 14:01:51 +00:00
|
|
|
_queueName = [NSString stringWithFormat:@"com.facebook.react.%@Queue", self.name];
|
2016-01-05 17:04:08 +00:00
|
|
|
_methodQueue = dispatch_queue_create(_queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
|
|
|
|
|
|
|
|
// assign it to the module
|
|
|
|
if (implementsMethodQueue) {
|
|
|
|
@try {
|
|
|
|
[(id)_instance setValue:_methodQueue forKey:@"methodQueue"];
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
2016-01-07 09:29:43 +00:00
|
|
|
RCTLogError(@"%@ is returning nil for its methodQueue, which is not "
|
2016-01-05 17:04:08 +00:00
|
|
|
"permitted. You must either return a pre-initialized "
|
|
|
|
"queue, or @synthesize the methodQueue to let the bridge "
|
|
|
|
"create a queue for you.", self.name);
|
|
|
|
}
|
2016-01-04 14:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
2016-01-05 17:04:08 +00:00
|
|
|
}
|
2015-11-25 11:09:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:04:08 +00:00
|
|
|
#pragma mark - public getters
|
2016-01-04 14:23:51 +00:00
|
|
|
|
|
|
|
- (BOOL)hasInstance
|
|
|
|
{
|
|
|
|
return _instance != nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<RCTBridgeModule>)instance
|
|
|
|
{
|
2016-01-05 17:04:08 +00:00
|
|
|
if (!_setupComplete) {
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, ([NSString stringWithFormat:@"[RCTModuleData instanceForClass:%@]", _moduleClass]), nil);
|
2016-06-06 14:57:55 +00:00
|
|
|
if (_requiresMainQueueSetup) {
|
2016-03-03 10:20:20 +00:00
|
|
|
// The chances of deadlock here are low, because module init very rarely
|
|
|
|
// calls out to other threads, however we can't control when a module might
|
|
|
|
// get accessed by client code during bridge setup, and a very low risk of
|
|
|
|
// deadlock is better than a fairly high risk of an assertion being thrown.
|
2016-05-14 00:15:05 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"[RCTModuleData instance] main thread setup", nil);
|
2016-08-05 18:19:11 +00:00
|
|
|
|
|
|
|
if (!RCTIsMainQueue()) {
|
|
|
|
RCTLogWarn(@"RCTBridge required dispatch_sync to load %@. This may lead to deadlocks", _moduleClass);
|
|
|
|
}
|
2017-01-16 23:03:19 +00:00
|
|
|
|
|
|
|
RCTUnsafeExecuteOnMainQueueSync(^{
|
2016-03-03 10:20:20 +00:00
|
|
|
[self setUpInstanceAndBridge];
|
2017-01-16 23:03:19 +00:00
|
|
|
});
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
2016-03-03 10:20:20 +00:00
|
|
|
} else {
|
|
|
|
[self setUpInstanceAndBridge];
|
2015-11-25 11:09:00 +00:00
|
|
|
}
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
2015-11-25 11:09:00 +00:00
|
|
|
}
|
2016-01-05 17:04:08 +00:00
|
|
|
return _instance;
|
2015-11-25 11:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)name
|
|
|
|
{
|
|
|
|
return RCTBridgeModuleNameForClass(_moduleClass);
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (NSArray<id<RCTBridgeMethod>> *)methods
|
[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
|
|
|
{
|
2015-08-07 13:42:34 +00:00
|
|
|
if (!_methods) {
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<id<RCTBridgeMethod>> *moduleMethods = [NSMutableArray new];
|
2015-09-18 22:01:21 +00:00
|
|
|
|
2016-01-05 17:04:08 +00:00
|
|
|
if ([_moduleClass instancesRespondToSelector:@selector(methodsToExport)]) {
|
2016-09-05 14:32:20 +00:00
|
|
|
[moduleMethods addObjectsFromArray:[self.instance methodsToExport]];
|
2015-09-18 22:01:21 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 13:42:34 +00:00
|
|
|
unsigned int methodCount;
|
Added native event emitter
Summary:
This is a solution for the problem I raised in https://www.facebook.com/groups/react.native.community/permalink/768218933313687/
I've added a new native base class, `RCTEventEmitter` as well as an equivalent JS class/module `NativeEventEmitter` (RCTEventEmitter.js and EventEmitter.js were taken already).
Instead of arbitrary modules sending events via `bridge.eventDispatcher`, the idea is that any module that sends events should now subclass `RCTEventEmitter`, and provide an equivalent JS module that subclasses `NativeEventEmitter`.
JS code that wants to observe the events should now observe it via the specific JS module rather than via `RCTDeviceEventEmitter` directly. e.g. to observer a keyboard event, instead of writing:
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
RCTDeviceEventEmitter.addListener('keyboardWillShow', (event) => { ... });
You'd now write:
const Keyboard = require('Keyboard');
Keyboard.addListener('keyboardWillShow', (event) => { ... });
Within a component, you can also use the `Subscribable.Mixin` as you would previously, but instead of:
this.addListenerOn(RCTDeviceEventEmitter, 'keyboardWillShow', ...);
Write:
this.addListenerOn(Keyboard, 'keyboardWillShow', ...);
This approach allows the native `RCTKeyboardObserver` module to be created lazily the first time a listener is added, and to stop sending events when the last listener is removed. It also allows us to validate that the event strings being observed and omitted match the supported events for that module.
As a proof-of-concept, I've converted the `RCTStatusBarManager` and `RCTKeyboardObserver` modules to use the new system. I'll convert the rest in a follow up diff.
For now, the new `NativeEventEmitter` JS module wraps the `RCTDeviceEventEmitter` JS module, and just uses the native `RCTEventEmitter` module for bookkeeping. This allows for full backwards compatibility (code that is observing the event via `RCTDeviceEventEmitter` instead of the specific module will still work as expected, albeit with a warning). Once all legacy calls have been removed, this could be refactored to something more elegant internally, whilst maintaining the same public interface.
Note: currently, all device events still share a single global namespace, since they're really all registered on the same emitter instance internally. We should move away from that as soon as possible because it's not intuitive and will likely lead to strange bugs if people add generic events such as "onChange" or "onError" to their modules (which is common practice for components, where it's not a problem).
Reviewed By: javache
Differential Revision: D3269966
fbshipit-source-id: 1412daba850cd373020e1086673ba38ef9193050
2016-05-11 13:26:53 +00:00
|
|
|
Class cls = _moduleClass;
|
|
|
|
while (cls && cls != [NSObject class] && cls != [NSProxy class]) {
|
|
|
|
Method *methods = class_copyMethodList(object_getClass(cls), &methodCount);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < methodCount; i++) {
|
|
|
|
Method method = methods[i];
|
|
|
|
SEL selector = method_getName(method);
|
|
|
|
if ([NSStringFromSelector(selector) hasPrefix:@"__rct_export__"]) {
|
|
|
|
IMP imp = method_getImplementation(method);
|
|
|
|
NSArray<NSString *> *entries =
|
|
|
|
((NSArray<NSString *> *(*)(id, SEL))imp)(_moduleClass, selector);
|
|
|
|
id<RCTBridgeMethod> moduleMethod =
|
|
|
|
[[RCTModuleMethod alloc] initWithMethodSignature:entries[1]
|
|
|
|
JSMethodName:entries[0]
|
|
|
|
moduleClass:_moduleClass];
|
|
|
|
|
|
|
|
[moduleMethods addObject:moduleMethod];
|
|
|
|
}
|
2015-08-07 13:42:34 +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
|
|
|
|
Added native event emitter
Summary:
This is a solution for the problem I raised in https://www.facebook.com/groups/react.native.community/permalink/768218933313687/
I've added a new native base class, `RCTEventEmitter` as well as an equivalent JS class/module `NativeEventEmitter` (RCTEventEmitter.js and EventEmitter.js were taken already).
Instead of arbitrary modules sending events via `bridge.eventDispatcher`, the idea is that any module that sends events should now subclass `RCTEventEmitter`, and provide an equivalent JS module that subclasses `NativeEventEmitter`.
JS code that wants to observe the events should now observe it via the specific JS module rather than via `RCTDeviceEventEmitter` directly. e.g. to observer a keyboard event, instead of writing:
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
RCTDeviceEventEmitter.addListener('keyboardWillShow', (event) => { ... });
You'd now write:
const Keyboard = require('Keyboard');
Keyboard.addListener('keyboardWillShow', (event) => { ... });
Within a component, you can also use the `Subscribable.Mixin` as you would previously, but instead of:
this.addListenerOn(RCTDeviceEventEmitter, 'keyboardWillShow', ...);
Write:
this.addListenerOn(Keyboard, 'keyboardWillShow', ...);
This approach allows the native `RCTKeyboardObserver` module to be created lazily the first time a listener is added, and to stop sending events when the last listener is removed. It also allows us to validate that the event strings being observed and omitted match the supported events for that module.
As a proof-of-concept, I've converted the `RCTStatusBarManager` and `RCTKeyboardObserver` modules to use the new system. I'll convert the rest in a follow up diff.
For now, the new `NativeEventEmitter` JS module wraps the `RCTDeviceEventEmitter` JS module, and just uses the native `RCTEventEmitter` module for bookkeeping. This allows for full backwards compatibility (code that is observing the event via `RCTDeviceEventEmitter` instead of the specific module will still work as expected, albeit with a warning). Once all legacy calls have been removed, this could be refactored to something more elegant internally, whilst maintaining the same public interface.
Note: currently, all device events still share a single global namespace, since they're really all registered on the same emitter instance internally. We should move away from that as soon as possible because it's not intuitive and will likely lead to strange bugs if people add generic events such as "onChange" or "onError" to their modules (which is common practice for components, where it's not a problem).
Reviewed By: javache
Differential Revision: D3269966
fbshipit-source-id: 1412daba850cd373020e1086673ba38ef9193050
2016-05-11 13:26:53 +00:00
|
|
|
free(methods);
|
|
|
|
cls = class_getSuperclass(cls);
|
|
|
|
}
|
2015-08-07 13:42:34 +00:00
|
|
|
|
|
|
|
_methods = [moduleMethods copy];
|
|
|
|
}
|
|
|
|
return _methods;
|
[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
|
|
|
}
|
|
|
|
|
2016-02-29 17:47:14 +00:00
|
|
|
- (void)gatherConstants
|
|
|
|
{
|
2016-03-07 17:30:20 +00:00
|
|
|
if (_hasConstantsToExport && !_constantsToExport) {
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, ([NSString stringWithFormat:@"[RCTModuleData gatherConstants] %@", _moduleClass]), nil);
|
2016-03-07 17:30:20 +00:00
|
|
|
(void)[self instance];
|
2016-08-05 18:19:11 +00:00
|
|
|
if (!RCTIsMainQueue()) {
|
|
|
|
RCTLogWarn(@"Required dispatch_sync to load constants for %@. This may lead to deadlocks", _moduleClass);
|
|
|
|
}
|
2017-01-16 23:03:19 +00:00
|
|
|
|
|
|
|
RCTUnsafeExecuteOnMainQueueSync(^{
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_constantsToExport = [self->_instance constantsToExport] ?: @{};
|
2017-01-16 23:03:19 +00:00
|
|
|
});
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
|
2016-02-29 17:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 22:14:38 +00:00
|
|
|
- (NSDictionary<NSString *, id> *)exportedConstants
|
[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
|
|
|
{
|
2016-03-03 10:20:20 +00:00
|
|
|
[self gatherConstants];
|
2017-04-08 22:14:38 +00:00
|
|
|
NSDictionary<NSString *, id> *constants = _constantsToExport;
|
2016-03-03 10:20:20 +00:00
|
|
|
_constantsToExport = nil; // Not needed anymore
|
2017-04-08 22:14:38 +00:00
|
|
|
return constants;
|
|
|
|
}
|
2015-11-25 11:09:00 +00:00
|
|
|
|
2017-04-08 22:14:38 +00:00
|
|
|
// TODO 10487027: this method can go once RCTBatchedBridge is gone
|
|
|
|
- (NSArray *)config
|
|
|
|
{
|
|
|
|
NSDictionary<NSString *, id> *constants = [self exportedConstants];
|
2015-11-25 11:09:00 +00:00
|
|
|
if (constants.count == 0 && self.methods.count == 0) {
|
2015-10-29 01:41:49 +00:00
|
|
|
return (id)kCFNull; // Nothing to export
|
2015-10-22 12:51:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, ([NSString stringWithFormat:@"[RCTModuleData config] %@", _moduleClass]), nil);
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<NSString *> *methods = self.methods.count ? [NSMutableArray new] : nil;
|
2016-09-05 14:32:20 +00:00
|
|
|
NSMutableArray<NSNumber *> *promiseMethods = nil;
|
|
|
|
NSMutableArray<NSNumber *> *syncMethods = nil;
|
|
|
|
|
2015-10-29 01:41:49 +00:00
|
|
|
for (id<RCTBridgeMethod> method in self.methods) {
|
2015-10-28 15:26:50 +00:00
|
|
|
if (method.functionType == RCTFunctionTypePromise) {
|
2016-09-05 14:32:20 +00:00
|
|
|
if (!promiseMethods) {
|
|
|
|
promiseMethods = [NSMutableArray new];
|
|
|
|
}
|
|
|
|
[promiseMethods addObject:@(methods.count)];
|
|
|
|
}
|
|
|
|
else if (method.functionType == RCTFunctionTypeSync) {
|
|
|
|
if (!syncMethods) {
|
|
|
|
syncMethods = [NSMutableArray new];
|
2015-10-29 01:41:49 +00:00
|
|
|
}
|
2016-09-05 14:32:20 +00:00
|
|
|
[syncMethods addObject:@(methods.count)];
|
2015-10-23 17:14:26 +00:00
|
|
|
}
|
2015-10-29 22:35:15 +00:00
|
|
|
[methods addObject:method.JSMethodName];
|
2015-10-23 17:14:26 +00:00
|
|
|
}
|
2015-10-28 15:26:50 +00:00
|
|
|
|
2016-09-05 14:32:20 +00:00
|
|
|
NSArray *config = @[
|
2016-09-23 18:12:53 +00:00
|
|
|
self.name,
|
|
|
|
RCTNullIfNil(constants),
|
|
|
|
RCTNullIfNil(methods),
|
|
|
|
RCTNullIfNil(promiseMethods),
|
|
|
|
RCTNullIfNil(syncMethods)
|
|
|
|
];
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, ([NSString stringWithFormat:@"[RCTModuleData config] %@", _moduleClass]));
|
2015-10-29 01:41:49 +00:00
|
|
|
return config;
|
[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
|
|
|
}
|
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
[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
|
|
|
{
|
2016-03-03 10:20:20 +00:00
|
|
|
(void)[self instance];
|
2015-11-25 11:09:00 +00:00
|
|
|
return _methodQueue;
|
[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
|
|
|
}
|
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
- (void)invalidate
|
[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
|
|
|
{
|
2015-11-25 11:09:00 +00:00
|
|
|
_methodQueue = nil;
|
[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
|
|
|
}
|
|
|
|
|
2016-03-01 17:44:05 +00:00
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"<%@: %p; name=\"%@\">", [self class], self, self.name];
|
|
|
|
}
|
|
|
|
|
[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
|