forked from cerc-io/plugeth
Implemented new watch
* Old watch methods have been removed * Implemented new callback handlers for onWatchCb * added new Filter JS object
This commit is contained in:
parent
1fd69e9569
commit
aadc5be3ff
@ -137,6 +137,7 @@ window.eth = {
|
|||||||
postData({call: "getSecretToAddress", args: [sec]}, cb);
|
postData({call: "getSecretToAddress", args: [sec]}, cb);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
watch: function(address, storageAddrOrCb, cb) {
|
watch: function(address, storageAddrOrCb, cb) {
|
||||||
var ev;
|
var ev;
|
||||||
if(cb === undefined) {
|
if(cb === undefined) {
|
||||||
@ -166,6 +167,16 @@ window.eth = {
|
|||||||
|
|
||||||
postData({call: "disconnect", args: [address, storageAddrOrCb]});
|
postData({call: "disconnect", args: [address, storageAddrOrCb]});
|
||||||
},
|
},
|
||||||
|
*/
|
||||||
|
|
||||||
|
watch: function(options) {
|
||||||
|
var filter = new Filter(options);
|
||||||
|
filter.number = newWatchNum().toString()
|
||||||
|
|
||||||
|
postData({call: "watch", args: [options, filter.number]})
|
||||||
|
|
||||||
|
return filter;
|
||||||
|
},
|
||||||
|
|
||||||
set: function(props) {
|
set: function(props) {
|
||||||
postData({call: "set", args: props});
|
postData({call: "set", args: props});
|
||||||
@ -208,11 +219,28 @@ window.eth = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.eth._callbacks = {}
|
window.eth._callbacks = {}
|
||||||
window.eth._onCallbacks = {}
|
window.eth._onCallbacks = {}
|
||||||
|
|
||||||
|
var Filter = function(options) {
|
||||||
|
this.options = options;
|
||||||
|
};
|
||||||
|
|
||||||
|
Filter.prototype.changed = function(callback) {
|
||||||
|
eth.on("watched:"+this.number, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
Filter.prototype.getMessages = function(cb) {
|
||||||
|
return eth.getMessages(this.options, cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
var watchNum = 0;
|
||||||
|
function newWatchNum() {
|
||||||
|
return watchNum++;
|
||||||
|
}
|
||||||
|
|
||||||
function postData(data, cb) {
|
function postData(data, cb) {
|
||||||
data._seed = Math.floor(Math.random() * 1000000)
|
data._seed = Math.floor(Math.random() * 1000000)
|
||||||
if(cb) {
|
if(cb) {
|
||||||
|
30
ethereal/assets/ext/test.html
Normal file
30
ethereal/assets/ext/test.html
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<!doctype>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Tests</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<button onclick="test();">Test me</button>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
function test() {
|
||||||
|
var filter = eth.watch({
|
||||||
|
latest: -1,
|
||||||
|
from: "e6716f9544a56c530d868e4bfbacb172315bdead",
|
||||||
|
});
|
||||||
|
|
||||||
|
filter.changed(function(messages) {
|
||||||
|
console.log("messages", messages)
|
||||||
|
})
|
||||||
|
|
||||||
|
filter.getMessages(function(messages) {
|
||||||
|
console.log("getMessages", messages)
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
@ -220,11 +220,16 @@ ApplicationWindow {
|
|||||||
postData(data._seed, key)
|
postData(data._seed, key)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
/*
|
||||||
case "watch":
|
case "watch":
|
||||||
require(1)
|
require(1)
|
||||||
eth.watch(data.args[0], data.args[1]);
|
eth.watch(data.args[0], data.args[1]);
|
||||||
|
|
||||||
break
|
break
|
||||||
|
*/
|
||||||
|
case "watch":
|
||||||
|
require(2)
|
||||||
|
eth.watch(data.args[0], data.args[1])
|
||||||
|
|
||||||
case "disconnect":
|
case "disconnect":
|
||||||
require(1)
|
require(1)
|
||||||
@ -269,6 +274,11 @@ ApplicationWindow {
|
|||||||
webview.experimental.postMessage(JSON.stringify({data: data, _event: event}))
|
webview.experimental.postMessage(JSON.stringify({data: data, _event: event}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onWatchedCb(data, id) {
|
||||||
|
var messages = JSON.parse(data)
|
||||||
|
postEvent("watched:"+id, messages)
|
||||||
|
}
|
||||||
|
|
||||||
function onNewBlockCb(block) {
|
function onNewBlockCb(block) {
|
||||||
postEvent("block:new", block)
|
postEvent("block:new", block)
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/ethereum/eth-go/ethpub"
|
"github.com/ethereum/eth-go/ethpub"
|
||||||
"github.com/ethereum/eth-go/ethreact"
|
"github.com/ethereum/eth-go/ethreact"
|
||||||
"github.com/ethereum/eth-go/ethstate"
|
"github.com/ethereum/eth-go/ethstate"
|
||||||
"github.com/ethereum/eth-go/ethutil"
|
|
||||||
"github.com/ethereum/go-ethereum/javascript"
|
"github.com/ethereum/go-ethereum/javascript"
|
||||||
"github.com/go-qml/qml"
|
"github.com/go-qml/qml"
|
||||||
)
|
)
|
||||||
@ -23,6 +22,7 @@ type AppContainer interface {
|
|||||||
ObjectChanged(*ethstate.StateObject)
|
ObjectChanged(*ethstate.StateObject)
|
||||||
StorageChanged(*ethstate.StorageState)
|
StorageChanged(*ethstate.StorageState)
|
||||||
NewWatcher(chan bool)
|
NewWatcher(chan bool)
|
||||||
|
Messages(ethstate.Messages, string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExtApplication struct {
|
type ExtApplication struct {
|
||||||
@ -31,9 +31,12 @@ type ExtApplication struct {
|
|||||||
|
|
||||||
blockChan chan ethreact.Event
|
blockChan chan ethreact.Event
|
||||||
changeChan chan ethreact.Event
|
changeChan chan ethreact.Event
|
||||||
|
messageChan chan ethreact.Event
|
||||||
quitChan chan bool
|
quitChan chan bool
|
||||||
watcherQuitChan chan bool
|
watcherQuitChan chan bool
|
||||||
|
|
||||||
|
filters map[string]*ethchain.Filter
|
||||||
|
|
||||||
container AppContainer
|
container AppContainer
|
||||||
lib *UiLib
|
lib *UiLib
|
||||||
registeredEvents []string
|
registeredEvents []string
|
||||||
@ -45,8 +48,10 @@ func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication {
|
|||||||
lib.eth,
|
lib.eth,
|
||||||
make(chan ethreact.Event, 100),
|
make(chan ethreact.Event, 100),
|
||||||
make(chan ethreact.Event, 100),
|
make(chan ethreact.Event, 100),
|
||||||
|
make(chan ethreact.Event, 100),
|
||||||
make(chan bool),
|
make(chan bool),
|
||||||
make(chan bool),
|
make(chan bool),
|
||||||
|
make(map[string]*ethchain.Filter),
|
||||||
container,
|
container,
|
||||||
lib,
|
lib,
|
||||||
nil,
|
nil,
|
||||||
@ -73,6 +78,7 @@ func (app *ExtApplication) run() {
|
|||||||
// Subscribe to events
|
// Subscribe to events
|
||||||
reactor := app.lib.eth.Reactor()
|
reactor := app.lib.eth.Reactor()
|
||||||
reactor.Subscribe("newBlock", app.blockChan)
|
reactor.Subscribe("newBlock", app.blockChan)
|
||||||
|
reactor.Subscribe("messages", app.messageChan)
|
||||||
|
|
||||||
app.container.NewWatcher(app.watcherQuitChan)
|
app.container.NewWatcher(app.watcherQuitChan)
|
||||||
|
|
||||||
@ -118,56 +124,27 @@ out:
|
|||||||
} else if storageObject, ok := object.Resource.(*ethstate.StorageState); ok {
|
} else if storageObject, ok := object.Resource.(*ethstate.StorageState); ok {
|
||||||
app.container.StorageChanged(storageObject)
|
app.container.StorageChanged(storageObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case msg := <-app.messageChan:
|
||||||
|
if messages, ok := msg.Resource.(ethstate.Messages); ok {
|
||||||
|
for id, filter := range app.filters {
|
||||||
|
msgs := filter.FilterMessages(messages)
|
||||||
|
if len(msgs) > 0 {
|
||||||
|
app.container.Messages(msgs, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *ExtApplication) Watch(addr, storageAddr string) {
|
func (self *ExtApplication) Watch(filterOptions map[string]interface{}, identifier string) {
|
||||||
var event string
|
self.filters[identifier] = ethchain.NewFilterFromMap(filterOptions, self.eth)
|
||||||
if len(storageAddr) == 0 {
|
|
||||||
event = "object:" + string(ethutil.Hex2Bytes(addr))
|
|
||||||
app.lib.eth.Reactor().Subscribe(event, app.changeChan)
|
|
||||||
} else {
|
|
||||||
event = "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr))
|
|
||||||
app.lib.eth.Reactor().Subscribe(event, app.changeChan)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.registeredEvents = append(app.registeredEvents, event)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *ExtApplication) GetMessages(object map[string]interface{}) string {
|
func (self *ExtApplication) GetMessages(object map[string]interface{}) string {
|
||||||
filter := ethchain.NewFilter(self.eth)
|
filter := ethchain.NewFilterFromMap(object, self.eth)
|
||||||
|
|
||||||
if object["earliest"] != nil {
|
|
||||||
earliest := object["earliest"]
|
|
||||||
if e, ok := earliest.(string); ok {
|
|
||||||
filter.SetEarliestBlock(ethutil.Hex2Bytes(e))
|
|
||||||
} else {
|
|
||||||
filter.SetEarliestBlock(earliest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if object["latest"] != nil {
|
|
||||||
latest := object["latest"]
|
|
||||||
if l, ok := latest.(string); ok {
|
|
||||||
filter.SetLatestBlock(ethutil.Hex2Bytes(l))
|
|
||||||
} else {
|
|
||||||
filter.SetLatestBlock(latest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if object["to"] != nil {
|
|
||||||
filter.AddTo(ethutil.Hex2Bytes(object["to"].(string)))
|
|
||||||
}
|
|
||||||
if object["from"] != nil {
|
|
||||||
filter.AddFrom(ethutil.Hex2Bytes(object["from"].(string)))
|
|
||||||
}
|
|
||||||
if object["max"] != nil {
|
|
||||||
filter.SetMax(object["max"].(int))
|
|
||||||
}
|
|
||||||
if object["skip"] != nil {
|
|
||||||
filter.SetSkip(object["skip"].(int))
|
|
||||||
}
|
|
||||||
|
|
||||||
messages := filter.Find()
|
messages := filter.Find()
|
||||||
var msgs []javascript.JSMessage
|
var msgs []javascript.JSMessage
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -12,6 +13,7 @@ import (
|
|||||||
"github.com/ethereum/eth-go/ethpub"
|
"github.com/ethereum/eth-go/ethpub"
|
||||||
"github.com/ethereum/eth-go/ethstate"
|
"github.com/ethereum/eth-go/ethstate"
|
||||||
"github.com/ethereum/eth-go/ethutil"
|
"github.com/ethereum/eth-go/ethutil"
|
||||||
|
"github.com/ethereum/go-ethereum/javascript"
|
||||||
"github.com/go-qml/qml"
|
"github.com/go-qml/qml"
|
||||||
"github.com/howeyc/fsnotify"
|
"github.com/howeyc/fsnotify"
|
||||||
)
|
)
|
||||||
@ -131,6 +133,17 @@ func (app *HtmlApplication) StorageChanged(storageObject *ethstate.StorageState)
|
|||||||
app.webView.Call("onStorageChangeCb", ethpub.NewPStorageState(storageObject))
|
app.webView.Call("onStorageChangeCb", ethpub.NewPStorageState(storageObject))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *HtmlApplication) Messages(messages ethstate.Messages, id string) {
|
||||||
|
var msgs []javascript.JSMessage
|
||||||
|
for _, m := range messages {
|
||||||
|
msgs = append(msgs, javascript.NewJSMessage(m))
|
||||||
|
}
|
||||||
|
|
||||||
|
b, _ := json.Marshal(msgs)
|
||||||
|
|
||||||
|
self.webView.Call("onWatchedCb", string(b), id)
|
||||||
|
}
|
||||||
|
|
||||||
func (app *HtmlApplication) Destroy() {
|
func (app *HtmlApplication) Destroy() {
|
||||||
app.engine.Destroy()
|
app.engine.Destroy()
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/ethereum/eth-go/ethchain"
|
"github.com/ethereum/eth-go/ethchain"
|
||||||
"github.com/ethereum/eth-go/ethpub"
|
"github.com/ethereum/eth-go/ethpub"
|
||||||
"github.com/ethereum/eth-go/ethstate"
|
"github.com/ethereum/eth-go/ethstate"
|
||||||
"github.com/ethereum/eth-go/ethutil"
|
"github.com/ethereum/eth-go/ethutil"
|
||||||
"github.com/go-qml/qml"
|
"github.com/go-qml/qml"
|
||||||
"runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type QmlApplication struct {
|
type QmlApplication struct {
|
||||||
@ -59,6 +61,10 @@ func (app *QmlApplication) StorageChanged(storageObject *ethstate.StorageState)
|
|||||||
app.win.Call("onStorageChangeCb", ethpub.NewPStorageState(storageObject))
|
app.win.Call("onStorageChangeCb", ethpub.NewPStorageState(storageObject))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *QmlApplication) Messages(msgs ethstate.Messages, id string) {
|
||||||
|
fmt.Println("IMPLEMENT QML APPLICATION MESSAGES METHOD")
|
||||||
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
func (app *QmlApplication) Engine() *qml.Engine {
|
func (app *QmlApplication) Engine() *qml.Engine {
|
||||||
return app.engine
|
return app.engine
|
||||||
|
Loading…
Reference in New Issue
Block a user