From 47557cf06ff06e94e6407ba62955a7c5768c71ec Mon Sep 17 00:00:00 2001 From: Nick Matantsev Date: Thu, 25 Dec 2025 18:38:55 -0500 Subject: [PATCH] Move device lookup inside input wrapper --- packages/midi/midi.mjs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/midi/midi.mjs b/packages/midi/midi.mjs index 27c865e72..97226dc55 100644 --- a/packages/midi/midi.mjs +++ b/packages/midi/midi.mjs @@ -481,11 +481,18 @@ class MidiInput { /** * * @param {string | number} input MIDI device name or index defaulting to 0 - * @param {WebMidi.MIDIInput | undefined} device MIDI input device instance (or empty to use as placeholder) */ - constructor(device) { + constructor(input) { + const device = getDevice(input, WebMidi.inputs); + if (!device) { + throw new Error( + `midiin: device "${input}" not found.. connected devices: ${getMidiDeviceNamesString(WebMidi.inputs)}`, + ); + } + this.id = device.id; this.name = device.name; + this.device = device; this._refs = {}; this._refsByChan = {}; @@ -572,15 +579,10 @@ export async function midin(input) { ); } const initial = await enableWebMidi(); // only returns on first init - const device = getDevice(input, WebMidi.inputs); - if (!device) { - throw new Error( - `midiin: device "${input}" not found.. connected devices: ${getMidiDeviceNamesString(WebMidi.inputs)}`, - ); - } - const instance = midiInputs[device.id] || new MidiInput(device); - midiInputs[device.id] = instance; + const instance = midiInputs[input] || new MidiInput(input); + midiInputs[input] = instance; + const device = instance.device; if (initial) { const otherInputs = WebMidi.inputs.filter((o) => o.name !== device.name);