Files
react-native/packages/rn-tester/js/utils/useJsStalls.js
T
Iwo Plaza bdc23fa2b4 Migrate files in Libraries/Interaction to use export syntax (#48933)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48933

## Motivation
Modernising the react-native codebase to allow for ingestion by modern Flow tooling

## This diff
- Updates files in Libraries/Interaction to use `export` syntax
- Appends `.default` to requires of the changed files.
- Updates the public API snapshot (intented breaking change)

Changelog:
[General][Breaking] - Files inside `Libraries/Interaction` use `export` syntax, which requires the addition of `.default` when imported with the CJS `require` syntax.

Reviewed By: huntie

Differential Revision: D68629953

fbshipit-source-id: 526b18d9b64c4b27b6e3198a9725075fa11e345a
2025-01-27 06:32:58 -08:00

99 lines
2.5 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.
*
* @format
* @flow strict-local
*/
import type {RNTesterJsStallsState} from '../types/RNTesterTypes';
import * as React from 'react';
const INITIAL_STATE: RNTesterJsStallsState = {
stallIntervalId: null,
busyTime: null,
filteredStall: 0,
tracking: false,
};
const FILTERED_STALL_MULTIPLIER = 0.97;
const BUSY_TIME_MULTIPLIER = 1 - FILTERED_STALL_MULTIPLIER;
const useJsStalls = (): ({
onDisableForceJsStalls: () => void,
onDisableJsStallsTracking: () => void,
onEnableForceJsStalls: () => void,
onEnableJsStallsTracking: () => void,
state: RNTesterJsStallsState,
}) => {
const [stallsState, setStallsState] =
React.useState<RNTesterJsStallsState>(INITIAL_STATE);
const {stallIntervalId} = stallsState;
React.useEffect(() => {
return () => clearInterval(stallIntervalId);
}, [stallIntervalId]);
const onDisableForceJsStalls = React.useCallback(
() => setStallsState(state => ({...state, stallIntervalId: null})),
[],
);
const onEnableForceJsStalls = React.useCallback(() => {
const intervalId = setInterval(() => {
const start = Date.now();
console.warn('burn CPU');
while (Date.now() - start < 100) {}
}, 300);
setStallsState(state => ({...state, stallIntervalId: intervalId}));
}, []);
const onEnableJsStallsTracking = React.useCallback(() => {
const JSEventLoopWatchdog =
require('react-native/Libraries/Interaction/JSEventLoopWatchdog').default;
JSEventLoopWatchdog.install({thresholdMS: 25});
setStallsState(state => ({...state, tracking: true}));
JSEventLoopWatchdog.addHandler({
onStall: ({busyTime}) =>
setStallsState(state => {
// If previous interval was cleared
if (!state.stallIntervalId) {
return state;
}
return {
...state,
busyTime: busyTime || state.busyTime,
filteredStall:
state.filteredStall * FILTERED_STALL_MULTIPLIER +
(busyTime || 0) * BUSY_TIME_MULTIPLIER,
};
}),
});
}, []);
const onDisableJsStallsTracking = React.useCallback(() => {
console.warn('Cannot disable yet...');
}, []);
return {
state: stallsState,
onDisableForceJsStalls,
onEnableForceJsStalls,
onEnableJsStallsTracking,
onDisableJsStallsTracking,
};
};
export default useJsStalls;