Skip to content

Commit

Permalink
Prevent errors when calling WebMidi.disable() (issue #361)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Philippe Côté committed Jun 3, 2023
1 parent 3bf6924 commit d300d50
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Output.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export class Output extends EventEmitter {
this.removeListener();
this.channels.forEach(ch => ch.destroy());
this.channels = [];
this._midiOutput.onstatechange = null;

// We check if it's still there because it could have theoretically be destroyed manually
if (this._midiOutput) this._midiOutput.onstatechange = null;

await this.close();
this._midiOutput = null;
}
Expand Down
6 changes: 4 additions & 2 deletions src/WebMidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,13 @@ class WebMidi extends EventEmitter {
*/
async disable() {

// This needs to be done right away to prevent racing conditions in listeners while the inputs
// are being destroyed.
if (this.interface) this.interface.onstatechange = undefined;

return this._destroyInputsAndOutputs().then(() => {

if (navigator && typeof navigator.close === "function") navigator.close(); // jzz

if (this.interface) this.interface.onstatechange = undefined;
this.interface = null; // also resets enabled, sysexEnabled

/**
Expand Down

0 comments on commit d300d50

Please sign in to comment.