event options

This commit is contained in:
Marek Kotewicz 2015-01-31 01:30:19 +01:00
parent 600c9dd27d
commit 995861de4d
8 changed files with 104 additions and 36 deletions

48
dist/ethereum.js vendored
View File

@ -563,6 +563,10 @@ var addEventsToContract = function (contract, desc, address) {
return web3.eth.watch(o); return web3.eth.watch(o);
}; };
// this property should be used by eth.filter to check if object is an event
impl._isEvent = true;
// TODO: we can remove address && topic properties, they are not used anymore since we introduced _isEvent
impl.address = address; impl.address = address;
Object.defineProperty(impl, 'topic', { Object.defineProperty(impl, 'topic', {
@ -656,13 +660,16 @@ module.exports = contract;
* @date 2014 * @date 2014
*/ */
var abi = require('./abi');
var implementationOfEvent = function (address, signature) { var implementationOfEvent = function (address, signature) {
return function (options) { // valid options are 'earliest', 'latest', 'offset' and 'max', as defined for 'eth.watch'
return function (indexed, options) {
var o = options || {}; var o = options || {};
o.address = o.address || address; o.address = address;
o.topics = o.topics || []; o.topic = [];
o.topics.push(signature); o.topic.push(signature);
return o; return o;
}; };
}; };
@ -670,7 +677,7 @@ var implementationOfEvent = function (address, signature) {
module.exports = implementationOfEvent; module.exports = implementationOfEvent;
},{}],4:[function(require,module,exports){ },{"./abi":1}],4:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
@ -700,16 +707,19 @@ var web3 = require('./web3'); // jshint ignore:line
/// should be used when we want to watch something /// should be used when we want to watch something
/// it's using inner polling mechanism and is notified about changes /// it's using inner polling mechanism and is notified about changes
var Filter = function(options, impl) { /// TODO: change 'options' name cause it may be not the best matching one, since we have events
this.impl = impl; var Filter = function(options, indexed, impl) {
this.callbacks = [];
if (typeof options !== "string") { if (options._isEvent) {
// evaluate lazy properties return options(indexed);
} else if (typeof options !== "string") {
// topics property is deprecated, warn about it!
if (options.topics) { if (options.topics) {
console.warn('"topics" is deprecated, use "topic" instead'); console.warn('"topics" is deprecated, use "topic" instead');
} }
// evaluate lazy properties
options = { options = {
to: options.to, to: options.to,
topic: options.topic, topic: options.topic,
@ -719,8 +729,12 @@ var Filter = function(options, impl) {
skip: options.skip, skip: options.skip,
address: options.address address: options.address
}; };
} }
this.impl = impl;
this.callbacks = [];
this.id = impl.newFilter(options); this.id = impl.newFilter(options);
web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this)); web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this));
}; };
@ -1261,8 +1275,11 @@ var web3 = {
return ret; return ret;
}; };
}, },
watch: function (params) {
return new web3.filter(params, ethWatch); /// @param filter may be a string, object or event
/// @param indexed is optional, this may be an object with optional event indexed params
watch: function (filter, indexed) {
return new web3.filter(filter, indexed, ethWatch);
} }
}, },
@ -1271,8 +1288,11 @@ var web3 = {
/// shh object prototype /// shh object prototype
shh: { shh: {
watch: function (params) {
return new web3.filter(params, shhWatch); /// @param filter may be a string, object or event
/// @param indexed is optional, this may be an object with optional event indexed params
watch: function (filter, indexed) {
return new web3.filter(filter, indexed, shhWatch);
} }
}, },

10
dist/ethereum.js.map vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -132,6 +132,10 @@ var addEventsToContract = function (contract, desc, address) {
return web3.eth.watch(o); return web3.eth.watch(o);
}; };
// this property should be used by eth.filter to check if object is an event
impl._isEvent = true;
// TODO: we can remove address && topic properties, they are not used anymore since we introduced _isEvent
impl.address = address; impl.address = address;
Object.defineProperty(impl, 'topic', { Object.defineProperty(impl, 'topic', {

View File

@ -20,13 +20,16 @@
* @date 2014 * @date 2014
*/ */
var abi = require('./abi');
var implementationOfEvent = function (address, signature) { var implementationOfEvent = function (address, signature) {
return function (options) { // valid options are 'earliest', 'latest', 'offset' and 'max', as defined for 'eth.watch'
return function (indexed, options) {
var o = options || {}; var o = options || {};
o.address = o.address || address; o.address = address;
o.topics = o.topics || []; o.topic = [];
o.topics.push(signature); o.topic.push(signature);
return o; return o;
}; };
}; };

View File

@ -27,16 +27,19 @@ var web3 = require('./web3'); // jshint ignore:line
/// should be used when we want to watch something /// should be used when we want to watch something
/// it's using inner polling mechanism and is notified about changes /// it's using inner polling mechanism and is notified about changes
var Filter = function(options, impl) { /// TODO: change 'options' name cause it may be not the best matching one, since we have events
this.impl = impl; var Filter = function(options, indexed, impl) {
this.callbacks = [];
if (typeof options !== "string") { if (options._isEvent) {
// evaluate lazy properties return options(indexed);
} else if (typeof options !== "string") {
// topics property is deprecated, warn about it!
if (options.topics) { if (options.topics) {
console.warn('"topics" is deprecated, use "topic" instead'); console.warn('"topics" is deprecated, use "topic" instead');
} }
// evaluate lazy properties
options = { options = {
to: options.to, to: options.to,
topic: options.topic, topic: options.topic,
@ -46,8 +49,12 @@ var Filter = function(options, impl) {
skip: options.skip, skip: options.skip,
address: options.address address: options.address
}; };
} }
this.impl = impl;
this.callbacks = [];
this.id = impl.newFilter(options); this.id = impl.newFilter(options);
web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this)); web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this));
}; };

View File

@ -278,8 +278,11 @@ var web3 = {
return ret; return ret;
}; };
}, },
watch: function (params) {
return new web3.filter(params, ethWatch); /// @param filter may be a string, object or event
/// @param indexed is optional, this may be an object with optional event indexed params
watch: function (filter, indexed) {
return new web3.filter(filter, indexed, ethWatch);
} }
}, },
@ -288,8 +291,11 @@ var web3 = {
/// shh object prototype /// shh object prototype
shh: { shh: {
watch: function (params) {
return new web3.filter(params, shhWatch); /// @param filter may be a string, object or event
/// @param indexed is optional, this may be an object with optional event indexed params
watch: function (filter, indexed) {
return new web3.filter(filter, indexed, shhWatch);
} }
}, },

View File

@ -2,7 +2,7 @@ var assert = require('assert');
var event = require('../lib/event.js'); var event = require('../lib/event.js');
describe('event', function () { describe('event', function () {
it('should create filter input object from given', function () { it('should create basic filter input object', function () {
// given // given
var address = '0x012345'; var address = '0x012345';
@ -14,9 +14,37 @@ describe('event', function () {
// then // then
assert.equal(result.address, address); assert.equal(result.address, address);
assert.equal(result.topics.length, 1); assert.equal(result.topic.length, 1);
assert.equal(result.topics[0], signature); assert.equal(result.topic[0], signature);
}); });
it('should create basic filter input object', function () {
// given
var address = '0x012345';
var signature = '0x987654';
var options = {
earliest: 1,
latest: 2,
offset: 3,
max: 4
};
// when
var impl = event(address, signature);
var result = impl({}, options);
// then
assert.equal(result.address, address);
assert.equal(result.topic.length, 1);
assert.equal(result.topic[0], signature);
assert.equal(result.earliest, options.earliest);
assert.equal(result.latest, options.latest);
assert.equal(result.offset, options.offset);
assert.equal(result.max, options.max);
});
}); });