mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-23 05:41: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)
|
||||
Reference in new issue
Block a user