From db3df3400a80353a0140442a5045eca344dfc76f Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 24 Jul 2017 03:34:05 -0700 Subject: [PATCH] Breaking - make RCTDeviceEventEmitter warnings fatal Reviewed By: davidaurelio Differential Revision: D5469832 fbshipit-source-id: 1eb4ad3a2b3e1fa10d75e42d1d2f7baa291cb6f4 --- .../EventEmitter/RCTDeviceEventEmitter.js | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/Libraries/EventEmitter/RCTDeviceEventEmitter.js b/Libraries/EventEmitter/RCTDeviceEventEmitter.js index 3e3195e8c..01e2e0ac5 100644 --- a/Libraries/EventEmitter/RCTDeviceEventEmitter.js +++ b/Libraries/EventEmitter/RCTDeviceEventEmitter.js @@ -16,6 +16,20 @@ const EventSubscriptionVendor = require('EventSubscriptionVendor'); import type EmitterSubscription from 'EmitterSubscription'; +function checkNativeEventModule(eventType: ?string) { + if (eventType) { + if (eventType.lastIndexOf('statusBar', 0) === 0) { + throw new Error('`' + eventType + '` event should be registered via the StatusBarIOS module'); + } + if (eventType.lastIndexOf('keyboard', 0) === 0) { + throw new Error('`' + eventType + '` event should be registered via the Keyboard module'); + } + if (eventType === 'appStateDidChange' || eventType === 'memoryWarning') { + throw new Error('`' + eventType + '` event should be registered via the AppState module'); + } + } +} + /** * Deprecated - subclass NativeEventEmitter to create granular event modules instead of * adding all event listeners directly to RCTDeviceEventEmitter. @@ -30,34 +44,19 @@ class RCTDeviceEventEmitter extends EventEmitter { this.sharedSubscriber = sharedSubscriber; } - _nativeEventModule(eventType: ?string) { - if (eventType) { - if (eventType.lastIndexOf('statusBar', 0) === 0) { - console.warn('`%s` event should be registered via the StatusBarIOS module', eventType); - return require('StatusBarIOS'); - } - if (eventType.lastIndexOf('keyboard', 0) === 0) { - console.warn('`%s` event should be registered via the Keyboard module', eventType); - return require('Keyboard'); - } - if (eventType === 'appStateDidChange' || eventType === 'memoryWarning') { - console.warn('`%s` event should be registered via the AppState module', eventType); - return require('AppState'); - } - } - return null; - } addListener(eventType: string, listener: Function, context: ?Object): EmitterSubscription { - const eventModule = this._nativeEventModule(eventType); - return eventModule ? eventModule.addListener(eventType, listener, context) - : super.addListener(eventType, listener, context); + if (__DEV__) { + checkNativeEventModule(eventType); + } + return super.addListener(eventType, listener, context); } removeAllListeners(eventType: ?string) { - const eventModule = this._nativeEventModule(eventType); - (eventModule && eventType) ? eventModule.removeAllListeners(eventType) - : super.removeAllListeners(eventType); + if (__DEV__) { + checkNativeEventModule(eventType); + } + super.removeAllListeners(eventType); } removeSubscription(subscription: EmitterSubscription) {