Use new enqueueJSCall method everywhere

Reviewed By: majak

Differential Revision: D3605263

fbshipit-source-id: 215f896d675b937593c8b796ed6ec5261ac74dbf
This commit is contained in:
Pieter De Baets 2016-08-02 11:06:19 -07:00 committed by Facebook Github Bot 8
parent 1ab4b2a792
commit e762d961cd
8 changed files with 62 additions and 30 deletions

View File

@ -19,7 +19,7 @@
#import "RCTEventDispatcher.h" #import "RCTEventDispatcher.h"
#import "RCTBridge+Private.h" #import "RCTBridge+Private.h"
@interface RCTTestEvent : NSObject <RCTEvent> @interface RCTTestEvent : NSObject <RCTEvent>
@property (atomic, assign, readwrite) BOOL canCoalesce; @property (atomic, assign, readwrite) BOOL canCoalesce;
@end @end
@ -54,7 +54,7 @@
+ (NSString *)moduleDotMethod + (NSString *)moduleDotMethod
{ {
return @"RCTDeviceEventEmitter.emit"; return @"MyCustomEventemitter.emit";
} }
- (NSArray *)arguments - (NSArray *)arguments
@ -100,8 +100,10 @@
- (void)testLegacyEventsAreImmediatelyDispatched - (void)testLegacyEventsAreImmediatelyDispatched
{ {
[[_bridge expect] enqueueJSCall:_JSMethod [[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter"
args:[_testEvent arguments]]; method:@"emit"
args:[_testEvent arguments]
completion:NULL];
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
@ -154,9 +156,8 @@
[_eventDispatcher sendEvent:_testEvent]; [_eventDispatcher sendEvent:_testEvent];
[_bridge verify]; [_bridge verify];
// eventsEmittingBlock would be called when js is no longer busy, which will result in emitting events // eventsEmittingBlock would be called when js is no longer busy, which will result in emitting events
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[_testEvent arguments]]; args:[_testEvent arguments]];
eventsEmittingBlock(); eventsEmittingBlock();
[_bridge verify]; [_bridge verify];
@ -174,7 +175,7 @@
eventsEmittingBlock = block; eventsEmittingBlock = block;
return YES; return YES;
}] queue:RCTJSThread]; }] queue:RCTJSThread];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[_testEvent arguments]]; args:[_testEvent arguments]];
RCTTestEvent *ignoredEvent = [[RCTTestEvent alloc] initWithViewTag:nil RCTTestEvent *ignoredEvent = [[RCTTestEvent alloc] initWithViewTag:nil
@ -201,9 +202,9 @@
eventsEmittingBlock = block; eventsEmittingBlock = block;
return YES; return YES;
}] queue:RCTJSThread]; }] queue:RCTJSThread];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[firstEvent arguments]]; args:[firstEvent arguments]];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[_testEvent arguments]]; args:[_testEvent arguments]];
@ -231,9 +232,9 @@
eventsEmittingBlock = block; eventsEmittingBlock = block;
return YES; return YES;
}] queue:RCTJSThread]; }] queue:RCTJSThread];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[firstEvent arguments]]; args:[firstEvent arguments]];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit" [[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[secondEvent arguments]]; args:[secondEvent arguments]];

View File

@ -201,6 +201,11 @@ class MessageQueue {
'Module %s is not a registered callable module.', 'Module %s is not a registered callable module.',
module module
); );
invariant(
!!moduleMethods[method],
'Method %s does not exist on module %s',
method, module
);
const result = moduleMethods[method].apply(moduleMethods, args); const result = moduleMethods[method].apply(moduleMethods, args);
Systrace.endEvent(); Systrace.endEvent();
return result; return result;
@ -219,7 +224,7 @@ class MessageQueue {
let errorMessage = `Callback with id ${cbID}: ${module}.${method}() not found`; let errorMessage = `Callback with id ${cbID}: ${module}.${method}() not found`;
if (method) { if (method) {
errorMessage = `The callback ${method}() exists in module ${module}, ` errorMessage = `The callback ${method}() exists in module ${module}, `
+ `but only one callback may be registered to a function in a native module.`; + 'but only one callback may be registered to a function in a native module.';
} }
invariant( invariant(
callback, callback,

View File

@ -557,7 +557,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)dele
NSString *path = [self.bundleURL.path substringFromIndex:1]; // strip initial slash NSString *path = [self.bundleURL.path substringFromIndex:1]; // strip initial slash
NSString *host = self.bundleURL.host; NSString *host = self.bundleURL.host;
NSNumber *port = self.bundleURL.port; NSNumber *port = self.bundleURL.port;
[self enqueueJSCall:@"HMRClient.enable" args:@[@"ios", path, host, RCTNullIfNil(port)]]; [self enqueueJSCall:@"HMRClient"
method:@"enable"
args:@[@"ios", path, host, RCTNullIfNil(port)]
completion:NULL];
} }
#endif #endif
} }
@ -745,8 +748,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
- (void)logMessage:(NSString *)message level:(NSString *)level - (void)logMessage:(NSString *)message level:(NSString *)level
{ {
if (RCT_DEBUG && [_javaScriptExecutor isValid]) { if (RCT_DEBUG && [_javaScriptExecutor isValid]) {
[self enqueueJSCall:@"RCTLog.logIfNoNativeHook" [self enqueueJSCall:@"RCTLog"
args:@[level, message]]; method:@"logIfNoNativeHook"
args:@[level, message]
completion:NULL];
} }
} }

View File

@ -63,14 +63,18 @@ RCT_EXPORT_MODULE()
- (void)sendAppEventWithName:(NSString *)name body:(id)body - (void)sendAppEventWithName:(NSString *)name body:(id)body
{ {
[_bridge enqueueJSCall:@"RCTNativeAppEventEmitter.emit" [_bridge enqueueJSCall:@"RCTNativeAppEventEmitter"
args:body ? @[name, body] : @[name]]; method:@"emit"
args:body ? @[name, body] : @[name]
completion:NULL];
} }
- (void)sendDeviceEventWithName:(NSString *)name body:(id)body - (void)sendDeviceEventWithName:(NSString *)name body:(id)body
{ {
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter.emit" [_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
args:body ? @[name, body] : @[name]]; method:@"emit"
args:body ? @[name, body] : @[name]
completion:NULL];
} }
- (void)sendInputEventWithName:(NSString *)name body:(NSDictionary *)body - (void)sendInputEventWithName:(NSString *)name body:(NSDictionary *)body
@ -81,8 +85,10 @@ RCT_EXPORT_MODULE()
} }
name = RCTNormalizeInputEventName(name); name = RCTNormalizeInputEventName(name);
[_bridge enqueueJSCall:@"RCTEventEmitter.receiveEvent" [_bridge enqueueJSCall:@"RCTEventEmitter"
args:body ? @[body[@"target"], name, body] : @[body[@"target"], name]]; method:@"receiveEvent"
args:body ? @[body[@"target"], name, body] : @[body[@"target"], name]
completion:NULL];
} }
- (void)sendTextEventWithType:(RCTTextEventType)type - (void)sendTextEventWithType:(RCTTextEventType)type

View File

@ -228,8 +228,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
@"initialProps": _appProperties ?: @{}, @"initialProps": _appProperties ?: @{},
}; };
[bridge enqueueJSCall:@"AppRegistry.runApplication" [bridge enqueueJSCall:@"AppRegistry"
args:@[moduleName, appParameters]]; method:@"runApplication"
args:@[moduleName, appParameters]
completion:NULL];
} }
- (void)setSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility - (void)setSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility
@ -376,8 +378,10 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder:(nonnull NSCoder *)aDecoder)
if (self.userInteractionEnabled) { if (self.userInteractionEnabled) {
self.userInteractionEnabled = NO; self.userInteractionEnabled = NO;
[(RCTRootView *)self.superview contentViewInvalidated]; [(RCTRootView *)self.superview contentViewInvalidated];
[_bridge enqueueJSCall:@"AppRegistry.unmountApplicationComponentAtRootTag" [_bridge enqueueJSCall:@"AppRegistry"
args:@[self.reactTag]]; method:@"unmountApplicationComponentAtRootTag"
args:@[self.reactTag]
completion:NULL];
} }
} }

View File

@ -559,7 +559,10 @@ static void installBasicSynchronousHooksOnContext(JSContext *context)
{ {
[self executeBlockOnJavaScriptQueue:^{ [self executeBlockOnJavaScriptQueue:^{
BOOL enabled = [notification.name isEqualToString:RCTProfileDidStartProfiling]; BOOL enabled = [notification.name isEqualToString:RCTProfileDidStartProfiling];
[self->_bridge enqueueJSCall:@"Systrace.setEnabled" args:@[enabled ? @YES : @NO]]; [self->_bridge enqueueJSCall:@"Systrace"
method:@"setEnabled"
args:@[enabled ? @YES : @NO]
completion:NULL];
}]; }];
} }

View File

@ -46,8 +46,10 @@
eventName, [self class], [[self supportedEvents] componentsJoinedByString:@"`, `"]); eventName, [self class], [[self supportedEvents] componentsJoinedByString:@"`, `"]);
} }
if (_listenerCount > 0) { if (_listenerCount > 0) {
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter.emit" [_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
args:body ? @[eventName, body] : @[eventName]]; method:@"emit"
args:body ? @[eventName, body] : @[eventName]
completion:NULL];
} else { } else {
RCTLogWarn(@"Sending `%@` with no listeners registered.", eventName); RCTLogWarn(@"Sending `%@` with no listeners registered.", eventName);
} }

View File

@ -192,7 +192,10 @@ RCT_EXPORT_MODULE()
// Call timers that need to be called // Call timers that need to be called
if (timersToCall.count > 0) { if (timersToCall.count > 0) {
[_bridge enqueueJSCall:@"JSTimersExecution.callTimers" args:@[timersToCall]]; [_bridge enqueueJSCall:@"JSTimersExecution"
method:@"callTimers"
args:@[timersToCall]
completion:NULL];
} }
if (_sendIdleEvents) { if (_sendIdleEvents) {
@ -200,7 +203,10 @@ RCT_EXPORT_MODULE()
if (kFrameDuration - frameElapsed >= kIdleCallbackFrameDeadline) { if (kFrameDuration - frameElapsed >= kIdleCallbackFrameDeadline) {
NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970]; NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
NSNumber *absoluteFrameStartMS = @((currentTimestamp - frameElapsed) * 1000); NSNumber *absoluteFrameStartMS = @((currentTimestamp - frameElapsed) * 1000);
[_bridge enqueueJSCall:@"JSTimersExecution.callIdleCallbacks" args:@[absoluteFrameStartMS]]; [_bridge enqueueJSCall:@"JSTimersExecution"
method:@"callIdleCallbacks"
args:@[absoluteFrameStartMS]
completion:NULL];
} }
} }