提交 44895196 编写于 作者: A Alex Dima

Handle RPC incoming messages in a single tick

上级 1893aaa1
......@@ -122,42 +122,23 @@ export class RPCProtocol {
class RPCMultiplexer {
private readonly _protocol: IMessagePassingProtocol;
private readonly _onMessage: (msg: string) => void;
private readonly _receiveOneMessageBound: () => void;
private readonly _sendAccumulatedBound: () => void;
private _messagesToSend: string[];
private _messagesToReceive: string[];
constructor(protocol: IMessagePassingProtocol, onMessage: (msg: string) => void) {
this._protocol = protocol;
this._onMessage = onMessage;
this._receiveOneMessageBound = this._receiveOneMessage.bind(this);
this._sendAccumulatedBound = this._sendAccumulated.bind(this);
this._messagesToSend = [];
this._messagesToReceive = [];
this._protocol.onMessage(data => {
// console.log('RECEIVED ' + rawmsg.length + ' MESSAGES.');
if (this._messagesToReceive.length === 0) {
process.nextTick(this._receiveOneMessageBound);
for (let i = 0, len = data.length; i < len; i++) {
onMessage(data[i]);
}
this._messagesToReceive = this._messagesToReceive.concat(data);
});
}
private _receiveOneMessage(): void {
const rawmsg = this._messagesToReceive.shift();
if (this._messagesToReceive.length > 0) {
process.nextTick(this._receiveOneMessageBound);
}
this._onMessage(rawmsg);
}
private _sendAccumulated(): void {
const tmp = this._messagesToSend;
this._messagesToSend = [];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册