Browse Source

retabed files

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
84b36a7193
  1. 31
      lib/httprpc.js
  2. 126
      lib/main.js
  3. 13
      lib/qt.js
  4. 24
      lib/websocket.js

31
lib/httprpc.js

@ -21,26 +21,25 @@
* @date 2014
*/
if(process.env.NODE_ENV !== "build") {
if (process.env.NODE_ENV !== "build") {
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
}
var HttpRpcProvider = function (host) {
var HttpRpcProvider = function (host) {
this.handlers = [];
this.host = host;
};
};
function formatJsonRpcObject(object) {
function formatJsonRpcObject(object) {
return {
jsonrpc: '2.0',
method: object.call,
params: object.args,
id: object._id
};
}
}
function formatJsonRpcMessage(message) {
function formatJsonRpcMessage(message) {
var object = JSON.parse(message);
return {
@ -48,9 +47,9 @@ if(process.env.NODE_ENV !== "build") {
data: object.result,
error: object.error
};
}
}
HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
var data = formatJsonRpcObject(payload);
var request = new XMLHttpRequest();
@ -61,18 +60,18 @@ if(process.env.NODE_ENV !== "build") {
cb(request);
}
};
};
};
HttpRpcProvider.prototype.send = function (payload) {
HttpRpcProvider.prototype.send = function (payload) {
var self = this;
this.sendRequest(payload, function (request) {
self.handlers.forEach(function (handler) {
handler.call(self, formatJsonRpcMessage(request.responseText));
});
});
};
};
HttpRpcProvider.prototype.poll = function (payload, id) {
HttpRpcProvider.prototype.poll = function (payload, id) {
var self = this;
this.sendRequest(payload, function (request) {
var parsed = JSON.parse(request.responseText);
@ -83,12 +82,12 @@ if(process.env.NODE_ENV !== "build") {
handler.call(self, {_event: payload.call, _id: id, data: parsed.result});
});
});
};
};
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", {
Object.defineProperty(HttpRpcProvider.prototype, "onmessage", {
set: function (handler) {
this.handlers.push(handler);
}
});
});
module.exports = HttpRpcProvider;

126
lib/main.js

@ -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;

13
lib/qt.js

@ -16,11 +16,12 @@
*/
/** @file qt.js
* @authors:
* Jeffrey Wilcke <jeff@ethdev.com>
* Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
var QtProvider = function() {
var QtProvider = function() {
this.handlers = [];
var self = this;
@ -29,16 +30,16 @@
handler.call(self, JSON.parse(message.data));
});
};
};
};
QtProvider.prototype.send = function(payload) {
QtProvider.prototype.send = function(payload) {
navigator.qt.postMessage(JSON.stringify(payload));
};
};
Object.defineProperty(QtProvider.prototype, "onmessage", {
Object.defineProperty(QtProvider.prototype, "onmessage", {
set: function(handler) {
this.handlers.push(handler);
}
});
});
module.exports = QtProvider;

24
lib/websocket.js

@ -16,16 +16,17 @@
*/
/** @file websocket.js
* @authors:
* Jeffrey Wilcke <jeff@ethdev.com>
* Marek Kotewicz <marek@ethdev.com>
* Marian Oancea <marian@ethdev.com>
* @date 2014
*/
if(process.env.NODE_ENV !== "build") {
if (process.env.NODE_ENV !== "build") {
var WebSocket = require('ws'); // jshint ignore:line
}
var WebSocketProvider = function(host) {
var WebSocketProvider = function(host) {
// onmessage handlers
this.handlers = [];
// queue will be filled with messages if send is invoked before the ws is ready
@ -49,8 +50,9 @@ if(process.env.NODE_ENV !== "build") {
self.send(self.queued[i]);
}
};
};
WebSocketProvider.prototype.send = function(payload) {
};
WebSocketProvider.prototype.send = function(payload) {
if(this.ready) {
var data = JSON.stringify(payload);
@ -58,17 +60,17 @@ if(process.env.NODE_ENV !== "build") {
} else {
this.queued.push(payload);
}
};
};
WebSocketProvider.prototype.onMessage = function(handler) {
WebSocketProvider.prototype.onMessage = function(handler) {
this.handlers.push(handler);
};
};
WebSocketProvider.prototype.unload = function() {
WebSocketProvider.prototype.unload = function() {
this.ws.close();
};
Object.defineProperty(WebSocketProvider.prototype, "onmessage", {
};
Object.defineProperty(WebSocketProvider.prototype, "onmessage", {
set: function(provider) { this.onMessage(provider); }
});
});
module.exports = WebSocketProvider;

Loading…
Cancel
Save