-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce global time tick and format time passed since epoch (#151)
* Introduce global time tick and format time passed since epoch --------- Co-authored-by: D13ce <[email protected]>
- Loading branch information
1 parent
a3deceb
commit cc9c2f9
Showing
13 changed files
with
169 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<script setup lang="ts"> | ||
import type { StringUnitLength } from 'luxon' | ||
import { formatEpochToRelative } from '~/utils/format' | ||
interface Props { | ||
value?: number, | ||
type?: 'epoch', // we can add slot and other types later when needed, we default to epoch | ||
noUpdate?: boolean, | ||
unitLength?: StringUnitLength | ||
} | ||
const props = defineProps<Props>() | ||
const { t: $t } = useI18n() | ||
const { timestamp } = useDate() | ||
const initTs = ref(timestamp.value) // store the initial timestamp, in case we don't want to auto update | ||
const label = computed(() => { | ||
if (props.value === undefined) { | ||
return | ||
} | ||
const ts: number = props.noUpdate ? initTs.value : timestamp.value | ||
switch (props.type) { | ||
default: | ||
return formatEpochToRelative(props.value, ts, props.unitLength, $t('locales.date')) | ||
} | ||
}) | ||
</script> | ||
<template> | ||
<span v-if="label">{{ label }}</span> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { inject } from 'vue' | ||
import type { DateInfo } from '~/types/date' | ||
|
||
export function useDate () { | ||
const date = inject<DateInfo>('date-info') | ||
|
||
if (!date) { | ||
throw new Error('useDate must be in a child of useDateProvider') | ||
} | ||
|
||
return date | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ref, provide } from 'vue' | ||
import type { DateInfo } from '~/types/date' | ||
|
||
// useDateProvider provides a global reactive timestamp, which should be more performant than every component ticking their own time. | ||
// -> a global heartbeat | ||
export function useDateProvider () { | ||
const date = ref(new Date()) | ||
const timestamp = computed(() => date.value.getTime()) | ||
let interval:NodeJS.Timeout | ||
|
||
const upDate = () => { | ||
date.value = new Date() | ||
} | ||
|
||
onMounted(() => { | ||
interval = setInterval(() => upDate(), 1000) | ||
}) | ||
|
||
onUnmounted(() => { | ||
interval && clearInterval(interval) | ||
}) | ||
|
||
provide<DateInfo>('date-info', { date, timestamp }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
export function useInterval (ms: number) { | ||
const refreshInterval = ref<NodeJS.Timeout | null>(null) | ||
const tick = ref<number>(0) | ||
import { DateTime } from 'luxon' | ||
|
||
onMounted(() => { | ||
refreshInterval.value = setInterval(() => { tick.value = new Date().getTime() }, ms) | ||
}) | ||
onUnmounted(() => { | ||
refreshInterval.value && clearInterval(refreshInterval.value) | ||
export function useInterval (seconds: number) { | ||
const { timestamp } = useDate() | ||
const tick = ref<number>(timestamp.value) | ||
|
||
watch(timestamp, (ts) => { | ||
if (!seconds) { | ||
return | ||
} | ||
const dt = DateTime.fromMillis(ts) | ||
if (!tick.value || dt.diff(DateTime.fromMillis(tick.value), 'seconds').seconds >= seconds) { | ||
tick.value = ts | ||
} | ||
}) | ||
|
||
return { tick } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type DateInfo = { | ||
date: globalThis.Ref<Date>, | ||
timestamp: globalThis.Ref<number>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters