https://github.com/worker-tools/caplink
A modernized fork of Comlink with many open PRs merged and the ability to use proxies in Caplink calls
https://github.com/worker-tools/caplink
Last synced: 5 months ago
JSON representation
A modernized fork of Comlink with many open PRs merged and the ability to use proxies in Caplink calls
- Host: GitHub
- URL: https://github.com/worker-tools/caplink
- Owner: worker-tools
- License: apache-2.0
- Created: 2024-07-14T09:36:14.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-11T07:29:30.000Z (over 1 year ago)
- Last Synced: 2025-04-09T03:51:09.489Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 2.17 MB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Caplink
A modernized fork of [Comlink](https://github.com/GoogleChromeLabs/comlink) with many open PRs merged and the ability to use proxies as values in Caplink calls.
```ts
// file: worker-1.ts
import * as Caplink from '@workers/caplink';
export class Greeter {
helloWorld(name = "World") {
console.log(`Hello, ${name}!`);
}
}
export class W1Fns {
static newGreeter() {
return Caplink.proxy(new Greeter());
}
}
Caplink.expose(W1Fns);
```
```ts
// file: worker-2.ts
import * as Caplink from '@workers/caplink';
import type { Greeter } from "./worker-1.ts";
export class W2Fns {
static async takeGreeter(greeter: Caplink.Remote) {
using greeter_ = greeter; // can opt into explicit resource management
await greeter_.helloWorld("Worker 2");
} // local resources freed
}
Caplink.expose(W2Fns);
```
```ts
// file: index.ts
import * as Caplink from '@workers/caplink';
import type { W1Fns } from "./worker-1.ts";
import type { W2Fns } from "./worker-2.ts";
const w1 = Caplink.wrap(
new Worker(new URL("./worker-1.ts", import.meta.url), { type: "module" }),
);
const w2 = Caplink.wrap(
new Worker(new URL("./worker-2.ts", import.meta.url), { type: "module" }),
);
using remoteGreeter = await w1.newGreeter();
await remoteGreeter.helloWorld(); // logs "Hello, World" in worker 1
await w2.takeGreeter(remoteGreeter); // logs "Hello, Worker 2" in worker 1
```