mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-29 11:11:20 -08:00
refactor(vast): [PKG-VAST-03] isolate lifecycle ownership checkpoint
This commit is contained in:
1 parent
d5e1feadf5
commit
4f324a8438
30 files changed
+1453
-383
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import type Artplayer from 'artplayer'
|
||||
import type { Callback, Result } from './types'
|
||||
import { loadImaSdk } from './sdk'
|
||||
import { createSession } from './session'
|
||||
|
||||
export default function artplayerPluginVast(callback?: Callback) {
|
||||
return async (art: Artplayer): Promise<Result> => {
|
||||
let disposed = false
|
||||
let session: ReturnType<typeof createSession> | undefined
|
||||
const closed = () => disposed || art.isDestroy
|
||||
const result: Result = { name: 'artplayerPluginVast', destroy: () => session?.destroy() }
|
||||
function dispose(): void {
|
||||
if (disposed)
|
||||
return
|
||||
disposed = true
|
||||
try {
|
||||
art.off('destroy', dispose)
|
||||
}
|
||||
finally { session?.destroy() }
|
||||
}
|
||||
if (closed())
|
||||
return result
|
||||
art.on('destroy', dispose)
|
||||
try {
|
||||
if (closed())
|
||||
return result
|
||||
const ima = await loadImaSdk()
|
||||
if (closed())
|
||||
return result
|
||||
const utils = (art.constructor as typeof Artplayer).utils
|
||||
session = createSession(art, ima, utils, closed)
|
||||
if (closed()) {
|
||||
dispose()
|
||||
return result
|
||||
}
|
||||
if (typeof callback === 'function')
|
||||
await callback(session.context)
|
||||
return result
|
||||
}
|
||||
catch (error) {
|
||||
try {
|
||||
dispose()
|
||||
}
|
||||
catch (cleanupError) { console.error('VAST cleanup error:', cleanupError) }
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user