Skip to content

Commit

Permalink
fix(useSpeechSynthesis): remove duplicated onend, fix demo (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
sibbng committed Jul 17, 2022
1 parent 6202b6e commit a524b37
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
10 changes: 3 additions & 7 deletions packages/core/useSpeechSynthesis/demo.vue
@@ -1,22 +1,18 @@
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { onMounted, ref } from 'vue'
import { useSpeechSynthesis } from '@vueuse/core'
const voice = ref<SpeechSynthesisVoice>({ lang: 'en-US' } as SpeechSynthesisVoice)
const voice = ref<SpeechSynthesisVoice>(undefined as unknown as SpeechSynthesisVoice)
const text = ref('Hello, everyone! Good morning!')
const speech = useSpeechSynthesis(text, {
lang: voice.value.lang,
voice,
})
let synth: SpeechSynthesis
const voices = ref<SpeechSynthesisVoice[]>([])
watch(voice, (newVoice) => {
speech.utterance.value.voice = newVoice
})
onMounted(() => {
if (speech.isSupported.value) {
// load at last
Expand Down
11 changes: 6 additions & 5 deletions packages/core/useSpeechSynthesis/index.ts
Expand Up @@ -98,11 +98,6 @@ export function useSpeechSynthesis(text: MaybeComputedRef<string>, options: UseS
utterance.onerror = (event) => {
error.value = event
}

utterance.onend = () => {
isPlaying.value = false
utterance.lang = unref(lang)
}
}

const utterance = computed(() => {
Expand All @@ -126,6 +121,12 @@ export function useSpeechSynthesis(text: MaybeComputedRef<string>, options: UseS
utterance.value.lang = lang
})

if (options.voice) {
watch(options.voice, () => {
synth!.cancel()
})
}

watch(isPlaying, () => {
if (isPlaying.value)
synth!.resume()
Expand Down

0 comments on commit a524b37

Please sign in to comment.