2024-10-15 15:59:17 +00:00
|
|
|
package mock_common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2024-10-23 20:33:05 +00:00
|
|
|
"github.com/status-im/status-go/common"
|
2024-10-15 15:59:17 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FeedSubscription struct {
|
|
|
|
events chan walletevent.Event
|
|
|
|
feed *event.Feed
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFeedSubscription(feed *event.Feed) *FeedSubscription {
|
|
|
|
events := make(chan walletevent.Event, 100)
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
subscription := feed.Subscribe(events)
|
|
|
|
|
|
|
|
go func() {
|
2024-10-23 20:33:05 +00:00
|
|
|
defer common.LogOnPanic()
|
2024-10-15 15:59:17 +00:00
|
|
|
<-done
|
|
|
|
subscription.Unsubscribe()
|
|
|
|
close(events)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return &FeedSubscription{events: events, feed: feed, done: done}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FeedSubscription) WaitForEvent(timeout time.Duration) (walletevent.Event, bool) {
|
|
|
|
select {
|
|
|
|
case evt := <-f.events:
|
|
|
|
return evt, true
|
|
|
|
case <-time.After(timeout):
|
|
|
|
return walletevent.Event{}, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FeedSubscription) GetFeed() *event.Feed {
|
|
|
|
return f.feed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FeedSubscription) Close() {
|
|
|
|
close(f.done)
|
|
|
|
}
|