Skip to content

Commit

Permalink
fix(dcellar-web-ui): fix FileSystemDirectoryReader.readEntries() only…
Browse files Browse the repository at this point in the history
… returns 100 entries a time
  • Loading branch information
devinxl committed Apr 8, 2024
1 parent 2e30933 commit d2865de
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions apps/dcellar-web-ui/src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ export type TransferItemTree = {
[key: string]: File;
};

export const readEntriesAsync = (reader: FileSystemDirectoryReader): Promise<FileSystemEntry[]> => {
return new Promise((resolve, reject) => {
reader.readEntries(
(entries) => {
resolve(entries);
},
(error) => reject(error),
);
});
};

export const readDirectoryEntries = async (directoryEntry: FileSystemDirectoryEntry) => {
const reader = directoryEntry.createReader();
let resultEntries: FileSystemEntry[] = [];

const read = async function () {
const entries = await readEntriesAsync(reader);
if (entries.length > 0) {
resultEntries = resultEntries.concat(entries);
await read();
}
};
await read();

return resultEntries;
};

export const isFileEntry = (entry: FileSystemEntry): entry is FileSystemFileEntry => {
return entry.isFile;
};
Expand Down Expand Up @@ -40,14 +67,12 @@ export const traverseEntry = async (
if (isDirectoryEntry(entry)) {
const newPath = path + '/';
tree[newPath] = new File([], newPath, { type: 'text/plain' });
const reader = entry.createReader();
return new Promise((resolve) => {
reader.readEntries(async (entries) => {
const entryTree = await Promise.all(entries.map((entry) => traverseEntry(entry, tree)));
resolve(entryTree.reduce((r, c) => ({ ...r, ...c }), {}));
});
});
const entries = await readDirectoryEntries(entry);
await Promise.all(entries.map((entry) => traverseEntry(entry, tree)));

return tree;
}

return tree;
};

Expand Down

0 comments on commit d2865de

Please sign in to comment.