Skip to content

Commit 0170145

Browse files
author
Ruben Bridgewater
committed
Remove event emitters from the parser as they are overhead that is not needed
1 parent 75b3fdb commit 0170145

File tree

4 files changed

+8
-39
lines changed

4 files changed

+8
-39
lines changed

index.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,8 @@ RedisClient.prototype.init_parser = function () {
302302
this.reply_parser = new this.parser_module.Parser({
303303
return_buffers: self.options.return_buffers || self.options.detect_buffers || false
304304
});
305-
306-
// "reply error" is an error sent back by Redis
307-
this.reply_parser.on("reply error", function (reply) {
308-
self.return_error(reply);
309-
});
310-
this.reply_parser.on("reply", function (reply) {
311-
self.return_reply(reply);
312-
});
313-
// "error" is bad. Somehow the parser got confused. It'll try to reset and continue.
314-
this.reply_parser.on("error", function (err) {
315-
self.emit("error", new Error("Redis reply parser error: " + err.stack));
316-
});
305+
this.reply_parser.send_error = this.return_error.bind(self);
306+
this.reply_parser.send_reply = this.return_reply.bind(self);
317307
};
318308

319309
RedisClient.prototype.on_ready = function () {

lib/parser/hiredis.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
'use strict';
22

3-
var events = require("events"),
4-
util = require("util"),
5-
hiredis = require("hiredis");
3+
var hiredis = require("hiredis");
64

75
exports.name = "hiredis";
86

97
function HiredisReplyParser(options) {
108
this.name = exports.name;
119
this.options = options || {};
1210
this.reset();
13-
events.EventEmitter.call(this);
1411
}
1512

16-
util.inherits(HiredisReplyParser, events.EventEmitter);
17-
1813
exports.Parser = HiredisReplyParser;
1914

2015
HiredisReplyParser.prototype.reset = function () {
@@ -27,21 +22,16 @@ HiredisReplyParser.prototype.execute = function (data) {
2722
var reply;
2823
this.reader.feed(data);
2924
while (true) {
30-
try {
31-
reply = this.reader.get();
32-
} catch (err) {
33-
this.emit("error", err);
34-
break;
35-
}
25+
reply = this.reader.get();
3626

3727
if (reply === undefined) {
3828
break;
3929
}
4030

4131
if (reply && reply.constructor === Error) {
42-
this.emit("reply error", reply);
32+
this.send_error(reply);
4333
} else {
44-
this.emit("reply", reply);
34+
this.send_reply(reply);
4535
}
4636
}
4737
};

lib/parser/javascript.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
var events = require("events"),
4-
util = require("util");
3+
var util = require("util");
54

65
function Packet(type, size) {
76
this.type = type;
@@ -20,8 +19,6 @@ function ReplyParser(options) {
2019
this._reply_type = null;
2120
}
2221

23-
util.inherits(ReplyParser, events.EventEmitter);
24-
2522
exports.Parser = ReplyParser;
2623

2724
function IncompleteReadBuffer(message) {
@@ -286,11 +283,3 @@ ReplyParser.prototype._packetEndOffset = function () {
286283
ReplyParser.prototype._bytesRemaining = function () {
287284
return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset);
288285
};
289-
290-
ReplyParser.prototype.send_error = function (reply) {
291-
this.emit("reply error", reply);
292-
};
293-
294-
ReplyParser.prototype.send_reply = function (reply) {
295-
this.emit("reply", reply);
296-
};

test/parser/javascript.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('javascript parser', function () {
1111
assert.deepEqual(reply, [['a']], "Expecting multi-bulk reply of [['a']]");
1212
reply_count++;
1313
}
14-
parser.on("reply", check_reply);
14+
parser.send_reply = check_reply;
1515

1616
parser.execute(new Buffer('*1\r\n*1\r\n$1\r\na\r\n'));
1717

0 commit comments

Comments
 (0)