Skip to content

Commit

Permalink
🐛 (bot) Fix setTimeout onCanPlay concurrency issues
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jul 12, 2023
1 parent abb363b commit 082084a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.0.76",
"version": "0.0.77",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Props = {

const showAnimationDuration = 400
const defaultTypingDuration = 5000
let isPlayed = false

let typingTimeout: NodeJS.Timeout

Expand All @@ -18,26 +19,27 @@ export const AudioBubble = (props: Props) => {
const [isTyping, setIsTyping] = createSignal(true)

const autoPlay = () => {
isPlayed = true
if (audioElement)
audioElement
.play()
.catch((e) => console.warn('Could not autoplay the audio:', e))
props.onTransitionEnd(ref?.offsetTop)
}

const onCanPlay = () => {
clearTimeout(typingTimeout)
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}

onMount(() => {
if (audioElement)
audioElement.oncanplay = () => {
if (isPlayed) return
clearTimeout(typingTimeout)
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}
typingTimeout = setTimeout(() => {
if (audioElement) audioElement.removeEventListener('canplay', onCanPlay)
if (isPlayed) return
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}, defaultTypingDuration)
if (audioElement) audioElement.addEventListener('canplay', onCanPlay)
})

onCleanup(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Props = {

export const showAnimationDuration = 400
const defaultTypingDuration = 5000
let isPlayed = false

let typingTimeout: NodeJS.Timeout

Expand All @@ -19,31 +20,30 @@ export const VideoBubble = (props: Props) => {
const [isTyping, setIsTyping] = createSignal(true)

const autoPlay = () => {
console.log(videoElement)
isPlayed = true
if (videoElement)
videoElement
.play()
.catch((e) => console.warn('Could not autoplay the video:', e))
props.onTransitionEnd(ref?.offsetTop)
}

const onCanPlay = () => {
clearTimeout(typingTimeout)
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}

onMount(() => {
console.log(videoElement)
if (videoElement)
videoElement.oncanplay = () => {
if (isPlayed) return
clearTimeout(typingTimeout)
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
}
typingTimeout = setTimeout(
() => {
if (videoElement) videoElement.removeEventListener('canplay', onCanPlay)
if (isPlayed) return
setIsTyping(false)
setTimeout(autoPlay, showAnimationDuration)
},
videoElement ? defaultTypingDuration : 2000
)
if (videoElement) videoElement.addEventListener('canplay', onCanPlay)
})

onCleanup(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.0.76",
"version": "0.0.77",
"description": "React library to display typebots on your website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

2 comments on commit 082084a

@vercel
Copy link

@vercel vercel bot commented on 082084a Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

bii.bj
1stop.au
wasap.nl
x.cr8.ai
yobot.me
klujo.com
me.cr8.ai
wachat.io
wassep.io
247987.com
8jours.top
aginap.com
ai.mprs.in
bee.cr8.ai
bot.aws.bj
bot.bbc.bj
cat.cr8.ai
finplex.be
jxi.cr8.ai
nepkit.com
pig.cr8.ai
sat.cr8.ai
typebot.io
wachats.me
wsapio.com
blogely.com
bot.aipr.kr
bot.joof.it
bot.kloo.me
broprio.com
bull.cr8.ai
docs.cr8.ai
getyour.sbs
icon.cr8.ai
minipost.uk
mole.cr8.ai
nurraysa.me
team.cr8.ai
wolf.cr8.ai
ai.meant.com
bet7k.online
bot.afric.ai
bot.grace.bj
cinecorn.com
gniorder.com
help.taxt.co
kusamint.com
receita.info
link.venturasuceder.com
mainmenu.diddancing.com
manualhandlingcourse.ie
mdb.baleia.progenbr.com
mdb.equipe.progenbr.com
mdb.evento.progenbr.com
prebook.enthrallart.com
primitive-shapes.cr8.ai
register.kandabrand.com
sell.sellitwithgary.com
signup.hypemarketing.in
sistemacanadense.online
subfooter.wpwakanda.com
survey.hypemarketing.in
testbot.afterorigin.com
tigerfull.amzooline.com
typebot.influencer.love
www.chatgpt-biliran.com
91181264.your-access.one
abg-assistent.m-vogel.de
ai.chromebookstoreph.com
bot.ativandograna.online
chat.ambassadorelena.com
chat.hiabhaykulkarni.com
concept.socialcliques.me
contextone.wpwakanda.com
form.sergiolimajr.com.br
hunterbot.saleshunter.ai
invite.bridesquadapp.com
link.cascadigital.com.br
login.algorithmpress.com
mail2wa.barrettamario.it
onboarding.growthside.io
reward.onlinebotdemo.xyz
stap.venturemarketing.in
type.opaulovieira.com.br
aibot.angrybranding.co.uk
app.photosbackupin1.click
bot.aidigitalmarketing.kr
bot.amicidisanfaustino.it
bot.arraesecenteno.com.br
bot.blackboxsports.com.br
bot.cabinrentalagency.com
bot.fusionstarreviews.com
bot.ristorantekintsugi.it
boyfriend-breakup.riku.ai
brigadeirosemdrama.com.br
budget.pizzeriaforno89.it
mdb.gabinete.brasilia.progenbr.com
preagendamento.sergiolimajr.com.br
prenotazione.ristorantekintsugi.it
studiotecnicoimmobiliaremerelli.it

@vercel
Copy link

@vercel vercel bot commented on 082084a Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app
app.typebot.io

Please sign in to comment.