simplified synchronous polling

This commit is contained in:
Marek Kotewicz 2015-01-22 17:57:26 +01:00
parent 3073511e30
commit 81bbe8c93b
5 changed files with 34 additions and 50 deletions

38
dist/ethereum.js vendored
View File

@ -558,8 +558,7 @@ var Filter = function(options, impl) {
this.callbacks = []; this.callbacks = [];
this.id = impl.newFilter(options); this.id = impl.newFilter(options);
web3.on(impl.changed, this.id, this.trigger.bind(this)); 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);
}; };
/// alias for changed* /// alias for changed*
@ -583,7 +582,6 @@ Filter.prototype.trigger = function(messages) {
Filter.prototype.uninstall = function() { Filter.prototype.uninstall = function() {
this.impl.uninstallFilter(this.id); this.impl.uninstallFilter(this.id);
web3.provider.stopPolling(this.id); web3.provider.stopPolling(this.id);
web3.off(impl.changed, this.id);
}; };
/// should be called to manually trigger getting latest messages from the client /// should be called to manually trigger getting latest messages from the client
@ -704,19 +702,26 @@ var web3 = require('./web3'); // jshint ignore:line
* and provider manager polling mechanism is not used * and provider manager polling mechanism is not used
*/ */
var ProviderManager = function() { var ProviderManager = function() {
this.queued = [];
this.polls = []; this.polls = [];
this.ready = false;
this.provider = undefined; this.provider = undefined;
this.id = 1; this.id = 1;
var self = this; var self = this;
var poll = function () { var poll = function () {
if (self.provider && self.provider.poll) { if (self.provider) {
self.polls.forEach(function (data) { self.polls.forEach(function (data) {
data.data._id = self.id; data.data._id = self.id;
self.id++; self.id++;
self.provider.poll(data.data, data.id); var result = self.provider.send(data.data);
result = JSON.parse(result);
// dont call the callback if result is an error, empty array or false
if (result.error || (result.result instanceof Array ? result.result.length === 0 : !result.result)) {
return;
}
data.callback(result);
}); });
} }
setTimeout(poll, 12000); setTimeout(poll, 12000);
@ -724,7 +729,7 @@ var ProviderManager = function() {
poll(); poll();
}; };
/// sends outgoing requests, if provider is not available, enqueue the request /// sends outgoing requests
ProviderManager.prototype.send = function(data) { ProviderManager.prototype.send = function(data) {
data.args = data.args || []; data.args = data.args || [];
@ -743,26 +748,13 @@ ProviderManager.prototype.send = function(data) {
/// setups provider, which will be used for sending messages /// setups provider, which will be used for sending messages
ProviderManager.prototype.set = function(provider) { ProviderManager.prototype.set = function(provider) {
if(this.provider !== undefined && this.provider.unload !== undefined) {
this.provider.unload();
}
this.provider = provider; this.provider = provider;
this.ready = true;
};
/// @returns true if the provider i properly set
ProviderManager.prototype.installed = function() {
return this.provider !== undefined;
}; };
/// this method is only used, when we do not have native qt bindings and have to do polling on our own /// this method is only used, when we do not have native qt bindings and have to do polling on our own
/// should be callled, on start watching for eth/shh changes /// should be callled, on start watching for eth/shh changes
ProviderManager.prototype.startPolling = function (data, pollId) { ProviderManager.prototype.startPolling = function (data, pollId, callback) {
if (!this.provider || !this.provider.poll) { this.polls.push({data: data, id: pollId, callback: callback});
return;
}
this.polls.push({data: data, id: pollId});
}; };
/// should be called to stop polling for certain watch changes /// should be called to stop polling for certain watch changes

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -32,8 +32,7 @@ var Filter = function(options, impl) {
this.callbacks = []; this.callbacks = [];
this.id = impl.newFilter(options); this.id = impl.newFilter(options);
web3.on(impl.changed, this.id, this.trigger.bind(this)); 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);
}; };
/// alias for changed* /// alias for changed*
@ -57,7 +56,6 @@ Filter.prototype.trigger = function(messages) {
Filter.prototype.uninstall = function() { Filter.prototype.uninstall = function() {
this.impl.uninstallFilter(this.id); this.impl.uninstallFilter(this.id);
web3.provider.stopPolling(this.id); web3.provider.stopPolling(this.id);
web3.off(impl.changed, this.id);
}; };
/// should be called to manually trigger getting latest messages from the client /// should be called to manually trigger getting latest messages from the client

View File

@ -35,19 +35,26 @@ var web3 = require('./web3'); // jshint ignore:line
* and provider manager polling mechanism is not used * and provider manager polling mechanism is not used
*/ */
var ProviderManager = function() { var ProviderManager = function() {
this.queued = [];
this.polls = []; this.polls = [];
this.ready = false;
this.provider = undefined; this.provider = undefined;
this.id = 1; this.id = 1;
var self = this; var self = this;
var poll = function () { var poll = function () {
if (self.provider && self.provider.poll) { if (self.provider) {
self.polls.forEach(function (data) { self.polls.forEach(function (data) {
data.data._id = self.id; data.data._id = self.id;
self.id++; self.id++;
self.provider.poll(data.data, data.id); var result = self.provider.send(data.data);
result = JSON.parse(result);
// dont call the callback if result is an error, empty array or false
if (result.error || (result.result instanceof Array ? result.result.length === 0 : !result.result)) {
return;
}
data.callback(result);
}); });
} }
setTimeout(poll, 12000); setTimeout(poll, 12000);
@ -55,7 +62,7 @@ var ProviderManager = function() {
poll(); poll();
}; };
/// sends outgoing requests, if provider is not available, enqueue the request /// sends outgoing requests
ProviderManager.prototype.send = function(data) { ProviderManager.prototype.send = function(data) {
data.args = data.args || []; data.args = data.args || [];
@ -74,26 +81,13 @@ ProviderManager.prototype.send = function(data) {
/// setups provider, which will be used for sending messages /// setups provider, which will be used for sending messages
ProviderManager.prototype.set = function(provider) { ProviderManager.prototype.set = function(provider) {
if(this.provider !== undefined && this.provider.unload !== undefined) {
this.provider.unload();
}
this.provider = provider; this.provider = provider;
this.ready = true;
};
/// @returns true if the provider i properly set
ProviderManager.prototype.installed = function() {
return this.provider !== undefined;
}; };
/// this method is only used, when we do not have native qt bindings and have to do polling on our own /// this method is only used, when we do not have native qt bindings and have to do polling on our own
/// should be callled, on start watching for eth/shh changes /// should be callled, on start watching for eth/shh changes
ProviderManager.prototype.startPolling = function (data, pollId) { ProviderManager.prototype.startPolling = function (data, pollId, callback) {
if (!this.provider || !this.provider.poll) { this.polls.push({data: data, id: pollId, callback: callback});
return;
}
this.polls.push({data: data, id: pollId});
}; };
/// should be called to stop polling for certain watch changes /// should be called to stop polling for certain watch changes