Skip to content

Commit af7d9d4

Browse files
author
Ruben Bridgewater
committed
Remove context variable
1 parent 9ef6b19 commit af7d9d4

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

Diff for: README.md

+16-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ new Parser(options);
2929
* `returnFatalError`: *function*; optional, defaults to the returnError function
3030
* `returnBuffers`: *boolean*; optional, defaults to false
3131
* `name`: *javascript|hiredis*; optional, defaults to hiredis and falls back to the js parser if not available
32-
* `context`: *A class instance that the return functions get bound to*; optional
3332

3433
### Example
3534

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

4746
var parser = new Parser({
48-
returnReply: returnReply,
49-
returnError: returnError,
50-
returnFatalError: returnFatalError,
51-
context: lib
47+
returnReply: function(reply) {
48+
lib.returnReply(reply);
49+
},
50+
returnError: function(err) {
51+
lib.returnError(err);
52+
},
53+
returnFatalError: function (err) {
54+
lib.returnFatalError(err);
55+
}
5256
}); // This returns either a hiredis or the js parser instance depending on what's available
5357

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

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

6569
```js
6670
// Same functions as in the first example
6771

6872
var parser = new Parser({
69-
returnReply: returnReply.bind(lib),
70-
returnError: returnError.bind(lib),
71-
returnFatalError: returnFatalError.bind(lib),
73+
returnReply: function(reply) {
74+
lib.returnReply(reply);
75+
},
76+
returnError: function(err) {
77+
lib.returnError(err);
78+
},
7279
returnBuffers: true // All strings are returned as buffer e.g. <Buffer 48 65 6c 6c 6f>
7380
});
7481

Diff for: lib/parser.js

-6
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ function Parser (options) {
3838
parser.returnError = options.returnError;
3939
parser.returnFatalError = options.returnFatalError || options.returnError;
4040
parser.returnReply = options.returnReply;
41-
42-
if (options.hasOwnProperty('context')) {
43-
parser.returnError = parser.returnError.bind(options.context);
44-
parser.returnFatalError = parser.returnFatalError.bind(options.context);
45-
parser.returnReply = parser.returnReply.bind(options.context);
46-
}
4741
return parser;
4842
}
4943

Diff for: test/parsers.spec.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ describe('parsers', function () {
4949
Abc.prototype.log = console.log;
5050
var test = new Abc();
5151
var parser = new Parser({
52-
returnReply: test.checkReply,
52+
returnReply: function (reply) {
53+
test.checkReply(reply);
54+
},
5355
returnError: returnError,
5456
returnFatalError: returnFatalError,
55-
name: parserName,
56-
context: test
57+
name: parserName
5758
});
5859

5960
parser.execute(new Buffer('*1\r\n*1\r\n$1\r\na\r\n'));
@@ -82,7 +83,9 @@ describe('parsers', function () {
8283
var parser = new Parser({
8384
returnReply: returnReply,
8485
returnError: returnError,
85-
returnFatalError: test.checkReply.bind(test),
86+
returnFatalError: function (err) {
87+
test.checkReply(err);
88+
},
8689
name: parserName
8790
});
8891

0 commit comments

Comments
 (0)