mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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:
committed by
Facebook Github Bot
parent
f0d0c1c688
commit
2c40cc714c
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user