Avoid referencing found MIDI device name

This commit is contained in:
Nick Matantsev 2025-12-25 19:06:19 -05:00
parent b8dc50267e
commit 0c01a997ac

View file

@ -483,6 +483,8 @@ class MidiInput {
* @param {string | number} input MIDI device name or index defaulting to 0 * @param {string | number} input MIDI device name or index defaulting to 0
*/ */
constructor(input) { constructor(input) {
this.stateKey = typeof input === 'string' ? input : undefined;
const device = getDevice(input, WebMidi.inputs); const device = getDevice(input, WebMidi.inputs);
if (!device) { if (!device) {
throw new Error( throw new Error(
@ -490,8 +492,6 @@ class MidiInput {
); );
} }
this.name = device.name;
this._refs = {}; this._refs = {};
this._refsByChan = {}; this._refsByChan = {};
@ -540,7 +540,13 @@ class MidiInput {
} }
_loadState(chan) { _loadState(chan) {
const initialDataRaw = localStorage.getItem(`strudel-midin-${this.name}-chan${chan !== undefined ? chan : 'all'}`); if (!this.stateKey) {
return {};
}
const initialDataRaw = localStorage.getItem(
`strudel-midin-${this.stateKey}-chan${chan !== undefined ? chan : 'all'}`,
);
if (!initialDataRaw) { if (!initialDataRaw) {
return {}; return {};
} }
@ -549,7 +555,7 @@ class MidiInput {
return JSON.parse(initialDataRaw); return JSON.parse(initialDataRaw);
} catch (err) { } catch (err) {
console.warn( console.warn(
`Failed to parse MIDI state from localStorage for input "${this.name}" and channel "${chan}"`, `Failed to parse MIDI state from localStorage for input "${this.stateKey}" and channel "${chan}"`,
initialDataRaw, initialDataRaw,
err, err,
); );
@ -558,9 +564,16 @@ class MidiInput {
} }
_saveState(chan, cc, value) { _saveState(chan, cc, value) {
if (!this.stateKey) {
return;
}
const state = this._loadState(chan); const state = this._loadState(chan);
state[cc] = value; state[cc] = value;
localStorage.setItem(`strudel-midin-${this.name}-chan${chan !== undefined ? chan : 'all'}`, JSON.stringify(state)); localStorage.setItem(
`strudel-midin-${this.stateKey}-chan${chan !== undefined ? chan : 'all'}`,
JSON.stringify(state),
);
} }
_sendAllStates(output) { _sendAllStates(output) {