web3.js/lib/web3/filter.js

137 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-01-13 18:28:49 +01:00
/*
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>
2015-03-12 16:33:19 +01:00
* Fabian Vogelsteller <fabian@ethdev.com>
2015-01-13 18:28:49 +01:00
* Gav Wood <g@ethdev.com>
* @date 2014
*/
2015-03-26 18:11:05 +01:00
var RequestManager = require('./requestmanager');
var formatters = require('./formatters');
2015-03-08 18:18:52 +01:00
var utils = require('../utils/utils');
2015-02-05 23:11:16 +01:00
/// 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) {
2015-03-26 15:43:35 +01:00
if (utils.isString(options)) {
2015-02-05 23:11:16 +01:00
return options;
}
2015-01-13 18:28:49 +01:00
2015-02-05 23:11:16 +01:00
options = options || {};
2015-01-13 18:28:49 +01:00
// make sure topics, get converted to hex
2015-03-26 15:43:35 +01:00
options.topics = options.topics || [];
options.topics = options.topics.map(function(topic){
return utils.toHex(topic);
});
2015-01-13 18:28:49 +01:00
2015-03-25 21:46:45 +01:00
// lazy load
return {
topics: options.topics,
to: options.to,
address: options.address,
fromBlock: formatters.inputBlockNumberFormatter(options.fromBlock),
toBlock: formatters.inputBlockNumberFormatter(options.toBlock)
2015-03-25 21:46:45 +01:00
};
2015-01-13 18:28:49 +01:00
};
2015-03-26 18:11:05 +01:00
var Filter = function (options, methods, formatter) {
var implementation = {};
methods.forEach(function (method) {
method.attachToObject(implementation);
});
this.options = getOptions(options);
this.implementation = implementation;
this.callbacks = [];
this.formatter = formatter;
this.filterId = this.implementation.newFilter(this.options);
};
Filter.prototype.watch = function (callback) {
this.callbacks.push(callback);
2015-04-09 09:15:04 +02:00
var self = this;
2015-03-26 18:11:05 +01:00
var onMessage = function (error, messages) {
2015-03-25 13:17:21 +01:00
if (error) {
2015-03-26 18:11:05 +01:00
return self.callbacks.forEach(function (callback) {
2015-03-25 21:46:45 +01:00
callback(error);
});
2015-03-25 13:17:21 +01:00
}
2015-02-05 23:11:16 +01:00
messages.forEach(function (message) {
2015-03-26 18:11:05 +01:00
message = self.formatter ? self.formatter(message) : message;
self.callbacks.forEach(function (callback) {
2015-03-25 13:17:21 +01:00
callback(null, message);
2015-02-05 23:11:16 +01:00
});
});
};
2015-03-31 13:16:07 +02:00
// call getFilterLogs on start
if (!utils.isString(this.options)) {
2015-04-09 09:15:04 +02:00
this.get(function (err, messages) {
// don't send all the responses to all the watches again... just to this one
if (err) {
callback(err);
}
messages.forEach(function (message) {
callback(null, message);
});
});
2015-03-31 13:16:07 +02:00
}
2015-04-09 09:15:04 +02:00
RequestManager.getInstance().startPolling({
2015-03-26 18:11:05 +01:00
method: this.implementation.poll.call,
params: [this.filterId],
}, this.filterId, onMessage, this.stopWatching.bind(this));
};
2015-02-05 23:11:16 +01:00
2015-03-26 18:11:05 +01:00
Filter.prototype.stopWatching = function () {
RequestManager.getInstance().stopPolling(this.filterId);
this.implementation.uninstallFilter(this.filterId);
this.callbacks = [];
2015-01-13 18:28:49 +01:00
};
2015-03-31 13:16:07 +02:00
Filter.prototype.get = function (callback) {
2015-03-26 18:11:05 +01:00
var self = this;
2015-04-09 09:15:04 +02:00
if (utils.isFunction(callback)) {
2015-03-31 13:16:07 +02:00
this.implementation.getLogs(this.filterId, function(err, res){
2015-04-09 09:15:04 +02:00
if (err) {
callback(err);
} else {
2015-03-31 13:16:07 +02:00
callback(null, res.map(function (log) {
return self.formatter ? self.formatter(log) : log;
}));
2015-04-09 09:15:04 +02:00
}
2015-03-31 13:16:07 +02:00
});
} else {
var logs = this.implementation.getLogs(this.filterId);
return logs.map(function (log) {
return self.formatter ? self.formatter(log) : log;
});
}
2015-03-26 18:11:05 +01:00
};
2015-03-26 15:43:35 +01:00
2015-03-26 18:11:05 +01:00
module.exports = Filter;
2015-02-05 23:11:16 +01:00