Skip to content

Commit 96d1197

Browse files
authored
Expose target to make comlink compatible with proxy-polyfill (GoogleChromeLabs#389)
Expose target to make comlink compatible with proxy-polyfill
2 parents d1389fc + 3280e68 commit 96d1197

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/comlink.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ function closeEndPoint(endpoint: Endpoint) {
202202
if (isMessagePort(endpoint)) endpoint.close();
203203
}
204204

205-
export function wrap<T>(ep: Endpoint): Remote<T> {
206-
return createProxy<T>(ep) as any;
205+
export function wrap<T>(ep: Endpoint, target?: any): Remote<T> {
206+
return createProxy<T>(ep, [], target) as any;
207207
}
208208

209209
function throwIfProxyReleased(isReleased: boolean) {
@@ -214,10 +214,11 @@ function throwIfProxyReleased(isReleased: boolean) {
214214

215215
function createProxy<T>(
216216
ep: Endpoint,
217-
path: (string | number | symbol)[] = []
217+
path: (string | number | symbol)[] = [],
218+
target: object = function() {}
218219
): Remote<T> {
219220
let isProxyReleased = false;
220-
const proxy = new Proxy(function() {}, {
221+
const proxy = new Proxy(target, {
221222
get(_target, prop) {
222223
throwIfProxyReleased(isProxyReleased);
223224
if (prop === releaseProxy) {

tests/same_window.comlink.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@ describe("Comlink in the same realm", function() {
518518
await instance[Comlink.releaseProxy]();
519519
expect(() => instance.method()).to.throw();
520520
});
521+
522+
it('can proxy with a given target', async function() {
523+
const thing = Comlink.wrap(this.port1, { value: {} });
524+
Comlink.expose({ value: 4 }, this.port2);
525+
expect(await thing.value).to.equal(4);
526+
});
521527
});
522528

523529
function guardedIt(f) {

0 commit comments

Comments
 (0)