Skip to content

Commit 9d247b1

Browse files
authored
Merge branch 'master' into fix-types
2 parents da818ac + 044cc9c commit 9d247b1

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "comlink",
3-
"version": "4.2.0",
3+
"version": "4.2.1",
44
"description": "Comlink makes WebWorkers enjoyable",
55
"main": "dist/umd/comlink.js",
66
"module": "dist/esm/comlink.mjs",

src/comlink.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export { Endpoint };
2525
export const proxyMarker = Symbol("Comlink.proxy");
2626
export const createEndpoint = Symbol("Comlink.endpoint");
2727
export const releaseProxy = Symbol("Comlink.releaseProxy");
28-
const throwSet = new WeakSet();
28+
const throwMarker = Symbol("Comlink.thrown");
2929

3030
/**
3131
* Interface of values that were marked to be proxied with `comlink.proxy()`.
@@ -201,24 +201,23 @@ export const transferHandlers = new Map<string, TransferHandler>([
201201
[
202202
"throw",
203203
{
204-
canHandle: obj => throwSet.has(obj),
205-
serialize(obj) {
206-
const isError = obj instanceof Error;
207-
let serialized = obj;
204+
canHandle: obj => typeof obj === "object" && throwMarker in obj,
205+
serialize({ value }) {
206+
const isError = value instanceof Error;
207+
let serialized = { isError, value };
208208
if (isError) {
209-
serialized = {
210-
isError,
211-
message: obj.message,
212-
stack: obj.stack
209+
serialized.value = {
210+
message: value.message,
211+
stack: value.stack
213212
};
214213
}
215214
return [serialized, []];
216215
},
217-
deserialize(obj) {
218-
if ((obj as any).isError) {
219-
throw Object.assign(new Error(), obj);
216+
deserialize(serialized) {
217+
if (serialized.isError) {
218+
throw Object.assign(new Error(), serialized.value);
220219
}
221-
throw obj;
220+
throw serialized.value;
222221
}
223222
}
224223
]
@@ -274,14 +273,12 @@ export function expose(obj: any, ep: Endpoint = self as any) {
274273
}
275274
break;
276275
}
277-
} catch (e) {
278-
returnValue = e;
279-
throwSet.add(e);
276+
} catch (value) {
277+
returnValue = { value, [throwMarker]: 0 };
280278
}
281279
Promise.resolve(returnValue)
282-
.catch(e => {
283-
throwSet.add(e);
284-
return e;
280+
.catch(value => {
281+
return { value, [throwMarker]: 0 };
285282
})
286283
.then(returnValue => {
287284
const [wireValue, transferables] = toWireValue(returnValue);

tests/same_window.comlink.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,26 @@ describe("Comlink in the same realm", function() {
142142
await thing();
143143
throw "Should have thrown";
144144
} catch (err) {
145-
expect(err).to.not.eq("Should have thrown");
145+
expect(err).to.not.equal("Should have thrown");
146146
expect(err.test).to.equal(true);
147147
}
148148
});
149149

150+
it("can rethrow scalars", async function() {
151+
const thing = Comlink.wrap(this.port1);
152+
Comlink.expose(_ => {
153+
throw "oops";
154+
}, this.port2);
155+
try {
156+
await thing();
157+
throw "Should have thrown";
158+
} catch (err) {
159+
expect(err).to.not.equal("Should have thrown");
160+
expect(err).to.equal("oops");
161+
expect(typeof err).to.equal("string");
162+
}
163+
});
164+
150165
it("can work with parameterized functions", async function() {
151166
const thing = Comlink.wrap(this.port1);
152167
Comlink.expose((a, b) => a + b, this.port2);

0 commit comments

Comments
 (0)