mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
1fc1873c21
commit
114be1d217
@@ -46,7 +46,26 @@ export default class NativeEventEmitter<TEventToArgsMap: {...}>
|
||||
'`new NativeEventEmitter()` requires a non-null argument.',
|
||||
);
|
||||
}
|
||||
this._nativeModule = nativeModule;
|
||||
|
||||
const hasAddListener =
|
||||
!!nativeModule && typeof nativeModule.addListener === 'function';
|
||||
const hasRemoveListeners =
|
||||
!!nativeModule && typeof nativeModule.removeListeners === 'function';
|
||||
|
||||
if (nativeModule && hasAddListener && hasRemoveListeners) {
|
||||
this._nativeModule = nativeModule;
|
||||
} else {
|
||||
if (!hasAddListener) {
|
||||
console.warn(
|
||||
'`new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method.',
|
||||
);
|
||||
}
|
||||
if (!hasRemoveListeners) {
|
||||
console.warn(
|
||||
'`new NativeEventEmitter()` was called with a non-null argument without the required `removeListeners` method.',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addListener<TEvent: $Keys<TEventToArgsMap>>(
|
||||
|
||||
Reference in New Issue
Block a user