Skip to content

Commit 48102e3

Browse files
committed
feat: enable(options.requestMIDIAccessFunction)
adds a requestMIDIAccessFunction property to the options object argument of `WebMIDI.enable()`. If this property exists (and is a function), it will be called instead of `navigator.requestMIDIAccess`
1 parent 9e946ae commit 48102e3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/WebMidi.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ class WebMidi extends EventEmitter {
218218
* @param [options.software=false] {boolean} Whether to request access to software synthesizers on
219219
* the host system. This is part of the spec but has not yet been implemented by most browsers as
220220
* of April 2020.
221+
*
222+
* @param [options.requestMIDIAccessFunction] {function} A custom function to use to return
223+
* the MIDIAccess object. This is useful if you want to use a polyfill for the Web MIDI API
224+
* or if you want to use a custom implementation of the Web MIDI API - probably for testing
225+
* purposes.
221226
*
222227
* @async
223228
*
@@ -360,9 +365,15 @@ class WebMidi extends EventEmitter {
360365

361366
// Request MIDI access (this iw where the prompt will appear)
362367
try {
363-
this.interface = await navigator.requestMIDIAccess(
364-
{sysex: options.sysex, software: options.software}
365-
);
368+
if (typeof options.requestMIDIAccessFunction === "function") {
369+
this.interface = await options.requestMIDIAccessFunction(
370+
{sysex: options.sysex, software: options.software}
371+
);
372+
} else {
373+
this.interface = await navigator.requestMIDIAccess(
374+
{sysex: options.sysex, software: options.software}
375+
);
376+
}
366377
} catch(err) {
367378
errorEvent.error = err;
368379
this.emit("error", errorEvent);

typescript/webmidi.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5540,6 +5540,7 @@ declare class WebMidi extends EventEmitter {
55405540
sysex?: boolean;
55415541
validation?: boolean;
55425542
software?: boolean;
5543+
requestMIDIAccessFunction?: Function;
55435544
}): Promise<WebMidi>;
55445545

55455546
/**

0 commit comments

Comments
 (0)