-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
61 lines (52 loc) · 1.33 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
58
59
60
61
package emitter
import "sync"
// Event is an interface representing the structure of an event.
type Event interface {
Topic() string
Payload() interface{}
SetPayload(interface{})
SetAborted(bool)
IsAborted() bool
}
// BaseEvent provides a basic implementation of the Event interface.
type BaseEvent struct {
topic string
payload interface{}
aborted bool
mu sync.RWMutex // Changed from sync.Mutex to sync.RWMutex
}
// NewBaseEvent creates a new instance of BaseEvent with a payload.
func NewBaseEvent(topic string, payload interface{}) *BaseEvent {
return &BaseEvent{
topic: topic,
payload: payload,
}
}
// Topic returns the event's topic.
func (e *BaseEvent) Topic() string {
return e.topic
}
// Payload returns the event's payload.
func (e *BaseEvent) Payload() interface{} {
e.mu.RLock() // Read lock
defer e.mu.RUnlock()
return e.payload
}
// SetPayload sets the event's payload.
func (e *BaseEvent) SetPayload(payload interface{}) {
e.mu.Lock() // Write lock
defer e.mu.Unlock()
e.payload = payload
}
// SetAborted sets the event's aborted status.
func (e *BaseEvent) SetAborted(abort bool) {
e.mu.Lock() // Write lock
defer e.mu.Unlock()
e.aborted = abort
}
// IsAborted checks the event's aborted status.
func (e *BaseEvent) IsAborted() bool {
e.mu.RLock() // Read lock
defer e.mu.RUnlock()
return e.aborted
}