Make RCTEventEmitter a RCTJSInvokerModule [2/n]

Summary:
Diff 1/N explained how calls to `[_bridge enqueueJSCall:]` can be replaced with `_invokeJS` in a bridgeless world.

This diff replaces RCTEventEmitter's usage of `enqueueJSCall` if the bridge isn't available.

Changelog: [Internal][iOS] Make RCTEventEmitter a RCTJSInvokerModule

Reviewed By: RSNara

Differential Revision: D18941955

fbshipit-source-id: 1b81e46585432e005cff5aa0ab4d151f50ea051b
This commit is contained in:
Peter Argany
2019-12-23 13:52:03 -08:00
committed by Facebook Github Bot
parent f0d0c1c688
commit 2c40cc714c
2 changed files with 8 additions and 3 deletions
+2 -1
View File
@@ -6,12 +6,13 @@
*/
#import <React/RCTBridge.h>
#import <React/RCTJSInvokerModule.h>
/**
* RCTEventEmitter is an abstract base class to be used for modules that emit
* events to be observed by JS.
*/
@interface RCTEventEmitter : NSObject <RCTBridgeModule>
@interface RCTEventEmitter : NSObject <RCTBridgeModule, RCTJSInvokerModule>
@property (nonatomic, weak) RCTBridge *bridge;
+6 -2
View File
@@ -15,6 +15,8 @@
NSInteger _listenerCount;
}
@synthesize invokeJS = _invokeJS;
+ (NSString *)moduleName
{
return @"";
@@ -35,7 +37,7 @@
- (void)sendEventWithName:(NSString *)eventName body:(id)body
{
RCTAssert(_bridge != nil, @"Error when sending event: %@ with body: %@. "
RCTAssert(_bridge != nil || _invokeJS != nil, @"Error when sending event: %@ with body: %@. "
"Bridge is not set. This is probably because you've "
"explicitly synthesized the bridge in %@, even though it's inherited "
"from RCTEventEmitter.", eventName, body, [self class]);
@@ -44,11 +46,13 @@
RCTLogError(@"`%@` is not a supported event type for %@. Supported events are: `%@`",
eventName, [self class], [[self supportedEvents] componentsJoinedByString:@"`, `"]);
}
if (_listenerCount > 0) {
if (_listenerCount > 0 && _bridge) {
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
method:@"emit"
args:body ? @[eventName, body] : @[eventName]
completion:NULL];
} else if (_listenerCount > 0 && _invokeJS) {
_invokeJS(@"RCTDeviceEventEmitter", @"emit", body ? @[eventName, body] : @[eventName]);
} else {
RCTLogWarn(@"Sending `%@` with no listeners registered.", eventName);
}