2
0

🐛 (bot) Fix setTimeout onCanPlay concurrency issues

This commit is contained in:
Baptiste Arnaud
2023-07-12 14:55:25 +02:00
parent abb363b59d
commit 082084a90c
4 changed files with 22 additions and 20 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/js", "name": "@typebot.io/js",
"version": "0.0.76", "version": "0.0.77",
"description": "Javascript library to display typebots on your website", "description": "Javascript library to display typebots on your website",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -9,6 +9,7 @@ type Props = {
const showAnimationDuration = 400 const showAnimationDuration = 400
const defaultTypingDuration = 5000 const defaultTypingDuration = 5000
let isPlayed = false
let typingTimeout: NodeJS.Timeout let typingTimeout: NodeJS.Timeout
@ -18,6 +19,7 @@ export const AudioBubble = (props: Props) => {
const [isTyping, setIsTyping] = createSignal(true) const [isTyping, setIsTyping] = createSignal(true)
const autoPlay = () => { const autoPlay = () => {
isPlayed = true
if (audioElement) if (audioElement)
audioElement audioElement
.play() .play()
@ -25,19 +27,19 @@ export const AudioBubble = (props: Props) => {
props.onTransitionEnd(ref?.offsetTop) props.onTransitionEnd(ref?.offsetTop)
} }
const onCanPlay = () => {
clearTimeout(typingTimeout)
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}
onMount(() => { onMount(() => {
if (audioElement)
audioElement.oncanplay = () => {
if (isPlayed) return
clearTimeout(typingTimeout)
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}
typingTimeout = setTimeout(() => { typingTimeout = setTimeout(() => {
if (audioElement) audioElement.removeEventListener('canplay', onCanPlay) if (isPlayed) return
setIsTyping(false) setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration) setTimeout(autoPlay, showAnimationDuration)
}, defaultTypingDuration) }, defaultTypingDuration)
if (audioElement) audioElement.addEventListener('canplay', onCanPlay)
}) })
onCleanup(() => { onCleanup(() => {

View File

@ -10,6 +10,7 @@ type Props = {
export const showAnimationDuration = 400 export const showAnimationDuration = 400
const defaultTypingDuration = 5000 const defaultTypingDuration = 5000
let isPlayed = false
let typingTimeout: NodeJS.Timeout let typingTimeout: NodeJS.Timeout
@ -19,7 +20,7 @@ export const VideoBubble = (props: Props) => {
const [isTyping, setIsTyping] = createSignal(true) const [isTyping, setIsTyping] = createSignal(true)
const autoPlay = () => { const autoPlay = () => {
console.log(videoElement) isPlayed = true
if (videoElement) if (videoElement)
videoElement videoElement
.play() .play()
@ -27,23 +28,22 @@ export const VideoBubble = (props: Props) => {
props.onTransitionEnd(ref?.offsetTop) props.onTransitionEnd(ref?.offsetTop)
} }
const onCanPlay = () => {
clearTimeout(typingTimeout)
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}
onMount(() => { onMount(() => {
console.log(videoElement) if (videoElement)
videoElement.oncanplay = () => {
if (isPlayed) return
clearTimeout(typingTimeout)
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}
typingTimeout = setTimeout( typingTimeout = setTimeout(
() => { () => {
if (videoElement) videoElement.removeEventListener('canplay', onCanPlay) if (isPlayed) return
setIsTyping(false) setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration) setTimeout(autoPlay, showAnimationDuration)
}, },
videoElement ? defaultTypingDuration : 2000 videoElement ? defaultTypingDuration : 2000
) )
if (videoElement) videoElement.addEventListener('canplay', onCanPlay)
}) })
onCleanup(() => { onCleanup(() => {

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/react", "name": "@typebot.io/react",
"version": "0.0.76", "version": "0.0.77",
"description": "React library to display typebots on your website", "description": "React library to display typebots on your website",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",