Преглед изворни кода

Note Deno unsupported for async functions

Arjun Barrett пре 4 година
родитељ
комит
1a67ed14e6
2 измењених фајлова са 2 додато и 14 уклоњено
  1. 1 1
      README.md
  2. 1 13
      src/worker.ts

+ 1 - 1
README.md

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

+ 1 - 13
src/worker.ts

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