From aebc3f781e32c40faf2ee7fa5a23042e6382ce2e Mon Sep 17 00:00:00 2001 From: かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Fri, 12 Sep 2025 17:12:50 +0900 Subject: perf(frontend): 低精度な現在時刻を一か所で管理するように (#16479) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf(frontend): 低精度な現在時刻を一か所で管理するように * lint * fix * remove unused imports * fix * Update Changelog * [ci skip] typo * enhance: カレンダーウィジェットの日付変更は時間通りに行うように * [ci skip] fix --- .../frontend/src/composables/use-lowres-time.ts | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 packages/frontend/src/composables/use-lowres-time.ts (limited to 'packages/frontend/src/composables') diff --git a/packages/frontend/src/composables/use-lowres-time.ts b/packages/frontend/src/composables/use-lowres-time.ts new file mode 100644 index 0000000000..3c5b561f51 --- /dev/null +++ b/packages/frontend/src/composables/use-lowres-time.ts @@ -0,0 +1,34 @@ +/* + * SPDX-FileCopyrightText: syuilo and misskey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { ref, readonly, computed } from 'vue'; + +const time = ref(Date.now()); + +export const TIME_UPDATE_INTERVAL = 10000; // 10秒 + +/** + * 精度が求められないが定期的に更新しないといけない時計で使用(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(); +}, TIME_UPDATE_INTERVAL); -- cgit v1.2.3-freya