@@ -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 = '$$=' + 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+ ( self . ImageBitmap && x instanceof ImageBitmap )
27+ ) ) ) ;
28+ } ,
29+ // error handler - callback(id, ERROR(1), error)
30+ er => { postMessage ( [ e . data [ 0 ] , 1 , '' + er ] ) ; }
31+ ) ;
32+ } ) ;
33+ const workerURL = URL . createObjectURL ( new Blob ( [ script ] ) ) ;
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]`:
@@ -60,8 +60,8 @@ export default function greenlet(asyncFunction) {
6060 worker . postMessage ( [ currentId , args ] , args . filter ( x => (
6161 ( x instanceof ArrayBuffer ) ||
6262 ( x instanceof MessagePort ) ||
63- ( x instanceof ImageBitmap )
63+ ( self . ImageBitmap && x instanceof ImageBitmap )
6464 ) ) ) ;
6565 } ) ;
6666 } ;
67- }
67+ }
0 commit comments