mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-19 11:21:20 -08:00
refactor(core): CORE-08 type plugin registration and guard closed instances
This commit is contained in:
1 parent
f9707bad71
commit
874dac9f22
36 files changed
+2865
-142
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import type { PluginFactory, Plugins } from 'artplayer'
|
||||
import Artplayer from 'artplayer'
|
||||
|
||||
declare module 'artplayer/types' {
|
||||
interface Plugins {
|
||||
typedExtension?: { update: (count: number) => void }
|
||||
}
|
||||
interface Events {
|
||||
'typed-extension:update': [count: number]
|
||||
}
|
||||
}
|
||||
|
||||
const factory: PluginFactory = function (art) {
|
||||
const receiver: number = this.id
|
||||
return { name: 'typedExtension', update(count: number) {
|
||||
art.emit('typed-extension:update', count + receiver)
|
||||
} }
|
||||
}
|
||||
const art = new Artplayer({ container: '#player', plugins: [factory] }, function (instance) {
|
||||
this.plugins.typedExtension?.update(1)
|
||||
instance.plugins.typedExtension?.update(2)
|
||||
})
|
||||
const plugins: Plugins = art.plugins
|
||||
plugins.typedExtension?.update(3)
|
||||
art.on('typed-extension:update', count => count.toFixed())
|
||||
// @ts-expect-error Installed result methods retain their argument types.
|
||||
plugins.typedExtension?.update('bad')
|
||||
// @ts-expect-error Declared event listener payloads remain checked.
|
||||
art.on('typed-extension:update', (count: string) => count)
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { PluginFactory, PluginHost } from '../../packages/artplayer/src/plugins/types'
|
||||
import Plugins from '../../packages/artplayer/src/plugins'
|
||||
|
||||
interface Host extends PluginHost<Host> {
|
||||
id: number
|
||||
}
|
||||
declare const host: Host
|
||||
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<Host> = () => 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]
|
||||
Reference in new issue
Block a user