Skip to content

Commit

Permalink
docs: update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
mys1024 committed Mar 9, 2024
1 parent 3f3e61c commit 676ed5f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
45 changes: 41 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ English | [中文文档](./README_zh.md)

![Concept](./docs/concept.png)

## Compatibility

`worker-fn` is compatible with runtimes that support the [Web Workers API](https://developer.mozilla.org/docs/Web/API/Web_Workers_API), such as browsers and [Deno](https://deno.com), but currently is not compatible with `node:worker_threads` in Node.js.

## Usage

### Using in browsers or Deno

In `math.worker.ts`:

```typescript
Expand Down Expand Up @@ -59,6 +57,45 @@ console.log(await add(1, 2)); // 3
console.log(await fib(5)); // 5
```

### Using in Node.js with `node:worker_threads`

In `math.worker.ts`:

```typescript
import { parentPort } from "node:worker_threads";
import { defineWorkerFn } from "worker-fn";

function add(a: number, b: number) {
return a + b;
}

function fib(n: number): number {
return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);
}

defineWorkerFn("add", add, { port: parentPort! });
defineWorkerFn("fib", fib, { port: parentPort! });

export type Add = typeof add;
export type Fib = typeof fib;
```

In `math.ts`:

```typescript
import { Worker } from "node:worker_threads";
import { useWorkerFn } from "worker-fn";
import type { Add, Fib } from "./math.worker.ts";

const worker = new Worker(new URL("./math.worker.ts", import.meta.url));

export const add = useWorkerFn<Add>("add", worker);
export const fib = useWorkerFn<Fib>("fib", worker);

console.log(await add(1, 2)); // 3
console.log(await fib(5)); // 5
```

## Importing from JSR

`worker-fn` is published on both [npm](https://www.npmjs.com/package/worker-fn) and [JSR](https://jsr.io/@mys/worker-fn).
Expand Down
45 changes: 41 additions & 4 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

![Concept](./docs/concept.png)

## 兼容性

`worker-fn` 兼容支持 [Web Workers API](https://developer.mozilla.org/docs/Web/API/Web_Workers_API) 的运行时,例如浏览器和 [Deno](https://deno.com),但目前不兼容 Node.js 内置的 Worker 模块 `node:worker_threads`

## 用法

### 在浏览器或 Deno 中使用

`math.worker.ts`:

```typescript
Expand Down Expand Up @@ -59,6 +57,45 @@ console.log(await add(1, 2)); // 3
console.log(await fib(5)); // 5
```

### 在 Node.js 中与 `node:worker_threads` 一起使用

`math.worker.ts`:

```typescript
import { parentPort } from "node:worker_threads";
import { defineWorkerFn } from "worker-fn";

function add(a: number, b: number) {
return a + b;
}

function fib(n: number): number {
return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);
}

defineWorkerFn("add", add, { port: parentPort! });
defineWorkerFn("fib", fib, { port: parentPort! });

export type Add = typeof add;
export type Fib = typeof fib;
```

`math.ts`:

```typescript
import { Worker } from "node:worker_threads";
import { useWorkerFn } from "worker-fn";
import type { Add, Fib } from "./math.worker.ts";

const worker = new Worker(new URL("./math.worker.ts", import.meta.url));

export const add = useWorkerFn<Add>("add", worker);
export const fib = useWorkerFn<Fib>("fib", worker);

console.log(await add(1, 2)); // 3
console.log(await fib(5)); // 5
```

## 从 JSR 导入

`worker-fn` 同时发布在 [npm](https://www.npmjs.com/package/worker-fn)[JSR](https://jsr.io/@mys/worker-fn) 上。
Expand Down

0 comments on commit 676ed5f

Please sign in to comment.