-
Notifications
You must be signed in to change notification settings - Fork 744
feat: Offload file change detection to background thread #1802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Offload file change detection to background thread #1802
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a significant performance improvement by offloading file change detection to a background thread. This is achieved by adding compareFileText and compareTexts methods to the native System Cordova plugin. The JavaScript side in editorFile.js is updated to correctly use these new native functions for different file protocols. The Java implementation is solid, though I've suggested a minor refactoring to improve code clarity by removing some unused variables.
| // Handle both file:// and content:// URIs | ||
| if ("file".equalsIgnoreCase(uri.getScheme())) { | ||
| File file = new File(uri.getPath()); | ||
| if (!file.exists()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also check file.canRead(). Sometimes invalid URIs get converted to /, which exists but is not what we expect.
Also check file.isFile() for the reasons stated above.
| } | ||
|
|
||
| // check length first | ||
| if (fileContent.length() != currentText.length()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this check above the stream-reading code to avoid reading the file. Also, use file.length() instead of the file content length. (Only applicable for java.io.File)
| // Read in chunks to handle large files | ||
| java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream(); | ||
| byte[] chunk = new byte[8192]; | ||
| while ((bytesRead = inputStream.read(chunk)) != -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are better ways to read from a stream/file, this is one of the most error prone method
For java.io.File you can just do file.readText(encoding)
Greptile SummaryOffloads file change detection from the JavaScript main thread to Android background threads, improving UI responsiveness during file comparison operations.
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Editor as EditorFile.js
participant Plugin as System Plugin (JS)
participant Cordova as Cordova Bridge
participant Java as System.java (Android)
participant BG as Background Thread
participant FS as File System
Note over Editor: User edits file
Editor->>Editor: isChanged() called
Editor->>Editor: session.getValue()
alt FTP/SFTP Protocol
Editor->>Plugin: compareFileText(cacheFileUri, encoding, text)
Plugin->>Cordova: exec('compare-file-text')
Cordova->>Java: compareFileText()
Java->>BG: Run on background thread
BG->>FS: Read file from cache
FS-->>BG: File content
BG->>BG: Compare lengths
alt Lengths differ
BG-->>Java: Return 1 (changed)
else Lengths same
BG->>BG: String.equals()
BG-->>Java: Return 0/1
end
Java-->>Cordova: Result
Cordova-->>Plugin: Result
Plugin-->>Editor: true/false
else File/Content Protocol
Editor->>Plugin: compareFileText(uri, encoding, text)
Plugin->>Cordova: exec('compare-file-text')
Cordova->>Java: compareFileText()
Java->>BG: Run on background thread
BG->>FS: Read file (file:// or content://)
FS-->>BG: File content
BG->>BG: Compare and return result
Java-->>Cordova: Result
Cordova-->>Plugin: Result
Plugin-->>Editor: true/false
else Other Protocols
Editor->>Editor: fsOperation(uri).readFile()
Editor->>Plugin: compareTexts(oldText, text)
Plugin->>Cordova: exec('compare-texts')
Cordova->>Java: compareTexts()
Java->>BG: Run on background thread
BG->>BG: Compare lengths then strings
Java-->>Cordova: Result
Cordova-->>Plugin: Result
Plugin-->>Editor: true/false
end
Note over Editor: Returns true if changed, false if not
|
|
@greptileai review it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional Comments (2)
-
src/plugins/system/android/com/foxdebug/system/System.java, line 706-711 (link)style: redundant stream closing - the try-with-resources at line 696 already closes
BufferedReader, which automatically closes the underlyingInputStreamReaderandinputStream -
src/lib/editorFile.js, line 625-628 (link)logic: returning
false(not changed) on error could lead to data loss - if native comparison fails but file actually changed on disk, editor won't prompt to save. should errors be handled differently (e.g., return true to be safe, or fallback to JS comparison)?
3 files reviewed, 2 comments
No description provided.