Skip to content

Commit

Permalink
var self = this isn't needed here
Browse files Browse the repository at this point in the history
You only need to do that when you're passing the value of this to
another function. Also remove wrapper function from `FluentSender.emit`
  • Loading branch information
notslang committed Jul 23, 2017
1 parent bb878f6 commit eaed988
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions lib/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function FluentSender(tag_prefix, options){
this._sendQueue = []; // queue for items waiting for being sent.
this._sendQueueTail = -1;
this._eventEmitter = new EventEmitter();
this._flushSendQueue = this._flushSendQueue.bind(this)
}

FluentSender.prototype.emit = function(/*[label] <data>, [timestamp], [callback] */){
Expand All @@ -46,39 +47,35 @@ FluentSender.prototype.emit = function(/*[label] <data>, [timestamp], [callback]
// Last argument is an optional callback
if (typeof args[0] === 'function') callback = args.shift();


var self = this;
var item = self._makePacketItem(label, data, timestamp);
var item = this._makePacketItem(label, data, timestamp);

var error;
var options;
if (item.tag === null) {
options = {
tag_prefix: self.tag_prefix,
tag_prefix: this.tag_prefix,
label: label
};
error = new FluentLoggerError.MissingTag('tag is missing', options);
self._handleEvent('error', error, callback);
this._handleEvent('error', error, callback);
return;
}
if (typeof item.data !== 'object') {
options = {
tag_prefix: self.tag_prefix,
tag_prefix: this.tag_prefix,
label: label,
record: item.data
};
error = new FluentLoggerError.DataTypeError('data must be an object', options);
self._handleEvent('error', error, callback);
this._handleEvent('error', error, callback);
return;
}

item.callback = callback;

self._sendQueue.push(item);
self._sendQueueTail++;
self._connect(function(){
self._flushSendQueue();
});
this._sendQueue.push(item);
this._sendQueueTail++;
this._connect(this._flushSendQueue);
};

['addListener', 'on', 'once', 'removeListener', 'removeAllListeners', 'setMaxListeners', 'getMaxListeners'].forEach(function(attr, i){
Expand Down Expand Up @@ -115,12 +112,11 @@ FluentSender.prototype._close = function() {


FluentSender.prototype._makePacketItem = function(label, data, time){
var self = this;
var tag = null;
if (self.tag_prefix && label) {
tag = self.tag_prefix + '.' + label;
} else if (self.tag_prefix) {
tag = self.tag_prefix;
if (this.tag_prefix && label) {
tag = this.tag_prefix + '.' + label;
} else if (this.tag_prefix) {
tag = this.tag_prefix;
} else if (label) {
tag = label;
}
Expand All @@ -131,7 +127,7 @@ FluentSender.prototype._makePacketItem = function(label, data, time){

var packet = [tag, time, data];
var options = {};
if (self.requireAckResponse) {
if (this.requireAckResponse) {
options = {
chunk: crypto.randomBytes(16).toString('base64')
};
Expand Down Expand Up @@ -225,10 +221,9 @@ FluentSender.prototype._flushSendQueue = function() {
};

FluentSender.prototype._handleEvent = function _handleEvent(signal, data, callback) {
var self = this;
callback && callback(data);
if (self._eventEmitter.listenerCount(signal) > 0) {
self._eventEmitter.emit(signal, data);
if (this._eventEmitter.listenerCount(signal) > 0) {
this._eventEmitter.emit(signal, data);
}
};

Expand Down

0 comments on commit eaed988

Please sign in to comment.