Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: rollup all types into a separate types.d.ts file #3451

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.7",
"rollup": "^3.26.2",
"rollup-plugin-dts": "^6.1.0",
"typescript": "^5.0.4"
}
}
8 changes: 8 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import glob from 'glob'
import typescript from '@rollup/plugin-typescript'
import terser from '@rollup/plugin-terser'
import dts from 'rollup-plugin-dts'

const plugins = [typescript(), terser()]

Expand Down Expand Up @@ -36,6 +37,13 @@ export default [
plugins,
},

// Compiled type definitions
{
input: './dist/wavesurfer.d.ts',
output: [{ file: 'dist/types.d.ts', format: 'es' }],
plugins: [dts()],
},

// Wavesurfer plugins
...glob
.sync('src/plugins/*.ts')
Expand Down
12 changes: 8 additions & 4 deletions src/base-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@ export type BasePluginEvents = {

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
}

onInit() {
// Overridden in plugin definition
/** Called after this.wavesurfer is available */
protected onInit() {
return
}

init(wavesurfer: WaveSurfer) {
/** Do not call directly, only called by WavesSurfer internally */
public _init(wavesurfer: WaveSurfer) {
this.wavesurfer = wavesurfer
this.onInit()
}

destroy() {
/** Destroy the plugin and unsubscribe from all events */
public destroy() {
this.emit('destroy')
this.subscriptions.forEach((unsubscribe) => unsubscribe())
}
Expand Down
21 changes: 8 additions & 13 deletions src/event-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type EventMap<EventTypes extends GeneralEventTypes> = {
class EventEmitter<EventTypes extends GeneralEventTypes> {
private listeners = {} as EventMap<EventTypes>

/** Add an event listener */
public addEventListener<EventName extends keyof EventTypes>(
/** Subscribe to an event. Returns an unsubscribe function. */
public on<EventName extends keyof EventTypes>(
event: EventName,
listener: EventListener<EventTypes, EventName>,
options?: { once?: boolean },
Expand All @@ -29,29 +29,24 @@ class EventEmitter<EventTypes extends GeneralEventTypes> {

if (options?.once) {
const unsubscribeOnce = () => {
this.removeEventListener(event, unsubscribeOnce)
this.removeEventListener(event, listener)
this.un(event, unsubscribeOnce)
this.un(event, listener)
}
this.addEventListener(event, unsubscribeOnce)
this.on(event, unsubscribeOnce)
return unsubscribeOnce
}

return () => this.removeEventListener(event, listener)
return () => this.un(event, listener)
}

public removeEventListener<EventName extends keyof EventTypes>(
/** Unsubscribe from an event */
public un<EventName extends keyof EventTypes>(
event: EventName,
listener: EventListener<EventTypes, EventName>,
): void {
this.listeners[event]?.delete(listener)
}

/** Subscribe to an event. Returns an unsubscribe function. */
public on = this.addEventListener

/** Unsubscribe from an event */
public un = this.removeEventListener

/** Subscribe to an event only once */
public once<EventName extends keyof EventTypes>(
event: EventName,
Expand Down
2 changes: 1 addition & 1 deletion src/wavesurfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class WaveSurfer extends Player<WaveSurferEvents> {

/** Register a wavesurfer.js plugin */
public registerPlugin<T extends GenericPlugin>(plugin: T): T {
plugin.init(this)
plugin._init(this)
this.plugins.push(plugin)

// Unregister plugin on destroy
Expand Down
17 changes: 17 additions & 0 deletions src/webaudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class WebAudioPlayer extends EventEmitter<WebAudioPlayerEvents> {
this.gainNode.connect(this.audioContext.destination)
}

/** Subscribe to an event. Returns an unsubscribe function. */
addEventListener = this.on

/** Unsubscribe from an event */
removeEventListener = this.un

async load() {
return
}
Expand Down Expand Up @@ -175,6 +181,17 @@ class WebAudioPlayer extends EventEmitter<WebAudioPlayerEvents> {
public getGainNode(): GainNode {
return this.gainNode
}

/** Get decoded audio */
public getChannelData(): Float32Array[] {
const channels: Float32Array[] = []
if (!this.buffer) return channels
const numChannels = this.buffer.numberOfChannels
for (let i = 0; i < numChannels; i++) {
channels.push(this.buffer.getChannelData(i))
}
return channels
}
}

export default WebAudioPlayer
47 changes: 45 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
resolved "https://registry.yarnpkg.com/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz#bd9154aec9983f77b3a034ecaa015c2e4201f6cf"
integrity sha512-1Yjs2SvM8TflER/OD3cOjhWWOZb58A2t7wpE2S9XfBYTiIl+XFhQG2bjy4Pu1I+EAlCNUzRDYDdFwFYUKvXcIA==

"@babel/code-frame@^7.22.13":
version "7.23.5"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.23.5.tgz#9009b69a8c602293476ad598ff53e4562e15c244"
integrity sha512-CgH3s1a96LipHCmSUmYFPwY7MNx8C3avkq7i4Wl3cfa662ldtUe4VM1TPXX70pfmrlWTb6jLqTYrZyT2ZTJBgA==
dependencies:
"@babel/highlight" "^7.23.4"
chalk "^2.4.2"

"@babel/helper-validator-identifier@^7.22.20":
version "7.22.20"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.20.tgz#c4ae002c61d2879e724581d96665583dbc1dc0e0"
integrity sha512-Y4OZ+ytlatR8AI+8KZfKuL5urKp7qey08ha31L8b3BwewJAoJamTzyvxPR/5D+KkdJCGPq/+8TukHBlY10FX9A==

"@babel/highlight@^7.23.4":
version "7.23.4"
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.23.4.tgz#edaadf4d8232e1a961432db785091207ead0621b"
integrity sha512-acGdbYSfp2WheJoJm/EBBBLh/ID8KDc64ISZ9DYtBmC8/Q204PZJLHyzeB5qMzJ5trcOkybd78M4x2KWsUq++A==
dependencies:
"@babel/helper-validator-identifier" "^7.22.20"
chalk "^2.4.2"
js-tokens "^4.0.0"

"@colors/[email protected]":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@colors/colors/-/colors-1.5.0.tgz#bb504579c1cae923e6576a4f5da43d25f97bdbd9"
Expand Down Expand Up @@ -122,7 +144,7 @@
"@jridgewell/gen-mapping" "^0.3.0"
"@jridgewell/trace-mapping" "^0.3.9"

"@jridgewell/sourcemap-codec@^1.4.10", "@jridgewell/sourcemap-codec@^1.4.14":
"@jridgewell/sourcemap-codec@^1.4.10", "@jridgewell/sourcemap-codec@^1.4.14", "@jridgewell/sourcemap-codec@^1.4.15":
version "1.4.15"
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz#d7c6e6755c78567a951e04ab52ef0fd26de59f32"
integrity sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==
Expand Down Expand Up @@ -548,7 +570,7 @@ chalk@^1.1.3:
strip-ansi "^3.0.0"
supports-color "^2.0.0"

chalk@^2.4.1:
chalk@^2.4.1, chalk@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
Expand Down Expand Up @@ -1492,6 +1514,11 @@ [email protected]:
rimraf "^2.6.2"
ssim.js "^3.1.1"

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==

js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
Expand Down Expand Up @@ -1644,6 +1671,13 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

magic-string@^0.30.4:
version "0.30.5"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.30.5.tgz#1994d980bd1c8835dc6e78db7cbd4ae4f24746f9"
integrity sha512-7xlpfBaQaP/T6Vh8MO/EqXSW5En6INHEvEXQiuff7Gku0PWjU3uf6w/j9o7O+SpB5fOAkrI5HeoNgwjEO0pFsA==
dependencies:
"@jridgewell/sourcemap-codec" "^1.4.15"

merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
Expand Down Expand Up @@ -2045,6 +2079,15 @@ rimraf@^3.0.0, rimraf@^3.0.2:
dependencies:
glob "^7.1.3"

rollup-plugin-dts@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-dts/-/rollup-plugin-dts-6.1.0.tgz#56e9c5548dac717213c6a4aa9df523faf04f75ae"
integrity sha512-ijSCPICkRMDKDLBK9torss07+8dl9UpY9z1N/zTeA1cIqdzMlpkV3MOOC7zukyvQfDyxa1s3Dl2+DeiP/G6DOw==
dependencies:
magic-string "^0.30.4"
optionalDependencies:
"@babel/code-frame" "^7.22.13"

rollup@^3.26.2:
version "3.29.4"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.4.tgz#4d70c0f9834146df8705bfb69a9a19c9e1109981"
Expand Down