add possible callback to filters

This commit is contained in:
Fabian Vogelsteller 2015-06-24 10:28:12 +02:00
parent e1b17a9db6
commit d78b512fa5
12 changed files with 222 additions and 61 deletions

44
dist/web3-light.js vendored
View File

@ -1429,22 +1429,26 @@ web3.version.api = version.version;
web3.eth = {};
/*jshint maxparams:4 */
web3.eth.filter = function (fil, eventParams, options, formatter) {
web3.eth.filter = function (fil, eventParams, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
// if its event, treat it differently
// TODO: simplify and remove
if (fil._isEvent) {
return fil(eventParams, options);
return fil(eventParams, options, callback);
}
// output logs works for blockFilter and pendingTransaction filters?
return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter);
return new Filter(fil, watches.eth(), callback, formatters.outputLogFormatter);
};
/*jshint maxparams:3 */
web3.shh = {};
web3.shh.filter = function (fil) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter);
web3.shh.filter = function (fil, callback) {
return new Filter(fil, watches.shh(), callback, formatters.outputPostFormatter);
};
web3.net = {};
web3.db = {};
@ -2178,6 +2182,8 @@ var coder = require('../solidity/coder');
var web3 = require('../web3');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
var Filter = require('./filter');
var watches = require('./watches');
/**
* This prototype should be used to create event filters
@ -2323,10 +2329,15 @@ SolidityEvent.prototype.decode = function (data) {
* @param {Object} options
* @return {Object} filter object
*/
SolidityEvent.prototype.execute = function (indexed, options) {
SolidityEvent.prototype.execute = function (indexed, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
var o = this.encode(indexed, options);
var formatter = this.decode.bind(this);
return web3.eth.filter(o, undefined, undefined, formatter);
return new Filter(o, watches.eth(), callback, formatter);
};
/**
@ -2347,7 +2358,7 @@ SolidityEvent.prototype.attachToContract = function (contract) {
module.exports = SolidityEvent;
},{"../solidity/coder":1,"../utils/sha3":5,"../utils/utils":6,"../web3":8,"./formatters":16}],15:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/sha3":5,"../utils/utils":6,"../web3":8,"./filter":15,"./formatters":16,"./watches":29}],15:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2477,7 +2488,7 @@ var pollFilter = function(self) {
};
var Filter = function (options, methods, formatter) {
var Filter = function (options, methods, callback, formatter) {
var self = this;
var implementation = {};
methods.forEach(function (method) {
@ -2490,18 +2501,25 @@ var Filter = function (options, methods, formatter) {
this.formatter = formatter;
this.implementation.newFilter(this.options, function(error, id){
if(error) {
self.callbacks.forEach(function(callback){
callback(error);
self.callbacks.forEach(function(cb){
cb(error);
});
} else {
self.filterId = id;
// get filter logs at start
self.callbacks.forEach(function(callback){
getLogsAtStart(self, callback);
self.callbacks.forEach(function(cb){
getLogsAtStart(self, cb);
});
pollFilter(self);
// start to watch immediately
if(callback) {
return self.watch(callback);
}
}
});
};
Filter.prototype.watch = function (callback) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

44
dist/web3.js vendored
View File

@ -1429,22 +1429,26 @@ web3.version.api = version.version;
web3.eth = {};
/*jshint maxparams:4 */
web3.eth.filter = function (fil, eventParams, options, formatter) {
web3.eth.filter = function (fil, eventParams, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
// if its event, treat it differently
// TODO: simplify and remove
if (fil._isEvent) {
return fil(eventParams, options);
return fil(eventParams, options, callback);
}
// output logs works for blockFilter and pendingTransaction filters?
return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter);
return new Filter(fil, watches.eth(), callback, formatters.outputLogFormatter);
};
/*jshint maxparams:3 */
web3.shh = {};
web3.shh.filter = function (fil) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter);
web3.shh.filter = function (fil, callback) {
return new Filter(fil, watches.shh(), callback, formatters.outputPostFormatter);
};
web3.net = {};
web3.db = {};
@ -2178,6 +2182,8 @@ var coder = require('../solidity/coder');
var web3 = require('../web3');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
var Filter = require('./filter');
var watches = require('./watches');
/**
* This prototype should be used to create event filters
@ -2323,10 +2329,15 @@ SolidityEvent.prototype.decode = function (data) {
* @param {Object} options
* @return {Object} filter object
*/
SolidityEvent.prototype.execute = function (indexed, options) {
SolidityEvent.prototype.execute = function (indexed, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
var o = this.encode(indexed, options);
var formatter = this.decode.bind(this);
return web3.eth.filter(o, undefined, undefined, formatter);
return new Filter(o, watches.eth(), callback, formatter);
};
/**
@ -2347,7 +2358,7 @@ SolidityEvent.prototype.attachToContract = function (contract) {
module.exports = SolidityEvent;
},{"../solidity/coder":1,"../utils/sha3":5,"../utils/utils":6,"../web3":8,"./formatters":16}],15:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/sha3":5,"../utils/utils":6,"../web3":8,"./filter":15,"./formatters":16,"./watches":29}],15:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2477,7 +2488,7 @@ var pollFilter = function(self) {
};
var Filter = function (options, methods, formatter) {
var Filter = function (options, methods, callback, formatter) {
var self = this;
var implementation = {};
methods.forEach(function (method) {
@ -2490,18 +2501,25 @@ var Filter = function (options, methods, formatter) {
this.formatter = formatter;
this.implementation.newFilter(this.options, function(error, id){
if(error) {
self.callbacks.forEach(function(callback){
callback(error);
self.callbacks.forEach(function(cb){
cb(error);
});
} else {
self.filterId = id;
// get filter logs at start
self.callbacks.forEach(function(callback){
getLogsAtStart(self, callback);
self.callbacks.forEach(function(cb){
getLogsAtStart(self, cb);
});
pollFilter(self);
// start to watch immediately
if(callback) {
return self.watch(callback);
}
}
});
};
Filter.prototype.watch = function (callback) {

8
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

11
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -86,22 +86,26 @@ web3.version.api = version.version;
web3.eth = {};
/*jshint maxparams:4 */
web3.eth.filter = function (fil, eventParams, options, formatter) {
web3.eth.filter = function (fil, eventParams, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
// if its event, treat it differently
// TODO: simplify and remove
if (fil._isEvent) {
return fil(eventParams, options);
return fil(eventParams, options, callback);
}
// output logs works for blockFilter and pendingTransaction filters?
return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter);
return new Filter(fil, watches.eth(), callback, formatters.outputLogFormatter);
};
/*jshint maxparams:3 */
web3.shh = {};
web3.shh.filter = function (fil) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter);
web3.shh.filter = function (fil, callback) {
return new Filter(fil, watches.shh(), callback, formatters.outputPostFormatter);
};
web3.net = {};
web3.db = {};

View File

@ -25,6 +25,8 @@ var coder = require('../solidity/coder');
var web3 = require('../web3');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
var Filter = require('./filter');
var watches = require('./watches');
/**
* This prototype should be used to create event filters
@ -170,10 +172,15 @@ SolidityEvent.prototype.decode = function (data) {
* @param {Object} options
* @return {Object} filter object
*/
SolidityEvent.prototype.execute = function (indexed, options) {
SolidityEvent.prototype.execute = function (indexed, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
}
var o = this.encode(indexed, options);
var formatter = this.decode.bind(this);
return web3.eth.filter(o, undefined, undefined, formatter);
return new Filter(o, watches.eth(), callback, formatter);
};
/**

View File

@ -127,7 +127,7 @@ var pollFilter = function(self) {
};
var Filter = function (options, methods, formatter) {
var Filter = function (options, methods, callback, formatter) {
var self = this;
var implementation = {};
methods.forEach(function (method) {
@ -140,18 +140,25 @@ var Filter = function (options, methods, formatter) {
this.formatter = formatter;
this.implementation.newFilter(this.options, function(error, id){
if(error) {
self.callbacks.forEach(function(callback){
callback(error);
self.callbacks.forEach(function(cb){
cb(error);
});
} else {
self.filterId = id;
// get filter logs at start
self.callbacks.forEach(function(callback){
getLogsAtStart(self, callback);
self.callbacks.forEach(function(cb){
getLogsAtStart(self, cb);
});
pollFilter(self);
// start to watch immediately
if(callback) {
return self.watch(callback);
}
}
});
};
Filter.prototype.watch = function (callback) {

View File

@ -127,6 +127,75 @@ describe('web3.eth.contract', function () {
});
});
it('should create event filter and watch immediately', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset(); // reset different polls
var signature = 'Changed(address,uint256,uint256,uint256)';
var step = 0;
provider.injectValidation(function (payload) {
if (step === 0) {
step = 1;
provider.injectResult('0x3');
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_newFilter');
assert.deepEqual(payload.params[0], {
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
null
],
address: '0x1234567890123456789012345678901234567890'
});
} else if (step === 1) {
step = 2;
provider.injectResult([{
address: address,
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]);
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_getFilterLogs');
} else if (step === 2 && utils.isArray(payload)) {
provider.injectBatchResults([[{
address: address,
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]]);
var r = payload.filter(function (p) {
return p.jsonrpc === '2.0' && p.method === 'eth_getFilterChanges' && p.params[0] === '0x3';
});
assert.equal(r.length > 0, true);
}
});
var contract = web3.eth.contract(desc).at(address);
var res = 0;
var event = contract.Changed({from: address}, function(err, result) {
assert.equal(result.args.from, address);
assert.equal(result.args.amount, 1);
assert.equal(result.args.t1, 1);
assert.equal(result.args.t2, 8);
res++;
if (res === 2) {
done();
}
});
});
it('should call constant function', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);

View File

@ -63,6 +63,8 @@ var testPolling = function (tests) {
var filter = web3[test.protocol].filter.apply(null, test.args);
provider.injectBatchResults([test.secondResult]);
filter.watch(function (err, result) {
console.log(err, result);
if (test.err) {
// todo
} else {
@ -72,6 +74,44 @@ var testPolling = function (tests) {
});
});
it('should create && successfully poll filter when passed as callback', function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
provider.injectResult(test.firstResult);
var step = 0;
provider.injectValidation(function (payload) {
if (step === 0) {
step = 1;
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.firstPayload.method);
assert.deepEqual(payload.params, test.firstPayload.params);
} else if (step === 1 && utils.isArray(payload)) {
var r = payload.filter(function (p) {
return p.jsonrpc === '2.0' && p.method === test.secondPayload.method && p.params[0] === test.firstResult;
});
assert.equal(r.length > 0, true);
}
});
// add callback
test.args.push(function (err, result) {
if (test.err) {
// todo
} else {
assert.equal(result, test.secondResult[0]);
}
done();
});
// when
var filter = web3[test.protocol].filter.apply(null, test.args);
provider.injectBatchResults([test.secondResult]);
});
});
});
};

View File

@ -14,10 +14,10 @@ var implementation = {
describe('web3.eth.filter', function () {
describe('methods', function () {
//var f = filter({}, implementation);
// var f = filter({}, implementation);
//u.methodExists(f, 'watch');
//u.methodExists(f, 'stopWatching');
//u.methodExists(f, 'get');
// u.methodExists(f, 'watch');
// u.methodExists(f, 'stopWatching');
// u.methodExists(f, 'get');
});
});