fix: add interval to uses of useNow so that it uses less resources #3255

Merged
dpschen merged 6 commits from feature/add-interval-to-use-now into main 2023-03-24 23:30:29 +00:00
3 changed files with 10 additions and 7 deletions

View File

@ -4,8 +4,11 @@ import { useNow } from '@vueuse/core'
import LogoFull from '@/assets/logo-full.svg?component'
import LogoFullPride from '@/assets/logo-full-pride.svg?component'
import {MILLISECONDS_A_HOUR} from '@/constants/date'
const now = useNow()
const now = useNow({
konrad marked this conversation as resolved
Review

We want that the Logo changes at midnight, correct? By setting this to change in an interval of a day this might happen at 23:00 the next day if the logo page with the logo was opened at that time the day before. Could we configure this somehow so that it's triggered every real daychange? Or hour change?

We want that the Logo changes at midnight, correct? By setting this to change in an interval of a day this might happen at 23:00 the next day if the logo page with the logo was opened at that time the day before. Could we configure this somehow so that it's triggered every real daychange? Or hour change?
Review

Calculating how much time left until the point of change + setTimeout (possibly setInterval in case the page is not reloaded for months) should work.

Calculating how much time left until the point of change + `setTimeout` (possibly `setInterval` in case the page is not reloaded for months) should work.
Review

Ideally we want to change the logo at midnight, yes. But since this is not something I'd consider critical and it will be shown for a whole month it should be enough to check this once a day. Even if that means it might lag behind one day.

Ideally we want to change the logo at midnight, yes. But since this is not something I'd consider critical and it will be shown for a whole month it should be enough to check this once a day. Even if that means it might lag behind one day.
Review

We should use the time unit constants for this.

How about using every hour? One tick per hour should really not create any performance issues (as said earlier I think once per second should also not be an issue.

We should use the time unit constants for this. How about using every hour? One tick per hour should really not create any performance issues (as said earlier I think once per second should also not be an issue.
Review

We should use the time unit constants for this.

How about using every hour? One tick per hour should really not create any performance issues (as said earlier I think once per second should also not be an issue.

We should use the time unit constants for this. How about using every hour? One tick per hour should really not create any performance issues (as said earlier I think once per second should also not be an issue.
Review

We should use the time unit constants for this.

Done!

> We should use the time unit constants for this. Done!
Review

How about using every hour? One tick per hour should really not create any performance issues (as said earlier I think once per second should also not be an issue.

and done!

> How about using every hour? One tick per hour should really not create any performance issues (as said earlier I think once per second should also not be an issue. and done!
interval: MILLISECONDS_A_HOUR,
})
const Logo = computed(() => now.value.getMonth() === 5 ? LogoFullPride : LogoFull)
</script>

View File

@ -38,9 +38,8 @@
</template>
<script setup lang="ts">
import {computed, ref, watch, toRefs} from 'vue'
import {computed, ref, watch, toRefs, onActivated} from 'vue'
import {useRouter} from 'vue-router'
import {useNow} from '@vueuse/core'
import {getHexColor} from '@/models/task'
@ -157,7 +156,8 @@ function openTask(e: {
const weekDayFromDate = useWeekDayFromDate()
const today = useNow()
const today = ref(new Date())
onActivated(() => today.value = new Date())
konrad marked this conversation as resolved Outdated

I think it should be onActivated

I think it should be `onActivated`

Because it the view is cached mounted will not trigger again.

Because it the view is cached mounted will not trigger again.

Ah yes, good point. Fixed.

Ah yes, good point. Fixed.
const dateIsToday = computed(() => (date: Date) => {
konrad marked this conversation as resolved Outdated

If we don't change this constantly I think it would be better to only change it when the user switches view. Changing it every 6 hours seems random in comparison.

If we don't change this constantly I think it would be better to only change it when the user switches view. Changing it every 6 hours seems random in comparison.

If we don't change this constantly I think it would be better to only change it when the user switches view.

That's a good point.

> If we don't change this constantly I think it would be better to only change it when the user switches view. That's a good point.

Done!

Done!
return (
date.getDate() === today.value.getDate() &&

View File

@ -1,6 +1,5 @@
import {computed} from 'vue'
import {computed, onActivated, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import {useNow} from '@vueuse/core'
import {useAuthStore} from '@/stores/auth'
import {hourToDaytime} from '@/helpers/hourToDaytime'
@ -9,7 +8,8 @@ export type Daytime = 'night' | 'morning' | 'day' | 'evening'
export function useDaytimeSalutation() {
const {t} = useI18n({useScope: 'global'})
const now = useNow()
const now = ref(new Date())
onActivated(() => now.value = new Date())
konrad marked this conversation as resolved Outdated

Same here

Same here

Done.

Done.
const authStore = useAuthStore()
dpschen marked this conversation as resolved Outdated

Same here.

Same here.

Done!

Done!
const name = computed(() => authStore.userDisplayName)