Files
react-native/Libraries/Utilities/BackHandler.android.js
T
Vitalii T 14c207d9e1 Refactor: Minor performance improvement of BackHandler.removeEventListener (#34281)
Summary:
I've noticed that `BackHandler.removeEventListener()` performs two same `indexOf()` calls on an array that is not changing. By removing extra `indexOf` we can slightly improve time complexity of `BackHandler.removeEventListener()` from O(2n) to O(n)

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Android] [Fixed] - Remove extra indexOf call in BackHandler.removeEventListener

Pull Request resolved: https://github.com/facebook/react-native/pull/34281

Test Plan:
1. Add the following code to any function component
```javascript
  BackHandler.addEventListener('hardwareBackPress', () => true).remove();
```

2. Press on hardware back button

Expected result: Application closes

Reviewed By: dmitryrykun

Differential Revision: D38198510

Pulled By: javache

fbshipit-source-id: eab6a57689a536623138a4b3ebddbf9ba87d281f
2022-07-27 06:57:21 -07:00

108 lines
2.9 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import NativeDeviceEventManager from '../../Libraries/NativeModules/specs/NativeDeviceEventManager';
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
const DEVICE_BACK_EVENT = 'hardwareBackPress';
type BackPressEventName = 'backPress' | 'hardwareBackPress';
const _backPressSubscriptions = [];
RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function () {
for (let i = _backPressSubscriptions.length - 1; i >= 0; i--) {
if (_backPressSubscriptions[i]()) {
return;
}
}
BackHandler.exitApp();
});
/**
* Detect hardware button presses for back navigation.
*
* Android: Detect hardware back button presses, and programmatically invoke the default back button
* functionality to exit the app if there are no listeners or if none of the listeners return true.
*
* iOS: Not applicable.
*
* The event subscriptions are called in reverse order (i.e. last registered subscription first),
* and if one subscription returns true then subscriptions registered earlier will not be called.
*
* Example:
*
* ```javascript
* BackHandler.addEventListener('hardwareBackPress', function() {
* // this.onMainScreen and this.goBack are just examples, you need to use your own implementation here
* // Typically you would use the navigator here to go to the last state.
*
* if (!this.onMainScreen()) {
* this.goBack();
* return true;
* }
* return false;
* });
* ```
*/
type TBackHandler = {|
+exitApp: () => void,
+addEventListener: (
eventName: BackPressEventName,
handler: () => ?boolean,
) => {remove: () => void, ...},
+removeEventListener: (
eventName: BackPressEventName,
handler: () => ?boolean,
) => void,
|};
const BackHandler: TBackHandler = {
exitApp: function (): void {
if (!NativeDeviceEventManager) {
return;
}
NativeDeviceEventManager.invokeDefaultBackPressHandler();
},
/**
* Adds an event handler. Supported events:
*
* - `hardwareBackPress`: Fires when the Android hardware back button is pressed.
*/
addEventListener: function (
eventName: BackPressEventName,
handler: () => ?boolean,
): {remove: () => void, ...} {
if (_backPressSubscriptions.indexOf(handler) === -1) {
_backPressSubscriptions.push(handler);
}
return {
remove: (): void => BackHandler.removeEventListener(eventName, handler),
};
},
/**
* Removes the event handler.
*/
removeEventListener: function (
eventName: BackPressEventName,
handler: () => ?boolean,
): void {
const index = _backPressSubscriptions.indexOf(handler);
if (index !== -1) {
_backPressSubscriptions.splice(index, 1);
}
},
};
module.exports = BackHandler;