Move entry list iterator to cache.js and simplify

This commit is contained in:
Jaifroid 2023-10-26 14:14:40 +01:00
parent 954cd5d192
commit 635fbd64de
2 changed files with 49 additions and 41 deletions

View File

@ -3746,49 +3746,23 @@ function processNativeDirHandle (dirHandle, callback) {
});
params.pickedFolder = dirHandle;
params.pickedFile = '';
// We have to iterate async function with Promise because IE11 compiler throws error if we use async
var archiveList = [];
var entryList = dirHandle.entries();
(function iterateAsyncDirEntryArray () {
return entryList.next().then(function (result) {
if (!result.done) {
var entry = result.value[1];
if (/\.zim(\w\w)?$/.test(entry.name)) {
if (callback) archiveList.push(entry);
// Hide all parts of split file except first in UI
else if (/\.zim(aa)?$/.test(entry.name)) archiveList.push(entry.name);
if (!params.pickedFolder.path) {
entry.getFile().then(function (file) {
params.pickedFolder.path = file.path;
});
}
}
iterateAsyncDirEntryArray();
} else {
var noZIMFound = document.getElementById('noZIMFound');
if (archiveList.length) {
if (callback) {
callback(archiveList);
} else {
noZIMFound.style.display = 'none';
populateDropDownListOfArchives(archiveList);
}
} else {
if (callback) {
callback(null);
} else {
noZIMFound.style.display = 'block';
populateDropDownListOfArchives(archiveList, true);
}
}
}
}).catch(function (err) {
uiUtil.systemAlert('<p>We could not find your archive! Is the location or file still available? Try picking the file or folder again.</p>' +
'<p>[System error message: ' + err.message + ']</p>', 'Error!');
});
})();
var archiveDisplay = document.getElementById('chooseArchiveFromLocalStorage');
archiveDisplay.style.display = 'block';
var iterableEntryList = dirHandle.entries();
return cache.iterateAsyncDirEntries(iterableEntryList, [], !!callback).then(function (archiveList) {
var noZIMFound = document.getElementById('noZIMFound');
var hasArchives = archiveList.length > 0;
if (!hasArchives) console.warn('No archives found in directory ' + dirHandle.name);
if (callback) {
callback(archiveList);
} else {
noZIMFound.style.display = hasArchives ? 'none' : 'block';
populateDropDownListOfArchives(archiveList, !hasArchives);
}
}).catch(function (err) {
uiUtil.systemAlert('<p>We could not find your archive! Is the location or file still available? Try picking the file or folder again.</p>' +
'<p>[System error message: ' + err.message + ']</p>', 'Error!');
});
}
function scanNodeFolderforArchives (folder, callback) {

View File

@ -880,6 +880,39 @@ function deleteOPFSEntry (name) {
}
}
/**
* Iterates an iterable entry list of files using the File System API and returns an array of entries found
*
* @param {Iterator} entries An asychronous iterator of entries derived from a directory handle
* @param {Array} archives An array to which to add the entries (may be an empty array)
* @param {Boolean} noFilter An optional flag to indicate that no filtering should be applied to the entries
* @returns {Promise<Array>} A Promise for an array of entries in the file system directory
*/
function iterateAsyncDirEntries (entries, archives, noFilter) {
return entries.next().then(function (result) {
if (!result.done) {
var entry = result.value[1];
if (/\.zim(\w\w)?$/.test(entry.name)) {
if (noFilter) archives.push(entry);
// Hide all parts of split file except first in UI
else if (/\.zim(aa)?$/.test(entry.name)) archives.push(entry.name);
// In an Electron app, we should be able to get the path of the files
if (window.fs && !params.pickedFolder.path) {
entry.getFile().then(function (file) {
params.pickedFolder.path = file.path;
});
}
}
return iterateAsyncDirEntries(entries, archives, noFilter);
} else {
// We've processed all the entries
return archives;
}
}).catch(function (err) {
throw err;
});
}
/**
* Iterates the OPFS file system and returns an array of entries found
*
@ -976,6 +1009,7 @@ export default {
importOPFSEntries: importOPFSEntries,
exportOPFSEntry: exportOPFSEntry,
deleteOPFSEntry: deleteOPFSEntry,
iterateAsyncDirEntries: iterateAsyncDirEntries,
iterateOPFSEntries: iterateOPFSEntries,
populateOPFSStorageQuota: populateOPFSStorageQuota
};