refactor(core): CORE-11 split media events and scope reconnect lifetimes

This commit is contained in:
Harvey Zhao committed 2026-09-11 03:19:52 +08:00
1 parent 5e31274f1d
commit b8039ad1f1
27 files changed
+4844 -475

No files matched your search

-159
View File
@@ -1,159 +0,0 @@
import config from '../config'
import { getScope } from '../lifecycle/instance'
import { wait } from '../lifecycle/resources'
import { addClass, isMobile, setStyle, silencePromise } from '../utils'
export default function eventInit(art) {
const {
i18n,
notice,
option,
constructor,
proxy,
template: { $player, $video, $poster },
} = art
let reconnectTime = 0
for (let index = 0; index < config.events.length; index++) {
proxy($video, config.events[index], (event) => {
art.emit(`video:${event.type}`, event)
})
}
// art.on('video:abort', () => {
// });
art.on('video:canplay', () => {
reconnectTime = 0
art.loading.show = false
})
art.once('video:canplay', () => {
art.loading.show = false
art.controls.show = true
art.mask.show = true
art.isReady = true
art.emit('ready')
})
// art.on('video:canplaythrough', () => {
// });
// art.on('video:durationchange', () => {
// });
// art.on('video:emptied', () => {
// });
art.on('video:ended', () => {
if (option.loop) {
art.seek = 0
silencePromise(art.play())
art.controls.show = false
art.mask.show = false
}
else {
art.controls.show = true
art.mask.show = true
}
})
art.on('video:error', async (error) => {
if (reconnectTime < constructor.RECONNECT_TIME_MAX) {
if (!await wait(getScope(art), constructor.RECONNECT_SLEEP_TIME) || getScope(art).closed)
return
reconnectTime += 1
art.url = option.url
notice.show = `${i18n.get('Reconnect')}: ${reconnectTime}`
art.emit('error', error, reconnectTime)
}
else {
art.mask.show = true
art.loading.show = false
art.controls.show = true
addClass($player, 'art-error')
if (!await wait(getScope(art), constructor.RECONNECT_SLEEP_TIME) || getScope(art).closed)
return
notice.show = i18n.get('Video Load Failed')
}
})
// art.on('video:loadeddata', () => {
// });
art.on('video:loadedmetadata', () => {
art.emit('resize')
if (isMobile) {
art.loading.show = false
art.controls.show = true
art.mask.show = true
}
})
art.on('video:loadstart', () => {
art.loading.show = true
art.mask.show = false
art.controls.show = true
})
art.on('video:pause', () => {
art.controls.show = true
art.mask.show = true
})
art.on('video:play', () => {
art.mask.show = false
setStyle($poster, 'display', 'none')
})
art.on('video:playing', () => {
art.mask.show = false
})
art.on('video:progress', () => {
if (art.playing) {
art.loading.show = false
}
})
// art.on('video:ratechange', () => {
// });
art.on('video:seeked', () => {
art.loading.show = false
art.mask.show = true
})
art.on('video:seeking', () => {
art.loading.show = true
art.mask.show = false
})
// art.on('video:stalled', () => {
// });
// art.on('video:suspend', () => {
// });
art.on('video:timeupdate', () => {
art.mask.show = false
})
// art.on('video:volumechange', () => {
// });
art.on('video:waiting', () => {
art.loading.show = true
art.mask.show = false
})
}
@@ -0,0 +1,15 @@
import type { MediaEventHost } from '../media/events/types'
import { forwardMediaEvents } from '../media/events/forward'
import { listenMedia } from '../media/events/listen'
import { installEnded, installPlaybackUI } from '../media/events/playback'
import { installReadiness } from '../media/events/readiness'
import { createReconnect } from '../media/events/reconnect'
export default function eventInit(art: MediaEventHost): void {
forwardMediaEvents(art)
const reconnect = createReconnect(art)
installReadiness(art, reconnect)
installEnded(art)
listenMedia(art, 'video:error', reconnect.schedule)
installPlaybackUI(art, reconnect)
}