diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-23 00:57:26 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-23 00:57:26 +0800 |
commit | 81bbe8c93b22007c62949ab5f141aec255134385 (patch) | |
tree | 38f19ee3e9ca97d44ade09803f9994e549bdf3f2 /lib | |
parent | 3073511e30e4df18c3ea5b7f71a4ee9c9175b02b (diff) | |
download | dexon-81bbe8c93b22007c62949ab5f141aec255134385.tar.gz dexon-81bbe8c93b22007c62949ab5f141aec255134385.tar.zst dexon-81bbe8c93b22007c62949ab5f141aec255134385.zip |
simplified synchronous polling
Diffstat (limited to 'lib')
-rw-r--r-- | lib/filter.js | 4 | ||||
-rw-r--r-- | lib/providermanager.js | 34 |
2 files changed, 15 insertions, 23 deletions
diff --git a/lib/filter.js b/lib/filter.js index 76e67e9c1..079c25049 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -32,8 +32,7 @@ var Filter = function(options, impl) { this.callbacks = []; 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); + web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this)); }; /// alias for changed* @@ -57,7 +56,6 @@ Filter.prototype.trigger = function(messages) { Filter.prototype.uninstall = function() { this.impl.uninstallFilter(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 diff --git a/lib/providermanager.js b/lib/providermanager.js index c3b121451..83e11605b 100644 --- a/lib/providermanager.js +++ b/lib/providermanager.js @@ -35,19 +35,26 @@ var web3 = require('./web3'); // jshint ignore:line * and provider manager polling mechanism is not used */ var ProviderManager = function() { - this.queued = []; this.polls = []; - this.ready = false; this.provider = undefined; this.id = 1; var self = this; var poll = function () { - if (self.provider && self.provider.poll) { + if (self.provider) { self.polls.forEach(function (data) { data.data._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); @@ -55,7 +62,7 @@ var ProviderManager = function() { poll(); }; -/// sends outgoing requests, if provider is not available, enqueue the request +/// sends outgoing requests ProviderManager.prototype.send = function(data) { data.args = data.args || []; @@ -74,26 +81,13 @@ ProviderManager.prototype.send = function(data) { /// setups provider, which will be used for sending messages ProviderManager.prototype.set = function(provider) { - if(this.provider !== undefined && this.provider.unload !== undefined) { - this.provider.unload(); - } - 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 /// should be callled, on start watching for eth/shh changes -ProviderManager.prototype.startPolling = function (data, pollId) { - if (!this.provider || !this.provider.poll) { - return; - } - this.polls.push({data: data, id: pollId}); +ProviderManager.prototype.startPolling = function (data, pollId, callback) { + this.polls.push({data: data, id: pollId, callback: callback}); }; /// should be called to stop polling for certain watch changes |