Skip to content

Commit

Permalink
Note Deno unsupported for async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
101arrowz committed Mar 21, 2021
1 parent 7261536 commit 1a67ed1
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ unzipper.push(zipChunk2);
unzipper.push(zipChunk3, true);
```

As you may have guessed, there is an asynchronous version of every method as well. Unlike most libraries, this will cause the compression or decompression run in a separate thread entirely and automatically by using Web (or Node) Workers. This means that the processing will not block the main thread at all.
As you may have guessed, there is an asynchronous version of every method as well. Unlike most libraries, this will cause the compression or decompression run in a separate thread entirely and automatically by using Web (or Node) Workers (as of now, Deno is unsupported). This means that the processing will not block the main thread at all.

Note that there is a significant initial overhead to using workers of about 70ms, so it's best to avoid the asynchronous API unless necessary. However, if you're compressing multiple large files at once, or the synchronous API causes the main thread to hang for too long, the callback APIs are an order of magnitude better.
```js
Expand Down
14 changes: 1 addition & 13 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
const ch2: Record<string, string> = {};

let durl = (c: string) => URL.createObjectURL(new Blob([c], { type: 'text/javascript' }));
let cwk = (u: string) => new Worker(u);

try {
URL.revokeObjectURL(durl(''));
} catch(e) {
// We're in Deno or a very old browser
durl = c => 'data:application/javascript;charset=UTF-8,' + encodeURI(c);
// If Deno, this is necessary; if not, this changes nothing
cwk = u => new Worker(u, { type: 'module' });
}

export default <T>(c: string, id: number, msg: unknown, transfer: ArrayBuffer[], cb: (err: Error, msg: T) => void) => {
const w = cwk(ch2[id] ||= durl(c));
const w = new Worker(ch2[id] ||= URL.createObjectURL(new Blob([c], { type: 'text/javascript' })));
w.onerror = e => cb(e.error, null);
w.onmessage = e => cb(null, e.data);
w.postMessage(msg, transfer);
Expand Down

0 comments on commit 1a67ed1

Please sign in to comment.