mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-28 18:41:20 -08:00
refactor(multiple-subtitles): [PKG-MULTI-SUB-04] type runtime modules and vendor boundaries
This commit is contained in:
1 parent
beaa93d6cc
commit
da64aae267
28 files changed
+7075
-86
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import type Artplayer from 'artplayer'
|
||||
import type { Option, Result, Track } from './types'
|
||||
import createLifetime from './lifetime'
|
||||
import { parseTracks, serializeTracks } from './merge'
|
||||
import createRenderer from './render'
|
||||
import loadVtt from './request'
|
||||
|
||||
export default function artplayerPluginMultipleSubtitles({ subtitles = [] }: Option) {
|
||||
return async (art: Artplayer): Promise<Result> => {
|
||||
const { unescape, getExt, srtToVtt, assToVtt } = (art.constructor as typeof Artplayer).utils
|
||||
const lifetime = createLifetime(art)
|
||||
const render = createRenderer(art, lifetime, unescape)
|
||||
let trees: Track[] = []
|
||||
function setTracks(selected: readonly (Track | undefined)[]): void {
|
||||
if (!lifetime.closed)
|
||||
render(serializeTracks(selected))
|
||||
}
|
||||
const result: Result = {
|
||||
name: 'multipleSubtitles',
|
||||
tracks(names = []) {
|
||||
if (!lifetime.closed)
|
||||
setTracks(names.map(name => trees.find(tree => tree.name === name)))
|
||||
},
|
||||
reset() {
|
||||
setTracks(trees)
|
||||
},
|
||||
}
|
||||
try {
|
||||
if (lifetime.closed)
|
||||
return result
|
||||
const vtts = await Promise.all(subtitles.map(option => loadVtt(option, { getExt, srtToVtt, assToVtt }, lifetime)))
|
||||
if (lifetime.closed)
|
||||
return result
|
||||
// A live completed request yields text; cancellation was checked above.
|
||||
trees = parseTracks(vtts as string[], subtitles)
|
||||
setTracks(trees)
|
||||
return result
|
||||
}
|
||||
catch (error) {
|
||||
lifetime.dispose()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user