Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added access to Worker's terminate method when run through comlink-loaded #487

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ Windows and Web Workers have a slightly different variants of `postMessage`. If

For a usage example, take a look at the non-worker examples in the `docs` folder.

### `<proxy>.terminate()`

Web Workers must be terminated to release resources and threads. When using comlink with [`comlink-loader`](https://github.com/GoogleChromeLabs/comlink-loader) call the terminate method on the proxy returned by `comlink-loader` as comlink-loader doesn't give access to the underlying Worker object.

```ts
import MyWorker from "comlink-loader!./worker";
const workerProxy = new MyWorker() as any;
const result = await workerProxy.doSomething();
workerProxy.terminate();
```

Note: `workerProxy[Comlink.releaseProxy]();` wouldn't work in this case as it only releases the endpoint created for the proxy.

## TypeScript

Comlink does provide TypeScript types. When you `expose()` something of type `T`, the corresponding `wrap()` call will return something of type `Comlink.Remote<T>`. While this type has been battle-tested over some time now, it is implemented on a best-effort basis. There are some nuances that are incredibly hard if not impossible to encode correctly in TypeScript’s type system. It _may_ sometimes be necessary to force a certain type using `as unknown as <type>`.
Expand Down
6 changes: 6 additions & 0 deletions src/comlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ function createProxy<T>(
apply(_target, _thisArg, rawArgumentList) {
throwIfProxyReleased(isProxyReleased);
const last = path[path.length - 1];
// Users would expect terminate to kill the Worker not call a terminate method inside the worker
if (last === "terminate" && ep instanceof Worker) {
isProxyReleased = true;
ep.terminate();
return Promise.resolve();
}
if ((last as any) === createEndpoint) {
return requestResponseMessage(ep, {
type: MessageType.ENDPOINT,
Expand Down
17 changes: 17 additions & 0 deletions tests/worker.comlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,21 @@ describe("Comlink across workers", function () {
const otherProxy = Comlink.wrap(otherEp);
expect(await otherProxy(20, 1)).to.equal(21);
});

it("honors terminate on the proxy", function () {
const originalTerminate = this.worker.terminate;

let terminateCalled = false;
// poorman's mock
this.worker.terminate = function () {
terminateCalled = true;
};

const proxy = Comlink.wrap(this.worker);
expect(terminateCalled).to.be.false;
proxy.terminate();
expect(terminateCalled).to.be.true;

this.worker.terminate = originalTerminate;
});
});