feat: watch for resource file changes and update in surf #36
+711
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Watches the resource directory in the system's file system and keeps track of file additions, moving and deletions to update the resources within Surf.
Behavior:
Unfortunately our cross renderer reactivity seems to be bonked again so you need to reload the "Media" tab for the resources to update.
Implementation
Initially wanted to use an existing library to watch for changes but none matched the requirements of reliably tracking file renaming and not having too many external dependencies.
Instead created my own
FileWatchermodule which tracks files using inode numbers - unique filesystem identifiers that persist across renames but change for new files. It scans the directory on startup to build an inode-to-filename map, then batchesfs.watch()events in a 100ms debounce window to catch related operations.After debouncing, it re-scans and compares states: same inode + different filename = rename, missing inode = delete, new inode = create. This is very accurate on Unix systems (MacOS + Linux). On Windows we fall back to less reliable size-based matching as Windows doesn't have reliable inode support.
When file movements are detected the main process sends an IPC event to the core renderer which then handles updating the resources using our SFFS APIs. Had to add a API endpoint to fetch a resource by the resource path to find the matching resource for a given file.