Skip to content

Commit

Permalink
fix(app-shell-odd): allow running offboard (#15105)
Browse files Browse the repository at this point in the history
It's useful to be able to run the odd shell on your laptop for testing
flows, and this code baked in a bunch of directory structure
assumptions. These fixes hopefully tolerate the changes by falling back
to not scanning for file changes anymore, allowing you to once again run
make -C app dev-odd.
  • Loading branch information
sfoster1 authored May 8, 2024
1 parent 3f430a1 commit 5f390fc
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions app-shell-odd/src/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,40 +90,48 @@ export function watchForMassStorage(dispatch: Dispatch): () => void {
prevDirs = present.filter((entry): entry is string => entry !== null)
})

const mediaWatcher = fs.watch(
FLEX_USB_MOUNT_DIR,
{ persistent: true },
(event, fileName) => {
if (!!!fileName) {
rescan(dispatch)
return
}
if (!fileName.match(FLEX_USB_MOUNT_FILTER)) {
return
}
const fullPath = join(FLEX_USB_MOUNT_DIR, fileName)
fsPromises
.stat(fullPath)
.then(info => {
if (!info.isDirectory) {
const mediaWatcherCreator = (): fs.FSWatcher | null => {
try {
return fs.watch(
FLEX_USB_MOUNT_DIR,
{ persistent: true },
(event, fileName) => {
if (!!!fileName) {
rescan(dispatch)
return
}
if (prevDirs.includes(fullPath)) {
if (!fileName.match(FLEX_USB_MOUNT_FILTER)) {
return
}
console.log(`New mass storage device ${fileName} detected`)
prevDirs.push(fullPath)
return handleNewlyPresent(fullPath)
})
.catch(() => {
if (prevDirs.includes(fullPath)) {
console.log(`Mass storage device at ${fileName} removed`)
prevDirs = prevDirs.filter(entry => entry !== fullPath)
dispatch(robotMassStorageDeviceRemoved(fullPath))
}
})
const fullPath = join(FLEX_USB_MOUNT_DIR, fileName)
fsPromises
.stat(fullPath)
.then(info => {
if (!info.isDirectory) {
return
}
if (prevDirs.includes(fullPath)) {
return
}
console.log(`New mass storage device ${fileName} detected`)
prevDirs.push(fullPath)
return handleNewlyPresent(fullPath)
})
.catch(() => {
if (prevDirs.includes(fullPath)) {
console.log(`Mass storage device at ${fileName} removed`)
prevDirs = prevDirs.filter(entry => entry !== fullPath)
dispatch(robotMassStorageDeviceRemoved(fullPath))
}
})
}
)
} catch {
return null
}
)
}

const mediaWatcher = mediaWatcherCreator()

const devWatcher = fs.watch(
FLEX_USB_DEVICE_DIR,
Expand All @@ -150,7 +158,7 @@ export function watchForMassStorage(dispatch: Dispatch): () => void {

rescan(dispatch)
return () => {
mediaWatcher.close()
mediaWatcher != null && mediaWatcher.close()
devWatcher.close()
}
}

0 comments on commit 5f390fc

Please sign in to comment.