Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The readEntries method of FileSystemDirectoryReader was incorrectly used in a synchronous manner instead of asynchronously. #33342

Closed
perterHUAN opened this issue Apr 30, 2024 · 0 comments · Fixed by #34276
Labels
area: File/File System Content:WebAPI Web API docs goal: accuracy (Experimental label) Issues about inaccurate/incorrect content. help wanted If you know something about this topic, we would love your help!

Comments

@perterHUAN
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader

What specific section or headline is this issue about?

Examples

What information was incorrect, unhelpful, or incomplete?

  • The returned entries always remains an empty array ([]), because by the time readDirectory function completes, the callback of readEntries hasn't executed yet, leaving entries unaltered.
  • The toArray() function is not defined here.
  • The recursive logic is flawed. getEntries() fails to retrieve subfolders, preventing further traversal and acquisition of all files within directories. It also potentially leads to an infinite loop.
function readDirectory(directory) {
  let dirReader = directory.createReader();
  let entries = [];

  let getEntries = () => {
    dirReader.readEntries(
      (results) => {
        if (results.length) {
          entries = entries.concat(toArray(results));
          getEntries();
        }
      },
      (error) => {
        /* handle error — error is a FileError object */
      },
    );
  };

  getEntries();
  return entries;
}

What did you expect to see?

Given a FileSystemDirectoryEntry, obtain all files within the directory (including subdirectories) represented by this object.

Below is my attempted modification using promises.

function readDirectory(directory) {
  return new Promise((resolve, reject) => {
    let dirReader = directory.createReader();
    let entries = [];

    let getEntries = () => {
      dirReader.readEntries(
        (results) => {
          if (results.length) {
            const p = [];
            for (const entry of results) {
              if (entry.isDirectory) {
                p.push(readDirectory(entry));
              } else {
                p.push(Promise.resolve(entry));
              }
            }
            Promise.all(p).then((res) => {
              for (const e of res) {
                if (Array.isArray(e)) entries.push(...e);
                else entries.push(e);
              }
              resolve(entries);
            });
          } else {
            resolve([]);
          }
        },
        (error) => {
          /* handle error — error is a FileError object */
        }
      );
    };
    getEntries();
  });
}

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@perterHUAN perterHUAN added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Apr 30, 2024
@github-actions github-actions bot added the Content:WebAPI Web API docs label Apr 30, 2024
@Josh-Cena Josh-Cena added help wanted If you know something about this topic, we would love your help! area: File/File System goal: accuracy (Experimental label) Issues about inaccurate/incorrect content. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: File/File System Content:WebAPI Web API docs goal: accuracy (Experimental label) Issues about inaccurate/incorrect content. help wanted If you know something about this topic, we would love your help!
2 participants