-
Notifications
You must be signed in to change notification settings - Fork 244
/
transmitter.go
65 lines (56 loc) · 1.3 KB
/
transmitter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package walletevent
import (
"sync"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/signal"
)
type Publisher interface {
Subscribe(interface{}) event.Subscription
}
// SignalsTransmitter transmits received events as wallet signals.
type SignalsTransmitter struct {
Publisher
wg sync.WaitGroup
quit chan struct{}
}
// Start runs loop in background.
func (tmr *SignalsTransmitter) Start() error {
if tmr.quit != nil {
// already running, nothing to do
return nil
}
tmr.quit = make(chan struct{})
events := make(chan Event, 10)
sub := tmr.Publisher.Subscribe(events)
tmr.wg.Add(1)
go func() {
defer tmr.wg.Done()
for {
select {
case <-tmr.quit:
sub.Unsubscribe()
return
case err := <-sub.Err():
// technically event.Feed cannot send an error to subscription.Err channel.
// the only time we will get an event is when that channel is closed.
if err != nil {
log.Error("wallet signals transmitter failed with", "error", err)
}
return
case event := <-events:
signal.SendWalletEvent(event)
}
}
}()
return nil
}
// Stop stops the loop and waits till it exits.
func (tmr *SignalsTransmitter) Stop() {
if tmr.quit == nil {
return
}
close(tmr.quit)
tmr.wg.Wait()
tmr.quit = nil
}