Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
okikio authored Mar 17, 2024
2 parents 4c552b4 + da7c792 commit a9c1c6e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
test:
strategy:
matrix:
# deno-version: [v1.x, vx.x.x]
# deno-version: [vx.x.x]
deno-version: [v1.41.0]
os: [ubuntu-latest, windows-latest]
fail-fast: false

name: deno ${{ matrix.deno-version }}, ${{ matrix.os }}
name: Test on Deno ${{ matrix.deno-version }}, ${{ matrix.os }}
runs-on: ${{ matrix.os }}

steps:
Expand Down
25 changes: 15 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ name: Release
on:
push:
tags:
- 'v*'

permissions:
contents: write
- v*

jobs:
test:
name: Test
runs-on: ubuntu-latest

steps:
Expand All @@ -25,8 +23,10 @@ jobs:
run: deno task test

publish-to-jsr:
needs: test
name: Publish to JSR
runs-on: ubuntu-latest
needs: test

permissions:
contents: read
id-token: write
Expand All @@ -42,8 +42,9 @@ jobs:
run: deno publish

publish-to-npm:
needs: publish-to-jsr
name: Publish to npm
runs-on: ubuntu-latest
needs: publish-to-jsr

steps:
- name: Checkout repository
Expand Down Expand Up @@ -73,19 +74,23 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

changelog:
needs: publish-to-jsr
name: Changelog
runs-on: ubuntu-latest
needs: publish-to-jsr

permissions:
contents: write

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Node
uses: actions/setup-node@v4
- name: Setup Deno
uses: denoland/setup-deno@v1

- name: Changelog
run: npx changelogithub
run: deno run -A npm:changelogithub
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
14 changes: 1 addition & 13 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
"deno.lint": true,
"deno.unstable": false,
"deno.cacheOnSave": true,
"deno.suggest.imports.hosts":{
"https://deno.land": true
},
"deno.codeLens.implementations": false,
"deno.codeLens.references": false,
"deno.codeLens.referencesAllFunctions": false,
"typescript.inlayHints.enumMemberValues.enabled": false,
"typescript.inlayHints.functionLikeReturnTypes.enabled": false,
"typescript.inlayHints.parameterNames.enabled": "none",
"typescript.inlayHints.parameterTypes.enabled": false,
"typescript.inlayHints.propertyDeclarationTypes.enabled": false,
"typescript.inlayHints.variableTypes.enabled": false,
"[javascript]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true
Expand All @@ -32,7 +20,7 @@
"corepack",
"deno",
"denoland",
"okikio",
"minzip",
"pnpx",
"runtimes",
"Transferables",
Expand Down
84 changes: 69 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
<div align="center">

# worker-fn

[![GitHub License](https://img.shields.io/github/license/mys1024/worker-fn?&style=flat-square)](./LICENSE)
[![GitHub Tag](https://img.shields.io/github/v/tag/mys1024/worker-fn?sort=semver&style=flat-square&label=JSR&color=rgb(247%2C223%2C30))](https://jsr.io/@mys/worker-fn)
[![NPM Version](https://img.shields.io/npm/v/worker-fn?style=flat-square&color=rgb(203%2C56%2C55))](https://www.npmjs.com/package/worker-fn)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/worker-fn?style=flat-square)](https://bundlephobia.com/package/worker-fn)
[![NPM Downloads](https://img.shields.io/npm/dy/worker-fn?&style=flat-square)](https://www.npmjs.com/package/worker-fn)
[![GitHub Actions Workflow CI Status](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/ci.yml?label=CI&&style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/ci.yml)
[![GitHub Actions Workflow Release Status](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/release.yml?label=Release&&style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/release.yml)
[![license](https://img.shields.io/github/license/mys1024/worker-fn?&style=flat-square)](./LICENSE)
[![docs](https://img.shields.io/badge/docs-reference-rgb(247%2C223%2C30)?style=flat-square)](https://jsr.io/@mys/worker-fn/doc?style=flat-square)
[![jsr-version](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fjsr.io%2F%40mys%2Fworker-fn%2Fmeta.json&query=%24.latest&style=flat-square&label=jsr&color=rgb(247%2C223%2C30))](https://jsr.io/@mys/worker-fn)
[![npm-version](https://img.shields.io/npm/v/worker-fn?style=flat-square&color=rgb(203%2C56%2C55))](https://www.npmjs.com/package/worker-fn)
[![npm-minzip](https://img.shields.io/bundlephobia/minzip/worker-fn?style=flat-square&label=minzip)](https://bundlephobia.com/package/worker-fn)
[![npm-downloads](https://img.shields.io/npm/dy/worker-fn?&style=flat-square)](https://www.npmjs.com/package/worker-fn)
[![workflow-ci](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/ci.yml?label=ci&style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/ci.yml)
[![workflow-release](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/release.yml?label=release&style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/release.yml)

English | [中文文档](./README_zh.md)

</div>

`worker-fn` hides the complexity of communication between the JavaScript main thread and [Worker](https://developer.mozilla.org/docs/Web/API/Web_Workers_API) threads, making it easy to call the functions defined in workers.

`worker-fn` allows you to create **proxy functions** in the main thread that call the corresponding **worker functions** defined in Worker threads through message events. Proxy functions have the same function signatures as the corresponding worker functions (except that the return values of proxy functions will be wrapped in Promises).

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

## Usage

### Using in browser or Deno
### Basic

In `math.worker.ts`:

Expand Down Expand Up @@ -50,14 +55,18 @@ const worker = new Worker(new URL("./math.worker.ts", import.meta.url), {
type: "module",
});

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

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

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

<details>

<summary>Example</summary>

In `math.worker.ts`:

Expand Down Expand Up @@ -89,17 +98,62 @@ 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);
const add = useWorkerFn<Add>("add", worker);
const fib = useWorkerFn<Fib>("fib", worker);

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

</details>

### Using `defineWorkerFns()` and `useWorkerFns()`

<details>

<summary>Example</summary>

In `math.worker.ts`:

```typescript
import { defineWorkerFns } from "worker-fn";

const fns = {
add(a: number, b: number) {
return a + b;
},
fib(n: number): number {
return n <= 2 ? 1 : fns.fib(n - 1) + fns.fib(n - 2);
},
};

defineWorkerFns(fns);

export type Fns = typeof fns;
```

In `math.ts`:

```typescript
import { useWorkerFns } from "worker-fn";
import type { Fns } from "./math.worker.ts";

const worker = new Worker(new URL("./math.worker.ts", import.meta.url), {
type: "module",
});

const { add, fib } = useWorkerFns<Fns>(worker);

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

</details>

## 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). If you want to import `worker-fn` from JSR, please refer to [this document](https://jsr.io/docs/introduction#using-jsr-packages).

## License

MIT License &copy; 2024-PRESENT mys1024
[MIT](./LICENSE) License &copy; 2024-PRESENT [mys1024](https://github.com/mys1024)
86 changes: 70 additions & 16 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
<div align="center">

# worker-fn

[![GitHub License](https://img.shields.io/github/license/mys1024/worker-fn?&style=flat-square)](./LICENSE)
[![GitHub Tag](https://img.shields.io/github/v/tag/mys1024/worker-fn?sort=semver&style=flat-square&label=JSR&color=rgb(247%2C223%2C30))](https://jsr.io/@mys/worker-fn)
[![NPM Version](https://img.shields.io/npm/v/worker-fn?style=flat-square&color=rgb(203%2C56%2C55))](https://www.npmjs.com/package/worker-fn)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/worker-fn?style=flat-square)](https://bundlephobia.com/package/worker-fn)
[![NPM Downloads](https://img.shields.io/npm/dy/worker-fn?&style=flat-square)](https://www.npmjs.com/package/worker-fn)
[![GitHub Actions Workflow CI Status](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/ci.yml?label=CI&&style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/ci.yml)
[![GitHub Actions Workflow Release Status](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/release.yml?label=Release&&style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/release.yml)
[![license](https://img.shields.io/github/license/mys1024/worker-fn?&style=flat-square)](./LICENSE)
[![docs](https://img.shields.io/badge/docs-reference-rgb(247%2C223%2C30)?style=flat-square)](https://jsr.io/@mys/worker-fn/doc?style=flat-square)
[![jsr-version](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fjsr.io%2F%40mys%2Fworker-fn%2Fmeta.json&query=%24.latest&style=flat-square&label=jsr&color=rgb(247%2C223%2C30))](https://jsr.io/@mys/worker-fn)
[![npm-version](https://img.shields.io/npm/v/worker-fn?style=flat-square&color=rgb(203%2C56%2C55))](https://www.npmjs.com/package/worker-fn)
[![npm-minzip](https://img.shields.io/bundlephobia/minzip/worker-fn?style=flat-square&label=minzip)](https://bundlephobia.com/package/worker-fn)
[![npm-downloads](https://img.shields.io/npm/dy/worker-fn?&style=flat-square)](https://www.npmjs.com/package/worker-fn)
[![workflow-ci](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/ci.yml?label=ci&style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/ci.yml)
[![workflow-release](https://img.shields.io/github/actions/workflow/status/mys1024/worker-fn/release.yml?label=release&style=flat-square)](https://github.com/mys1024/worker-fn/actions/workflows/release.yml)

[English](./README.md) | 中文文档

</div>

`worker-fn` 隐藏了 JavaScript 主线程与 [Worker](https://developer.mozilla.org/docs/Web/API/Web_Workers_API) 线程之间的通信的复杂性,可以让你方便地调用定义在 Worker 中的函数。

你可以通过 `worker-fn` 在 JavaScript 主线程中创建函数签名与**工作函数**一致的**代理函数**(除了代理函数的返回值会被包裹在 Promise 中),代理函数会通过消息事件调用定义在 Worker 中的对应的工作函数。

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

## 用法

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

`math.worker.ts`:

Expand Down Expand Up @@ -50,14 +55,18 @@ const worker = new Worker(new URL("./math.worker.ts", import.meta.url), {
type: "module",
});

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

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

### 在 Node.js 中使用
### 在 Node.js 中配合 `node:worker_threads` 使用

<details>

<summary>例子</summary>

`math.worker.ts`:

Expand Down Expand Up @@ -89,17 +98,62 @@ 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);
const add = useWorkerFn<Add>("add", worker);
const fib = useWorkerFn<Fib>("fib", worker);

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

</details>

### 使用 `defineWorkerFns()``useWorkerFns()`

<details>

<summary>例子</summary>

`math.worker.ts`:

```typescript
import { defineWorkerFns } from "worker-fn";

const fns = {
add(a: number, b: number) {
return a + b;
},
fib(n: number): number {
return n <= 2 ? 1 : fns.fib(n - 1) + fns.fib(n - 2);
},
};

defineWorkerFns(fns);

export type Fns = typeof fns;
```

`math.ts`:

```typescript
import { useWorkerFns } from "worker-fn";
import type { Fns } from "./math.worker.ts";

const worker = new Worker(new URL("./math.worker.ts", import.meta.url), {
type: "module",
});

const { add, fib } = useWorkerFns<Fns>(worker);

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

</details>

## 从 JSR 导入

`worker-fn` 同时发布在 [npm](https://www.npmjs.com/package/worker-fn)[JSR](https://jsr.io/@mys/worker-fn)如果你想从 JSR 导入 `worker-fn`,请参考[这篇文档](https://jsr.io/docs/introduction#using-jsr-packages)
`worker-fn` 同时发布在 [npm](https://www.npmjs.com/package/worker-fn)[JSR](https://jsr.io/@mys/worker-fn)如果你想从 JSR 导入 `worker-fn`,请参考[这篇文档](https://jsr.io/docs/introduction#using-jsr-packages)

## License

MIT License &copy; 2024-PRESENT mys1024
[MIT](./LICENSE) License &copy; 2024-PRESENT [mys1024](https://github.com/mys1024)
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mys/worker-fn",
"version": "3.0.3",
"version": "3.0.4",
"tasks": {
"run": "deno run src/main.ts",
"run:watch": "deno run --watch src/main.ts",
Expand All @@ -22,7 +22,7 @@
"publish": {
"include": [
"src",
"docs/concept.png",
"docs/concepts.png",
"README*.md",
"LICENSE",
"deno.json"
Expand Down
File renamed without changes.
File renamed without changes

0 comments on commit a9c1c6e

Please sign in to comment.