GoogleChromeLabs / GoogleChromeLabs/comlink

Set operation is not awaitable

Open
#638 2 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

I'm currently working on something that has parts similar to comlink, and I read through the code, as to not reinvent the wheel.

I found this piece of code, which has been marked as FIXME.

https://github.com/GoogleChromeLabs/comlink/blob/dffe9050f63b1b39f30213adeb1dd4b9ed7d2594/src/comlink.ts#L476-L490

This code causes problems. `set` **has to** return boolean because you can't await the assignment.

Unfortunately, there's nothing like this in ES currently, and there probably won't be for the foreseeable future:

```
const variable (await =) 'some value';
```

I even did some tests on awaiting the entire expression itself, and it is indeed not trackable in any way.

You cannot chain operations if you depend on setting a value through a proxy. Of course, there's always the option of endpoints, but there is a viable alternative to the current implementation, and it would make this use case possible.

Moreover, it is not entirely clear that this should not work as expected.

There are two possible approaches to this issue:

1. Export a unique symbol that would "end" the chain to an assignment. This would translate in adding an extra "if" case in the `get` interface.
2. On top of that, use ts-morph (or maybe even a JS AST manipulation library) to compile the code, so that the normal assignment translates.

Option 1. would make the code look like this:
```ts
import {set} from 'comlink'; // this is the symbol

await myProxy.foo.bar[set](3); // this is now awaitable
```

Option 2. would keep the code as it currently is, but would require an extra compilation step plus a babel/ts-morph/rollup/unplugin/etc. implementation.

This would be a big design change in terms of DX, but it would change the library for the better, I think.

I can provide a PR with tests as well, just let me know if this is something you're interested in.

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.