1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-07-25 17:55:06 +02:00
Files
misskey/packages/frontend-shared/js/use-interval.ts

60 lines
1.1 KiB
TypeScript

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { onActivated, onDeactivated, onMounted, onUnmounted } from 'vue';
import { createVisibilityAwareInterval } from './interval.js';
export function useInterval(fn: () => void, interval: number, options: {
immediate: boolean;
afterMounted: boolean;
keepRunningWhenHidden?: boolean;
}): (() => void) | undefined {
if (Number.isNaN(interval)) return;
let disposer: (() => void) | null = null;
const start = () => {
if (options.keepRunningWhenHidden) {
if (options.immediate) fn();
const intervalId = window.setInterval(fn, interval);
disposer = () => {
window.clearInterval(intervalId);
};
} else {
disposer = createVisibilityAwareInterval(fn, interval, { immediate: options.immediate });
}
};
const clear = () => {
if (disposer) {
disposer();
disposer = null;
}
};
if (options.afterMounted) {
onMounted(() => {
start();
});
} else {
start();
}
onActivated(() => {
if (disposer) return;
start();
});
onDeactivated(() => {
clear();
});
onUnmounted(() => {
clear();
});
return clear;
}