-
Notifications
You must be signed in to change notification settings - Fork 52
/
event.go
57 lines (47 loc) · 1.74 KB
/
event.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
// Copyright (c) 2024 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.
package popm
import (
"github.com/hemilabs/heminetwork/hemi"
)
// EventHandler is a function that can handle an event.
type EventHandler = func(event EventType, data any)
// EventType represents a type of event.
type EventType int
const (
// EventTypeMineKeystone is an event dispatched when a L2 keystone is being
// mined.
EventTypeMineKeystone EventType = iota + 1
// EventTypeTransactionBroadcast is an event dispatched when a Bitcoin
// transaction has been broadcast to the network.
EventTypeTransactionBroadcast
)
// EventMineKeystone is the data for EventTypeMineKeystone.
type EventMineKeystone struct {
Keystone *hemi.L2Keystone
}
// EventTransactionBroadcast is the data for EventTypeTransactionBroadcast.
type EventTransactionBroadcast struct {
Keystone *hemi.L2Keystone
TxHash string
}
// RegisterEventHandler registers an event handler to receive all events
// dispatched by the miner. The dispatched events can be filtered by EventType
// when received.
func (m *Miner) RegisterEventHandler(handler EventHandler) {
m.eventHandlersMtx.Lock()
defer m.eventHandlersMtx.Unlock()
m.eventHandlers = append(m.eventHandlers, handler)
}
// dispatchEvent calls all registered event handlers with the given eventType
// and data. It is recommended to call this function in a go routine to avoid
// blocking operation while the event is being dispatched, as all event handlers
// will be executed synchronously.
func (m *Miner) dispatchEvent(eventType EventType, data any) {
m.eventHandlersMtx.RLock()
defer m.eventHandlersMtx.RUnlock()
for _, handler := range m.eventHandlers {
handler(eventType, data)
}
}