From 654c9a53f6d4f854cb5270ce56c8777bac1908ea Mon Sep 17 00:00:00 2001 From: "beth.null" Date: Thu, 27 Aug 2015 09:02:09 +0200 Subject: [PATCH 1/4] fix asynchronous version of Filter.get(). Calls to get(callback) are postponed while filterId is not available. --- lib/web3/filter.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/web3/filter.js b/lib/web3/filter.js index 7e5274e..f70fdee 100644 --- a/lib/web3/filter.js +++ b/lib/web3/filter.js @@ -139,6 +139,7 @@ var Filter = function (options, methods, formatter, callback) { this.implementation = implementation; this.filterId = null; this.callbacks = []; + this.getPendingCallbacks = []; this.pollFilters = []; this.formatter = formatter; this.implementation.newFilter(this.options, function(error, id){ @@ -149,6 +150,13 @@ var Filter = function (options, methods, formatter, callback) { } else { self.filterId = id; + // check if there are get pending callbacks as a consequence + // of calling get() with filterId unassigned. + self.getPendingCallbacks.forEach(function (cb){ + self.get(cb); + }); + self.getPendingCallbacks = []; + // get filter logs for the already existing watch calls self.callbacks.forEach(function(cb){ getLogsAtStart(self, cb); @@ -187,15 +195,21 @@ Filter.prototype.stopWatching = function () { Filter.prototype.get = function (callback) { var self = this; if (utils.isFunction(callback)) { - this.implementation.getLogs(this.filterId, function(err, res){ - if (err) { - callback(err); - } else { - callback(null, res.map(function (log) { - return self.formatter ? self.formatter(log) : log; - })); - } - }); + if (this.filterId === null) { + // If filterId is not set yet, call it back + // when newFilter() assigns it. + this.getPendingCallbacks.push(callback); + } else { + this.implementation.getLogs(this.filterId, function(err, res){ + if (err) { + callback(err); + } else { + callback(null, res.map(function (log) { + return self.formatter ? self.formatter(log) : log; + })); + } + }); + } } else { var logs = this.implementation.getLogs(this.filterId); return logs.map(function (log) { From 383efa7c0d6fe58937f12dd1181e9fa661a55154 Mon Sep 17 00:00:00 2001 From: "beth.null" Date: Fri, 4 Sep 2015 08:10:20 +0200 Subject: [PATCH 2/4] getPendingCallbacks renamed to getLogsCallbacks. --- lib/web3/filter.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/web3/filter.js b/lib/web3/filter.js index f70fdee..4a45a33 100644 --- a/lib/web3/filter.js +++ b/lib/web3/filter.js @@ -139,7 +139,7 @@ var Filter = function (options, methods, formatter, callback) { this.implementation = implementation; this.filterId = null; this.callbacks = []; - this.getPendingCallbacks = []; + this.getLogsCallbacks = []; this.pollFilters = []; this.formatter = formatter; this.implementation.newFilter(this.options, function(error, id){ @@ -152,10 +152,10 @@ var Filter = function (options, methods, formatter, callback) { // check if there are get pending callbacks as a consequence // of calling get() with filterId unassigned. - self.getPendingCallbacks.forEach(function (cb){ + self.getLogsCallbacks.forEach(function (cb){ self.get(cb); }); - self.getPendingCallbacks = []; + self.getLogsCallbacks = []; // get filter logs for the already existing watch calls self.callbacks.forEach(function(cb){ @@ -198,7 +198,7 @@ Filter.prototype.get = function (callback) { if (this.filterId === null) { // If filterId is not set yet, call it back // when newFilter() assigns it. - this.getPendingCallbacks.push(callback); + this.getLogsCallbacks.push(callback); } else { this.implementation.getLogs(this.filterId, function(err, res){ if (err) { From a2ae2d7d959dc60de4d9a7310c45c39234dfe047 Mon Sep 17 00:00:00 2001 From: "beth.null" Date: Sat, 5 Sep 2015 15:24:22 +0200 Subject: [PATCH 3/4] Throwing an error in case of a synchronous call of get() and filterId is not set. --- lib/web3/filter.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/web3/filter.js b/lib/web3/filter.js index 4a45a33..131dd1b 100644 --- a/lib/web3/filter.js +++ b/lib/web3/filter.js @@ -211,6 +211,9 @@ Filter.prototype.get = function (callback) { }); } } else { + if (this.filterId === null) { + throw new Error('Filter ID Error: filter().get() can\'t be chained synchronous, please provide a callback for the get() method.'); + } var logs = this.implementation.getLogs(this.filterId); return logs.map(function (log) { return self.formatter ? self.formatter(log) : log; From 4fa7e016f37b5a65781e9188108b6cab8d0c3a7c Mon Sep 17 00:00:00 2001 From: "beth.null" Date: Tue, 15 Sep 2015 00:13:52 +0200 Subject: [PATCH 4/4] Tests added for filter.get() . --- test/web3.eth.filter.js | 53 +++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/test/web3.eth.filter.js b/test/web3.eth.filter.js index 9f196b3..0dcb6aa 100644 --- a/test/web3.eth.filter.js +++ b/test/web3.eth.filter.js @@ -54,23 +54,46 @@ describe('web3.eth', function () { describe(method, function () { tests.forEach(function (test, index) { it('property test: ' + index, function () { - - // given - var provider = new FakeHttpProvider(); - web3.setProvider(provider); - provider.injectResult(test.result); - provider.injectValidation(function (payload) { - assert.equal(payload.jsonrpc, '2.0'); - assert.equal(payload.method, test.call); - assert.deepEqual(payload.params, test.formattedArgs); - }); - // call - web3.eth[method].apply(null, test.args); - + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, test.formattedArgs); + }); + + // call + var filter = web3.eth[method].apply(null, test.args); + + // test filter.get + if(typeof test.args === 'object') { + + var logs = [{data: '0xb'}, {data: '0x11'}]; + + provider.injectResult(logs); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, 'eth_getFilterLogs'); + assert.deepEqual(payload.params, [test.formattedResult]); + }); + + // sync should throw an error + try { + assert.throws(filter.get()); + } catch(e){ + assert.instanceOf(e, Error); + } + + // async should get the fake logs + filter.get(function(e, res){ + assert.equal(logs, res); + done(); + }); + } }); }); }); }); - -