-
Notifications
You must be signed in to change notification settings - Fork 351
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
Async #49
Comments
@joshnoe i think that is one bad idea. normaly:
|
@LuoZijun You're right, I misspoke. Parallel is what I want. This is easy to do with a web worker, which is what I ended up doing. It would be nice if it was integrated into the existing API though. I'd be happy to take a crack at it if there's a chance it'd be merged in. |
@joshnoe thank you let me know about web woker. some browser do not allow this: // IE Problem.
var worker = new Worker('data:text/javascript;charset=US-ASCII,onmessage%20%3D%20function%20%28oEvent%29%20%7B%0A%09postMessage%28%7B%0A%09%09%22id%22%3A%20oEvent.data.id%2C%0A%09%09%22evaluated%22%3A%20eval%28oEvent.data.code%29%0A%09%7D%29%3B%0A%7D'); i think that is one big problem. |
Good point, but even without support from all browsers, I still think this is a worthwhile feature as long as its browser compatibility is well documented. You could always fall-back to synchronous if the browser doesn't support web workers. |
@joshnoe okay, this maybe work. zlib.min.js var Zlib = {
// include zlib source code
};
// worker code
onmessage = function(evt) {
var id = evt.data.id;
var action = evt.data.action;
var data = evt.data.data;
var result = null;
if ( action == 'compress' ){
result = (new Zlib.Deflate(data)).compress();
} else if ( action == 'decompress' ){
result = (new Zlib.Inflate(data)).decompress();
}
postMessage({
"id" : id,
"result": result
});
} app.js var worker = new Worker('./zlib.min.js');
// compress data
worker.postMessage({
"id" : 1, // random id or sha1-256 hash
"action": "compress",
"data" : "Hello, 世界!"
});
// decompress data
worker.postMessage({
"id" : 1, // random id or sha1-256 hash
"action": "decompress",
"data" : new Uint8Array([256, 123,97, 96])
});
worker.onmessage = function (evt){
console.log("Result: ", evt.data);
}; |
@LuoZijun Yea something like that would work. |
@joshnoe :) |
@LuoZijun Are you interested in integrating this into the Library? You could have two new methods: "CompressParallel" and "DecompressParallel". These could return promises which would be resolved when they're complete (when the web worker "onmessage" is called. If the browser doesn't support web workers, it could just run synchronously. What do you think? I'd be happy to write something up and submit a pull request if you're busy. |
Currently, only synchronous compression / decompression is supported. It would be great if a callback could be passed (or better, a promise returned) to provide a way to compress / decompress asynchronously.
The text was updated successfully, but these errors were encountered: