You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
5.7 KiB
178 lines
5.7 KiB
10 years ago
|
/*
|
||
|
This file is part of ethereum.js.
|
||
|
|
||
|
ethereum.js is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU Lesser General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
ethereum.js is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU Lesser General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Lesser General Public License
|
||
|
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
/** @file filter.js
|
||
|
* @authors:
|
||
|
* Jeffrey Wilcke <jeff@ethdev.com>
|
||
|
* Marek Kotewicz <marek@ethdev.com>
|
||
|
* Marian Oancea <marian@ethdev.com>
|
||
|
* Gav Wood <g@ethdev.com>
|
||
|
* @date 2014
|
||
|
*/
|
||
|
|
||
10 years ago
|
var utils = require('../utils/utils');
|
||
|
|
||
10 years ago
|
/// Should be called to check if filter implementation is valid
|
||
|
/// @returns true if it is, otherwise false
|
||
|
var implementationIsValid = function (i) {
|
||
|
return !!i &&
|
||
|
typeof i.newFilter === 'function' &&
|
||
10 years ago
|
typeof i.getLogs === 'function' &&
|
||
10 years ago
|
typeof i.uninstallFilter === 'function' &&
|
||
|
typeof i.startPolling === 'function' &&
|
||
|
typeof i.stopPolling === 'function';
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
/// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones
|
||
|
/// @param should be string or object
|
||
|
/// @returns options string or object
|
||
|
var getOptions = function (options) {
|
||
10 years ago
|
/*jshint maxcomplexity:5 */
|
||
|
|
||
10 years ago
|
if (typeof options === 'string') {
|
||
|
return options;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
options = options || {};
|
||
10 years ago
|
|
||
10 years ago
|
if (options.topic) {
|
||
|
console.warn('"topic" is deprecated, is "topics" instead');
|
||
|
options.topics = options.topic;
|
||
|
}
|
||
|
|
||
|
if (options.earliest) {
|
||
|
console.warn('"earliest" is deprecated, is "fromBlock" instead');
|
||
|
options.fromBlock = options.earliest;
|
||
|
}
|
||
|
|
||
|
if (options.latest) {
|
||
|
console.warn('"latest" is deprecated, is "toBlock" instead');
|
||
|
options.toBlock = options.latest;
|
||
|
}
|
||
|
|
||
|
if (options.skip) {
|
||
|
console.warn('"skip" is deprecated, is "offset" instead');
|
||
|
options.offset = options.skip;
|
||
|
}
|
||
|
|
||
|
if (options.max) {
|
||
|
console.warn('"max" is deprecated, is "limit" instead');
|
||
|
options.limit = options.max;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// make sure topics, get converted to hex
|
||
|
if(options.topics instanceof Array) {
|
||
|
options.topics = options.topics.map(function(topic){
|
||
|
return utils.toHex(topic);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
// evaluate lazy properties
|
||
|
return {
|
||
10 years ago
|
fromBlock: utils.toHex(options.fromBlock),
|
||
|
toBlock: utils.toHex(options.toBlock),
|
||
|
limit: utils.toHex(options.limit),
|
||
|
offset: utils.toHex(options.offset),
|
||
10 years ago
|
to: options.to,
|
||
10 years ago
|
address: options.address,
|
||
|
topics: options.topics
|
||
10 years ago
|
};
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
/// Should be used when we want to watch something
|
||
|
/// it's using inner polling mechanism and is notified about changes
|
||
|
/// @param options are filter options
|
||
|
/// @param implementation, an abstract polling implementation
|
||
|
/// @param formatter (optional), callback function which formats output before 'real' callback
|
||
|
var filter = function(options, implementation, formatter) {
|
||
|
if (!implementationIsValid(implementation)) {
|
||
|
console.error('filter implemenation is invalid');
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
options = getOptions(options);
|
||
|
var callbacks = [];
|
||
|
var filterId = implementation.newFilter(options);
|
||
10 years ago
|
|
||
|
// call the callbacks
|
||
10 years ago
|
var onMessages = function (messages) {
|
||
|
messages.forEach(function (message) {
|
||
10 years ago
|
message = formatter ? formatter(message) : message;
|
||
10 years ago
|
callbacks.forEach(function (callback) {
|
||
|
callback(message);
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
|
||
|
implementation.startPolling(filterId, onMessages, implementation.uninstallFilter);
|
||
|
|
||
10 years ago
|
var watch = function(callback) {
|
||
10 years ago
|
callbacks.push(callback);
|
||
|
};
|
||
|
|
||
10 years ago
|
var stopWatching = function() {
|
||
10 years ago
|
implementation.stopPolling(filterId);
|
||
|
implementation.uninstallFilter(filterId);
|
||
|
callbacks = [];
|
||
|
};
|
||
|
|
||
10 years ago
|
var get = function () {
|
||
|
var results = implementation.getLogs(filterId);
|
||
|
|
||
|
return (results instanceof Array)
|
||
|
? results.map(function(message){
|
||
|
return formatter ? formatter(message) : message;
|
||
|
})
|
||
|
: results;
|
||
|
};
|
||
|
|
||
10 years ago
|
return {
|
||
10 years ago
|
watch: watch,
|
||
|
stopWatching: stopWatching,
|
||
|
get: get,
|
||
|
|
||
|
// DEPRECATED methods
|
||
|
changed: function(){
|
||
|
console.warn('watch().changed() is deprecated please use filter().watch() instead.');
|
||
|
return watch.apply(this, arguments);
|
||
|
},
|
||
|
arrived: function(){
|
||
|
console.warn('watch().arrived() is deprecated please use filter().watch() instead.');
|
||
|
return watch.apply(this, arguments);
|
||
|
},
|
||
|
happened: function(){
|
||
|
console.warn('watch().happened() is deprecated please use filter().watch() instead.');
|
||
|
return watch.apply(this, arguments);
|
||
|
},
|
||
|
uninstall: function(){
|
||
|
console.warn('watch().uninstall() is deprecated please use filter().stopWatching() instead.');
|
||
|
return stopWatching.apply(this, arguments);
|
||
|
},
|
||
|
messages: function(){
|
||
|
console.warn('watch().messages() is deprecated please use filter().get() instead.');
|
||
|
return get.apply(this, arguments);
|
||
|
},
|
||
|
logs: function(){
|
||
|
console.warn('watch().logs() is deprecated please use filter().get() instead.');
|
||
|
return get.apply(this, arguments);
|
||
|
}
|
||
10 years ago
|
};
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
module.exports = filter;
|
||
|
|