Skip to content

Commit 371d10c

Browse files
committed
Clean up and comment data URI implementation
1 parent a7ab747 commit 371d10c

File tree

1 file changed

+2
-31
lines changed

1 file changed

+2
-31
lines changed

greenlet.js

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ export default function greenlet(asyncFunction) {
1111

1212
// Create an "inline" worker (1:1 at definition time)
1313
const worker = new Worker(
14+
// Use a data URI for the worker's src. It inlines the target function and an RPC handler:
1415
'data:,$$='+asyncFunction+';onmessage='+(e => {
1516
/* global $$ */
1617

17-
// Invoking within then() captures exceptions in userFunc() as rejections
18+
// Invoking within then() captures exceptions in the supplied async function as rejections
1819
Promise.resolve().then(
1920
$$.bind.apply($$, e.data)
2021
).then(
@@ -31,36 +32,6 @@ export default function greenlet(asyncFunction) {
3132
er => { postMessage([e.data[0], 1, '' + er]); }
3233
);
3334
})
34-
35-
/*
36-
// The URL is a pointer to a stringified function (as a blob object)
37-
URL.createObjectURL(
38-
new Blob([
39-
'$$=', asyncFunction,
40-
// Register our wrapper function as the message handler
41-
';onmessage=',
42-
// userFunc() is the user-supplied async function
43-
e => {
44-
// Invoking within then() captures exceptions in userFunc() as rejections
45-
Promise.resolve().then(
46-
$$.bind.apply($$, e.data)
47-
).then(
48-
// success handler - callback(id, SUCCESS(0), result)
49-
// if `d` is transferable transfer zero-copy
50-
d => {
51-
postMessage([e.data[0], 0, d], [d].filter(x => (
52-
(x instanceof ArrayBuffer) ||
53-
(x instanceof MessagePort) ||
54-
(x instanceof ImageBitmap)
55-
)));
56-
},
57-
// error handler - callback(id, ERROR(1), error)
58-
er => { postMessage([e.data[0], 1, '' + er]); }
59-
);
60-
}
61-
])
62-
)
63-
*/
6435
);
6536

6637
/** Handle RPC results/errors coming back out of the worker.

0 commit comments

Comments
 (0)