summaryrefslogtreecommitdiff
path: root/frontend/app/utils
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2021-04-28 23:15:53 +0200
committerJules Laplace <julescarbon@gmail.com>2021-04-28 23:15:53 +0200
commit51288f81110850e346e6303115a0780b3d4668b4 (patch)
tree1337a298ea2941df22aecffe628fad29cd97aed9 /frontend/app/utils
parent938545387a234b2cd34bd5f52e0efe5fdd1b71c0 (diff)
preloading...
Diffstat (limited to 'frontend/app/utils')
-rw-r--r--frontend/app/utils/index.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/frontend/app/utils/index.js b/frontend/app/utils/index.js
index b8a2ed7..e2072b6 100644
--- a/frontend/app/utils/index.js
+++ b/frontend/app/utils/index.js
@@ -181,6 +181,35 @@ export const preloadVideo = url => (
})
)
+export const preloadAudio = url => (
+ new Promise((resolve, reject) => {
+ const audio = document.createElement('audio')
+ let loaded = false
+ const bind = () => {
+ audio.addEventListener('loadedmetadata', onload)
+ audio.addEventListener('error', onerror)
+ }
+ const unbind = () => {
+ audio.removeEventListener('loadedmetadata', onload)
+ audio.removeEventListener('error', onerror)
+ }
+ const onload = () => {
+ if (loaded) return
+ loaded = true
+ unbind()
+ resolve(audio)
+ }
+ const onerror = (error) => {
+ if (loaded) return
+ loaded = true
+ unbind()
+ reject(error)
+ }
+ bind()
+ audio.src = url
+ })
+)
+
export const cropImage = (url, crop) => {
return new Promise((resolve, reject) => {
let { x, y, w, h } = crop