Skip to content

Commit a88f600

Browse files
committed
Switch to throw marker to allow throwing scalars
1 parent 911d2d2 commit a88f600

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

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
// prettier-ignore
3131
type Promisify<T> =
@@ -100,24 +100,23 @@ export const transferHandlers = new Map<string, TransferHandler>([
100100
[
101101
"throw",
102102
{
103-
canHandle: obj => throwSet.has(obj),
104-
serialize(obj) {
105-
const isError = obj instanceof Error;
106-
let serialized = obj;
103+
canHandle: obj => typeof obj === "object" && throwMarker in obj,
104+
serialize({ value }) {
105+
const isError = value instanceof Error;
106+
let serialized = { isError, value };
107107
if (isError) {
108-
serialized = {
109-
isError,
110-
message: obj.message,
111-
stack: obj.stack
108+
serialized.value = {
109+
message: value.message,
110+
stack: value.stack
112111
};
113112
}
114113
return [serialized, []];
115114
},
116-
deserialize(obj) {
117-
if ((obj as any).isError) {
118-
throw Object.assign(new Error(), obj);
115+
deserialize(serialized) {
116+
if (serialized.isError) {
117+
throw Object.assign(new Error(), serialized.value);
119118
}
120-
throw obj;
119+
throw serialized.value;
121120
}
122121
}
123122
]
@@ -173,14 +172,12 @@ export function expose(obj: any, ep: Endpoint = self as any) {
173172
}
174173
break;
175174
}
176-
} catch (e) {
177-
returnValue = e;
178-
throwSet.add(e);
175+
} catch (value) {
176+
returnValue = { value, [throwMarker]: 0 };
179177
}
180178
Promise.resolve(returnValue)
181-
.catch(e => {
182-
throwSet.add(e);
183-
return e;
179+
.catch(value => {
180+
return { value, [throwMarker]: 0 };
184181
})
185182
.then(returnValue => {
186183
const [wireValue, transferables] = toWireValue(returnValue);

tests/same_window.comlink.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ 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
});
@@ -156,9 +156,9 @@ describe("Comlink in the same realm", function() {
156156
await thing();
157157
throw "Should have thrown";
158158
} catch (err) {
159-
expect(err).to.not.eq("Should have thrown");
160-
expect(err).to.be("oops");
161-
expect(typeof err).to.be("string");
159+
expect(err).to.not.equal("Should have thrown");
160+
expect(err).to.equal("oops");
161+
expect(typeof err).to.equal("string");
162162
}
163163
});
164164

0 commit comments

Comments
 (0)