Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from facebook:master #285

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Libraries/EventEmitter/NativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,29 @@ type NativeModule = {
...
};

type NativeEventEmitterOptions = $ReadOnly<{|
__SECRET_DISABLE_CALLS_INTO_MODULE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: boolean,
|}>;

const DEFAULT_NATIVE_EVENT_EMITTER_OPTIONS = {
__SECRET_DISABLE_CALLS_INTO_MODULE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: false,
};

/**
* Abstract base class for implementing event-emitting modules. This implements
* a subset of the standard EventEmitter node module API.
*/
export default class NativeEventEmitter extends EventEmitter {
_nativeModule: ?NativeModule;
_disableCallsIntoModule: boolean;

constructor(nativeModule: ?NativeModule) {
constructor(
nativeModule: ?NativeModule,
options: NativeEventEmitterOptions = DEFAULT_NATIVE_EVENT_EMITTER_OPTIONS,
) {
super(RCTDeviceEventEmitter.sharedSubscriber);
this._disableCallsIntoModule =
options.__SECRET_DISABLE_CALLS_INTO_MODULE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
if (Platform.OS === 'ios') {
invariant(nativeModule, 'Native module cannot be null.');
this._nativeModule = nativeModule;
Expand All @@ -42,7 +56,7 @@ export default class NativeEventEmitter extends EventEmitter {
listener: Function,
context: ?Object,
): EventSubscription {
if (this._nativeModule != null) {
if (this._nativeModule != null && !this._disableCallsIntoModule) {
this._nativeModule.addListener(eventType);
}
return super.addListener(eventType, listener, context);
Expand All @@ -51,14 +65,14 @@ export default class NativeEventEmitter extends EventEmitter {
removeAllListeners(eventType: string) {
invariant(eventType, 'eventType argument is required.');
const count = this.listenerCount(eventType);
if (this._nativeModule != null) {
if (this._nativeModule != null && !this._disableCallsIntoModule) {
this._nativeModule.removeListeners(count);
}
super.removeAllListeners(eventType);
}

removeSubscription(subscription: EventSubscription) {
if (this._nativeModule != null) {
if (this._nativeModule != null && !this._disableCallsIntoModule) {
this._nativeModule.removeListeners(1);
}
super.removeSubscription(subscription);
Expand Down
9 changes: 8 additions & 1 deletion Libraries/Network/RCTNetworking.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ import type {RequestBody} from './convertRequestBody';

class RCTNetworking extends NativeEventEmitter {
constructor() {
super(NativeNetworkingIOS);
const disableCallsIntoModule =
typeof global.__disableRCTNetworkingExtraneousModuleCalls === 'function'
? global.__disableRCTNetworkingExtraneousModuleCalls()
: false;

super(NativeNetworkingIOS, {
__SECRET_DISABLE_CALLS_INTO_MODULE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: disableCallsIntoModule,
});
}

sendRequest(
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Network/RCTNetworking.mm
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ @implementation RCTNetworking

- (instancetype)initWithHandlersProvider:(NSArray<id<RCTURLRequestHandler>> * (^)(void))getHandlers
{
if (self = [super init]) {
if (self = [super initWithDisabledObservation]) {
_handlersProvider = getHandlers;
}
return self;
Expand Down
2 changes: 2 additions & 0 deletions React/Modules/RCTEventEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

@property (nonatomic, weak) RCTBridge *bridge;

- (instancetype)initWithDisabledObservation;

/**
* Override this method to return an array of supported event names. Attempting
* to observe or send an event that isn't included in this list will result in
Expand Down
27 changes: 25 additions & 2 deletions React/Modules/RCTEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@implementation RCTEventEmitter {
NSInteger _listenerCount;
BOOL _observationDisabled;
}

@synthesize invokeJS = _invokeJS;
Expand All @@ -32,6 +33,13 @@ + (void)initialize
}
}

- (instancetype)initWithDisabledObservation
{
self = [super init];
_observationDisabled = YES;
return self;
}

- (NSArray<NSString *> *)supportedEvents
{
return nil;
Expand All @@ -56,12 +64,15 @@ - (void)sendEventWithName:(NSString *)eventName body:(id)body
[self class],
[[self supportedEvents] componentsJoinedByString:@"`, `"]);
}
if (_listenerCount > 0 && _bridge) {

BOOL shouldEmitEvent = (_observationDisabled || _listenerCount > 0);

if (shouldEmitEvent && _bridge) {
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
method:@"emit"
args:body ? @[ eventName, body ] : @[ eventName ]
completion:NULL];
} else if (_listenerCount > 0 && _invokeJS) {
} else if (shouldEmitEvent && _invokeJS) {
_invokeJS(@"RCTDeviceEventEmitter", @"emit", body ? @[ eventName, body ] : @[ eventName ]);
} else {
RCTLogWarn(@"Sending `%@` with no listeners registered.", eventName);
Expand All @@ -80,13 +91,21 @@ - (void)stopObserving

- (void)invalidate
{
if (_observationDisabled) {
return;
}

if (_listenerCount > 0) {
[self stopObserving];
}
}

RCT_EXPORT_METHOD(addListener : (NSString *)eventName)
{
if (_observationDisabled) {
return;
}

if (RCT_DEBUG && ![[self supportedEvents] containsObject:eventName]) {
RCTLogError(
@"`%@` is not a supported event type for %@. Supported events are: `%@`",
Expand All @@ -102,6 +121,10 @@ - (void)invalidate

RCT_EXPORT_METHOD(removeListeners : (double)count)
{
if (_observationDisabled) {
return;
}

int currentCount = (int)count;
if (RCT_DEBUG && currentCount > _listenerCount) {
RCTLogError(@"Attempted to remove more %@ listeners than added", [self class]);
Expand Down