aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/simple-bar/simple-bar-source/lib/hooks/use-widget-refresh.js
diff options
context:
space:
mode:
Diffstat (limited to 'users/ryan/modules/simple-bar/simple-bar-source/lib/hooks/use-widget-refresh.js')
-rwxr-xr-xusers/ryan/modules/simple-bar/simple-bar-source/lib/hooks/use-widget-refresh.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/users/ryan/modules/simple-bar/simple-bar-source/lib/hooks/use-widget-refresh.js b/users/ryan/modules/simple-bar/simple-bar-source/lib/hooks/use-widget-refresh.js
new file mode 100755
index 0000000..cf79515
--- /dev/null
+++ b/users/ryan/modules/simple-bar/simple-bar-source/lib/hooks/use-widget-refresh.js
@@ -0,0 +1,37 @@
1import * as Uebersicht from "uebersicht";
2
3const { React } = Uebersicht;
4
5/**
6 * Custom hook to refresh a widget at a specified frequency.
7 *
8 * @param {boolean} active - Flag indicating whether the widget is active.
9 * @param {Function} getter - Function to fetch or update the widget data.
10 * @param {number} refreshFrequency - Frequency in milliseconds to refresh the widget.
11 */
12export default function useWidgetRefresh(active, getter, refreshFrequency) {
13 const abortableGetter = React.useCallback(
14 (signal) => {
15 if (!active || signal.aborted) return;
16 getter();
17 },
18 [active, getter],
19 );
20
21 React.useEffect(() => {
22 const controller = new AbortController();
23 if (active) {
24 abortableGetter(controller.signal);
25 const interval = setInterval(
26 () => abortableGetter(controller.signal),
27 refreshFrequency,
28 );
29 return () => {
30 controller.abort();
31 clearInterval(interval);
32 };
33 } else {
34 controller.abort();
35 }
36 }, [active, abortableGetter, refreshFrequency]);
37}