Skip to content

Commit 4a00d85

Browse files
committed
apploader - enhance file selection process with improved debounce and metadata handling
1 parent 4e8fbec commit 4a00d85

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

install_from_files.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44
function installFromFiles() {
55
return new Promise(resolve => {
6+
var MAX_WAIT_MS = 5000; // maximum time to wait for metadata.json
7+
var RESCHEDULE_MS = 400; // retry interval while waiting
68
// Ask user to select all files at once (multi-select)
79
Espruino.Core.Utils.fileOpenDialog({
810
id:"installappfiles",
@@ -14,7 +16,8 @@ function installFromFiles() {
1416
if (!installFromFiles.fileCollection) {
1517
installFromFiles.fileCollection = {
1618
files: [],
17-
count: 0
19+
count: 0,
20+
firstTs: Date.now()
1821
};
1922
}
2023

@@ -29,21 +32,38 @@ function installFromFiles() {
2932
// Use setTimeout to batch-process after all callbacks complete
3033
clearTimeout(installFromFiles.processTimeout);
3134
// Android may deliver multi-file selections with noticeable gaps.
32-
// Use a longer debounce to ensure we collect all files before processing.
33-
installFromFiles.processTimeout = setTimeout(function() {
34-
var files = installFromFiles.fileCollection.files;
35-
installFromFiles.fileCollection = null; // reset for next use
36-
37-
if (!files || files.length === 0) return resolve();
35+
// Use a longer debounce and reschedule until metadata is present or max wait exceeded.
36+
installFromFiles.processTimeout = setTimeout(function processSelection() {
37+
var fc = installFromFiles.fileCollection;
38+
var files = fc ? fc.files : null;
39+
if (!files || files.length === 0) {
40+
// nothing yet; keep waiting until max wait then resolve silently
41+
if (fc && (Date.now() - fc.firstTs) < MAX_WAIT_MS) {
42+
installFromFiles.processTimeout = setTimeout(processSelection, RESCHEDULE_MS);
43+
return;
44+
}
45+
installFromFiles.fileCollection = null; // reset
46+
return resolve();
47+
}
3848

3949
// Find metadata.json
4050
var metadataFile = files.find(f => f.name === 'metadata.json' || f.name.endsWith('/metadata.json'));
4151

4252
if (!metadataFile) {
53+
if (fc && (Date.now() - fc.firstTs) < MAX_WAIT_MS) {
54+
// Keep waiting for the rest of the files
55+
installFromFiles.processTimeout = setTimeout(processSelection, RESCHEDULE_MS);
56+
return;
57+
}
58+
// Timed out waiting for metadata.json
59+
installFromFiles.fileCollection = null; // reset
4360
showToast('No metadata.json found in selected files', 'error');
4461
return resolve();
4562
}
4663

64+
// We have metadata.json; stop collecting and proceed
65+
installFromFiles.fileCollection = null; // reset for next use
66+
4767
// Parse metadata.json
4868
var metadata;
4969
try {

0 commit comments

Comments
 (0)