mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-28 10:31: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,50 @@
|
||||
import type { Cleanup, Lifetime, LifetimeHost } from './types'
|
||||
|
||||
export default function createLifetime(art: LifetimeHost): Lifetime {
|
||||
let closed = Boolean(art.isDestroy)
|
||||
const cleanups = new Set<Cleanup>()
|
||||
let cancel!: Cleanup
|
||||
const cancelled = new Promise<void>((resolve) => {
|
||||
cancel = resolve
|
||||
})
|
||||
function run(cleanup: Cleanup): void {
|
||||
try {
|
||||
cleanup()
|
||||
}
|
||||
catch (error) {
|
||||
console.warn('Failed to clean up multiple subtitles:', error)
|
||||
}
|
||||
}
|
||||
const lifetime: Lifetime = {
|
||||
get closed() { return closed },
|
||||
own(cleanup) {
|
||||
if (closed)
|
||||
run(cleanup)
|
||||
else
|
||||
cleanups.add(cleanup)
|
||||
return () => cleanups.delete(cleanup)
|
||||
},
|
||||
wait(value) { return Promise.race([value, cancelled]) },
|
||||
dispose() {
|
||||
if (closed)
|
||||
return
|
||||
closed = true
|
||||
cancel()
|
||||
const pending = [...cleanups].reverse()
|
||||
cleanups.clear()
|
||||
for (const cleanup of pending)
|
||||
run(cleanup)
|
||||
},
|
||||
}
|
||||
if (!closed) {
|
||||
lifetime.own(() => art.off('destroy', lifetime.dispose))
|
||||
try {
|
||||
art.on('destroy', lifetime.dispose)
|
||||
}
|
||||
catch (error) {
|
||||
lifetime.dispose()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
return lifetime
|
||||
}
|
||||
Reference in new issue
Block a user