github.com/koko1123/flow-go-1@v0.29.6/state/protocol/events/distributor.go (about) 1 package events 2 3 import ( 4 "sync" 5 6 "github.com/koko1123/flow-go-1/model/flow" 7 "github.com/koko1123/flow-go-1/state/protocol" 8 ) 9 10 // Distributor distributes events to a list of subscribers. 11 type Distributor struct { 12 subscribers []protocol.Consumer 13 mu sync.RWMutex 14 } 15 16 // NewDistributor returns a new events distributor. 17 func NewDistributor() *Distributor { 18 return &Distributor{} 19 } 20 21 func (d *Distributor) AddConsumer(consumer protocol.Consumer) { 22 d.mu.Lock() 23 defer d.mu.Unlock() 24 d.subscribers = append(d.subscribers, consumer) 25 } 26 27 func (d *Distributor) BlockFinalized(block *flow.Header) { 28 d.mu.RLock() 29 defer d.mu.RUnlock() 30 for _, sub := range d.subscribers { 31 sub.BlockFinalized(block) 32 } 33 } 34 35 func (d *Distributor) BlockProcessable(block *flow.Header) { 36 d.mu.RLock() 37 defer d.mu.RUnlock() 38 for _, sub := range d.subscribers { 39 sub.BlockProcessable(block) 40 } 41 } 42 43 func (d *Distributor) EpochTransition(newEpoch uint64, first *flow.Header) { 44 d.mu.RLock() 45 defer d.mu.RUnlock() 46 for _, sub := range d.subscribers { 47 sub.EpochTransition(newEpoch, first) 48 } 49 } 50 51 func (d *Distributor) EpochSetupPhaseStarted(epoch uint64, first *flow.Header) { 52 d.mu.RLock() 53 defer d.mu.RUnlock() 54 for _, sub := range d.subscribers { 55 sub.EpochSetupPhaseStarted(epoch, first) 56 } 57 } 58 59 func (d *Distributor) EpochCommittedPhaseStarted(epoch uint64, first *flow.Header) { 60 d.mu.RLock() 61 defer d.mu.RUnlock() 62 for _, sub := range d.subscribers { 63 sub.EpochCommittedPhaseStarted(epoch, first) 64 } 65 }