GoogleChromeLabs / GoogleChromeLabs/comlink

Unable to `Function.apply` on proxied object

Open
#579 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
12.8k
Forks
435
PR merge metrics
No merged PRs in 30d

Description

#### `worker.js`

```js
importScripts("https://unpkg.com/comlink/dist/umd/comlink.js");
// importScripts("../../../dist/umd/comlink.js");

const obj = {
foo(...args) {
console.log(this);
const list = this.bar();
console.log(list);
list.push(...args);
return list;
},
bar() {
return [1, 2];
}
};

Comlink.expose(obj);
```

#### `main.js`

```js
import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs";
// import * as Comlink from "../../../dist/esm/comlink.mjs";

async function init() {
const obj = Comlink.wrap(new Worker("worker.js"));
// const list = await obj.foo(3, 4); // this is fine
// const list = await obj.foo(...[3, 4]); // this is fine
const list = await obj.foo.apply(obj, [3, 4]); // this is not
console.log(list);
}

init();
```

Running this code will result in:

```
Proxy {length: 0, name: 'target', prototype: {…}}
Promise {}
Uncaught (in promise) TypeError: list.push is not a function
at Proxy.foo (worker.js:9:10)
at callback (comlink.js:100:50)
```

While I can workaround this using the spread syntax, Babel and other transpiler will [desugar](https://babeljs.io/repl#?browsers=ie%2011&code_lz=MYewdgzgLgBCBGArGBeGBvAUASAGYhAAoA6UgQwCcBzCASg0xiedEhABsBTY9kKwyjVoBuRswC-mSZgSJi-IqWIBtAIwAaGACYAuiKA) it to `obj.foo.apply(obj, [1, 2])` again.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.