mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-25 03:20:23 +01:00
d359429f04
* Stop GasFeeController polling when pop closes * Stop estimate gas polling on window unload * lint + comments * Improve client closed logic * lint * Add back _beforeUnload on unmount in gas-modal-page-container * Add full check and call onClientClosed method for notifcation environment * Add gas pollingToken tracking to appStateController and use to disconnect polling for each environment type * remove unused method * move controller manipulation logic from background.js to metamask-controller, disaggregate methods * add beforeunload handling to reset gas polling tokens from root of send page * cleanup, lint and address feedback * clear appState gasPollingTokens when all instances of all env types are closed, fix pollingTokenType arg from onEnvironmentTypeClosed call in metamask-controller * mock new methods to fix tests * final bit of cleanup + comments Co-authored-by: Dan Miller <danjm.com@gmail.com>
48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
import { useEffect } from 'react';
|
|
import {
|
|
disconnectGasFeeEstimatePoller,
|
|
getGasFeeEstimatesAndStartPolling,
|
|
addPollingTokenToAppState,
|
|
removePollingTokenFromAppState,
|
|
} from '../store/actions';
|
|
|
|
/**
|
|
* Provides a reusable hook that can be used for safely updating the polling
|
|
* data in the gas fee controller. It makes a request to get estimates and
|
|
* begin polling, keeping track of the poll token for the lifetime of the hook.
|
|
* It then disconnects polling upon unmount. If the hook is unmounted while waiting
|
|
* for `getGasFeeEstimatesAndStartPolling` to resolve, the `active` flag ensures
|
|
* that a call to disconnect happens after promise resolution.
|
|
*/
|
|
export function useSafeGasEstimatePolling() {
|
|
useEffect(() => {
|
|
let active = true;
|
|
let pollToken;
|
|
|
|
const cleanup = () => {
|
|
active = false;
|
|
if (pollToken) {
|
|
disconnectGasFeeEstimatePoller(pollToken);
|
|
removePollingTokenFromAppState(pollToken);
|
|
}
|
|
};
|
|
|
|
getGasFeeEstimatesAndStartPolling().then((newPollToken) => {
|
|
if (active) {
|
|
pollToken = newPollToken;
|
|
addPollingTokenToAppState(pollToken);
|
|
} else {
|
|
disconnectGasFeeEstimatePoller(newPollToken);
|
|
removePollingTokenFromAppState(pollToken);
|
|
}
|
|
});
|
|
|
|
window.addEventListener('beforeunload', cleanup);
|
|
|
|
return () => {
|
|
cleanup();
|
|
window.removeEventListener('beforeunload', cleanup);
|
|
};
|
|
}, []);
|
|
}
|