Skip to content
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

Open
joshnoe opened this issue Feb 8, 2017 · 9 comments
Open

Async #49

joshnoe opened this issue Feb 8, 2017 · 9 comments

Comments

@joshnoe
Copy link

joshnoe commented Feb 8, 2017

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.

@LuoZijun
Copy link

@joshnoe i think that is one bad idea.

normaly:

parallel for computing and asynchronous for I/O .

@joshnoe
Copy link
Author

joshnoe commented Feb 10, 2017

@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.

@LuoZijun
Copy link

@joshnoe thank you let me know about web woker.
i have do some research about web worker: https://gist.github.com/LuoZijun/9efafd7a69ffb00b0ad8db373073315e

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.

@joshnoe
Copy link
Author

joshnoe commented Feb 13, 2017

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.

@LuoZijun
Copy link

LuoZijun commented Feb 13, 2017

@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);
};

@joshnoe
Copy link
Author

joshnoe commented Feb 14, 2017

@LuoZijun Yea something like that would work.

@LuoZijun
Copy link

@joshnoe :)

@joshnoe
Copy link
Author

joshnoe commented Feb 14, 2017

@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.

@LuoZijun
Copy link

@joshnoe i'm not this Library Author, you should talk with @imaya .
at last, i think support parallel compress/decompress is very cool. i like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants