From bbde892d5012203bb984b83fcb2fe11467841643 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 24 Apr 2014 14:43:00 +0200 Subject: [PATCH] Added callback mechanism and updated UI * UI Now updates when a new block has been broadcasted * Added a on, off and trigger --- ethereal/assets/ethereum.js | 65 +++++++++++++++---- .../qml/newTransaction/_new_contract.qml | 1 + ethereal/assets/qml/wallet.qml | 2 - ethereal/assets/qml/webapp.qml | 4 ++ ethereal/assets/test.html | 11 +++- 5 files changed, 64 insertions(+), 19 deletions(-) diff --git a/ethereal/assets/ethereum.js b/ethereal/assets/ethereum.js index 74f8519363..fefad584a0 100644 --- a/ethereal/assets/ethereum.js +++ b/ethereal/assets/ethereum.js @@ -6,7 +6,7 @@ function postData(data, cb) { } if(data.args === undefined) { - data.args = [] + data.args = []; } navigator.qt.postMessage(JSON.stringify(data)); @@ -24,11 +24,11 @@ window.eth = { getBlock: function(numberOrHash, cb) { var func; if(typeof numberOrHash == "string") { - func = "getBlockByHash" + func = "getBlockByHash"; } else { - func = "getBlockByNumber" + func = "getBlockByNumber"; } - postData({call: func, args: [numberOrHash]}, cb) + postData({call: func, args: [numberOrHash]}, cb); }, // Create transaction @@ -36,18 +36,51 @@ window.eth = { // Creates a transaction with the current account // If no recipient is set, the Ethereum API will see it as a contract creation 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) { - postData({call: "getStorage", args: [address, storageAddress]}, cb) + postData({call: "getStorage", args: [address, storageAddress]}, 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._onCallbacks = {} function debug(/**/) { var args = arguments; @@ -66,13 +99,17 @@ function debug(/**/) { navigator.qt.onmessage = function(ev) { var data = JSON.parse(ev.data) - if(data._seed) { - var cb = eth._callbacks[data._seed]; - if(cb) { - // Call the callback - cb(data.data); - // Remove the "trigger" callback - delete eth._callbacks[ev._seed]; + if(data._event !== undefined) { + eth.trigger(data._event, data.data); + } else { + if(data._seed) { + var cb = eth._callbacks[data._seed]; + if(cb) { + // Call the callback + cb(data.data); + // Remove the "trigger" callback + delete eth._callbacks[ev._seed]; + } } } } diff --git a/ethereal/assets/qml/newTransaction/_new_contract.qml b/ethereal/assets/qml/newTransaction/_new_contract.qml index 29e26a5621..0794d3dcdc 100644 --- a/ethereal/assets/qml/newTransaction/_new_contract.qml +++ b/ethereal/assets/qml/newTransaction/_new_contract.qml @@ -117,6 +117,7 @@ Component { TextArea { id: codeView + height: 300 anchors.topMargin: 5 Layout.fillWidth: true width: parent.width /2 diff --git a/ethereal/assets/qml/wallet.qml b/ethereal/assets/qml/wallet.qml index ed06f3518a..574fbef861 100644 --- a/ethereal/assets/qml/wallet.qml +++ b/ethereal/assets/qml/wallet.qml @@ -18,13 +18,11 @@ ApplicationWindow { MenuBar { Menu { title: "File" - /* MenuItem { text: "Import App" shortcut: "Ctrl+o" onTriggered: openAppDialog.open() } - */ } Menu { diff --git a/ethereal/assets/qml/webapp.qml b/ethereal/assets/qml/webapp.qml index ee7dea0ca5..9cf154e9b1 100644 --- a/ethereal/assets/qml/webapp.qml +++ b/ethereal/assets/qml/webapp.qml @@ -79,6 +79,10 @@ ApplicationWindow { function postData(seed, data) { webview.experimental.postMessage(JSON.stringify({data: data, _seed: seed})) } + + function onNewBlockCb(block) { + webview.experimental.postMessage(JSON.stringify({data: block, _event: "block:new"})) + } } Rectangle { diff --git a/ethereal/assets/test.html b/ethereal/assets/test.html index 1cfcad6bb1..476283f60e 100644 --- a/ethereal/assets/test.html +++ b/ethereal/assets/test.html @@ -23,11 +23,16 @@ function tests() { function init() { 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) { - debug("Currently in storage: ", storage); document.querySelector("#currentAmount").innerHTML = "Amount: " + storage; - }) - }) + }); + }); + }); }