@hile/message-ipc 1.0.1 → 1.0.3
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 +11 -2
- package/SKILL.md +25 -4
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -39,6 +39,10 @@ class WorkerIpc extends MessageIpc {
|
|
|
39
39
|
return data; // echo
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
+
|
|
43
|
+
public request<T = any>(data: T, timeout?: number) {
|
|
44
|
+
return this._send(data, timeout);
|
|
45
|
+
}
|
|
42
46
|
}
|
|
43
47
|
```
|
|
44
48
|
|
|
@@ -51,6 +55,10 @@ class ParentIpc extends MessageIpc {
|
|
|
51
55
|
protected async exec(data: any): Promise<any> {
|
|
52
56
|
return { reply: 'from parent', query: data };
|
|
53
57
|
}
|
|
58
|
+
|
|
59
|
+
public request<T = any>(data: T, timeout?: number) {
|
|
60
|
+
return this._send(data, timeout);
|
|
61
|
+
}
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
const child = fork('./worker.js');
|
|
@@ -77,10 +85,11 @@ const ipc = new WorkerIpc(); // 无参数 → 自动使用 process
|
|
|
77
85
|
|------|------|------|
|
|
78
86
|
| `constructor` | `new SubClass(channel?: ChildProcess)` | 父进程传 `fork()` 返回值;子进程不传参数 |
|
|
79
87
|
| `exec` | `protected abstract exec(data: any): Promise<any>` | 子类实现:处理对端请求的业务逻辑 |
|
|
80
|
-
| `
|
|
88
|
+
| `_send` | `protected _send<T>(data: T, timeout?: number)` | 发送双向请求(`twoway: true`),返回 `{ abort, response }`。子类自行暴露为 public |
|
|
89
|
+
| `_push` | `protected _push<T>(data: T, timeout?: number)` | 发送单向推送(`twoway: false`),接收方不回复 RESPONSE |
|
|
81
90
|
| `dispose` | `dispose(): void` | 移除消息监听,释放资源 |
|
|
82
91
|
|
|
83
|
-
### `
|
|
92
|
+
### `_send` 返回值
|
|
84
93
|
|
|
85
94
|
| 属性 | 类型 | 说明 |
|
|
86
95
|
|------|------|------|
|
package/SKILL.md
CHANGED
|
@@ -39,8 +39,14 @@ abstract class MessageIpc extends MessageModem {
|
|
|
39
39
|
// 子类必须实现
|
|
40
40
|
protected abstract exec(data: any): Promise<any>;
|
|
41
41
|
|
|
42
|
-
//
|
|
43
|
-
|
|
42
|
+
// 发送双向请求(protected,子类自行暴露)
|
|
43
|
+
protected _send<T = any>(data: T, timeout?: number): {
|
|
44
|
+
abort: () => void;
|
|
45
|
+
response: <U = any>() => Promise<U>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// 发送单向推送(protected,子类自行暴露)
|
|
49
|
+
protected _push<T = any>(data: T, timeout?: number): {
|
|
44
50
|
abort: () => void;
|
|
45
51
|
response: <U = any>() => Promise<U>;
|
|
46
52
|
};
|
|
@@ -69,6 +75,11 @@ class MyIpc extends MessageIpc {
|
|
|
69
75
|
return data;
|
|
70
76
|
}
|
|
71
77
|
}
|
|
78
|
+
|
|
79
|
+
// 自行暴露发送方法
|
|
80
|
+
public request<T = any>(data: T, timeout?: number) {
|
|
81
|
+
return this._send(data, timeout);
|
|
82
|
+
}
|
|
72
83
|
}
|
|
73
84
|
```
|
|
74
85
|
|
|
@@ -82,6 +93,10 @@ class ParentIpc extends MessageIpc {
|
|
|
82
93
|
// 处理子进程发来的请求
|
|
83
94
|
return { reply: 'from parent', query: data };
|
|
84
95
|
}
|
|
96
|
+
|
|
97
|
+
public request<T = any>(data: T, timeout?: number) {
|
|
98
|
+
return this._send(data, timeout);
|
|
99
|
+
}
|
|
85
100
|
}
|
|
86
101
|
|
|
87
102
|
const child = fork('./worker.js');
|
|
@@ -107,6 +122,10 @@ class WorkerIpc extends MessageIpc {
|
|
|
107
122
|
if (data.action === 'restricted') throw new Exception(403, 'not allowed');
|
|
108
123
|
return data;
|
|
109
124
|
}
|
|
125
|
+
|
|
126
|
+
public request<T = any>(data: T, timeout?: number) {
|
|
127
|
+
return this._send(data, timeout);
|
|
128
|
+
}
|
|
110
129
|
}
|
|
111
130
|
|
|
112
131
|
const ipc = new WorkerIpc(); // 无参数 → 使用 process
|
|
@@ -120,7 +139,8 @@ const ipc = new WorkerIpc(); // 无参数 → 使用 process
|
|
|
120
139
|
| **父进程端必须传入 `ChildProcess`** | `fork()` 返回值 |
|
|
121
140
|
| **子进程端不传参数** | 自动使用 `process`,要求进程通过 `fork()` 启动 |
|
|
122
141
|
| **用完必须 `dispose()`** | 避免内存泄漏 |
|
|
123
|
-
| **`
|
|
142
|
+
| **`_send` 是 `protected`** | 子类需自行暴露为 public 方法(如 `request`) |
|
|
143
|
+
| **`_push` 是 `protected`** | 单向推送,接收方不回复 RESPONSE。子类自行暴露 |
|
|
124
144
|
|
|
125
145
|
### 3.5 反模式
|
|
126
146
|
|
|
@@ -128,9 +148,10 @@ const ipc = new WorkerIpc(); // 无参数 → 使用 process
|
|
|
128
148
|
// ❌ 不能直接实例化 MessageIpc
|
|
129
149
|
const ipc = new MessageIpc(child); // TypeError: Cannot construct abstract class
|
|
130
150
|
|
|
131
|
-
// ✅ 继承并实现 exec
|
|
151
|
+
// ✅ 继承并实现 exec,自行暴露 _send
|
|
132
152
|
class MyIpc extends MessageIpc {
|
|
133
153
|
protected async exec(data: any) { return data; }
|
|
154
|
+
public request<T = any>(data: T, timeout?: number) { return this._send(data, timeout); }
|
|
134
155
|
}
|
|
135
156
|
const ipc = new MyIpc(child);
|
|
136
157
|
|
package/dist/index.d.ts
CHANGED
|
@@ -26,13 +26,6 @@ export declare abstract class MessageIpc extends MessageModem {
|
|
|
26
26
|
private readonly listener;
|
|
27
27
|
constructor(channel?: ChildProcess);
|
|
28
28
|
protected post<T = any>(data: MessageTransferFormat<T>): void;
|
|
29
|
-
/**
|
|
30
|
-
* 向对端发送请求
|
|
31
|
-
*/
|
|
32
|
-
request<T = any>(data: T, timeout?: number): {
|
|
33
|
-
abort: () => void;
|
|
34
|
-
response: <U = any>() => Promise<U>;
|
|
35
|
-
};
|
|
36
29
|
/**
|
|
37
30
|
* 移除消息监听,释放资源
|
|
38
31
|
*/
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/message-ipc",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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.
|
|
25
|
+
"@hile/message-modem": "^1.0.3"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "0e351166b8351d21e1a97543bc8583ef9513fb67"
|
|
28
28
|
}
|