mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-09 15:08:12 -05:00
refactor: simplify the handling of the "drain" event
The two event handlers are merged into one.
This commit is contained in:
@@ -223,25 +223,40 @@ export class Socket extends EventEmitter {
|
||||
private setTransport(transport) {
|
||||
const onError = this.onError.bind(this);
|
||||
const onPacket = this.onPacket.bind(this);
|
||||
const flush = this.flush.bind(this);
|
||||
const onDrain = this.onDrain.bind(this);
|
||||
const onClose = this.onClose.bind(this, "transport close");
|
||||
|
||||
this.transport = transport;
|
||||
this.transport.once("error", onError);
|
||||
this.transport.on("packet", onPacket);
|
||||
this.transport.on("drain", flush);
|
||||
this.transport.on("drain", onDrain);
|
||||
this.transport.once("close", onClose);
|
||||
// this function will manage packet events (also message callbacks)
|
||||
this.setupSendCallback();
|
||||
|
||||
this.cleanupFn.push(function () {
|
||||
transport.removeListener("error", onError);
|
||||
transport.removeListener("packet", onPacket);
|
||||
transport.removeListener("drain", flush);
|
||||
transport.removeListener("drain", onDrain);
|
||||
transport.removeListener("close", onClose);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Upon transport "drain" event
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private onDrain() {
|
||||
this.flush();
|
||||
|
||||
if (this.sentCallbackFn.length > 0) {
|
||||
debug("executing batch send callback");
|
||||
const seqFn = this.sentCallbackFn.shift();
|
||||
for (let i = 0; i < seqFn.length; i++) {
|
||||
seqFn[i](this.transport);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrades socket to the given transport
|
||||
*
|
||||
@@ -388,31 +403,6 @@ export class Socket extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup and manage send callback
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
private setupSendCallback() {
|
||||
// the message was sent successfully, execute the callback
|
||||
const onDrain = () => {
|
||||
if (this.sentCallbackFn.length > 0) {
|
||||
debug("executing batch send callback");
|
||||
const seqFn = this.sentCallbackFn.shift();
|
||||
const l = seqFn.length;
|
||||
for (let i = 0; i < l; i++) {
|
||||
seqFn[i](this.transport);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.transport.on("drain", onDrain);
|
||||
|
||||
this.cleanupFn.push(() => {
|
||||
this.transport.removeListener("drain", onDrain);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message packet.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user