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.07k stars 220 forks source link

AsyncMirror sample #146

Closed andres-asm closed 8 years ago

andres-asm commented 8 years ago

I tried to copy your AsyncMirror examplecode

/**
 * Run this prior to starting your Emscripten module.
 * @param dropboxClient An authenticated DropboxJS client.
 */
function asyncSetup(dropboxClient, cb) {
  var dbfs = new BrowserFS.FileSystem.Dropbox(dropboxClient);
  // Wrap in AsyncMirrorFS.
  var asyncMirror = new BrowserFS.FileSystem.AsyncMirrorFS(
    new BrowserFS.FileSystem.InMemory(), dbfs);

  // Downloads the entire contents of the Dropbox backend into memory.
  // You'll probably want to use an app folder, and check that you
  // aren't pulling in a huge amount of data here.
  asyncMirror.initialize((err?) => {
    // Initialize it as the root file system.
    BrowserFS.initialize(asyncMirror);
    // BFS is ready for Emscripten!
    cb();
  });
}
function setupBFS() {
  // Grab the BrowserFS Emscripten FS plugin.
  var BFS = new BrowserFS.EmscriptenFS();
  // Create the folder that we'll turn into a mount point.
  FS.createFolder(FS.root, 'data', true, true);
  // Mount BFS's root folder into the '/data' folder.
  FS.mount(BFS, {root: '/'}, '/data');
}

but I have two issues (so far)

SyntaxError: expected expression, got ')' here asyncMirror.initialize((err?) => {

And AsyncMirrorFS is not a constructor.

I'm a js newbie, I'm trying to integrate dropbox to retroarch emscripten, I'm not sure if there is a sintax error there or I'm just failing to understand js

jvilk commented 8 years ago

Oof, thanks for the report! You've found two bugs in the code:

  1. I used lambda syntax in that example without thinking, which will only with in ECMAScript 6-compatible browsers. Transpilers like TypeScript will convert that into a regular function.
  2. It's AsyncMirror, not AsyncMirrorFS.

I'll have to update the docs when I next get a chance! Here's the corrected version:

/**
 * Run this prior to starting your Emscripten module.
 * @param dropboxClient An authenticated DropboxJS client.
 */
function asyncSetup(dropboxClient, cb) {
  var dbfs = new BrowserFS.FileSystem.Dropbox(dropboxClient);
  // Wrap in AsyncMirrorFS.
  var asyncMirror = new BrowserFS.FileSystem.AsyncMirror(
    new BrowserFS.FileSystem.InMemory(), dbfs);

  // Downloads the entire contents of the Dropbox backend into memory.
  // You'll probably want to use an app folder, and check that you
  // aren't pulling in a huge amount of data here.
  asyncMirror.initialize(function(err) {
    // Initialize it as the root file system.
    BrowserFS.initialize(asyncMirror);
    // BFS is ready for Emscripten!
    cb();
  });
}
function setupBFS() {
  // Grab the BrowserFS Emscripten FS plugin.
  var BFS = new BrowserFS.EmscriptenFS();
  // Create the folder that we'll turn into a mount point.
  FS.createFolder(FS.root, 'data', true, true);
  // Mount BFS's root folder into the '/data' folder.
  FS.mount(BFS, {root: '/'}, '/data');
}
andres-asm commented 8 years ago

Thanks!

I got further now, but I'm still getting an error

     console.log("WEBPLAYER: Initializing DropBoxFS");
     var BFS = new BrowserFS.EmscriptenFS();
     FS.createFolder(FS.root, 'retroarch', true, true);
     FS.mount(BFS, {root: '/'}, '/retroarch');
     console.log('WEBPLAYER: DropBox initialized');

TypeError: this.root is null on this call FS.mount(BFS, {root: '/'}, '/retroarch');

andres-asm commented 8 years ago

NVM, finally understood it and it's working thanks a lot!

andres-asm commented 8 years ago

Thanks a lot for your help This is my code, it might be bad or not idk, it's my first ever interaction with js but it works and I wasn't able to find any other samples.

https://github.com/libretro/RetroArch/blob/master/emscripten/webplayer.html https://bot.libretro.com/.radius/webplayer.html

jvilk commented 8 years ago

:) The code looks decent to me! Glad to hear it all worked out.