mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
9cdd0db138
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51614 `JSEventLoopWatchdog` is not used in react-native package. This diff moves it to rn-tester which previously deep imported it from react-native (which we want to avoid). Changelog: [Internal] Reviewed By: huntie Differential Revision: D75410548 fbshipit-source-id: d4996742578e3b068e7acad9479394388b1907ac
98 lines
2.5 KiB
JavaScript
98 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.
|
|
*
|
|
* @flow strict-local
|
|
* @format
|
|
*/
|
|
|
|
import type {RNTesterJsStallsState} from '../types/RNTesterTypes';
|
|
|
|
import {useCallback, useEffect, useState} 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] =
|
|
useState<RNTesterJsStallsState>(INITIAL_STATE);
|
|
|
|
const {stallIntervalId} = stallsState;
|
|
|
|
useEffect(() => {
|
|
return () => clearInterval(stallIntervalId);
|
|
}, [stallIntervalId]);
|
|
|
|
const onDisableForceJsStalls = useCallback(
|
|
() => setStallsState(state => ({...state, stallIntervalId: null})),
|
|
[],
|
|
);
|
|
|
|
const onEnableForceJsStalls = 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 = useCallback(() => {
|
|
const JSEventLoopWatchdog = require('./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 = useCallback(() => {
|
|
console.warn('Cannot disable yet...');
|
|
}, []);
|
|
|
|
return {
|
|
state: stallsState,
|
|
onDisableForceJsStalls,
|
|
onEnableForceJsStalls,
|
|
onEnableJsStallsTracking,
|
|
onDisableJsStallsTracking,
|
|
};
|
|
};
|
|
|
|
export default useJsStalls;
|