Skip to content

Commit 9833c48

Browse files
cursor plugin: fix crash when destroy triggered before ready (#2606)
* cursor plugin: fix crash when destroy triggered before ready (#2602) * update changelog,update inline comments * fix indentation * add parentheses Co-authored-by: Thijs Triemstra <[email protected]>
1 parent 9e8aed5 commit 9833c48

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ wavesurfer.js changelog
66
- Markers plugin:
77
- Check for event after every add/remove (#2560)
88
- Add tooltip (#2595)
9+
- Cursor plugin:
10+
- Fix crash when `destroy` is called before `ready` event fired (#2606)
911

1012
6.3.0 (03.10.2022)
1113
------------------

src/plugin/cursor/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* the cursor follow the x and the y-position of the mouse. Use `false` to make the
1818
* it only follow the x-position of the mouse.
1919
* @property {function} formatTimeCallback Formats the timestamp on the cursor.
20+
* @property {boolean} isDestroyCalled true if called destroy before the ready event fired
2021
*/
2122

2223
/**
@@ -140,11 +141,19 @@ export default class CursorPlugin {
140141
* @type {?HTMLElement}
141142
*/
142143
this.displayTime = null;
144+
/**
145+
* true if call destory before ready event
146+
* @type {boolean}
147+
*/
148+
this.isDestroyCalled = false;
143149

144150
this.params = Object.assign({}, this.defaultParams, params);
145151
}
146152

147153
_onReady() {
154+
if (this.isDestroyCalled) {
155+
return;
156+
}
148157
this.wrapper = this.wavesurfer.drawer.wrapper;
149158
this.cursor = this.util.withOrientation(this.wrapper.appendChild(
150159
document.createElement('cursor'),
@@ -237,10 +246,14 @@ export default class CursorPlugin {
237246
* Destroy the plugin (used by the Plugin API)
238247
*/
239248
destroy() {
249+
if (!this.cursorTime || !this.showTime){
250+
this.isDestroyCalled = true;
251+
return;
252+
}
240253
if (this.params.showTime) {
241-
this.showTime.remove();
254+
this.showTime && this.showTime.remove();
242255
}
243-
this.cursor.remove();
256+
this.cursor && this.cursor.remove();
244257
this.wrapper.removeEventListener('mousemove', this._onMousemove);
245258
if (this.params.hideOnBlur) {
246259
this.wrapper.removeEventListener('mouseenter', this._onMouseenter);
@@ -323,7 +336,6 @@ export default class CursorPlugin {
323336
*/
324337
formatTime(cursorTime) {
325338
cursorTime = isNaN(cursorTime) ? 0 : cursorTime;
326-
327339
if (this.params.formatTimeCallback) {
328340
return this.params.formatTimeCallback(cursorTime);
329341
}

0 commit comments

Comments
 (0)