@x-oasis/async-call-rpc-misc 0.1.38
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 +13 -0
- package/dist/async-call-rpc-misc.cjs.development.js +221 -0
- package/dist/async-call-rpc-misc.cjs.development.js.map +1 -0
- package/dist/async-call-rpc-misc.cjs.production.min.js +2 -0
- package/dist/async-call-rpc-misc.cjs.production.min.js.map +1 -0
- package/dist/async-call-rpc-misc.esm.js +216 -0
- package/dist/async-call-rpc-misc.esm.js.map +1 -0
- package/dist/buffer/DataBuffer.d.ts +2 -0
- package/dist/buffer/ReadBuffer.d.ts +12 -0
- package/dist/buffer/WriteBuffer.d.ts +3 -0
- package/dist/buffer/index.d.ts +3 -0
- package/dist/channel-protocol/AbstractChannelProtocol.d.ts +51 -0
- package/dist/channel-protocol/Channel.d.ts +16 -0
- package/dist/channel-protocol/DeferredMessageChannelProtocol.d.ts +8 -0
- package/dist/channel-protocol/IPCMainChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/IPCMainGlobalChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/IPCRendererChannelProtocol.d.ts +8 -0
- package/dist/channel-protocol/IPCRendererMessageChannelProtocol.d.ts +8 -0
- package/dist/channel-protocol/MessageChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/ProcessChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/ProcessClientChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/middlewares/buffer.d.ts +18 -0
- package/dist/channel-protocol/middlewares/handleDisconnectedRequest.d.ts +7 -0
- package/dist/channel-protocol/middlewares/handlePortRequest.d.ts +3 -0
- package/dist/channel-protocol/middlewares/handleRequest.d.ts +3 -0
- package/dist/channel-protocol/middlewares/handleRequestUtils.d.ts +10 -0
- package/dist/channel-protocol/middlewares/handleResponse.d.ts +3 -0
- package/dist/channel-protocol/middlewares/index.d.ts +10 -0
- package/dist/channel-protocol/middlewares/logger.d.ts +4 -0
- package/dist/channel-protocol/middlewares/normalize.d.ts +12 -0
- package/dist/channel-protocol/middlewares/prepareRequestData.d.ts +28 -0
- package/dist/channel-protocol/middlewares/sendRequest.d.ts +6 -0
- package/dist/channel-protocol/middlewares/updateSeqInfo.d.ts +6 -0
- package/dist/channel-protocol/middlewares/utils.d.ts +3 -0
- package/dist/common.d.ts +4 -0
- package/dist/error.d.ts +12 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +8 -0
- package/dist/rpc-service/ProxyRPCClient.d.ts +9 -0
- package/dist/rpc-service/RPCServiceHost.d.ts +11 -0
- package/dist/rpc-service/RPCServiceProvider.d.ts +9 -0
- package/dist/types/buffer.d.ts +4 -0
- package/dist/types/channel.d.ts +12 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/messageChannel.d.ts +76 -0
- package/dist/types/middleware.d.ts +44 -0
- package/dist/types/proxyChannel.d.ts +3 -0
- package/dist/types/proxyService.d.ts +10 -0
- package/dist/types/rpc.d.ts +31 -0
- package/dist/types/rpcProtocol.d.ts +22 -0
- package/package.json +38 -0
- package/src/buffer/DataBuffer.ts +1 -0
- package/src/buffer/ReadBuffer.ts +41 -0
- package/src/buffer/WriteBuffer.ts +5 -0
- package/src/buffer/index.ts +3 -0
- package/src/channel-protocol/AbstractChannelProtocol.ts +204 -0
- package/src/channel-protocol/Channel.ts +37 -0
- package/src/channel-protocol/DeferredMessageChannelProtocol.ts +85 -0
- package/src/channel-protocol/IPCMainChannelProtocol.ts +65 -0
- package/src/channel-protocol/IPCMainGlobalChannelProtocol.ts +66 -0
- package/src/channel-protocol/IPCRendererChannelProtocol.ts +81 -0
- package/src/channel-protocol/IPCRendererMessageChannelProtocol.ts +79 -0
- package/src/channel-protocol/MessageChannelProtocol.ts +63 -0
- package/src/channel-protocol/ProcessChannelProtocol.ts +60 -0
- package/src/channel-protocol/ProcessClientChannelProtocol.ts +59 -0
- package/src/channel-protocol/middlewares/buffer.ts +33 -0
- package/src/channel-protocol/middlewares/handleDisconnectedRequest.ts +30 -0
- package/src/channel-protocol/middlewares/handlePortRequest.ts +118 -0
- package/src/channel-protocol/middlewares/handleRequest.ts +163 -0
- package/src/channel-protocol/middlewares/handleRequestUtils.ts +42 -0
- package/src/channel-protocol/middlewares/handleResponse.ts +38 -0
- package/src/channel-protocol/middlewares/index.ts +11 -0
- package/src/channel-protocol/middlewares/logger.ts +22 -0
- package/src/channel-protocol/middlewares/normalize.ts +28 -0
- package/src/channel-protocol/middlewares/prepareRequestData.ts +137 -0
- package/src/channel-protocol/middlewares/sendRequest.ts +15 -0
- package/src/channel-protocol/middlewares/updateSeqInfo.ts +27 -0
- package/src/channel-protocol/middlewares/utils.ts +67 -0
- package/src/common.ts +20 -0
- package/src/error.ts +32 -0
- package/src/index.ts +5 -0
- package/src/rpc-service/ProxyRPCClient.ts +82 -0
- package/src/rpc-service/RPCServiceHost.ts +35 -0
- package/src/rpc-service/RPCServiceProvider.ts +22 -0
- package/src/types/buffer.ts +5 -0
- package/src/types/channel.ts +24 -0
- package/src/types/index.ts +6 -0
- package/src/types/messageChannel.ts +134 -0
- package/src/types/middleware.ts +53 -0
- package/src/types/proxyChannel.ts +3 -0
- package/src/types/proxyService.ts +16 -0
- package/src/types/rpc.ts +61 -0
- package/src/types/rpcProtocol.ts +24 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export enum DataType {
|
|
2
|
+
Undefined = 0,
|
|
3
|
+
String = 1,
|
|
4
|
+
Buffer = 2,
|
|
5
|
+
VSBuffer = 3,
|
|
6
|
+
Array = 4,
|
|
7
|
+
Object = 5,
|
|
8
|
+
Int = 6,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default class ReadBuffer {
|
|
12
|
+
decode(data: any) {
|
|
13
|
+
return JSON.parse(data);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// export class BufferReader implements IReader {
|
|
18
|
+
|
|
19
|
+
// private pos = 0;
|
|
20
|
+
|
|
21
|
+
// constructor(private buffer: VSBuffer) { }
|
|
22
|
+
|
|
23
|
+
// read(bytes: number): VSBuffer {
|
|
24
|
+
// const result = this.buffer.slice(this.pos, this.pos + bytes);
|
|
25
|
+
// this.pos += result.byteLength;
|
|
26
|
+
// return result;
|
|
27
|
+
// }
|
|
28
|
+
// }
|
|
29
|
+
|
|
30
|
+
// export class BufferWriter implements IWriter {
|
|
31
|
+
|
|
32
|
+
// private buffers: VSBuffer[] = [];
|
|
33
|
+
|
|
34
|
+
// get buffer(): VSBuffer {
|
|
35
|
+
// return VSBuffer.concat(this.buffers);
|
|
36
|
+
// }
|
|
37
|
+
|
|
38
|
+
// write(buffer: VSBuffer): void {
|
|
39
|
+
// this.buffers.push(buffer);
|
|
40
|
+
// }
|
|
41
|
+
// }
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { Disposable } from '@x-oasis/disposable';
|
|
2
|
+
import { Event } from '@x-oasis/emitter';
|
|
3
|
+
import { Deferred } from '@x-oasis/deferred';
|
|
4
|
+
import { generateRandomKey } from '@x-oasis/id';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
SendingProps,
|
|
8
|
+
IMessageChannel,
|
|
9
|
+
ClientMiddleware,
|
|
10
|
+
SenderMiddleware,
|
|
11
|
+
RequestRawSequenceId,
|
|
12
|
+
AbstractChannelProtocolProps,
|
|
13
|
+
PendingSendEntry,
|
|
14
|
+
} from '../types';
|
|
15
|
+
import { runMiddlewares } from './middlewares';
|
|
16
|
+
import RPCServiceHost from '../rpc-service/RPCServiceHost';
|
|
17
|
+
import { WriteBuffer, ReadBuffer } from '../buffer';
|
|
18
|
+
import { Channel } from './Channel';
|
|
19
|
+
import { resumeMiddlewares } from './middlewares/utils';
|
|
20
|
+
|
|
21
|
+
abstract class AbstractChannelProtocol
|
|
22
|
+
extends Disposable
|
|
23
|
+
implements IMessageChannel
|
|
24
|
+
{
|
|
25
|
+
private readonly _masterProcessName: string;
|
|
26
|
+
|
|
27
|
+
private _key: string;
|
|
28
|
+
|
|
29
|
+
public channel: Channel;
|
|
30
|
+
|
|
31
|
+
private _serviceHost: RPCServiceHost;
|
|
32
|
+
|
|
33
|
+
private readonly _description: string;
|
|
34
|
+
|
|
35
|
+
private _seqId: RequestRawSequenceId = -1;
|
|
36
|
+
|
|
37
|
+
// decoder should comes first !!!!
|
|
38
|
+
protected _onMessageMiddleware: ClientMiddleware[] = [];
|
|
39
|
+
|
|
40
|
+
private _senderMiddleware: SenderMiddleware[] = [];
|
|
41
|
+
|
|
42
|
+
private _readBuffer: ReadBuffer;
|
|
43
|
+
|
|
44
|
+
private _writeBuffer: WriteBuffer;
|
|
45
|
+
|
|
46
|
+
private _isConnected = true;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 如果说channel存在的话,那么就是ongoing request
|
|
50
|
+
*/
|
|
51
|
+
public ongoingRequests: Map<string, Deferred> = new Map();
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 如果说channel不存在,那么请求就会暂时放到这个里面
|
|
55
|
+
*/
|
|
56
|
+
public pendingSendEntries = new Set<PendingSendEntry>();
|
|
57
|
+
|
|
58
|
+
public requestEvents: Map<string, any> = new Map();
|
|
59
|
+
|
|
60
|
+
private onDidConnectedEvent = new Event({ name: 'on-did-connected' });
|
|
61
|
+
|
|
62
|
+
onDidConnected = this.onDidConnectedEvent.subscribe;
|
|
63
|
+
|
|
64
|
+
private onDidDisconnectedEvent = new Event({ name: 'on-did-disconnect' });
|
|
65
|
+
|
|
66
|
+
onDidDisconnected = this.onDidDisconnectedEvent.subscribe;
|
|
67
|
+
|
|
68
|
+
constructor(props: AbstractChannelProtocolProps) {
|
|
69
|
+
super();
|
|
70
|
+
const {
|
|
71
|
+
description,
|
|
72
|
+
masterProcessName,
|
|
73
|
+
serviceHost,
|
|
74
|
+
connected = true,
|
|
75
|
+
} = props;
|
|
76
|
+
this._readBuffer = new ReadBuffer();
|
|
77
|
+
this._writeBuffer = new WriteBuffer();
|
|
78
|
+
this._description = description;
|
|
79
|
+
this._serviceHost = serviceHost;
|
|
80
|
+
this._isConnected = connected;
|
|
81
|
+
this._masterProcessName = masterProcessName;
|
|
82
|
+
// 需要创建,否则创建'message'监听时,会出现混乱
|
|
83
|
+
this._key = generateRandomKey();
|
|
84
|
+
this.registerDisposable(this.onDidConnected(this.didConnected.bind(this)));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get serviceHost() {
|
|
88
|
+
return this._serviceHost;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
setServiceHost(serviceHost: RPCServiceHost) {
|
|
92
|
+
this._serviceHost = serviceHost;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get senderMiddleware() {
|
|
96
|
+
return this._senderMiddleware;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get readBuffer() {
|
|
100
|
+
return this._readBuffer;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get writeBuffer() {
|
|
104
|
+
return this._writeBuffer;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// start from 1
|
|
108
|
+
get seqId() {
|
|
109
|
+
this._seqId += 1;
|
|
110
|
+
return `${this._key}_${this._seqId}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get description() {
|
|
114
|
+
return this._description;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get masterProcessName() {
|
|
118
|
+
return this._masterProcessName;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
addPendingSendEntry(entry: PendingSendEntry) {
|
|
122
|
+
this.pendingSendEntries.add(entry);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
applyOnMessageMiddleware(fns: Function | Function[]) {
|
|
126
|
+
[].concat(fns).forEach((fn) => {
|
|
127
|
+
if (typeof fn === 'function') {
|
|
128
|
+
this._onMessageMiddleware.push(fn(this));
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
applySendMiddleware(fns: Function | Function[]) {
|
|
134
|
+
[].concat(fns).forEach((fn) => {
|
|
135
|
+
if (typeof fn === 'function') {
|
|
136
|
+
this._senderMiddleware.push(fn(this));
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
isConnected() {
|
|
142
|
+
return this._isConnected;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
resumePendingEntry() {
|
|
146
|
+
this.pendingSendEntries.forEach((entry) => {
|
|
147
|
+
this.pendingSendEntries.delete(entry);
|
|
148
|
+
resumeMiddlewares(this.senderMiddleware, entry);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
didConnected() {
|
|
153
|
+
this.resumePendingEntry();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
protected bindChannel(channel: Channel) {
|
|
157
|
+
this.channel = channel;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
connect(channel: Channel) {
|
|
161
|
+
this.channel = channel;
|
|
162
|
+
this.onDidConnectedEvent.fire();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 已经绑定了channel,这个时候需要直接触发
|
|
166
|
+
activate() {
|
|
167
|
+
this._isConnected = true;
|
|
168
|
+
this.onDidConnectedEvent.fire();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
disconnect() {
|
|
172
|
+
this.channel = null;
|
|
173
|
+
this._isConnected = false;
|
|
174
|
+
this.onDidDisconnectedEvent.fire();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
send(props: SendingProps, transfer?: MessagePort[]): Deferred | void;
|
|
178
|
+
|
|
179
|
+
send(requestPath: string, fnName: string, ...args: any[]): Deferred | void;
|
|
180
|
+
|
|
181
|
+
send(...args: any[]) {
|
|
182
|
+
const { returnValue } = runMiddlewares(this.senderMiddleware, args);
|
|
183
|
+
if (returnValue) return returnValue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
sendReply(...args: any[]) {
|
|
187
|
+
// TODO: this.channel may be null when disconnect
|
|
188
|
+
this.channel?.send(...args);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
onMessage(...args: any[]) {
|
|
192
|
+
runMiddlewares(this._onMessageMiddleware, args);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
runWithMiddlewares(middlewares: ClientMiddleware[], ...args: any[]) {
|
|
196
|
+
runMiddlewares(
|
|
197
|
+
// @ts-ignore
|
|
198
|
+
middlewares.map((m) => m(this)),
|
|
199
|
+
args
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export default AbstractChannelProtocol;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Emitter } from '@x-oasis/emitter';
|
|
2
|
+
import { Disposable, toDisposable } from '@x-oasis/disposable';
|
|
3
|
+
|
|
4
|
+
type ChannelSend = (...args: any[]) => void;
|
|
5
|
+
type ChannelInitListener = (emitter: Emitter) => void;
|
|
6
|
+
|
|
7
|
+
type ChannelProps = {
|
|
8
|
+
send: ChannelSend;
|
|
9
|
+
initListener: ChannelInitListener;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export class Channel extends Disposable {
|
|
13
|
+
private _send: (...args: any[]) => void;
|
|
14
|
+
|
|
15
|
+
emitter = new Emitter({ name: 'channel' });
|
|
16
|
+
|
|
17
|
+
constructor(props: ChannelProps) {
|
|
18
|
+
super();
|
|
19
|
+
const { send, initListener } = props;
|
|
20
|
+
this._send = send;
|
|
21
|
+
initListener(this.emitter);
|
|
22
|
+
|
|
23
|
+
this.registerDisposable(
|
|
24
|
+
toDisposable(() => {
|
|
25
|
+
this.emitter.dispose();
|
|
26
|
+
})
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
send(...args: any[]) {
|
|
31
|
+
this._send(...args);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
on(eventName: string, listener: Function) {
|
|
35
|
+
this.emitter.getEvent(eventName).subscribe(listener);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import AbstractChannelProtocol from './AbstractChannelProtocol';
|
|
2
|
+
import { MainPort, DeferredMessageChannelProtocolProps } from '../types';
|
|
3
|
+
import {
|
|
4
|
+
serialize,
|
|
5
|
+
deserialize,
|
|
6
|
+
handleRequest,
|
|
7
|
+
handleResponse,
|
|
8
|
+
updateSeqInfo,
|
|
9
|
+
sendRequest,
|
|
10
|
+
prepareNormalData,
|
|
11
|
+
handleDisconnectedRequest,
|
|
12
|
+
normalizeMessageChannelRawMessage,
|
|
13
|
+
} from './middlewares';
|
|
14
|
+
import { Channel } from './Channel';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Basically it is the same with `MessageChannelProtocol`, but for this function
|
|
18
|
+
* `port` is not required on initlization.
|
|
19
|
+
*/
|
|
20
|
+
export default class DeferredMessageChannelProtocol extends AbstractChannelProtocol {
|
|
21
|
+
private port: MainPort;
|
|
22
|
+
|
|
23
|
+
constructor(props: DeferredMessageChannelProtocolProps) {
|
|
24
|
+
super({
|
|
25
|
+
connected: false,
|
|
26
|
+
...props,
|
|
27
|
+
});
|
|
28
|
+
const { clientMiddlewares, senderMiddlewares, port } = props;
|
|
29
|
+
|
|
30
|
+
this.applySendMiddleware(
|
|
31
|
+
[].concat(
|
|
32
|
+
prepareNormalData,
|
|
33
|
+
updateSeqInfo,
|
|
34
|
+
handleDisconnectedRequest,
|
|
35
|
+
serialize,
|
|
36
|
+
senderMiddlewares,
|
|
37
|
+
sendRequest
|
|
38
|
+
)
|
|
39
|
+
);
|
|
40
|
+
this.applyOnMessageMiddleware(
|
|
41
|
+
[].concat(
|
|
42
|
+
normalizeMessageChannelRawMessage,
|
|
43
|
+
deserialize,
|
|
44
|
+
clientMiddlewares,
|
|
45
|
+
handleRequest,
|
|
46
|
+
handleResponse
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
if (port) this.bindPort(port);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
bindPort(port: MainPort) {
|
|
54
|
+
if (!port) return;
|
|
55
|
+
if (this.port) {
|
|
56
|
+
if (this.port === port) return;
|
|
57
|
+
this.port.close();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.port = port;
|
|
61
|
+
|
|
62
|
+
const channel = new Channel({
|
|
63
|
+
send: (...args) => {
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
this.port.postMessage(...args);
|
|
66
|
+
},
|
|
67
|
+
initListener: (emitter) => {
|
|
68
|
+
emitter.register('message');
|
|
69
|
+
this.port.on('message', (...args: any[]) => {
|
|
70
|
+
this.onMessage(...args);
|
|
71
|
+
emitter.getEvent('message').fire(...args);
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
this.port.start();
|
|
76
|
+
|
|
77
|
+
this.bindChannel(channel);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
disconnect() {
|
|
81
|
+
this.dispose();
|
|
82
|
+
this.port.close();
|
|
83
|
+
super.disconnect();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { ipcMain } from 'electron';
|
|
2
|
+
import { IPCMainChannelProtocolProps } from '../types';
|
|
3
|
+
import {
|
|
4
|
+
serialize,
|
|
5
|
+
deserialize,
|
|
6
|
+
handleRequest,
|
|
7
|
+
handleResponse,
|
|
8
|
+
preparePortData,
|
|
9
|
+
updateSeqInfo,
|
|
10
|
+
sendRequest,
|
|
11
|
+
handleDisconnectedRequest,
|
|
12
|
+
normalizeIPCChannelRawMessage,
|
|
13
|
+
} from './middlewares';
|
|
14
|
+
import AbstractChannelProtocol from './AbstractChannelProtocol';
|
|
15
|
+
import { Channel } from './Channel';
|
|
16
|
+
|
|
17
|
+
export default class IPCMainChannelProtocol extends AbstractChannelProtocol {
|
|
18
|
+
private channelName: string;
|
|
19
|
+
|
|
20
|
+
constructor(props: IPCMainChannelProtocolProps) {
|
|
21
|
+
super(props);
|
|
22
|
+
const {
|
|
23
|
+
channelName,
|
|
24
|
+
webContents,
|
|
25
|
+
clientMiddlewares = [],
|
|
26
|
+
senderMiddlewares = [],
|
|
27
|
+
} = props;
|
|
28
|
+
this.channelName = channelName;
|
|
29
|
+
this.channel = new Channel({
|
|
30
|
+
send: (...args: any[]) =>
|
|
31
|
+
webContents.postMessage(this.channelName, ...args),
|
|
32
|
+
initListener: (emitter) => {
|
|
33
|
+
emitter.register(this.channelName);
|
|
34
|
+
ipcMain.on(this.channelName, (...args: any[]) => {
|
|
35
|
+
this.onMessage(...args);
|
|
36
|
+
emitter.getEvent(this.channelName).fire(...args);
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
this.applyOnMessageMiddleware(
|
|
41
|
+
[].concat(
|
|
42
|
+
normalizeIPCChannelRawMessage,
|
|
43
|
+
deserialize,
|
|
44
|
+
clientMiddlewares,
|
|
45
|
+
handleRequest,
|
|
46
|
+
handleResponse
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
this.applySendMiddleware(
|
|
50
|
+
[].concat(
|
|
51
|
+
preparePortData,
|
|
52
|
+
updateSeqInfo,
|
|
53
|
+
handleDisconnectedRequest,
|
|
54
|
+
serialize,
|
|
55
|
+
senderMiddlewares,
|
|
56
|
+
sendRequest
|
|
57
|
+
)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
disconnect() {
|
|
62
|
+
this.dispose();
|
|
63
|
+
super.disconnect();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ipcMain } from 'electron';
|
|
2
|
+
import { IPCMainGlobalChannelProtocolProps } from '../types';
|
|
3
|
+
import {
|
|
4
|
+
serialize,
|
|
5
|
+
deserialize,
|
|
6
|
+
preparePortData,
|
|
7
|
+
updateSeqInfo,
|
|
8
|
+
handlePortRequest,
|
|
9
|
+
sendRequest,
|
|
10
|
+
handleDisconnectedRequest,
|
|
11
|
+
normalizeIPCChannelRawMessage,
|
|
12
|
+
} from './middlewares';
|
|
13
|
+
import AbstractChannelProtocol from './AbstractChannelProtocol';
|
|
14
|
+
import { Channel } from './Channel';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* mainly send through sender to renderer
|
|
18
|
+
*/
|
|
19
|
+
export default class IPCMainGlobalChannelProtocol extends AbstractChannelProtocol {
|
|
20
|
+
private channelName: string;
|
|
21
|
+
|
|
22
|
+
constructor(props: IPCMainGlobalChannelProtocolProps) {
|
|
23
|
+
super(props);
|
|
24
|
+
const {
|
|
25
|
+
clientMiddlewares = [],
|
|
26
|
+
senderMiddlewares = [],
|
|
27
|
+
channelName,
|
|
28
|
+
} = props;
|
|
29
|
+
this.channelName = channelName;
|
|
30
|
+
this.channel = new Channel({
|
|
31
|
+
send: () => {
|
|
32
|
+
// do nothing...
|
|
33
|
+
},
|
|
34
|
+
initListener: (emitter) => {
|
|
35
|
+
emitter.register(this.channelName);
|
|
36
|
+
ipcMain.on(this.channelName, (...args: any[]) => {
|
|
37
|
+
this.onMessage(...args);
|
|
38
|
+
emitter.getEvent(this.channelName).fire(...args);
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
this.applyOnMessageMiddleware(
|
|
43
|
+
[].concat(
|
|
44
|
+
normalizeIPCChannelRawMessage,
|
|
45
|
+
deserialize,
|
|
46
|
+
clientMiddlewares,
|
|
47
|
+
handlePortRequest
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
this.applySendMiddleware(
|
|
51
|
+
[].concat(
|
|
52
|
+
preparePortData,
|
|
53
|
+
updateSeqInfo,
|
|
54
|
+
handleDisconnectedRequest,
|
|
55
|
+
serialize,
|
|
56
|
+
senderMiddlewares,
|
|
57
|
+
sendRequest
|
|
58
|
+
)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
disconnect() {
|
|
63
|
+
this.dispose();
|
|
64
|
+
super.disconnect();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { IPCRendererChannelProtocolProps } from '../types';
|
|
2
|
+
import {
|
|
3
|
+
serialize,
|
|
4
|
+
deserialize,
|
|
5
|
+
handleRequest,
|
|
6
|
+
handleResponse,
|
|
7
|
+
updateSeqInfo,
|
|
8
|
+
prepareHostPortData,
|
|
9
|
+
sendRequest,
|
|
10
|
+
handleDisconnectedRequest,
|
|
11
|
+
normalizeIPCChannelRawMessage,
|
|
12
|
+
} from './middlewares';
|
|
13
|
+
import AbstractChannelProtocol from './AbstractChannelProtocol';
|
|
14
|
+
import { Channel } from './Channel';
|
|
15
|
+
import { fromDomEvent } from '@x-oasis/emitter';
|
|
16
|
+
|
|
17
|
+
class IPCRendererChannelProtocol extends AbstractChannelProtocol {
|
|
18
|
+
private channelName: string;
|
|
19
|
+
|
|
20
|
+
constructor(props: IPCRendererChannelProtocolProps) {
|
|
21
|
+
super(props);
|
|
22
|
+
this.projectName = props.projectName;
|
|
23
|
+
const {
|
|
24
|
+
channelName,
|
|
25
|
+
clientMiddlewares = [],
|
|
26
|
+
senderMiddlewares = [],
|
|
27
|
+
ipcRenderer,
|
|
28
|
+
} = props;
|
|
29
|
+
this.channelName = channelName;
|
|
30
|
+
this.applyOnMessageMiddleware(
|
|
31
|
+
[].concat(
|
|
32
|
+
normalizeIPCChannelRawMessage,
|
|
33
|
+
deserialize,
|
|
34
|
+
clientMiddlewares,
|
|
35
|
+
handleRequest,
|
|
36
|
+
handleResponse
|
|
37
|
+
)
|
|
38
|
+
);
|
|
39
|
+
this.applySendMiddleware(
|
|
40
|
+
[].concat(
|
|
41
|
+
prepareHostPortData,
|
|
42
|
+
updateSeqInfo,
|
|
43
|
+
handleDisconnectedRequest,
|
|
44
|
+
serialize,
|
|
45
|
+
senderMiddlewares,
|
|
46
|
+
sendRequest
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
this.channel = new Channel({
|
|
51
|
+
send: (...args) => ipcRenderer.send(this.channelName, ...args),
|
|
52
|
+
initListener: (emitter) => {
|
|
53
|
+
emitter.register(this.channelName);
|
|
54
|
+
ipcRenderer.on(this.channelName, (...args: any[]) => {
|
|
55
|
+
this.onMessage(...args);
|
|
56
|
+
emitter.getEvent(this.channelName).fire(...args);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
fromDomEvent(
|
|
60
|
+
window,
|
|
61
|
+
'message'
|
|
62
|
+
)((event: MessageEvent) => {
|
|
63
|
+
if (event.data?.channel === this.channelName) {
|
|
64
|
+
this.runWithMiddlewares([deserialize, handleResponse], {
|
|
65
|
+
event,
|
|
66
|
+
ports: event.ports,
|
|
67
|
+
data: event.data?.data,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
disconnect() {
|
|
76
|
+
this.dispose();
|
|
77
|
+
super.disconnect();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export default IPCRendererChannelProtocol;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import AbstractChannelProtocol from './AbstractChannelProtocol';
|
|
2
|
+
import { MainPort, MessageChannelProtocolProps } from '../types';
|
|
3
|
+
import {
|
|
4
|
+
serialize,
|
|
5
|
+
deserialize,
|
|
6
|
+
handleRequest,
|
|
7
|
+
handleResponse,
|
|
8
|
+
updateSeqInfo,
|
|
9
|
+
sendRequest,
|
|
10
|
+
prepareNormalData,
|
|
11
|
+
handleDisconnectedRequest,
|
|
12
|
+
normalizeMessageChannelRawMessage,
|
|
13
|
+
} from './middlewares';
|
|
14
|
+
import { Channel } from './Channel';
|
|
15
|
+
|
|
16
|
+
export default class IPCRendererMessageChannelProtocol extends AbstractChannelProtocol {
|
|
17
|
+
private readonly port: MainPort;
|
|
18
|
+
|
|
19
|
+
constructor(props: MessageChannelProtocolProps) {
|
|
20
|
+
super({
|
|
21
|
+
connected: false,
|
|
22
|
+
...props,
|
|
23
|
+
});
|
|
24
|
+
const { clientMiddlewares, senderMiddlewares, port } = props;
|
|
25
|
+
|
|
26
|
+
this.applySendMiddleware(
|
|
27
|
+
[].concat(
|
|
28
|
+
prepareNormalData,
|
|
29
|
+
updateSeqInfo,
|
|
30
|
+
handleDisconnectedRequest,
|
|
31
|
+
serialize,
|
|
32
|
+
senderMiddlewares,
|
|
33
|
+
sendRequest
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
this.applyOnMessageMiddleware(
|
|
37
|
+
[].concat(
|
|
38
|
+
normalizeMessageChannelRawMessage,
|
|
39
|
+
deserialize,
|
|
40
|
+
clientMiddlewares,
|
|
41
|
+
handleRequest,
|
|
42
|
+
handleResponse
|
|
43
|
+
)
|
|
44
|
+
);
|
|
45
|
+
if (port) this.bindPort(port);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
bindPort(port: MainPort) {
|
|
49
|
+
if (!port) return;
|
|
50
|
+
if (this.port) {
|
|
51
|
+
if (this.port === port) return;
|
|
52
|
+
this.port.close();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.port = port;
|
|
56
|
+
|
|
57
|
+
const channel = new Channel({
|
|
58
|
+
send: (...args) => {
|
|
59
|
+
this.port.postMessage(...args);
|
|
60
|
+
},
|
|
61
|
+
initListener: (emitter) => {
|
|
62
|
+
emitter.register('onmessage');
|
|
63
|
+
this.port.onmessage = (...args: any[]) => {
|
|
64
|
+
this.onMessage(...args);
|
|
65
|
+
emitter.getEvent('onmessage').fire(...args);
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
this.port.start();
|
|
70
|
+
|
|
71
|
+
this.bindChannel(channel);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
disconnect() {
|
|
75
|
+
this.dispose();
|
|
76
|
+
this.port.close();
|
|
77
|
+
super.disconnect();
|
|
78
|
+
}
|
|
79
|
+
}
|