Skip to content

Commit

Permalink
fix(ui): debounce reactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 27, 2023
1 parent cc5cdc6 commit caac05c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 12 deletions.
8 changes: 7 additions & 1 deletion client/app.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts" setup>
import { useDebounceFn } from '@vueuse/core'
import { containerWidth, description, path, refreshSources, rpc } from './util/logic'
import { devtoolsClient } from '~/composables/devtools-client'
Expand All @@ -21,6 +22,11 @@ const constrainsWidth = computed(() => {
const config = await rpc.useServerConfig()
const options = await fetchOptions()
const setPath = useDebounceFn((e) => {
path.value = e.target.value
refreshSources()
}, 1000)
</script>

<template>
Expand Down Expand Up @@ -101,7 +107,7 @@ const options = await fetchOptions()
Path
</div>
<div class="flex items-center space-x-1">
<NTextInput v-model="path" placeholder="Search..." n="primary" @input="refreshSources" />
<NTextInput :model-value="path" placeholder="Search..." n="primary" @input="setPath" />
</div>
</div>
<div v-if="description" class="text-xs opacity-70">
Expand Down
7 changes: 4 additions & 3 deletions client/components/IFrameLoader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const props = defineProps({
description: String,
})
defineEmits(['refresh'])
const src = ref(props.src)
const iframe = ref()
const timeTakenMs = ref(0)
Expand All @@ -30,11 +30,12 @@ const setSource = useDebounceFn(() => {
frame.style.opacity = '1'
timeTakenMs.value = Date.now() - now
}
frame.src = `${props.src}&scale=${scale}`
frame.src = `${src.value}&scale=${scale}`
}, 200)
onMounted(() => {
watch(() => props.src, (src) => {
watch(() => props.src, (val) => {
src.value = val
setSource()
}, {
immediate: true,
Expand Down
2 changes: 0 additions & 2 deletions client/components/ImageLoader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const props = defineProps({
description: String,
})
defineEmits(['refresh'])
const image = ref()
const timeTakenMs = ref(0)
Expand Down
3 changes: 1 addition & 2 deletions client/pages/png.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { absoluteBasePath, refreshSources, refreshTime, rpc } from '~/util/logic'
import { absoluteBasePath, refreshTime, rpc } from '~/util/logic'
const config = await rpc.useServerConfig()
Expand All @@ -16,6 +16,5 @@ const options = await fetchOptions()
:src="`${absoluteBasePath}/__og_image__/og.png?timestamp=${refreshTime}`"
:aspect-ratio="aspectRatio"
description="[PNG] Generated in %sms using Satori & Resvg."
@refresh="refreshSources"
/>
</template>
3 changes: 1 addition & 2 deletions client/pages/svg.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { absoluteBasePath, refreshSources, refreshTime, rpc } from '~/util/logic'
import { absoluteBasePath, refreshTime, rpc } from '~/util/logic'
const config = await rpc.useServerConfig()
Expand All @@ -16,6 +16,5 @@ const options = await fetchOptions()
:src="`${absoluteBasePath}/__og_image__/svg?timestamp=${refreshTime}`"
:aspect-ratio="aspectRatio"
description="[SVG] Generated in %sms using Satori."
@refresh="refreshSources"
/>
</template>
5 changes: 3 additions & 2 deletions client/util/logic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref } from 'vue'
import { joinURL } from 'ufo'
import { useDebounceFn } from '@vueuse/core'
import type { PlaygroundClientFunctions } from '../../src/types'

export const refreshTime = ref(Date.now())
Expand All @@ -11,9 +12,9 @@ export const path = ref('/')
export const optionsPath = computed(() => joinURL(path.value as string, '__og_image__/options'))
export const vnodePath = computed(() => joinURL(path.value as string, '__og_image__/vnode'))

export function refreshSources() {
export const refreshSources = useDebounceFn(() => {
refreshTime.value = Date.now()
}
}, 200)

const clientFunctions: PlaygroundClientFunctions = {
refresh() {
Expand Down

0 comments on commit caac05c

Please sign in to comment.