Skip to content

Commit 04ac430

Browse files
Merge pull request #68 from muditjuneja/main
Improve file waiting mechanism in EmulatorFileSystem
2 parents 74659f0 + 8e18cb1 commit 04ac430

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)