@hile/message-worker-thread 1.0.6 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/SKILL.md +0 -179
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/message-worker-thread",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"vitest": "^4.0.18"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@hile/message-modem": "^
|
|
25
|
+
"@hile/message-modem": "^2.0.1"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "8e0fd1f78b5a8abd21218d1f596ada2533a0c8e7"
|
|
28
28
|
}
|
package/SKILL.md
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: message-worker-thread
|
|
3
|
-
description: Code generation and contribution rules for @hile/message-worker-thread. Use when editing this package or when the user asks about @hile/message-worker-thread patterns or API.
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# @hile/message-worker-thread
|
|
7
|
-
|
|
8
|
-
本文档是面向 AI 编码模型和人类开发者的 **代码生成规范**,阅读后应能正确地使用本库编写符合架构规则的代码。
|
|
9
|
-
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
## 1. 架构总览
|
|
13
|
-
|
|
14
|
-
`@hile/message-worker-thread` 是 `@hile/message-modem` 的 **Node.js Worker Threads 抽象实现**,用于主线程与 Worker 线程之间的请求/响应通信。
|
|
15
|
-
|
|
16
|
-
`MessageWorkerThread` 本身是 **抽象类**,实现了 `post`(通过 `postMessage` 发送),`exec` 方法留给子类实现。
|
|
17
|
-
|
|
18
|
-
继承链:
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
MessageModem (abstract) ← post + exec 均抽象
|
|
22
|
-
└── MessageWorkerThread (abstract) ← 实现 post,exec 仍抽象
|
|
23
|
-
└── 用户子类 ← 实现 exec
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
通道类型:
|
|
27
|
-
|
|
28
|
-
- 主线程端:传入 `Worker` 实例或 `MessagePort`
|
|
29
|
-
- Worker 线程端:不传参数,自动使用 `parentPort`
|
|
30
|
-
|
|
31
|
-
---
|
|
32
|
-
|
|
33
|
-
## 2. 类型签名
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
import { MessageModem, type MessageTransferFormat } from '@hile/message-modem';
|
|
37
|
-
import type { Worker, MessagePort } from 'node:worker_threads';
|
|
38
|
-
|
|
39
|
-
abstract class MessageWorkerThread extends MessageModem {
|
|
40
|
-
constructor(port?: Worker | MessagePort);
|
|
41
|
-
|
|
42
|
-
// 子类必须实现
|
|
43
|
-
protected abstract exec(data: any): Promise<any>;
|
|
44
|
-
|
|
45
|
-
// 发送双向请求(protected,子类自行暴露)
|
|
46
|
-
protected _send<T = any>(data: T, timeout?: number): {
|
|
47
|
-
abort: () => void;
|
|
48
|
-
response: <U = any>() => Promise<U>;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
// 发送单向推送(protected,子类自行暴露)
|
|
52
|
-
protected _push<T = any>(data: T, timeout?: number): {
|
|
53
|
-
abort: () => void;
|
|
54
|
-
response: <U = any>() => Promise<U>;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
// 移除消息监听
|
|
58
|
-
public dispose(): void;
|
|
59
|
-
}
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
|
|
64
|
-
## 3. 代码生成模板与规则
|
|
65
|
-
|
|
66
|
-
### 3.1 基本子类模板
|
|
67
|
-
|
|
68
|
-
```typescript
|
|
69
|
-
import { MessageWorkerThread } from '@hile/message-worker-thread';
|
|
70
|
-
|
|
71
|
-
class MyWorkerThread extends MessageWorkerThread {
|
|
72
|
-
protected async exec(data: any): Promise<any> {
|
|
73
|
-
switch (data?.action) {
|
|
74
|
-
case 'ping':
|
|
75
|
-
return 'pong';
|
|
76
|
-
default:
|
|
77
|
-
return data;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public request<T = any>(data: T, timeout?: number) {
|
|
82
|
-
return this._send(data, timeout);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### 3.2 主线程端模板
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
import { Worker } from 'node:worker_threads';
|
|
91
|
-
|
|
92
|
-
class MainThread extends MessageWorkerThread {
|
|
93
|
-
protected async exec(data: any): Promise<any> {
|
|
94
|
-
return { reply: 'from main', query: data };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public request<T = any>(data: T, timeout?: number) {
|
|
98
|
-
return this._send(data, timeout);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const worker = new Worker('./worker.js');
|
|
103
|
-
const wt = new MainThread(worker);
|
|
104
|
-
|
|
105
|
-
const result = await wt.request({ action: 'compute', value: 42 }).response();
|
|
106
|
-
console.log(result);
|
|
107
|
-
|
|
108
|
-
wt.dispose();
|
|
109
|
-
await worker.terminate();
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### 3.3 Worker 线程端模板
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
// worker.js
|
|
116
|
-
import { MessageWorkerThread } from '@hile/message-worker-thread';
|
|
117
|
-
import { Exception } from '@hile/message-modem';
|
|
118
|
-
|
|
119
|
-
class WorkerThread extends MessageWorkerThread {
|
|
120
|
-
protected async exec(data: any): Promise<any> {
|
|
121
|
-
if (data.action === 'compute') return data.value * 2;
|
|
122
|
-
if (data.action === 'restricted') throw new Exception(403, 'not allowed');
|
|
123
|
-
return data;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public request<T = any>(data: T, timeout?: number) {
|
|
127
|
-
return this._send(data, timeout);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const wt = new WorkerThread(); // 无参数 → 使用 parentPort
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### 3.4 MessageChannel 场景模板
|
|
135
|
-
|
|
136
|
-
```typescript
|
|
137
|
-
import { MessageChannel } from 'node:worker_threads';
|
|
138
|
-
|
|
139
|
-
const { port1, port2 } = new MessageChannel();
|
|
140
|
-
const side1 = new MySide1(port1);
|
|
141
|
-
const side2 = new MySide2(port2);
|
|
142
|
-
|
|
143
|
-
// 双向通信
|
|
144
|
-
const res = await side1.request('hello').response();
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### 3.5 强制规则
|
|
148
|
-
|
|
149
|
-
| 规则 | 说明 |
|
|
150
|
-
|------|------|
|
|
151
|
-
| **必须继承并实现 `exec`** | `MessageWorkerThread` 是抽象类 |
|
|
152
|
-
| **主线程端必须传入 `Worker` 或 `MessagePort`** | `new Worker()` 返回值 |
|
|
153
|
-
| **Worker 线程端不传参数** | 自动使用 `parentPort` |
|
|
154
|
-
| **用完必须 `dispose()`** | 避免内存泄漏 |
|
|
155
|
-
| **主线程还需 `worker.terminate()`** | 关闭 Worker 线程 |
|
|
156
|
-
|
|
157
|
-
### 3.6 反模式
|
|
158
|
-
|
|
159
|
-
```typescript
|
|
160
|
-
// ❌ 不能直接实例化
|
|
161
|
-
const wt = new MessageWorkerThread(worker); // abstract
|
|
162
|
-
|
|
163
|
-
// ✅ 继承并实现 exec,自行暴露 _send
|
|
164
|
-
class MyWT extends MessageWorkerThread {
|
|
165
|
-
protected async exec(data: any) { return data; }
|
|
166
|
-
public request<T = any>(data: T, timeout?: number) { return this._send(data, timeout); }
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// ❌ Worker 线程里传入 Worker 实例没有意义
|
|
170
|
-
const wt = new MyWT(someWorker); // Worker 线程里没有子 Worker
|
|
171
|
-
|
|
172
|
-
// ✅ Worker 线程不传参数
|
|
173
|
-
const wt = new MyWT();
|
|
174
|
-
|
|
175
|
-
// ❌ 忘记 dispose 和 terminate
|
|
176
|
-
// ✅ 清理
|
|
177
|
-
wt.dispose();
|
|
178
|
-
await worker.terminate();
|
|
179
|
-
```
|