erigon-pulse/ethdb/remote/remotedbserver/events.go
ledgerwatch a583f7f6ca
Try to fix multiple head subscriptions (#1565)
Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
2021-03-19 17:36:38 +00:00

39 lines
868 B
Go

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 {
headerSubscriptions []HeaderSubscription
}
func NewEvents() *Events {
return &Events{}
}
func (e *Events) AddHeaderSubscription(s HeaderSubscription) {
e.headerSubscriptions = append(e.headerSubscriptions, s)
}
func (e *Events) OnNewHeader(newHeader *types.Header) {
for i, sub := range e.headerSubscriptions {
if err := sub(newHeader); err != nil {
// remove subscription
if i == len(e.headerSubscriptions)-1 {
e.headerSubscriptions = e.headerSubscriptions[:i]
} else if i < len(e.headerSubscriptions)-1 {
e.headerSubscriptions = append(e.headerSubscriptions[:i], e.headerSubscriptions[i+1:]...)
}
}
}
}