Skip to content

Commit

Permalink
Merge pull request #197 from kiwiirc/xpaw/remove-read-buffer
Browse files Browse the repository at this point in the history
Remove read_buffer and parse irc line straight away
  • Loading branch information
prawnsalad authored Jul 12, 2020
2 parents ae3262f + d921109 commit cfc5cf6
Showing 1 changed file with 5 additions and 57 deletions.
62 changes: 5 additions & 57 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ module.exports = class Connection extends EventEmitter {
// When an IRC connection was successfully registered.
this.registered = false;

this.read_buffer = [];
this.reading_buffer = false;

this.read_command_buffer = [];

this.transport = null;

this._timers = [];
Expand Down Expand Up @@ -94,8 +89,7 @@ module.exports = class Connection extends EventEmitter {
}

function socketLine(line) {
that.read_buffer.push(line);
that.processReadBuffer();
that.addReadBuffer(line);
}

function socketClose(err) {
Expand Down Expand Up @@ -158,8 +152,10 @@ module.exports = class Connection extends EventEmitter {
}

addReadBuffer(line) {
this.read_buffer.push(line);
this.processReadBuffer();
const message = ircLineParser(line);

this.emit('raw', { line: line, from_server: true });
this.emit('message', message, line);
}

write(data, callback) {
Expand Down Expand Up @@ -246,52 +242,4 @@ module.exports = class Connection extends EventEmitter {
return this.transport.setEncoding(encoding);
}
}

/**
* Process the buffered messages recieved from the IRCd
* Will only process 4 lines per JS tick so that node can handle any other events while
* handling a large buffer
*/
processReadBuffer(continue_processing) {
// If we already have the read buffer being iterated through, don't start
// another one.
if (this.reading_buffer && !continue_processing) {
return;
}

const that = this;
const lines_per_js_tick = 40;
let processed_lines = 0;
let line;
let message;

this.reading_buffer = true;

while (processed_lines < lines_per_js_tick && this.read_buffer.length > 0) {
line = this.read_buffer.shift();
if (!line) {
continue;
}

message = ircLineParser(line);

if (!message) {
// A malformed IRC line
continue;
}
this.emit('raw', { line: line, from_server: true });
this.emit('message', message, line);

processed_lines++;
}

// If we still have items left in our buffer then continue reading them in a few ticks
if (this.read_buffer.length > 0) {
this.setTimeout(function() {
that.processReadBuffer(true);
}, 1);
} else {
this.reading_buffer = false;
}
}
};

0 comments on commit cfc5cf6

Please sign in to comment.