Skip to content

Commit 1049835

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Simplify Keyboard
Summary: Simplifies `Keyboard` by removing redundant methods and changing `addEventListener` to return an `EventSubscription`. Changelog: [General][Changed] - `Keyboard.addListener` now returns an `EventSubscription` object. [General][Removed] - Removed `Keyboard.removeListener`. Instead, use the `remove()` method on the object returned by `Keyboard.addListener`. [General][Removed] - `Keyboard` no longer inherits from `NativeEventEmitter`, so it no longer implements `removeAllListeners`, and `removeSubscription`. Reviewed By: milroc Differential Revision: D26163536 fbshipit-source-id: b4bd91627cd027a13fcba269a253823913eb7589
1 parent 88a41f1 commit 1049835

File tree

2 files changed

+9
-35
lines changed

2 files changed

+9
-35
lines changed

Libraries/Components/Keyboard/Keyboard.js

+9-22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import NativeEventEmitter from '../../EventEmitter/NativeEventEmitter';
1212
import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation';
1313
import dismissKeyboard from '../../Utilities/dismissKeyboard';
1414
import NativeKeyboardObserver from './NativeKeyboardObserver';
15-
import type EmitterSubscription from '../../vendor/emitter/_EmitterSubscription';
15+
import {type EventSubscription} from '../../vendor/emitter/EventEmitter';
1616

1717
export type KeyboardEventName = $Keys<KeyboardEventDefinitions>;
1818

@@ -101,10 +101,10 @@ type KeyboardEventDefinitions = {
101101
*```
102102
*/
103103

104-
class Keyboard extends NativeEventEmitter<KeyboardEventDefinitions> {
105-
constructor() {
106-
super(NativeKeyboardObserver);
107-
}
104+
class Keyboard {
105+
_emitter: NativeEventEmitter<KeyboardEventDefinitions> = new NativeEventEmitter(
106+
NativeKeyboardObserver,
107+
);
108108

109109
/**
110110
* The `addListener` function connects a JavaScript function to an identified native
@@ -132,22 +132,9 @@ class Keyboard extends NativeEventEmitter<KeyboardEventDefinitions> {
132132
addListener<K: $Keys<KeyboardEventDefinitions>>(
133133
eventType: K,
134134
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
135-
context: $FlowFixMe,
136-
): EmitterSubscription<KeyboardEventDefinitions, K> {
137-
return super.addListener(eventType, listener);
138-
}
139-
140-
/**
141-
* Removes a specific listener.
142-
*
143-
* @param {string} eventName The `nativeEvent` is the string that identifies the event you're listening for.
144-
* @param {function} callback function to be called when the event fires.
145-
*/
146-
removeListener<K: $Keys<KeyboardEventDefinitions>>(
147-
eventType: K,
148-
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
149-
): void {
150-
super.removeListener(eventType, listener);
135+
context?: mixed,
136+
): EventSubscription {
137+
return this._emitter.addListener(eventType, listener);
151138
}
152139

153140
/**
@@ -156,7 +143,7 @@ class Keyboard extends NativeEventEmitter<KeyboardEventDefinitions> {
156143
* @param {string} eventType The native event string listeners are watching which will be removed.
157144
*/
158145
removeAllListeners<K: $Keys<KeyboardEventDefinitions>>(eventType: ?K): void {
159-
super.removeAllListeners(eventType);
146+
this._emitter.removeAllListeners(eventType);
160147
}
161148

162149
/**

Libraries/Components/Keyboard/__tests__/Keyboard-test.js

-13
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
* @emails oncall+react_native
1010
*/
1111

12-
const NativeModules = require('../../../BatchedBridge/NativeModules');
1312
const LayoutAnimation = require('../../../LayoutAnimation/LayoutAnimation');
1413
const dismissKeyboard = require('../../../Utilities/dismissKeyboard');
1514
const Keyboard = require('../Keyboard');
1615

17-
import NativeEventEmitter from '../../../EventEmitter/NativeEventEmitter';
18-
1916
jest.mock('../../../LayoutAnimation/LayoutAnimation');
2017
jest.mock('../../../Utilities/dismissKeyboard');
2118

@@ -24,16 +21,6 @@ describe('Keyboard', () => {
2421
jest.resetAllMocks();
2522
});
2623

27-
it('exposes KeyboardEventEmitter methods', () => {
28-
const KeyboardObserver = NativeModules.KeyboardObserver;
29-
const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver);
30-
31-
// $FlowFixMe
32-
expect(Keyboard._subscriber).toBe(KeyboardEventEmitter._subscriber);
33-
// $FlowFixMe Cannot access private property
34-
expect(Keyboard._nativeModule).toBe(KeyboardEventEmitter._nativeModule);
35-
});
36-
3724
it('uses dismissKeyboard utility', () => {
3825
Keyboard.dismiss();
3926
expect(dismissKeyboard).toHaveBeenCalled();

0 commit comments

Comments
 (0)