Added callback mechanism and updated UI

* UI Now updates when a new block has been broadcasted
* Added a on, off and trigger
This commit is contained in:
obscuren 2014-04-24 14:43:00 +02:00
parent c535d0d246
commit bbde892d50
5 changed files with 64 additions and 19 deletions

@ -6,7 +6,7 @@ function postData(data, cb) {
} }
if(data.args === undefined) { if(data.args === undefined) {
data.args = [] data.args = [];
} }
navigator.qt.postMessage(JSON.stringify(data)); navigator.qt.postMessage(JSON.stringify(data));
@ -24,11 +24,11 @@ window.eth = {
getBlock: function(numberOrHash, cb) { getBlock: function(numberOrHash, cb) {
var func; var func;
if(typeof numberOrHash == "string") { if(typeof numberOrHash == "string") {
func = "getBlockByHash" func = "getBlockByHash";
} else { } else {
func = "getBlockByNumber" func = "getBlockByNumber";
} }
postData({call: func, args: [numberOrHash]}, cb) postData({call: func, args: [numberOrHash]}, cb);
}, },
// Create transaction // Create transaction
@ -36,18 +36,51 @@ window.eth = {
// Creates a transaction with the current account // Creates a transaction with the current account
// If no recipient is set, the Ethereum API will see it as a contract creation // If no recipient is set, the Ethereum API will see it as a contract creation
createTx: function(recipient, value, gas, gasPrice, data, cb) { createTx: function(recipient, value, gas, gasPrice, data, cb) {
postData({call: "createTx", args: [recipient, value, gas, gasPrice, data]}, cb) postData({call: "createTx", args: [recipient, value, gas, gasPrice, data]}, cb);
}, },
getStorage: function(address, storageAddress, cb) { getStorage: function(address, storageAddress, cb) {
postData({call: "getStorage", args: [address, storageAddress]}, cb) postData({call: "getStorage", args: [address, storageAddress]}, cb);
}, },
getKey: function(cb) { getKey: function(cb) {
postData({call: "getKey"}, cb) postData({call: "getKey"}, cb);
},
on: function(event, cb) {
if(eth._onCallbacks[event] === undefined) {
eth._onCallbacks[event] = [];
}
eth._onCallbacks[event].push(cb);
return this
},
off: function(event, cb) {
if(eth._onCallbacks[event] !== undefined) {
var callbacks = eth._onCallbacks[event];
for(var i = 0; i < callbacks.length; i++) {
if(callbacks[i] === cb) {
delete callbacks[i];
}
}
}
return this
},
trigger: function(event, data) {
var callbacks = eth._onCallbacks[event];
if(callbacks !== undefined) {
for(var i = 0; i < callbacks.length; i++) {
callbacks[i](data);
}
}
}, },
} }
window.eth._callbacks = {} window.eth._callbacks = {}
window.eth._onCallbacks = {}
function debug(/**/) { function debug(/**/) {
var args = arguments; var args = arguments;
@ -66,13 +99,17 @@ function debug(/**/) {
navigator.qt.onmessage = function(ev) { navigator.qt.onmessage = function(ev) {
var data = JSON.parse(ev.data) var data = JSON.parse(ev.data)
if(data._seed) { if(data._event !== undefined) {
var cb = eth._callbacks[data._seed]; eth.trigger(data._event, data.data);
if(cb) { } else {
// Call the callback if(data._seed) {
cb(data.data); var cb = eth._callbacks[data._seed];
// Remove the "trigger" callback if(cb) {
delete eth._callbacks[ev._seed]; // Call the callback
cb(data.data);
// Remove the "trigger" callback
delete eth._callbacks[ev._seed];
}
} }
} }
} }

@ -117,6 +117,7 @@ Component {
TextArea { TextArea {
id: codeView id: codeView
height: 300
anchors.topMargin: 5 anchors.topMargin: 5
Layout.fillWidth: true Layout.fillWidth: true
width: parent.width /2 width: parent.width /2

@ -18,13 +18,11 @@ ApplicationWindow {
MenuBar { MenuBar {
Menu { Menu {
title: "File" title: "File"
/*
MenuItem { MenuItem {
text: "Import App" text: "Import App"
shortcut: "Ctrl+o" shortcut: "Ctrl+o"
onTriggered: openAppDialog.open() onTriggered: openAppDialog.open()
} }
*/
} }
Menu { Menu {

@ -79,6 +79,10 @@ ApplicationWindow {
function postData(seed, data) { function postData(seed, data) {
webview.experimental.postMessage(JSON.stringify({data: data, _seed: seed})) webview.experimental.postMessage(JSON.stringify({data: data, _seed: seed}))
} }
function onNewBlockCb(block) {
webview.experimental.postMessage(JSON.stringify({data: block, _event: "block:new"}))
}
} }
Rectangle { Rectangle {

@ -23,11 +23,16 @@ function tests() {
function init() { function init() {
eth.getKey(function(key) { eth.getKey(function(key) {
eth.getStorage(jefcoinAddr, key, function(storage) {
document.querySelector("#currentAmount").innerHTML = "Amount: " + storage;
});
eth.on("block:new", function() {
eth.getStorage(jefcoinAddr, key, function(storage) { eth.getStorage(jefcoinAddr, key, function(storage) {
debug("Currently in storage: ", storage);
document.querySelector("#currentAmount").innerHTML = "Amount: " + storage; document.querySelector("#currentAmount").innerHTML = "Amount: " + storage;
}) });
}) });
});
} }
</script> </script>