@hile/message-worker-thread 1.0.0 → 1.0.2

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/README.md CHANGED
@@ -37,6 +37,10 @@ class ComputeWorker extends MessageWorkerThread {
37
37
  return data;
38
38
  }
39
39
  }
40
+
41
+ public request<T = any>(data: T, timeout?: number) {
42
+ return this._send(data, timeout);
43
+ }
40
44
  }
41
45
  ```
42
46
 
@@ -49,6 +53,10 @@ class MainThread extends MessageWorkerThread {
49
53
  protected async exec(data: any): Promise<any> {
50
54
  return { reply: 'from main', query: data };
51
55
  }
56
+
57
+ public request<T = any>(data: T, timeout?: number) {
58
+ return this._send(data, timeout);
59
+ }
52
60
  }
53
61
 
54
62
  const worker = new Worker('./worker.js');
@@ -75,10 +83,11 @@ const wt = new ComputeWorker(); // 无参数 → 自动使用 parentPort
75
83
  |------|------|------|
76
84
  | `constructor` | `new SubClass(port?: Worker \| MessagePort)` | 主线程传 Worker/MessagePort;Worker 线程不传参数 |
77
85
  | `exec` | `protected abstract exec(data: any): Promise<any>` | 子类实现:处理对端请求 |
78
- | `request` | `request<T>(data: T, timeout?: number)` | 向对端发送请求,返回 `{ abort, response }` |
86
+ | `_send` | `protected _send<T>(data: T, timeout?: number)` | 发送双向请求(`twoway: true`),返回 `{ abort, response }`。子类自行暴露为 public |
87
+ | `_push` | `protected _push<T>(data: T, timeout?: number)` | 发送单向推送(`twoway: false`),接收方不回复 RESPONSE |
79
88
  | `dispose` | `dispose(): void` | 移除消息监听,释放资源 |
80
89
 
81
- ### `request` 返回值
90
+ ### `_send` 返回值
82
91
 
83
92
  | 属性 | 类型 | 说明 |
84
93
  |------|------|------|
package/SKILL.md CHANGED
@@ -42,8 +42,14 @@ abstract class MessageWorkerThread extends MessageModem {
42
42
  // 子类必须实现
43
43
  protected abstract exec(data: any): Promise<any>;
44
44
 
45
- // 向对端发送请求
46
- public request<T = any>(data: T, timeout?: number): {
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): {
47
53
  abort: () => void;
48
54
  response: <U = any>() => Promise<U>;
49
55
  };
@@ -71,6 +77,10 @@ class MyWorkerThread extends MessageWorkerThread {
71
77
  return data;
72
78
  }
73
79
  }
80
+
81
+ public request<T = any>(data: T, timeout?: number) {
82
+ return this._send(data, timeout);
83
+ }
74
84
  }
75
85
  ```
76
86
 
@@ -83,6 +93,10 @@ class MainThread extends MessageWorkerThread {
83
93
  protected async exec(data: any): Promise<any> {
84
94
  return { reply: 'from main', query: data };
85
95
  }
96
+
97
+ public request<T = any>(data: T, timeout?: number) {
98
+ return this._send(data, timeout);
99
+ }
86
100
  }
87
101
 
88
102
  const worker = new Worker('./worker.js');
@@ -108,6 +122,10 @@ class WorkerThread extends MessageWorkerThread {
108
122
  if (data.action === 'restricted') throw new Exception(403, 'not allowed');
109
123
  return data;
110
124
  }
125
+
126
+ public request<T = any>(data: T, timeout?: number) {
127
+ return this._send(data, timeout);
128
+ }
111
129
  }
112
130
 
113
131
  const wt = new WorkerThread(); // 无参数 → 使用 parentPort
@@ -142,9 +160,10 @@ const res = await side1.request('hello').response();
142
160
  // ❌ 不能直接实例化
143
161
  const wt = new MessageWorkerThread(worker); // abstract
144
162
 
145
- // ✅ 继承并实现 exec
163
+ // ✅ 继承并实现 exec,自行暴露 _send
146
164
  class MyWT extends MessageWorkerThread {
147
165
  protected async exec(data: any) { return data; }
166
+ public request<T = any>(data: T, timeout?: number) { return this._send(data, timeout); }
148
167
  }
149
168
 
150
169
  // ❌ Worker 线程里传入 Worker 实例没有意义
package/dist/index.d.ts CHANGED
@@ -29,13 +29,6 @@ export declare abstract class MessageWorkerThread extends MessageModem {
29
29
  private readonly listener;
30
30
  constructor(port?: Worker | MessagePort);
31
31
  protected post<T = any>(data: MessageTransferFormat<T>): void;
32
- /**
33
- * 向对端发送请求
34
- */
35
- request<T = any>(data: T, timeout?: number): {
36
- abort: () => void;
37
- response: <U = any>() => Promise<U>;
38
- };
39
32
  /**
40
33
  * 移除消息监听,释放资源
41
34
  */
package/dist/index.js CHANGED
@@ -44,12 +44,6 @@ export class MessageWorkerThread extends MessageModem {
44
44
  post(data) {
45
45
  this.port.postMessage(data);
46
46
  }
47
- /**
48
- * 向对端发送请求
49
- */
50
- request(data, timeout) {
51
- return this.send(data, timeout);
52
- }
53
47
  /**
54
48
  * 移除消息监听,释放资源
55
49
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hile/message-worker-thread",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
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": "^1.0.1"
25
+ "@hile/message-modem": "^1.0.3"
26
26
  },
27
- "gitHead": "426933bf98ed09d0e52b6cb63792cd3b2cf3fee1"
27
+ "gitHead": "0e351166b8351d21e1a97543bc8583ef9513fb67"
28
28
  }