Browse Source

updated eth.js

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
702f283d9d
  1. 119
      libethrpc/eth.js

119
libethrpc/eth.js

@ -177,7 +177,9 @@ window.eth = (function ethScope() {
{ name: "transact", async: "makeTransact"}, { name: "transact", async: "makeTransact"},
{ name: "call", async: "makeCall" }, { name: "call", async: "makeCall" },
{ name: "messages", async: "getMessages" }, { name: "messages", async: "getMessages" },
{ name: "transaction", async: "getTransaction" } { name: "block", async: "getBlock" },
{ name: "transaction", async: "getTransaction" },
{ name: "uncle", async: "getUncle" }
]; ];
methods.forEach(function (method) { methods.forEach(function (method) {
@ -192,81 +194,60 @@ window.eth = (function ethScope() {
}); });
}; };
setupProperties(ret, window.spec); var setupWatch = function (root) {
setupMethods(ret, window.spec); root.watch = function (val) {
if (typeof val !== 'string') {
/* val = JSON.stringify(val);
function isEmpty(obj) {
for (var prop in obj)
if (obj.hasOwnProperty(prop))
return false
return true
};
var m_watching = {};
for (si in spec) (function(s) {
var m = s.method;
var am = "get" + m.slice(0, 1).toUpperCase() + m.slice(1);
var getParams = function(a) {
var p = s.params ? {} : null;
for (j in s.order)
p[s.order[j]] = a[j];
return p
};
if (m == "create" || m == "transact")
ret[m] = function() { return reqAsync(m, getParams(arguments), arguments[s.order.length]) }
else
{
ret[am] = function() { return reqAsync(m, getParams(arguments), arguments[s.order.length]) }
if (s.params)
ret[m] = function() { return reqSync(m, getParams(arguments)) }
else
Object.defineProperty(ret, m, {
get: function() { return reqSync(m, {}); },
set: function(v) {}
})
} }
})(spec[si]);
var id;
ret.check = function(force) { reqAsync('watch', {params: val}, function (result) {
if (!force && isEmpty(m_watching)) id = result;
return }); // async send watch
var watching = []; var callbacks = [];
for (var w in m_watching) var exist = true;
watching.push(w) var w = {
var changed = reqSync("check", { "a": watching } ); changed: function (f) {
// console.log("Got " + JSON.stringify(changed)); callbacks.push(f);
for (var c in changed) },
m_watching[changed[c]]() uninstall: function (f) {
var that = this; reqAsync('killWatch', {id: id});
setTimeout(function() { that.check() }, 12000) exist = false;
},
messages: function () {
// TODO!
},
getMessages: function (f) {
// TODO!
} }
};
ret.watch = function(a, fx, f) { var check = function () {
var old = isEmpty(m_watching) if (!exist) {
if (f) return;
m_watching[a + fx] = f }
else if (callbacks.length) {
m_watching[a] = fx reqAsync('check', {id: id}, function (res) {
(f ? f : fx)() if (!res) {
if (isEmpty(m_watching) != old) return;
this.check()
} }
ret.unwatch = function(f, fx) { callbacks.forEach(function (f) {
delete m_watching[fx ? f + fx : f]; f();
});
});
} }
ret.newBlock = function(f) { window.setTimeout(check, 12000);
var old = isEmpty(m_watching) };
m_watching[""] = f
f() check();
if (isEmpty(m_watching) != old) return w;
this.check()
} }
*/ };
setupProperties(ret, window.spec);
setupMethods(ret, window.spec);
setupWatch(ret);
return ret; return ret;
}()); }());

Loading…
Cancel
Save