Skip to content

Commit

Permalink
Remove context variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Bridgewater committed Nov 30, 2015
1 parent 9ef6b19 commit af7d9d4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ new Parser(options);
* `returnFatalError`: *function*; optional, defaults to the returnError function
* `returnBuffers`: *boolean*; optional, defaults to false
* `name`: *javascript|hiredis*; optional, defaults to hiredis and falls back to the js parser if not available
* `context`: *A class instance that the return functions get bound to*; optional

### Example

Expand All @@ -45,10 +44,15 @@ Library.prototype.returnFatalError = function (err) { ... }
var lib = new Library();

var parser = new Parser({
returnReply: returnReply,
returnError: returnError,
returnFatalError: returnFatalError,
context: lib
returnReply: function(reply) {
lib.returnReply(reply);
},
returnError: function(err) {
lib.returnError(err);
},
returnFatalError: function (err) {
lib.returnFatalError(err);
}
}); // This returns either a hiredis or the js parser instance depending on what's available

Library.prototype.streamHandler = function () {
Expand All @@ -58,17 +62,20 @@ Library.prototype.streamHandler = function () {
});
};
```
You do not have to use the context variable, but can also bind the function while passing them to the option object.
You do not have to use the returnFatalError function. Fatal errors will be returned in the normal error function in that case.

And if you want to return buffers instead of strings, you can do this by adding the returnBuffers option.

```js
// Same functions as in the first example

var parser = new Parser({
returnReply: returnReply.bind(lib),
returnError: returnError.bind(lib),
returnFatalError: returnFatalError.bind(lib),
returnReply: function(reply) {
lib.returnReply(reply);
},
returnError: function(err) {
lib.returnError(err);
},
returnBuffers: true // All strings are returned as buffer e.g. <Buffer 48 65 6c 6c 6f>
});

Expand Down
6 changes: 0 additions & 6 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ function Parser (options) {
parser.returnError = options.returnError;
parser.returnFatalError = options.returnFatalError || options.returnError;
parser.returnReply = options.returnReply;

if (options.hasOwnProperty('context')) {
parser.returnError = parser.returnError.bind(options.context);
parser.returnFatalError = parser.returnFatalError.bind(options.context);
parser.returnReply = parser.returnReply.bind(options.context);
}
return parser;
}

Expand Down
11 changes: 7 additions & 4 deletions test/parsers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ describe('parsers', function () {
Abc.prototype.log = console.log;
var test = new Abc();
var parser = new Parser({
returnReply: test.checkReply,
returnReply: function (reply) {
test.checkReply(reply);
},
returnError: returnError,
returnFatalError: returnFatalError,
name: parserName,
context: test
name: parserName
});

parser.execute(new Buffer('*1\r\n*1\r\n$1\r\na\r\n'));
Expand Down Expand Up @@ -82,7 +83,9 @@ describe('parsers', function () {
var parser = new Parser({
returnReply: returnReply,
returnError: returnError,
returnFatalError: test.checkReply.bind(test),
returnFatalError: function (err) {
test.checkReply(err);
},
name: parserName
});

Expand Down

0 comments on commit af7d9d4

Please sign in to comment.