Files
ArtPlayer/test/types/plugins-source.ts
T

20 lines
1.0 KiB
TypeScript

import type Artplayer from '../../packages/artplayer/src'
import type { PluginFactory } from '../../packages/artplayer/src/plugins/types'
import Plugins from '../../packages/artplayer/src/plugins'
declare const host: Artplayer
const registry = new Plugins(host)
const sync: typeof registry = registry.add(function (art) {
const id: number = this.id + art.id
return { name: 'sync', id }
})
const asyncResult: Promise<typeof registry> = registry.add(async () => ({ name: 'async' }))
const mixed: typeof registry | Promise<typeof registry> = registry.add(() => Math.random() ? Promise.resolve(1) : 1)
const untyped: PluginFactory<Artplayer> = () => null
const unknown: typeof registry | Promise<typeof registry> = registry.add(untyped)
// @ts-expect-error Synchronous plugins do not always return a Promise.
const bad: Promise<typeof registry> = registry.add(() => ({ name: 'sync' }))
// @ts-expect-error The factory receiver and argument must match the actual host.
registry.add((art: { incompatible: string }) => art)
void [sync, asyncResult, mixed, unknown, bad]