Skip to content

Commit

Permalink
Feat: add init event (katspaugh#3454)
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh authored Dec 29, 2023
1 parent b22e8b5 commit 93c623e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Extract version
id: version
run: |
OLD_VERSION=$(npm show wavesurfer.js version)
OLD_VERSION=$(npm show . version)
NEW_VERSION=$(node -p 'require("./package.json").version')
if [ $NEW_VERSION != $OLD_VERSION ]; then
echo "New version $NEW_VERSION detected"
Expand Down Expand Up @@ -55,9 +55,8 @@ jobs:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
- uses: ./.github/workflows/yarn
if: ${{ steps.version.outputs.version }}
run: npm install --legacy-peer-deps

- name: Publish to NPM
if: ${{ steps.version.outputs.version }}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "7.6.0",
"license": "BSD-3-Clause",
"author": "katspaugh",
"description": "Navigable audio waveform player",
"description": "Audio waveform player",
"homepage": "https://wavesurfer.xyz",
"keywords": [
"waveform",
Expand All @@ -15,7 +15,7 @@
],
"repository": {
"type": "git",
"url": "[email protected]:wavesurfer-js/wavesurfer.js.git"
"url": "[email protected]:katspaugh/wavesurfer.js.git"
},
"type": "module",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default [
file: 'dist/wavesurfer.esm.js',
format: 'esm',
},
plugins: [typescript(), terser()],
plugins,
},
// CommonJS module (Node.js)
{
Expand Down
23 changes: 15 additions & 8 deletions src/wavesurfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const defaultOptions = {
}

export type WaveSurferEvents = {
/** After wavesurfer is created */
init: []
/** When audio starts loading */
load: [url: string]
/** During audio loading */
Expand Down Expand Up @@ -165,12 +167,17 @@ class WaveSurfer extends Player<WaveSurferEvents> {
this.initTimerEvents()
this.initPlugins()

// Load audio if URL or an external media with an src is passed,
// of render w/o audio if pre-decoded peaks and duration are provided
const url = this.options.url || this.getSrc() || ''
if (url || (this.options.peaks && this.options.duration)) {
this.load(url, this.options.peaks, this.options.duration)
}
// Init and load async to allow external events to be registered
Promise.resolve().then(() => {
this.emit('init')

// Load audio if URL or an external media with an src is passed,
// of render w/o audio if pre-decoded peaks and duration are provided
const url = this.options.url || this.getSrc() || ''
if (url || (this.options.peaks && this.options.duration)) {
this.load(url, this.options.peaks, this.options.duration)
}
})
}

private initTimerEvents() {
Expand Down Expand Up @@ -349,9 +356,9 @@ class WaveSurfer extends Player<WaveSurferEvents> {
this.setSrc(url, blob)

// Wait for the audio duration
// It should be a promise to allow event listeners to subscribe to the ready and decode events
const audioDuration =
(await Promise.resolve(duration || this.getDuration())) ||
duration ||
this.getDuration() ||
(await new Promise((resolve) => {
this.onceMediaEvent('loadedmetadata', () => resolve(this.getDuration()))
}))
Expand Down

0 comments on commit 93c623e

Please sign in to comment.