Skip to content

Commit

Permalink
[minor] Avoid using process.nextTick()
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Feb 11, 2017
1 parent c1f3b21 commit b6ac431
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions lib/Sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ Sender.prototype.close = function(code, data, mask, cb) {
if (dataBuffer.length > 2) dataBuffer.write(data, 2);

var self = this;
this.messageHandlers.push(function(callback) {
this.messageHandlers.push(function() {
self.frameAndSend(0x8, dataBuffer, true, mask);
callback();
if (typeof cb == 'function') cb();
});
this.flush();
Expand All @@ -71,9 +70,8 @@ Sender.prototype.close = function(code, data, mask, cb) {
Sender.prototype.ping = function(data, options) {
var mask = options && options.mask;
var self = this;
this.messageHandlers.push(function(callback) {
this.messageHandlers.push(function() {
self.frameAndSend(0x9, data || '', true, mask);
callback();
});
this.flush();
};
Expand All @@ -87,9 +85,8 @@ Sender.prototype.ping = function(data, options) {
Sender.prototype.pong = function(data, options) {
var mask = options && options.mask;
var self = this;
this.messageHandlers.push(function(callback) {
this.messageHandlers.push(function() {
self.frameAndSend(0xa, data || '', true, mask);
callback();
});
this.flush();
};
Expand Down Expand Up @@ -117,15 +114,22 @@ Sender.prototype.send = function(data, options, cb) {
var compressFragment = this.compress;

var self = this;
this.messageHandlers.push(function(callback) {
this.messageHandlers.push(function() {
if (!data || !compressFragment) {
self.frameAndSend(opcode, data, finalFragment, mask, compress, cb);
return;
}

self.processing = true;
self.applyExtensions(data, finalFragment, compressFragment, function(err, data) {
if (err) {
if (typeof cb == 'function') cb(err);
else self.emit('error', err);
return;
}
self.frameAndSend(opcode, data, finalFragment, mask, compress, cb);
callback();
self.processing = false;
self.flush();
});
});
this.flush();
Expand Down Expand Up @@ -257,21 +261,9 @@ Sender.prototype.frameAndSend = function(opcode, data, finalFragment, maskData,
*/

Sender.prototype.flush = function() {
if (this.processing) return;

var handler = this.messageHandlers.shift();
if (!handler) return;

this.processing = true;

var self = this;

handler(function() {
process.nextTick(function() {
self.processing = false;
self.flush();
});
});
while (!this.processing && this.messageHandlers.length) {
this.messageHandlers.shift()();
}
};

/**
Expand All @@ -281,14 +273,10 @@ Sender.prototype.flush = function() {
*/

Sender.prototype.applyExtensions = function(data, fin, compress, callback) {
if (compress && data) {
if ((data.buffer || data) instanceof ArrayBuffer) {
data = getArrayBuffer(data);
}
this.extensions[PerMessageDeflate.extensionName].compress(data, fin, callback);
} else {
callback(null, data);
if ((data.buffer || data) instanceof ArrayBuffer) {
data = getArrayBuffer(data);
}
this.extensions[PerMessageDeflate.extensionName].compress(data, fin, callback);
};

module.exports = Sender;
Expand Down

2 comments on commit b6ac431

@lpinca
Copy link
Member Author

@lpinca lpinca commented on b6ac431 Feb 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@websockets/admin does this LGTY? If so I'll cut 1.1.2 which will hopefully be the last 1.x release.

@JacksonTian
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Please sign in to comment.