Open Yoric opened 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?
Can you put the source code that fails somewhere?
I've created a new branch "dev" and update the source code in there. The commit is ce8720d75d
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.
I do not really understand, do you have some examples?
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.
Any news on this?
This problem was left open. Not sure it will be available for v3. This will surely be treated for v4.
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.