element-portable/src/hooks/useTimeout.ts
David Langley 491f0cd08a
Change license (#13)
* Copyright headers 1

* Licence headers 2

* Copyright Headers 3

* Copyright Headers 4

* Copyright Headers 5

* Copyright Headers 6

* Copyright headers 7

* Add copyright headers for html and config file

* Replace license files and update package.json

* Update with CLA

* lint
2024-09-09 13:57:16 +00:00

59 lines
1.8 KiB
TypeScript

/*
Copyright 2024 New Vector Ltd.
Copyright 2020 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { useEffect, useRef, useState } from "react";
type Handler = () => void;
// Hook to simplify timeouts in functional components
export const useTimeout = (handler: Handler, timeoutMs: number): void => {
// Create a ref that stores handler
const savedHandler = useRef<Handler>();
// Update ref.current value if handler changes.
useEffect(() => {
savedHandler.current = handler;
}, [handler]);
// Set up timer
useEffect(() => {
const timeoutID = window.setTimeout(() => {
savedHandler.current?.();
}, timeoutMs);
return () => clearTimeout(timeoutID);
}, [timeoutMs]);
};
// Hook to simplify intervals in functional components
export const useInterval = (handler: Handler, intervalMs: number): void => {
// Create a ref that stores handler
const savedHandler = useRef<Handler>();
// Update ref.current value if handler changes.
useEffect(() => {
savedHandler.current = handler;
}, [handler]);
// Set up timer
useEffect(() => {
const intervalID = window.setInterval(() => {
savedHandler.current?.();
}, intervalMs);
return () => clearInterval(intervalID);
}, [intervalMs]);
};
// Hook to simplify a variable counting down to 0, handler called when it reached 0
export const useExpiringCounter = (handler: Handler, intervalMs: number, initialCount: number): number => {
const [count, setCount] = useState(initialCount);
useInterval(() => setCount((c) => c - 1), intervalMs);
if (count === 0) {
handler();
}
return count;
};