@@ -9,30 +9,30 @@ export default function greenlet(asyncFunction) {
99 // Outward-facing promises store their "controllers" (`[request, reject]`) here:
1010 const promises = { } ;
1111
12- // Create an "inline" worker (1:1 at definition time)
13- const worker = new Worker (
14- // Use a data URI for the worker's src. It inlines the target function and an RPC handler:
15- 'data:,$$=' + asyncFunction + ';onmessage=' + ( e => {
16- /* global $$ */
12+ // Use a data URI for the worker's src. It inlines the target function and an RPC handler:
13+ const script = 'data:$$=' + asyncFunction + ';onmessage=' + ( e => {
14+ /* global $$ */
1715
18- // Invoking within then() captures exceptions in the supplied async function as rejections
19- Promise . resolve ( e . data [ 1 ] ) . then (
20- v => $$ . apply ( $$ , v )
21- ) . then (
22- // success handler - callback(id, SUCCESS(0), result)
23- // if `d` is transferable transfer zero-copy
24- d => {
25- postMessage ( [ e . data [ 0 ] , 0 , d ] , [ d ] . filter ( x => (
26- ( x instanceof ArrayBuffer ) ||
27- ( x instanceof MessagePort ) ||
28- ( x instanceof ImageBitmap )
29- ) ) ) ;
30- } ,
31- // error handler - callback(id, ERROR(1), error)
32- er => { postMessage ( [ e . data [ 0 ] , 1 , '' + er ] ) ; }
33- ) ;
34- } )
35- ) ;
16+ // Invoking within then() captures exceptions in the supplied async function as rejections
17+ Promise . resolve ( e . data [ 1 ] ) . then (
18+ v => $$ . apply ( $$ , v )
19+ ) . then (
20+ // success handler - callback(id, SUCCESS(0), result)
21+ // if `d` is transferable transfer zero-copy
22+ d => {
23+ postMessage ( [ e . data [ 0 ] , 0 , d ] , [ d ] . filter ( x => (
24+ ( x instanceof ArrayBuffer ) ||
25+ ( x instanceof MessagePort )
26+ ) ) ) ;
27+ } ,
28+ // error handler - callback(id, ERROR(1), error)
29+ er => { postMessage ( [ e . data [ 0 ] , 1 , '' + er ] ) ; }
30+ ) ;
31+ } ) ;
32+ const blob = new Blob ( [ script ] ) ;
33+ const workerURL = URL . createObjectURL ( blob ) ;
34+ // Create an "inline" worker (1:1 at definition time)
35+ const worker = new Worker ( workerURL ) ;
3636
3737 /** Handle RPC results/errors coming back out of the worker.
3838 * Messages coming from the worker take the form `[id, status, result]`:
@@ -59,8 +59,7 @@ export default function greenlet(asyncFunction) {
5959 // The filter is to provide a list of transferables to send zero-copy
6060 worker . postMessage ( [ currentId , args ] , args . filter ( x => (
6161 ( x instanceof ArrayBuffer ) ||
62- ( x instanceof MessagePort ) ||
63- ( x instanceof ImageBitmap )
62+ ( x instanceof MessagePort )
6463 ) ) ) ;
6564 } ) ;
6665 } ;
0 commit comments