Skip to content

Commit 8e18cb1

Browse files
authored
Improve file waiting mechanism in EmulatorFileSystem
## Improve file waiting mechanism in EmulatorFileSystemThis PR enhances the `waitForFile` method in the `EmulatorFileSystem` class to be more efficient and reliable when waiting for emulator files to finish writing.### Changes- **Added `stat` method**: New helper method that safely wraps `FS.stat()` with error handling- **Refactored `waitForFile` logic**: - Replaced expensive buffer content reading with lightweight file size checking - Increased max retries from 30 to 120 for better handling of slow file systems - Adjusted timing from max 1000ms to max 500ms delays for more responsive checking - Simplified completion detection to wait for file size stabilization### Benefits- **Performance**: Significantly faster file polling using `stat()` instead of full content reads- **Memory**: Reduced memory usage by eliminating buffer storage during polling- **Reliability**: More robust file completion detection through size comparison- **Compatibility**: Better handling of various file system speeds and file sizes
1 parent 74659f0 commit 8e18cb1

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/classes/emulator-file-system.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ export class EmulatorFileSystem {
6161
return FS.readFile(path, { encoding })
6262
}
6363

64+
stat(path: string) {
65+
const { FS } = this
66+
try {
67+
return FS.stat(path)
68+
} catch {
69+
return null
70+
}
71+
}
72+
6473
unlink(path: string) {
6574
const { FS } = this
6675
try {
@@ -69,29 +78,28 @@ export class EmulatorFileSystem {
6978
}
7079

7180
async waitForFile(fileName: string) {
72-
const maxRetries = 30
73-
let buffer: ArrayBufferView<ArrayBuffer> | undefined
81+
const maxRetries = 120
82+
let lastSize = -1
7483
let isFinished = false
7584
let retryTimes = 0
7685
while (retryTimes <= maxRetries && !isFinished) {
77-
const delayTime = Math.min(100 * 2 ** retryTimes, 1000)
86+
const delayTime = Math.min(50 * 2 ** retryTimes, 500)
7887
await delay(delayTime)
7988
checkIsAborted(this.signal)
80-
try {
81-
const newBuffer = this.readFile(fileName, 'binary').buffer
82-
if (buffer) {
83-
isFinished = buffer.byteLength > 0 && buffer.byteLength === newBuffer.byteLength
89+
const stats = this.stat(fileName)
90+
if (stats) {
91+
const currentSize = stats.size
92+
if (lastSize > 0 && currentSize === lastSize) {
93+
isFinished = true
8494
}
85-
buffer = newBuffer
86-
} catch (error) {
87-
console.warn(error)
95+
lastSize = currentSize
8896
}
8997
retryTimes += 1
9098
}
91-
if (!(isFinished && buffer)) {
99+
if (!isFinished) {
92100
throw new Error('fs timeout')
93101
}
94-
return buffer
102+
return this.readFile(fileName, 'binary').buffer
95103
}
96104

97105
async writeFile(filePath: string, fileContent: Parameters<typeof ResolvableFile.create>[0]) {

0 commit comments

Comments
 (0)