|
|
@ -22,7 +22,7 @@ |
|
|
|
* @date 2014 |
|
|
|
*/ |
|
|
|
|
|
|
|
function flattenPromise (obj) { |
|
|
|
function flattenPromise (obj) { |
|
|
|
if (obj instanceof Promise) { |
|
|
|
return Promise.resolve(obj); |
|
|
|
} |
|
|
@ -59,9 +59,9 @@ |
|
|
|
} |
|
|
|
|
|
|
|
return Promise.resolve(obj); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var ethMethods = function () { |
|
|
|
var ethMethods = function () { |
|
|
|
var blockCall = function (args) { |
|
|
|
return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber"; |
|
|
|
}; |
|
|
@ -88,9 +88,9 @@ |
|
|
|
{ name: 'lll', call: 'eth_lll' } |
|
|
|
]; |
|
|
|
return methods; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var ethProperties = function () { |
|
|
|
var ethProperties = function () { |
|
|
|
return [ |
|
|
|
{ name: 'coinbase', getter: 'eth_coinbase', setter: 'eth_setCoinbase' }, |
|
|
|
{ name: 'listening', getter: 'eth_listening', setter: 'eth_setListening' }, |
|
|
@ -102,18 +102,18 @@ |
|
|
|
{ name: 'defaultBlock', getter: 'eth_defaultBlock', setter: 'eth_setDefaultBlock' }, |
|
|
|
{ name: 'number', getter: 'eth_number'} |
|
|
|
]; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var dbMethods = function () { |
|
|
|
var dbMethods = function () { |
|
|
|
return [ |
|
|
|
{ name: 'put', call: 'db_put' }, |
|
|
|
{ name: 'get', call: 'db_get' }, |
|
|
|
{ name: 'putString', call: 'db_putString' }, |
|
|
|
{ name: 'getString', call: 'db_getString' } |
|
|
|
]; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var shhMethods = function () { |
|
|
|
var shhMethods = function () { |
|
|
|
return [ |
|
|
|
{ name: 'post', call: 'shh_post' }, |
|
|
|
{ name: 'newIdentity', call: 'shh_newIdentity' }, |
|
|
@ -121,9 +121,9 @@ |
|
|
|
{ name: 'newGroup', call: 'shh_newGroup' }, |
|
|
|
{ name: 'addToGroup', call: 'shh_addToGroup' } |
|
|
|
]; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var ethWatchMethods = function () { |
|
|
|
var ethWatchMethods = function () { |
|
|
|
var newFilter = function (args) { |
|
|
|
return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter'; |
|
|
|
}; |
|
|
@ -133,17 +133,17 @@ |
|
|
|
{ name: 'uninstallFilter', call: 'eth_uninstallFilter' }, |
|
|
|
{ name: 'getMessages', call: 'eth_getMessages' } |
|
|
|
]; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var shhWatchMethods = function () { |
|
|
|
var shhWatchMethods = function () { |
|
|
|
return [ |
|
|
|
{ name: 'newFilter', call: 'shh_newFilter' }, |
|
|
|
{ name: 'uninstallFilter', call: 'shh_uninstallFilter' }, |
|
|
|
{ name: 'getMessage', call: 'shh_getMessages' } |
|
|
|
]; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var setupMethods = function (obj, methods) { |
|
|
|
var setupMethods = function (obj, methods) { |
|
|
|
methods.forEach(function (method) { |
|
|
|
obj[method.name] = function () { |
|
|
|
return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) { |
|
|
@ -164,9 +164,9 @@ |
|
|
|
}); |
|
|
|
}; |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var setupProperties = function (obj, properties) { |
|
|
|
var setupProperties = function (obj, properties) { |
|
|
|
properties.forEach(function (property) { |
|
|
|
var proto = {}; |
|
|
|
proto.get = function () { |
|
|
@ -199,9 +199,9 @@ |
|
|
|
} |
|
|
|
Object.defineProperty(obj, property.name, proto); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var web3 = { |
|
|
|
var web3 = { |
|
|
|
_callbacks: {}, |
|
|
|
_events: {}, |
|
|
|
providers: {}, |
|
|
@ -288,24 +288,24 @@ |
|
|
|
var cb = callbacks[id]; |
|
|
|
cb(data); |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var eth = web3.eth; |
|
|
|
setupMethods(eth, ethMethods()); |
|
|
|
setupProperties(eth, ethProperties()); |
|
|
|
setupMethods(web3.db, dbMethods()); |
|
|
|
setupMethods(web3.shh, shhMethods()); |
|
|
|
var eth = web3.eth; |
|
|
|
setupMethods(eth, ethMethods()); |
|
|
|
setupProperties(eth, ethProperties()); |
|
|
|
setupMethods(web3.db, dbMethods()); |
|
|
|
setupMethods(web3.shh, shhMethods()); |
|
|
|
|
|
|
|
var ethWatch = { |
|
|
|
var ethWatch = { |
|
|
|
changed: 'eth_changed' |
|
|
|
}; |
|
|
|
setupMethods(ethWatch, ethWatchMethods()); |
|
|
|
var shhWatch = { |
|
|
|
}; |
|
|
|
setupMethods(ethWatch, ethWatchMethods()); |
|
|
|
var shhWatch = { |
|
|
|
changed: 'shh_changed' |
|
|
|
}; |
|
|
|
setupMethods(shhWatch, shhWatchMethods()); |
|
|
|
}; |
|
|
|
setupMethods(shhWatch, shhWatchMethods()); |
|
|
|
|
|
|
|
var ProviderManager = function() { |
|
|
|
var ProviderManager = function() { |
|
|
|
this.queued = []; |
|
|
|
this.polls = []; |
|
|
|
this.ready = false; |
|
|
@ -324,9 +324,9 @@ |
|
|
|
setTimeout(poll, 12000); |
|
|
|
}; |
|
|
|
poll(); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
ProviderManager.prototype.send = function(data, cb) { |
|
|
|
ProviderManager.prototype.send = function(data, cb) { |
|
|
|
data._id = this.id; |
|
|
|
if (cb) { |
|
|
|
web3._callbacks[data._id] = cb; |
|
|
@ -341,53 +341,53 @@ |
|
|
|
console.warn("provider is not set"); |
|
|
|
this.queued.push(data); |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
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.ready = true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
ProviderManager.prototype.sendQueued = function() { |
|
|
|
ProviderManager.prototype.sendQueued = function() { |
|
|
|
for(var i = 0; this.queued.length; i++) { |
|
|
|
// Resend
|
|
|
|
this.send(this.queued[i]); |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
ProviderManager.prototype.installed = function() { |
|
|
|
ProviderManager.prototype.installed = function() { |
|
|
|
return this.provider !== undefined; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
ProviderManager.prototype.startPolling = function (data, pollId) { |
|
|
|
ProviderManager.prototype.startPolling = function (data, pollId) { |
|
|
|
if (!this.provider || !this.provider.poll) { |
|
|
|
return; |
|
|
|
} |
|
|
|
this.polls.push({data: data, id: pollId}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
ProviderManager.prototype.stopPolling = function (pollId) { |
|
|
|
ProviderManager.prototype.stopPolling = function (pollId) { |
|
|
|
for (var i = this.polls.length; i--;) { |
|
|
|
var poll = this.polls[i]; |
|
|
|
if (poll.id === pollId) { |
|
|
|
this.polls.splice(i, 1); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
web3.provider = new ProviderManager(); |
|
|
|
web3.provider = new ProviderManager(); |
|
|
|
|
|
|
|
web3.setProvider = function(provider) { |
|
|
|
web3.setProvider = function(provider) { |
|
|
|
provider.onmessage = messageHandler; |
|
|
|
web3.provider.set(provider); |
|
|
|
web3.provider.sendQueued(); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
var Filter = function(options, impl) { |
|
|
|
var Filter = function(options, impl) { |
|
|
|
this.impl = impl; |
|
|
|
this.callbacks = []; |
|
|
|
|
|
|
@ -398,42 +398,42 @@ |
|
|
|
web3.on(impl.changed, id, self.trigger.bind(self)); |
|
|
|
web3.provider.startPolling({call: impl.changed, args: [id]}, id); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
Filter.prototype.arrived = function(callback) { |
|
|
|
Filter.prototype.arrived = function(callback) { |
|
|
|
this.changed(callback); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
Filter.prototype.changed = function(callback) { |
|
|
|
Filter.prototype.changed = function(callback) { |
|
|
|
var self = this; |
|
|
|
this.promise.then(function(id) { |
|
|
|
self.callbacks.push(callback); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
Filter.prototype.trigger = function(messages) { |
|
|
|
Filter.prototype.trigger = function(messages) { |
|
|
|
for(var i = 0; i < this.callbacks.length; i++) { |
|
|
|
this.callbacks[i].call(this, messages); |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
Filter.prototype.uninstall = function() { |
|
|
|
Filter.prototype.uninstall = function() { |
|
|
|
var self = this; |
|
|
|
this.promise.then(function (id) { |
|
|
|
self.impl.uninstallFilter(id); |
|
|
|
web3.provider.stopPolling(id); |
|
|
|
web3.off(impl.changed, id); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
Filter.prototype.messages = function() { |
|
|
|
Filter.prototype.messages = function() { |
|
|
|
var self = this; |
|
|
|
return this.promise.then(function (id) { |
|
|
|
return self.impl.getMessages(id); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
function messageHandler(data) { |
|
|
|
function messageHandler(data) { |
|
|
|
if(data._event !== undefined) { |
|
|
|
web3.trigger(data._event, data._id, data.data); |
|
|
|
return; |
|
|
@ -446,7 +446,7 @@ |
|
|
|
delete web3._callbacks[data._id]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = web3; |
|
|
|
module.exports = web3; |
|
|
|
|
|
|
|