Skip to content

Commit b6ac431

Browse files
committed
[minor] Avoid using process.nextTick()
1 parent c1f3b21 commit b6ac431

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

lib/Sender.js

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ Sender.prototype.close = function(code, data, mask, cb) {
5454
if (dataBuffer.length > 2) dataBuffer.write(data, 2);
5555

5656
var self = this;
57-
this.messageHandlers.push(function(callback) {
57+
this.messageHandlers.push(function() {
5858
self.frameAndSend(0x8, dataBuffer, true, mask);
59-
callback();
6059
if (typeof cb == 'function') cb();
6160
});
6261
this.flush();
@@ -71,9 +70,8 @@ Sender.prototype.close = function(code, data, mask, cb) {
7170
Sender.prototype.ping = function(data, options) {
7271
var mask = options && options.mask;
7372
var self = this;
74-
this.messageHandlers.push(function(callback) {
73+
this.messageHandlers.push(function() {
7574
self.frameAndSend(0x9, data || '', true, mask);
76-
callback();
7775
});
7876
this.flush();
7977
};
@@ -87,9 +85,8 @@ Sender.prototype.ping = function(data, options) {
8785
Sender.prototype.pong = function(data, options) {
8886
var mask = options && options.mask;
8987
var self = this;
90-
this.messageHandlers.push(function(callback) {
88+
this.messageHandlers.push(function() {
9189
self.frameAndSend(0xa, data || '', true, mask);
92-
callback();
9390
});
9491
this.flush();
9592
};
@@ -117,15 +114,22 @@ Sender.prototype.send = function(data, options, cb) {
117114
var compressFragment = this.compress;
118115

119116
var self = this;
120-
this.messageHandlers.push(function(callback) {
117+
this.messageHandlers.push(function() {
118+
if (!data || !compressFragment) {
119+
self.frameAndSend(opcode, data, finalFragment, mask, compress, cb);
120+
return;
121+
}
122+
123+
self.processing = true;
121124
self.applyExtensions(data, finalFragment, compressFragment, function(err, data) {
122125
if (err) {
123126
if (typeof cb == 'function') cb(err);
124127
else self.emit('error', err);
125128
return;
126129
}
127130
self.frameAndSend(opcode, data, finalFragment, mask, compress, cb);
128-
callback();
131+
self.processing = false;
132+
self.flush();
129133
});
130134
});
131135
this.flush();
@@ -257,21 +261,9 @@ Sender.prototype.frameAndSend = function(opcode, data, finalFragment, maskData,
257261
*/
258262

259263
Sender.prototype.flush = function() {
260-
if (this.processing) return;
261-
262-
var handler = this.messageHandlers.shift();
263-
if (!handler) return;
264-
265-
this.processing = true;
266-
267-
var self = this;
268-
269-
handler(function() {
270-
process.nextTick(function() {
271-
self.processing = false;
272-
self.flush();
273-
});
274-
});
264+
while (!this.processing && this.messageHandlers.length) {
265+
this.messageHandlers.shift()();
266+
}
275267
};
276268

277269
/**
@@ -281,14 +273,10 @@ Sender.prototype.flush = function() {
281273
*/
282274

283275
Sender.prototype.applyExtensions = function(data, fin, compress, callback) {
284-
if (compress && data) {
285-
if ((data.buffer || data) instanceof ArrayBuffer) {
286-
data = getArrayBuffer(data);
287-
}
288-
this.extensions[PerMessageDeflate.extensionName].compress(data, fin, callback);
289-
} else {
290-
callback(null, data);
276+
if ((data.buffer || data) instanceof ArrayBuffer) {
277+
data = getArrayBuffer(data);
291278
}
279+
this.extensions[PerMessageDeflate.extensionName].compress(data, fin, callback);
292280
};
293281

294282
module.exports = Sender;

0 commit comments

Comments
 (0)