mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
* Added sort to all workspaces homepage and remember preference * Fixed feedback * Fixed migration conflict * Fixed feedback
34 lines
996 B
JavaScript
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)
|
|
}
|