refactor(auto-thumbnail): [PKG-AUTO-THUMB-03] type internal extraction contracts

This commit is contained in:
Harvey Zhao committed 2026-09-13 23:39:06 +08:00
1 parent 00e81309ca
commit 6c989deccd
20 files changed
+3968 -48

No files matched your search

@@ -0,0 +1,46 @@
import type { Cleanup, ThumbnailHost, ThumbnailOptions } from './types'
import extract from './extraction'
import { readOptions } from './options'
import createSession, { cleanupAll } from './session'
export default function artplayerPluginAutoThumbnail(option: ThumbnailOptions) {
return async (art: ThumbnailHost) => {
const report = (error: unknown) => console.warn('ArtPlayer auto-thumbnail failed:', error)
const session = createSession((config) => {
art.thumbnails = config
}, report)
const subscriptions: Cleanup[] = []
const onMetadata = () => {
const job = session.start()
if (job) {
job.guard(() => {
const config = readOptions(option, () => art.option.url)
if (job.active())
extract(job, config)
})()
}
}
const onDestroy = () => {
const actions = subscriptions.splice(0)
const result = cleanupAll([session.destroy, ...actions])
if (result.failed)
report(result.failure)
}
try {
const events: [string, Cleanup][] = [['destroy', onDestroy], ['restart', session.cancel], ['video:loadedmetadata', onMetadata]]
for (const [name, callback] of events) {
if (session.closed)
break
subscriptions.push(() => art.off(name, callback))
art.on(name, callback)
if (session.closed)
art.off(name, callback)
}
}
catch (error) {
onDestroy()
throw error
}
return { name: 'artplayerPluginAutoThumbnail' }
}
}