polling whisper in progress

This commit is contained in:
Marek Kotewicz 2014-10-29 12:44:50 +01:00
parent 0b10cbd713
commit 9adb625846

77
main.js
View File

@ -102,6 +102,28 @@
]; ];
}; };
var ethWatchMethods = function () {
var newFilter = function (args) {
return typeof args[0] === 'string' ? 'newFilterString' : 'newFilter';
};
return [
{ name: 'newFilter', call: newFilter },
{ name: 'uninstallFilter', call: 'uninstallFilter' },
{ name: 'changed', call: 'changed' },
{ name: 'getMessages', call: 'getMessages' }
];
};
var shhWatchMethods = function () {
return [
{ name: 'newFilter', call: 'shhNewFilter' },
{ name: 'uninstallFilter', call: 'shhUninstallFilter' },
{ name: 'changed', call: 'shhChanged' },
{ name: 'getMessage', call: 'shhGetMessages' }
];
};
var setupMethods = function (obj, methods) { var setupMethods = function (obj, methods) {
methods.forEach(function (method) { methods.forEach(function (method) {
obj[method.name] = function () { obj[method.name] = function () {
@ -186,6 +208,10 @@
return str; return str;
}, },
toDecimal: function (val) {
return parseInt(val, 16);
},
fromAscii: function(str, pad) { fromAscii: function(str, pad) {
if(pad === undefined) { if(pad === undefined) {
pad = 32 pad = 32
@ -201,8 +227,8 @@
eth: { eth: {
prototype: Object(), prototype: Object(),
watch: function(params) { watch: function (params) {
return new Filter(params); return new Filter(params, ethWatch);
}, },
}, },
@ -211,7 +237,10 @@
}, },
shh: { shh: {
prototype: Object() prototype: Object(),
watch: function (params) {
return new Filter(params, shhWatch);
}
}, },
on: function(event, cb) { on: function(event, cb) {
@ -259,6 +288,11 @@
setupMethods(web3.db, dbMethods()); setupMethods(web3.db, dbMethods());
setupMethods(web3.shh, shhMethods()); setupMethods(web3.shh, shhMethods());
var ethWatch = {};
setupMethods(ethWatch, ethWatchMethods());
var shhWatch = {};
setupMethods(shhWatch, shhWatchMethods());
var ProviderManager = function() { var ProviderManager = function() {
this.queued = []; this.queued = [];
this.polls = []; this.polls = [];
@ -340,33 +374,22 @@
web3.setProvider = function(provider) { web3.setProvider = function(provider) {
provider.onmessage = messageHandler; provider.onmessage = messageHandler;
web3.provider.set(provider); web3.provider.set(provider);
web3.provider.sendQueued(); web3.provider.sendQueued();
}; };
var filters = []; var filters = [];
var Filter = function(options) { var Filter = function(options, impl) {
filters.push(this); filters.push(this);
this.impl = impl;
this.callbacks = []; this.callbacks = [];
this.options = options;
var call;
if(options === "chain" || options === "pending") {
call = "newFilterString"
} else if(typeof options === "object") {
call = "newFilter"
}
var self = this; // Cheaper than binding var self = this; // Cheaper than binding
this.promise = new Promise(function(resolve, reject) { this.promise = impl.newFilter(options);
web3.provider.send({call: call, args: [options]}, function(id) { this.promise.then(function (id) {
self.id = id; self.id = id;
web3.provider.startPolling({call: "changed", args: [id]}, id); web3.provider.startPolling({call: 'changed', args: [id]}, id);
resolve(id);
});
}); });
}; };
@ -386,21 +409,17 @@
}; };
Filter.prototype.uninstall = function() { Filter.prototype.uninstall = function() {
this.promise.then(function(id) { var self = this;
web3.provider.send({call: "uninstallFilter", args:[id]}); this.promise.then(function (id) {
self.impl.uninstallFilter(id);
web3.provider.stopPolling(id); web3.provider.stopPolling(id);
}); });
}; };
Filter.prototype.messages = function() { Filter.prototype.messages = function() {
var self=this; var self = this;
return Promise.all([this.promise]).then(function() { return this.promise.then(function (id) {
var id = self.id return self.impl.getMessages(id);
return new Promise(function(resolve, reject) {
web3.provider.send({call: "getMessages", args: [id]}, function(messages) {
resolve(messages);
});
});
}); });
}; };