1 import { useEffect, useRef } from 'react';
3 import { logger } from '@proton/pass/utils/logger';
5 export type ActivityProbe = ReturnType<typeof createActivityProbe>;
7 export const createActivityProbe = () => {
8 const state: { interval?: NodeJS.Timeout } = {};
10 const cancel = () => {
11 logger.debug(`[ActivityProbe] Cancelling probe..`);
12 clearInterval(state?.interval);
15 const start = (onProbe: () => void, timeout: number) => {
16 logger.debug(`[ActivityProbe] probe set to ${timeout}ms`);
18 clearInterval(state?.interval);
19 state.interval = setInterval(onProbe, timeout);
23 return { start, cancel };
26 export const useActivityProbe = (): ActivityProbe => {
27 const probe = useRef(createActivityProbe()).current;
28 useEffect(() => () => probe.cancel(), []);