2020-11-17 19:13:41 +00:00
|
|
|
package remotedbserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RpcEventType uint64
|
|
|
|
|
|
|
|
const (
|
|
|
|
EventTypeHeader = RpcEventType(iota)
|
|
|
|
)
|
|
|
|
|
|
|
|
type HeaderSubscription func(*types.Header) error
|
|
|
|
|
|
|
|
type Events struct {
|
2021-02-12 16:46:50 +00:00
|
|
|
headerSubscriptions []HeaderSubscription
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEvents() *Events {
|
|
|
|
return &Events{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) AddHeaderSubscription(s HeaderSubscription) {
|
2021-02-12 16:46:50 +00:00
|
|
|
e.headerSubscriptions = append(e.headerSubscriptions, s)
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) OnNewHeader(newHeader *types.Header) {
|
2021-02-12 16:46:50 +00:00
|
|
|
for i, sub := range e.headerSubscriptions {
|
|
|
|
if err := sub(newHeader); err != nil {
|
|
|
|
// remove subscription
|
2021-03-02 22:47:44 +00:00
|
|
|
if i == len(e.headerSubscriptions)-1 {
|
|
|
|
e.headerSubscriptions = e.headerSubscriptions[:i]
|
2021-03-19 17:36:38 +00:00
|
|
|
} else if i < len(e.headerSubscriptions)-1 {
|
2021-03-02 22:47:44 +00:00
|
|
|
e.headerSubscriptions = append(e.headerSubscriptions[:i], e.headerSubscriptions[i+1:]...)
|
|
|
|
}
|
2021-02-12 16:46:50 +00:00
|
|
|
}
|
2020-11-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
}
|