Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/puny-places-fall.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@arcanewizards/timecode-toolbox': patch
---

Fix occasional MacOS MIDI crashing

CoreMIDI can sometimes return with response code -304 when attempting to
initialize a client to listen for changes to the MIDI devices.
Timecode Toolbox now handles this by falling-back to polling mode if this error
is encountered. It is usually temporary, and future attempts to establish
listeners (e.g. after the app is restarted) usually works.
35 changes: 25 additions & 10 deletions apps/timecode-toolbox/src/lib/midi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,32 @@ export const useMidiDeviceWatcher = (
return;
}

let establishedListener = false;
if (supportInfo.notifications.supported) {
m.addEventListener('endpointschanged', listener);
// Get the initial list of devices
m.getEndpoints()
.then((endpoints) => setAvailableDevices(endpoints[type]))
.catch((cause) => {
const error = new Error(`Failed to get MIDI ${type}`, { cause });
log.error(error);
});
} else {
// If notifications aren't supported, poll for changes every 5 seconds
try {
m.addEventListener('endpointschanged', listener);
establishedListener = true;
// Get the initial list of devices
m.getEndpoints()
.then((endpoints) => setAvailableDevices(endpoints[type]))
.catch((cause) => {
const error = new Error(`Failed to get MIDI ${type}`, {
cause,
});
log.error(error);
});
} catch (cause) {
const error = new Error(
`Failed to initialize MIDI endpoint change listener, falling-back to polling`,
{ cause },
);
log.error(error);
}
}

if (!establishedListener) {
// If notifications aren't supported, or listening failed,
// poll for changes every 5 seconds
interval = setInterval(() => {
m.getEndpoints()
.then((endpoints) => setAvailableDevices(endpoints[type]))
Expand Down