mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-17 10:01:20 -08:00
24 lines
751 B
TypeScript
24 lines
751 B
TypeScript
import type { Lifetime } from './types'
|
|
|
|
export default async function requestVtt(url = '', lifetime: Lifetime): Promise<string | void> {
|
|
if (lifetime.closed)
|
|
return
|
|
const controller = typeof AbortController === 'function' ? new AbortController() : null
|
|
const release = lifetime.own(() => controller?.abort())
|
|
try {
|
|
if (lifetime.closed)
|
|
return
|
|
const response = await lifetime.wait(controller ? fetch(url, { signal: controller.signal }) : fetch(url))
|
|
if (lifetime.closed)
|
|
return
|
|
if (response!.ok === false)
|
|
throw new Error(`Failed to fetch VTT thumbnails: HTTP ${response!.status}`)
|
|
if (lifetime.closed)
|
|
return
|
|
return await lifetime.wait(response!.text())
|
|
}
|
|
finally {
|
|
release()
|
|
}
|
|
}
|