forked from katspaugh/wavesurfer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-plugin.ts
40 lines (32 loc) · 1.01 KB
/
base-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import EventEmitter from './event-emitter.js'
import type WaveSurfer from './wavesurfer.js'
export type BasePluginEvents = {
destroy: []
}
export type GenericPlugin = BasePlugin<BasePluginEvents, unknown>
/** Base class for wavesurfer plugins */
export class BasePlugin<EventTypes extends BasePluginEvents, Options> extends EventEmitter<EventTypes> {
protected wavesurfer?: WaveSurfer
protected subscriptions: (() => void)[] = []
protected options: Options
/** Create a plugin instance */
constructor(options: Options) {
super()
this.options = options
}
/** Called after this.wavesurfer is available */
protected onInit() {
return
}
/** Do not call directly, only called by WavesSurfer internally */
public _init(wavesurfer: WaveSurfer) {
this.wavesurfer = wavesurfer
this.onInit()
}
/** Destroy the plugin and unsubscribe from all events */
public destroy() {
this.emit('destroy')
this.subscriptions.forEach((unsubscribe) => unsubscribe())
}
}
export default BasePlugin