jvilk / BrowserFS

BrowserFS is an in-browser filesystem that emulates the Node JS filesystem API and supports storing and retrieving files from various backends.
Other
3.06k stars 215 forks source link

Knowing when ready #265

Closed aolney closed 5 years ago

aolney commented 5 years ago

If I configure and then immediately list files, I get what appears to be the same error as if I hadn't configured at all. However, if I trigger the file listing using UI (so giving plenty of time for the configuration to happen) there is no error.

This raises the question of how to know when browserfs is "ready"

Code to configure:

configureBrowserFS: function() {
    fetch('brill.zip').then(function(response) {
        return response.arrayBuffer();
    }).then(function(zipData) {
        var Buffer = BrowserFS.BFSRequire('buffer').Buffer;
        BrowserFS.configure({
                    fs: "ZipFS",
                    options: {
                        zipData: Buffer.from(zipData)
                    }
          }, function(e) {
            if (e) {
              // An error occurred.
              throw e;
            }
            // Otherwise, BrowserFS is ready to use!
          });
        });
  }

Code to list files:

    listFiles : function() {
        var fs = BrowserFS.BFSRequire('fs');
        fs.readdirSync("/", (err, files) => {
            files.forEach(file => {
                console.warn(file);
            });
        })
    }
jvilk commented 5 years ago

I don't quite follow; there's literally a comment telling you where it's ready! You know it's ready because that callback is called w/o an error:

configureBrowserFS: function() {
    fetch('brill.zip').then(function(response) {
        return response.arrayBuffer();
    }).then(function(zipData) {
        var Buffer = BrowserFS.BFSRequire('buffer').Buffer;
        BrowserFS.configure({
                    fs: "ZipFS",
                    options: {
                        zipData: Buffer.from(zipData)
                    }
          }, function(e) {
            if (e) {
              // An error occurred.
              throw e;
            }
            // Otherwise, BrowserFS is ready to use! <-- right here, it's ready here.
          });
        });
  }