|
2 | 2 |
|
3 | 3 | var hiredis = require('hiredis');
|
4 | 4 |
|
5 |
| -function HiredisReplyParser(options) { |
6 |
| - this.name = 'hiredis'; |
7 |
| - this.options = options; |
8 |
| - this.reader = new hiredis.Reader(options); |
| 5 | +/** |
| 6 | + * Parse data |
| 7 | + * @param parser |
| 8 | + * @returns {*} |
| 9 | + */ |
| 10 | +function parseData(parser) { |
| 11 | + try { |
| 12 | + return parser.reader.get(); |
| 13 | + } catch (err) { |
| 14 | + // Protocol errors land here |
| 15 | + // Reset the parser. Otherwise new commands can't be processed properly |
| 16 | + parser.reader = new hiredis.Reader(parser.options); |
| 17 | + parser.returnFatalError(err); |
| 18 | + } |
9 | 19 | }
|
10 | 20 |
|
11 |
| -HiredisReplyParser.prototype.parseData = function () { |
12 |
| - try { |
13 |
| - return this.reader.get(); |
14 |
| - } catch (err) { |
15 |
| - // Protocol errors land here |
16 |
| - // Reset the parser. Otherwise new commands can't be processed properly |
17 |
| - this.reader = new hiredis.Reader(this.options); |
18 |
| - this.returnFatalError(err); |
19 |
| - } |
20 |
| -}; |
| 21 | +/** |
| 22 | + * Hiredis Parser |
| 23 | + * @param options |
| 24 | + * @constructor |
| 25 | + */ |
| 26 | +function HiredisReplyParser(options) { |
| 27 | + this.name = 'hiredis'; |
| 28 | + this.options = options; |
| 29 | + this.reader = new hiredis.Reader(options); |
| 30 | +} |
21 | 31 |
|
22 | 32 | HiredisReplyParser.prototype.execute = function (data) {
|
23 |
| - this.reader.feed(data); |
24 |
| - var reply = this.parseData(); |
| 33 | + this.reader.feed(data); |
| 34 | + var reply = parseData(this); |
25 | 35 |
|
26 |
| - while (reply !== undefined) { |
27 |
| - if (reply && reply.name === 'Error') { |
28 |
| - this.returnError(reply); |
29 |
| - } else { |
30 |
| - this.returnReply(reply); |
31 |
| - } |
32 |
| - reply = this.parseData(); |
33 |
| - } |
| 36 | + while (reply !== undefined) { |
| 37 | + if (reply && reply.name === 'Error') { |
| 38 | + this.returnError(reply); |
| 39 | + } else { |
| 40 | + this.returnReply(reply); |
| 41 | + } |
| 42 | + reply = parseData(this); |
| 43 | + } |
34 | 44 | };
|
35 | 45 |
|
36 | 46 | module.exports = HiredisReplyParser;
|
0 commit comments