Files
Bram eb1c1c613e All workspaces homepage [5/7] All workspaces sort functionality (#6002)
* Added sort to all workspaces homepage and remember preference

* Fixed feedback

* Fixed migration conflict

* Fixed feedback
2026-09-25 13:04:06 +02:00

34 lines
996 B
JavaScript

import { inject, onBeforeUnmount, onMounted, provide, ref } from 'vue'
export const nowKey = Symbol('now')
/**
* A timestamp that keeps ticking while the page is open, so relative dates
* computed from it keep ageing instead of freezing at the moment they were
* rendered. One page provides it and every card reads the same value, so a
* long list costs a single timer.
*
* @param {number} intervalMs How often the value advances.
* @returns {import('vue').Ref<number>} The current time in milliseconds.
*/
export function provideNow(intervalMs = 60 * 1000) {
const now = ref(Date.now())
let timer = null
onMounted(() => {
timer = setInterval(() => {
now.value = Date.now()
}, intervalMs)
})
onBeforeUnmount(() => clearInterval(timer))
provide(nowKey, now)
return now
}
/**
* @returns {import('vue').Ref<number>|null} The provided clock, or `null`
* outside of a page that provides one.
*/
export function injectNow() {
return inject(nowKey, null)
}