jbarbin / EPub-Reader

An open web app for reading epub e-Books
9 stars 5 forks source link

Move zip handling to a worker #15

Open Yoric opened 11 years ago

Yoric commented 11 years ago

Your zip library is synchronous. On a slow processor, this will completely freeze the application while it is reading large files. You should move it to a worker, to ensure that it doesn't freeze anything.

aratagobal commented 11 years ago

I tried to use a worker to do this: var zip = new JSZip(data); . Unfortunately i am having some problems because the worker returns the object without its functions. I read that workers can't pass data with functions. And i really need those functions...

How can i do this?

Yoric commented 11 years ago

Can you put the source code that fails somewhere?

aratagobal commented 11 years ago

I've created a new branch "dev" and update the source code in there. The commit is ce8720d75d

Yoric commented 11 years ago

You understand correctly that you can't pass functions (or methods, or closures) with postMessage.

What you need to do is keep the object on the worker thread and call the methods from the main thread through messages. e.g. to call method file, send a message {method: "file", args: [...]} to the worker.

Note that this task is rather long, and is probably not your first priority.

aratagobal commented 11 years ago

I do not really understand, do you have some examples?

Yoric commented 11 years ago

It's rather tricky. I will give you an example with simple arithmetics.

// worker.js (extracts)
self.onmessage = function(e) {
   var data = e.data;
   /**
    * We expect that data will look like:
    * { method: ..., args: ... }
    * where `method` is the name of the method and `args`
    * gives the arguments to pass to the method.
    */
  var method = Methods[method];
  var result = method.apply(Methods, args);
  self.postMessage(result);
};

var Methods = {
   plus: function plus(x, y) {
      return x + y;
   }
};
// main.js (extracts)
var worker = new Worker("worker.js");

var add = function add(x, y, callback) {
    worker.onmessage = function(e) {
        worker.onmessage = null; // Unregister self
        callback(e.data);
    }
    // FIXME: Handle errors
    worker.postMessage({method: "plus", args: [x, y]});
}

Alternatively, you could use zip.js which already does all the worker bits for you.

Yoric commented 11 years ago

Any news on this?

aratagobal commented 11 years ago

This problem was left open. Not sure it will be available for v3. This will surely be treated for v4.