diff --git a/greenlet.js b/greenlet.js index e83eea3..4d317e6 100644 --- a/greenlet.js +++ b/greenlet.js @@ -11,10 +11,11 @@ export default function greenlet(asyncFunction) { // Create an "inline" worker (1:1 at definition time) const worker = new Worker( + // Use a data URI for the worker's src. It inlines the target function and an RPC handler: 'data:,$$='+asyncFunction+';onmessage='+(e => { /* global $$ */ - // Invoking within then() captures exceptions in userFunc() as rejections + // Invoking within then() captures exceptions in the supplied async function as rejections Promise.resolve().then( $$.bind.apply($$, e.data) ).then( @@ -31,36 +32,6 @@ export default function greenlet(asyncFunction) { er => { postMessage([e.data[0], 1, '' + er]); } ); }) - - /* - // The URL is a pointer to a stringified function (as a blob object) - URL.createObjectURL( - new Blob([ - '$$=', asyncFunction, - // Register our wrapper function as the message handler - ';onmessage=', - // userFunc() is the user-supplied async function - e => { - // Invoking within then() captures exceptions in userFunc() as rejections - Promise.resolve().then( - $$.bind.apply($$, e.data) - ).then( - // success handler - callback(id, SUCCESS(0), result) - // if `d` is transferable transfer zero-copy - d => { - postMessage([e.data[0], 0, d], [d].filter(x => ( - (x instanceof ArrayBuffer) || - (x instanceof MessagePort) || - (x instanceof ImageBitmap) - ))); - }, - // error handler - callback(id, ERROR(1), error) - er => { postMessage([e.data[0], 1, '' + er]); } - ); - } - ]) - ) - */ ); /** Handle RPC results/errors coming back out of the worker.