Skip to content

Commit ef6ac3d

Browse files
authored
Merge pull request #38 from spkellydev/master
Add support for Edge, Safari
2 parents d4d3f0d + 1eb56d1 commit ef6ac3d

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

greenlet.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"eslint": "^4.16.0",
3939
"eslint-config-developit": "^1.1.1",
4040
"karmatic": "^1.0.6",
41-
"microbundle": "^0.4.3"
41+
"microbundle": "^0.4.3",
42+
"webpack": "^4.29.6"
4243
}
4344
}

0 commit comments

Comments
 (0)