mirror of https://github.com/status-im/web3.js.git
Merge pull request #308 from bethnull/develop
fix asynchronous version of Filter.get().
This commit is contained in:
commit
0a2c203425
|
@ -139,6 +139,7 @@ var Filter = function (options, methods, formatter, callback) {
|
|||
this.implementation = implementation;
|
||||
this.filterId = null;
|
||||
this.callbacks = [];
|
||||
this.getLogsCallbacks = [];
|
||||
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.getLogsCallbacks.forEach(function (cb){
|
||||
self.get(cb);
|
||||
});
|
||||
self.getLogsCallbacks = [];
|
||||
|
||||
// get filter logs for the already existing watch calls
|
||||
self.callbacks.forEach(function(cb){
|
||||
getLogsAtStart(self, cb);
|
||||
|
@ -187,16 +195,25 @@ 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.getLogsCallbacks.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 {
|
||||
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;
|
||||
|
|
|
@ -56,21 +56,44 @@ describe('web3.eth', function () {
|
|||
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);
|
||||
});
|
||||
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);
|
||||
// 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();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue