diff --git a/ts/hooks/useInterval.ts b/ts/hooks/useInterval.ts new file mode 100644 index 000000000..793a8a0f3 --- /dev/null +++ b/ts/hooks/useInterval.ts @@ -0,0 +1,25 @@ +import React from 'react'; + +export const useInterval = (callback: any, delay: number | null) => { + const savedCallback = React.useRef(); + + React.useEffect(() => { + savedCallback.current = callback; + }, [callback]); + + React.useEffect(() => { + function tick() { + if (savedCallback && savedCallback.current && savedCallback.current) { + savedCallback.current(); + } + } + if (delay !== null) { + const id = global.setInterval(tick, delay); + tick(); + return () => { + global.clearInterval(id); + }; + } + return; + }, [delay]); +}; diff --git a/ts/components/session/network/useNetwork.ts b/ts/hooks/useNetwork.ts similarity index 100% rename from ts/components/session/network/useNetwork.ts rename to ts/hooks/useNetwork.ts