filters refactored

This commit is contained in:
Marek Kotewicz 2014-10-29 14:45:39 +01:00
parent 9adb625846
commit 31c6159019
2 changed files with 36 additions and 66 deletions

@ -52,7 +52,7 @@
return; return;
} }
self.handlers.forEach(function (handler) { self.handlers.forEach(function (handler) {
handler.call(self, {_event: "messages", data: id}); handler.call(self, {_event: payload.call, _id: id, data: parsed.result});
}); });
}); });
}; };

88
main.js

@ -110,7 +110,6 @@
return [ return [
{ name: 'newFilter', call: newFilter }, { name: 'newFilter', call: newFilter },
{ name: 'uninstallFilter', call: 'uninstallFilter' }, { name: 'uninstallFilter', call: 'uninstallFilter' },
{ name: 'changed', call: 'changed' },
{ name: 'getMessages', call: 'getMessages' } { name: 'getMessages', call: 'getMessages' }
]; ];
}; };
@ -119,7 +118,6 @@
return [ return [
{ name: 'newFilter', call: 'shhNewFilter' }, { name: 'newFilter', call: 'shhNewFilter' },
{ name: 'uninstallFilter', call: 'shhUninstallFilter' }, { name: 'uninstallFilter', call: 'shhUninstallFilter' },
{ name: 'changed', call: 'shhChanged' },
{ name: 'getMessage', call: 'shhGetMessages' } { name: 'getMessage', call: 'shhGetMessages' }
]; ];
}; };
@ -213,15 +211,10 @@
}, },
fromAscii: function(str, pad) { fromAscii: function(str, pad) {
if(pad === undefined) { pad = pad === undefined ? 32 : pad;
pad = 32
}
var hex = this.toHex(str); var hex = this.toHex(str);
while(hex.length < pad*2) while(hex.length < pad*2)
hex += "00"; hex += "00";
return hex return hex
}, },
@ -243,42 +236,30 @@
} }
}, },
on: function(event, cb) { on: function(event, id, cb) {
if(web3._events[event] === undefined) { if(web3._events[event] === undefined) {
web3._events[event] = []; web3._events[event] = {};
} }
web3._events[event].push(cb); web3._events[event][id] = cb;
return this return this
}, },
off: function(event, cb) { off: function(event, id) {
if(web3._events[event] !== undefined) { if(web3._events[event] !== undefined) {
var callbacks = web3._events[event]; delete web3._events[event][id];
for(var i = 0; i < callbacks.length; i++) {
if(callbacks[i] === cb) {
delete callbacks[i];
}
}
} }
return this return this
}, },
trigger: function(event, data) { trigger: function(event, id, data) {
var callbacks = web3._events[event]; var callbacks = web3._events[event];
if(callbacks !== undefined) { if (!callbacks || !callbacks[id]) {
for(var i = 0; i < callbacks.length; i++) { return;
// Figure out whether the returned data was an array
// array means multiple return arguments (multiple params)
if(data instanceof Array) {
callbacks[i].apply(this, data);
} else {
callbacks[i].call(this, undefined, data);
}
}
} }
var cb = callbacks[id];
cb(data);
}, },
}; };
@ -288,9 +269,13 @@
setupMethods(web3.db, dbMethods()); setupMethods(web3.db, dbMethods());
setupMethods(web3.shh, shhMethods()); setupMethods(web3.shh, shhMethods());
var ethWatch = {}; var ethWatch = {
changed: 'changed'
};
setupMethods(ethWatch, ethWatchMethods()); setupMethods(ethWatch, ethWatchMethods());
var shhWatch = {}; var shhWatch = {
changed: 'shhChanged'
};
setupMethods(shhWatch, shhWatchMethods()); setupMethods(shhWatch, shhWatchMethods());
var ProviderManager = function() { var ProviderManager = function() {
@ -316,14 +301,11 @@
ProviderManager.prototype.send = function(data, cb) { ProviderManager.prototype.send = function(data, cb) {
data._id = this.id; data._id = this.id;
if(cb) { if (cb) {
web3._callbacks[data._id] = cb; web3._callbacks[data._id] = cb;
} }
if(data.args === undefined) { data.args = data.args || [];
data.args = [];
}
this.id++; this.id++;
if(this.provider !== undefined) { if(this.provider !== undefined) {
@ -378,18 +360,16 @@
web3.provider.sendQueued(); web3.provider.sendQueued();
}; };
var filters = [];
var Filter = function(options, impl) { var Filter = function(options, impl) {
filters.push(this);
this.impl = impl; this.impl = impl;
this.callbacks = []; this.callbacks = [];
var self = this; // Cheaper than binding var self = this;
this.promise = impl.newFilter(options); this.promise = impl.newFilter(options);
this.promise.then(function (id) { this.promise.then(function (id) {
self.id = id; self.id = id;
web3.provider.startPolling({call: 'changed', args: [id]}, id); web3.on(impl.changed, id, self.trigger.bind(self));
web3.provider.startPolling({call: impl.changed, args: [id]}, id);
}); });
}; };
@ -400,12 +380,10 @@
}); });
}; };
Filter.prototype.trigger = function(messages, id) { Filter.prototype.trigger = function(messages) {
if(id == this.id) {
for(var i = 0; i < this.callbacks.length; i++) { for(var i = 0; i < this.callbacks.length; i++) {
this.callbacks[i].call(this, messages); this.callbacks[i].call(this, messages);
} }
}
}; };
Filter.prototype.uninstall = function() { Filter.prototype.uninstall = function() {
@ -413,6 +391,7 @@
this.promise.then(function (id) { this.promise.then(function (id) {
self.impl.uninstallFilter(id); self.impl.uninstallFilter(id);
web3.provider.stopPolling(id); web3.provider.stopPolling(id);
web3.off(impl.changed, id);
}); });
}; };
@ -423,29 +402,20 @@
}); });
}; };
// Register to the messages callback. "messages" will be emitted when new messages
// from the client have been created.
web3.on("messages", function(messages, id) {
for(var i = 0; i < filters.length; i++) {
filters[i].trigger(messages, id);
}
});
function messageHandler(data) { function messageHandler(data) {
if(data._event !== undefined) { if(data._event !== undefined) {
web3.trigger(data._event, data.data); web3.trigger(data._event, data._id, data.data);
} else { return;
}
if(data._id) { if(data._id) {
var cb = web3._callbacks[data._id]; var cb = web3._callbacks[data._id];
if(cb) { if (cb) {
cb.call(this, data.data) cb.call(this, data.data)
// Remove the "trigger" callback
delete web3._callbacks[data._id]; delete web3._callbacks[data._id];
} }
} }
} }
}
/* /*
// Install default provider // Install default provider