misskey/packages/frontend/src/composables/use-lowres-time.ts

33 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { ref, readonly, computed } from 'vue';
const time = ref(Date.now());
/**
* 精度が求められないが定期的に更新しないといけない時計で使用10秒に一度更新
* tickを各コンポーネントで行うのではなく、ここで一括して行うことでパフォーマンスを改善する。
*
* ※ マウント前の時刻を返す可能性があるため、通常は`useLowresTime`を使用する
*/
export const lowresTime = readonly(time);
/**
* 精度が求められないが定期的に更新しないといけない時計で使用10秒に一度更新
* tickを各コンポーネントで行うのではなく、ここで一括して行うことでパフォーマンスを改善する。
*
* 必ず現在時刻以降を返すことを保証するコンポーサブル
*/
export function useLowresTime() {
// lowresTime自体はマウント前の時刻を返す可能性があるため、必ず現在時刻以降を返すことを保証する
const now = Date.now();
return computed(() => Math.max(time.value, now));
}
window.setInterval(() => {
time.value = Date.now();
}, 10000);