Skip to content

Commit 114be1d

Browse files
rubennortefacebook-github-bot
authored andcommitted
Check existence of native methods before calling them in NativeEventEmitter
Summary: Check the existence of `addListener` and `removeListeners` in the native module passed to `NativeEventEmitter` to determine if it can be used. Changelog: [General][Changed] Show warning when native module without `addListener` or `removeListeners` is passed to `NativeEventEmitter` Reviewed By: yungsters Differential Revision: D27851425 fbshipit-source-id: c0ad3ba65a9239f5bf84548dab36e8dfbc51058a
1 parent 1fc1873 commit 114be1d

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Libraries/EventEmitter/NativeEventEmitter.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,26 @@ export default class NativeEventEmitter<TEventToArgsMap: {...}>
4646
'`new NativeEventEmitter()` requires a non-null argument.',
4747
);
4848
}
49-
this._nativeModule = nativeModule;
49+
50+
const hasAddListener =
51+
!!nativeModule && typeof nativeModule.addListener === 'function';
52+
const hasRemoveListeners =
53+
!!nativeModule && typeof nativeModule.removeListeners === 'function';
54+
55+
if (nativeModule && hasAddListener && hasRemoveListeners) {
56+
this._nativeModule = nativeModule;
57+
} else {
58+
if (!hasAddListener) {
59+
console.warn(
60+
'`new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method.',
61+
);
62+
}
63+
if (!hasRemoveListeners) {
64+
console.warn(
65+
'`new NativeEventEmitter()` was called with a non-null argument without the required `removeListeners` method.',
66+
);
67+
}
68+
}
5069
}
5170

5271
addListener<TEvent: $Keys<TEventToArgsMap>>(

0 commit comments

Comments
 (0)