The VFS in f4 is designed to be fully non-blocking. This architecture ensures that the UI remains responsive even when performing operations on high-latency remote systems (SFTP, FTP) or slow storage devices.
Every method in the vfs.VFS interface accepts a context.Context. This allows for:
- Instant Cancellation: If a user navigates away from a directory that is still loading, the background operation is immediately aborted.
- Timeouts: Prevention of UI hangs on stale network connections.
ReadDir does not return a complete slice of items. Instead, it uses a callback pattern:
ReadDir(ctx context.Context, path string, onChunk func([]VFSItem)) errorAs chunks of files are read from the source (e.g., first 100 files from a directory of 10,000), they are immediately posted to the UI thread. The user can start interacting with visible files while the rest are still being fetched in the background.
For random access operations (used by Viewer and Editor), the VFS and its buffers use a "Try-and-Trigger" approach:
- The UI requests a range of bytes.
- If the data is not in the local cache, the buffer immediately returns
piecetable.ErrLoadingand triggers a background fetch for that specific chunk. - The UI renders a
[ Loading... ]placeholder and continues its loop. - Once the data arrives, a
Redrawis triggered, and the actual content replaces the placeholder.
To support features like word wrapping and fast navigation in the Editor, f4 performs background indexing of line breaks (\n). As bytes stream in, a background goroutine scans them and updates the LineIndex incrementally.
This architecture was specifically chosen to support the FISH+ protocol (see FISH+.md). By allowing operations to be partial, cancellable, and asynchronous, we can offload heavy computations (like searching or indexing) to the remote server while keeping the local f4 instance lightweight and fast.