github.com/deso-protocol/core@v1.2.9/lib/event_manager.go (about)

     1  package lib
     2  
     3  type TransactionEventFunc func(event *TransactionEvent)
     4  type BlockEventFunc func(event *BlockEvent)
     5  
     6  type TransactionEvent struct {
     7  	Txn     *MsgDeSoTxn
     8  	TxnHash *BlockHash
     9  
    10  	// Optional
    11  	UtxoView *UtxoView
    12  	UtxoOps  []*UtxoOperation
    13  }
    14  
    15  type BlockEvent struct {
    16  	Block *MsgDeSoBlock
    17  
    18  	// Optional
    19  	UtxoView *UtxoView
    20  	UtxoOps  [][]*UtxoOperation
    21  }
    22  
    23  type EventManager struct {
    24  	transactionConnectedHandlers []TransactionEventFunc
    25  	blockConnectedHandlers       []BlockEventFunc
    26  	blockDisconnectedHandlers    []BlockEventFunc
    27  	blockAcceptedHandlers        []BlockEventFunc
    28  }
    29  
    30  func NewEventManager() *EventManager {
    31  	return &EventManager{}
    32  }
    33  
    34  func (em *EventManager) OnTransactionConnected(handler TransactionEventFunc) {
    35  	em.transactionConnectedHandlers = append(em.transactionConnectedHandlers, handler)
    36  }
    37  
    38  func (em *EventManager) transactionConnected(event *TransactionEvent) {
    39  	for _, handler := range em.transactionConnectedHandlers {
    40  		handler(event)
    41  	}
    42  }
    43  
    44  func (em *EventManager) OnBlockConnected(handler BlockEventFunc) {
    45  	em.blockConnectedHandlers = append(em.blockConnectedHandlers, handler)
    46  }
    47  
    48  func (em *EventManager) blockConnected(event *BlockEvent) {
    49  	for _, handler := range em.blockConnectedHandlers {
    50  		handler(event)
    51  	}
    52  }
    53  
    54  func (em *EventManager) OnBlockDisconnected(handler BlockEventFunc) {
    55  	em.blockDisconnectedHandlers = append(em.blockDisconnectedHandlers, handler)
    56  }
    57  
    58  func (em *EventManager) blockDisconnected(event *BlockEvent) {
    59  	for _, handler := range em.blockDisconnectedHandlers {
    60  		handler(event)
    61  	}
    62  }
    63  
    64  func (em *EventManager) OnBlockAccepted(handler BlockEventFunc) {
    65  	em.blockAcceptedHandlers = append(em.blockAcceptedHandlers, handler)
    66  }
    67  
    68  func (em *EventManager) blockAccepted(event *BlockEvent) {
    69  	for _, handler := range em.blockAcceptedHandlers {
    70  		handler(event)
    71  	}
    72  }