Skip to content

Commit

Permalink
Use new enqueueJSCall method everywhere
Browse files Browse the repository at this point in the history
Reviewed By: majak

Differential Revision: D3605263

fbshipit-source-id: 215f896d675b937593c8b796ed6ec5261ac74dbf
  • Loading branch information
javache authored and Facebook Github Bot 8 committed Aug 2, 2016
1 parent 1ab4b2a commit e762d96
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 30 deletions.
23 changes: 12 additions & 11 deletions Examples/UIExplorer/UIExplorerUnitTests/RCTEventDispatcherTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#import "RCTEventDispatcher.h"
#import "RCTBridge+Private.h"

@interface RCTTestEvent : NSObject <RCTEvent>
@interface RCTTestEvent : NSObject <RCTEvent>
@property (atomic, assign, readwrite) BOOL canCoalesce;
@end

Expand Down Expand Up @@ -54,7 +54,7 @@ - (instancetype)initWithViewTag:(NSNumber *)viewTag

+ (NSString *)moduleDotMethod
{
return @"RCTDeviceEventEmitter.emit";
return @"MyCustomEventemitter.emit";
}

- (NSArray *)arguments
Expand Down Expand Up @@ -100,8 +100,10 @@ - (void)setUp

- (void)testLegacyEventsAreImmediatelyDispatched
{
[[_bridge expect] enqueueJSCall:_JSMethod
args:[_testEvent arguments]];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter"
method:@"emit"
args:[_testEvent arguments]
completion:NULL];

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Expand Down Expand Up @@ -154,9 +156,8 @@ - (void)testRunningTheDispatchedBlockResultInANewOneBeingEnqueued
[_eventDispatcher sendEvent:_testEvent];
[_bridge verify];


// 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]];
eventsEmittingBlock();
[_bridge verify];
Expand All @@ -174,7 +175,7 @@ - (void)testBasicCoalescingReturnsLastEvent
eventsEmittingBlock = block;
return YES;
}] queue:RCTJSThread];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[_testEvent arguments]];

RCTTestEvent *ignoredEvent = [[RCTTestEvent alloc] initWithViewTag:nil
Expand All @@ -201,9 +202,9 @@ - (void)testDifferentEventTypesDontCoalesce
eventsEmittingBlock = block;
return YES;
}] queue:RCTJSThread];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[firstEvent arguments]];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[_testEvent arguments]];


Expand Down Expand Up @@ -231,9 +232,9 @@ - (void)testSameEventTypesWithDifferentCoalesceKeysDontCoalesce
eventsEmittingBlock = block;
return YES;
}] queue:RCTJSThread];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[firstEvent arguments]];
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
[[_bridge expect] enqueueJSCall:[[_testEvent class] moduleDotMethod]
args:[secondEvent arguments]];


Expand Down
7 changes: 6 additions & 1 deletion Libraries/Utilities/MessageQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ class MessageQueue {
'Module %s is not a registered callable module.',
module
);
invariant(
!!moduleMethods[method],
'Method %s does not exist on module %s',
method, module
);
const result = moduleMethods[method].apply(moduleMethods, args);
Systrace.endEvent();
return result;
Expand All @@ -219,7 +224,7 @@ class MessageQueue {
let errorMessage = `Callback with id ${cbID}: ${module}.${method}() not found`;
if (method) {
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(
callback,
Expand Down
11 changes: 8 additions & 3 deletions React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,10 @@ - (void)executeSourceCode:(NSData *)sourceCode
NSString *path = [self.bundleURL.path substringFromIndex:1]; // strip initial slash
NSString *host = self.bundleURL.host;
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
}
Expand Down Expand Up @@ -745,8 +748,10 @@ - (void)invalidate
- (void)logMessage:(NSString *)message level:(NSString *)level
{
if (RCT_DEBUG && [_javaScriptExecutor isValid]) {
[self enqueueJSCall:@"RCTLog.logIfNoNativeHook"
args:@[level, message]];
[self enqueueJSCall:@"RCTLog"
method:@"logIfNoNativeHook"
args:@[level, message]
completion:NULL];
}
}

Expand Down
18 changes: 12 additions & 6 deletions React/Base/RCTEventDispatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ - (void)setBridge:(RCTBridge *)bridge

- (void)sendAppEventWithName:(NSString *)name body:(id)body
{
[_bridge enqueueJSCall:@"RCTNativeAppEventEmitter.emit"
args:body ? @[name, body] : @[name]];
[_bridge enqueueJSCall:@"RCTNativeAppEventEmitter"
method:@"emit"
args:body ? @[name, body] : @[name]
completion:NULL];
}

- (void)sendDeviceEventWithName:(NSString *)name body:(id)body
{
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter.emit"
args:body ? @[name, body] : @[name]];
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
method:@"emit"
args:body ? @[name, body] : @[name]
completion:NULL];
}

- (void)sendInputEventWithName:(NSString *)name body:(NSDictionary *)body
Expand All @@ -81,8 +85,10 @@ - (void)sendInputEventWithName:(NSString *)name body:(NSDictionary *)body
}

name = RCTNormalizeInputEventName(name);
[_bridge enqueueJSCall:@"RCTEventEmitter.receiveEvent"
args:body ? @[body[@"target"], name, body] : @[body[@"target"], name]];
[_bridge enqueueJSCall:@"RCTEventEmitter"
method:@"receiveEvent"
args:body ? @[body[@"target"], name, body] : @[body[@"target"], name]
completion:NULL];
}

- (void)sendTextEventWithType:(RCTTextEventType)type
Expand Down
12 changes: 8 additions & 4 deletions React/Base/RCTRootView.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,10 @@ - (void)runApplication:(RCTBridge *)bridge
@"initialProps": _appProperties ?: @{},
};

[bridge enqueueJSCall:@"AppRegistry.runApplication"
args:@[moduleName, appParameters]];
[bridge enqueueJSCall:@"AppRegistry"
method:@"runApplication"
args:@[moduleName, appParameters]
completion:NULL];
}

- (void)setSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility
Expand Down Expand Up @@ -376,8 +378,10 @@ - (void)invalidate
if (self.userInteractionEnabled) {
self.userInteractionEnabled = NO;
[(RCTRootView *)self.superview contentViewInvalidated];
[_bridge enqueueJSCall:@"AppRegistry.unmountApplicationComponentAtRootTag"
args:@[self.reactTag]];
[_bridge enqueueJSCall:@"AppRegistry"
method:@"unmountApplicationComponentAtRootTag"
args:@[self.reactTag]
completion:NULL];
}
}

Expand Down
5 changes: 4 additions & 1 deletion React/Executors/RCTJSCExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,10 @@ - (void)toggleProfilingFlag:(NSNotification *)notification
{
[self executeBlockOnJavaScriptQueue:^{
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];
}];
}

Expand Down
6 changes: 4 additions & 2 deletions React/Modules/RCTEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ - (void)sendEventWithName:(NSString *)eventName body:(id)body
eventName, [self class], [[self supportedEvents] componentsJoinedByString:@"`, `"]);
}
if (_listenerCount > 0) {
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter.emit"
args:body ? @[eventName, body] : @[eventName]];
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
method:@"emit"
args:body ? @[eventName, body] : @[eventName]
completion:NULL];
} else {
RCTLogWarn(@"Sending `%@` with no listeners registered.", eventName);
}
Expand Down
10 changes: 8 additions & 2 deletions React/Modules/RCTTiming.m
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,21 @@ - (void)didUpdateFrame:(__unused RCTFrameUpdate *)update

// Call timers that need to be called
if (timersToCall.count > 0) {
[_bridge enqueueJSCall:@"JSTimersExecution.callTimers" args:@[timersToCall]];
[_bridge enqueueJSCall:@"JSTimersExecution"
method:@"callTimers"
args:@[timersToCall]
completion:NULL];
}

if (_sendIdleEvents) {
NSTimeInterval frameElapsed = (CACurrentMediaTime() - update.timestamp);
if (kFrameDuration - frameElapsed >= kIdleCallbackFrameDeadline) {
NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
NSNumber *absoluteFrameStartMS = @((currentTimestamp - frameElapsed) * 1000);
[_bridge enqueueJSCall:@"JSTimersExecution.callIdleCallbacks" args:@[absoluteFrameStartMS]];
[_bridge enqueueJSCall:@"JSTimersExecution"
method:@"callIdleCallbacks"
args:@[absoluteFrameStartMS]
completion:NULL];
}
}

Expand Down

0 comments on commit e762d96

Please sign in to comment.