diff --git a/src/inputs/MidiInput.js b/src/inputs/MidiInput.js index cd770190..e2694a3e 100644 --- a/src/inputs/MidiInput.js +++ b/src/inputs/MidiInput.js @@ -8,6 +8,11 @@ import getConnectedDevice from '../selectors/getConnectedDevice' let store, midiAccess +const getMidiAccess = () => { + if (!midiAccess) midiAccess = navigator.requestMIDIAccess() + return midiAccess +} + const onMessage = (rawMessage) => { const state = store.getState() const deviceId = rawMessage.target.name @@ -62,9 +67,11 @@ const onMessage = (rawMessage) => { } } -export const processDevices = () => { +export const processDevices = async () => { const devices = {} + const midiAccess = await getMidiAccess() + midiAccess.inputs.forEach((entry) => { devices[entry.name] = { title: entry.name, @@ -77,15 +84,13 @@ export const processDevices = () => { store.dispatch(midiUpdateDevices(devices)) } -export default (injectedStore) => { +export default async (injectedStore) => { store = injectedStore + const midiAccess = await getMidiAccess() - navigator.requestMIDIAccess().then((injectedMidiAccess) => { - midiAccess = injectedMidiAccess - processDevices() + processDevices() - midiAccess.onstatechange = () => { - processDevices() - } - }) + midiAccess.onstatechange = () => { + processDevices() + } }