Skip to content

Commit

Permalink
RN: Simplify Keyboard
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yungsters authored and facebook-github-bot committed Feb 9, 2021
1 parent 88a41f1 commit 1049835
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
31 changes: 9 additions & 22 deletions Libraries/Components/Keyboard/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import NativeEventEmitter from '../../EventEmitter/NativeEventEmitter';
import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation';
import dismissKeyboard from '../../Utilities/dismissKeyboard';
import NativeKeyboardObserver from './NativeKeyboardObserver';
import type EmitterSubscription from '../../vendor/emitter/_EmitterSubscription';
import {type EventSubscription} from '../../vendor/emitter/EventEmitter';

export type KeyboardEventName = $Keys<KeyboardEventDefinitions>;

Expand Down Expand Up @@ -101,10 +101,10 @@ type KeyboardEventDefinitions = {
*```
*/

class Keyboard extends NativeEventEmitter<KeyboardEventDefinitions> {
constructor() {
super(NativeKeyboardObserver);
}
class Keyboard {
_emitter: NativeEventEmitter<KeyboardEventDefinitions> = new NativeEventEmitter(
NativeKeyboardObserver,
);

/**
* The `addListener` function connects a JavaScript function to an identified native
Expand Down Expand Up @@ -132,22 +132,9 @@ class Keyboard extends NativeEventEmitter<KeyboardEventDefinitions> {
addListener<K: $Keys<KeyboardEventDefinitions>>(
eventType: K,
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
context: $FlowFixMe,
): EmitterSubscription<KeyboardEventDefinitions, K> {
return super.addListener(eventType, listener);
}

/**
* Removes a specific listener.
*
* @param {string} eventName The `nativeEvent` is the string that identifies the event you're listening for.
* @param {function} callback function to be called when the event fires.
*/
removeListener<K: $Keys<KeyboardEventDefinitions>>(
eventType: K,
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
): void {
super.removeListener(eventType, listener);
context?: mixed,
): EventSubscription {
return this._emitter.addListener(eventType, listener);
}

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

/**
Expand Down
13 changes: 0 additions & 13 deletions Libraries/Components/Keyboard/__tests__/Keyboard-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
* @emails oncall+react_native
*/

const NativeModules = require('../../../BatchedBridge/NativeModules');
const LayoutAnimation = require('../../../LayoutAnimation/LayoutAnimation');
const dismissKeyboard = require('../../../Utilities/dismissKeyboard');
const Keyboard = require('../Keyboard');

import NativeEventEmitter from '../../../EventEmitter/NativeEventEmitter';

jest.mock('../../../LayoutAnimation/LayoutAnimation');
jest.mock('../../../Utilities/dismissKeyboard');

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

it('exposes KeyboardEventEmitter methods', () => {
const KeyboardObserver = NativeModules.KeyboardObserver;
const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver);

// $FlowFixMe
expect(Keyboard._subscriber).toBe(KeyboardEventEmitter._subscriber);
// $FlowFixMe Cannot access private property
expect(Keyboard._nativeModule).toBe(KeyboardEventEmitter._nativeModule);
});

it('uses dismissKeyboard utility', () => {
Keyboard.dismiss();
expect(dismissKeyboard).toHaveBeenCalled();
Expand Down

0 comments on commit 1049835

Please sign in to comment.