forked from joeblew99/gu
/
viewupdate_event.go
122 lines (99 loc) · 3.35 KB
/
viewupdate_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gu
import "sync"
// ViewUpdateSubscriber defines a interface that which is used to subscribe specifically for
// events ViewUpdate type.
type ViewUpdateSubscriber interface {
Receive(ViewUpdate)
}
//=========================================================================================================
// ViewUpdateHandler defines a structure type which implements the
// ViewUpdateSubscriber interface and the EventDistributor interface.
type ViewUpdateHandler struct {
handle func(ViewUpdate)
}
// NewViewUpdateHandler returns a new instance of a ViewUpdateHandler.
func NewViewUpdateHandler(fn func(ViewUpdate)) *ViewUpdateHandler {
return &ViewUpdateHandler{
handle: fn,
}
}
// Receive takes the giving value and execute it against the underline handler.
func (sn *ViewUpdateHandler) Receive(elem ViewUpdate) {
sn.handle(elem)
}
// Handle takes the giving value and asserts the expected value to match the
// ViewUpdate type then passes it to the Receive method.
func (sn *ViewUpdateHandler) Handle(receive interface{}) {
if elem, ok := receive.(ViewUpdate); ok {
sn.Receive(elem)
}
}
//=========================================================================================================
// ViewUpdateNotification defines a structure type which must be used to
// receive ViewUpdate type has a event.
type ViewUpdateNotification struct {
sml sync.Mutex
subs []ViewUpdateSubscriber
validation func(ViewUpdate) bool
register map[ViewUpdateSubscriber]int
}
// NewViewUpdateNotificationWith returns a new instance of ViewUpdateNotification.
func NewViewUpdateNotificationWith(validation func(ViewUpdate) bool) *ViewUpdateNotification {
var elem ViewUpdateNotification
elem.validation = validation
elem.register = make(map[ViewUpdateSubscriber]int, 0)
return &elem
}
// NewViewUpdateNotification returns a new instance of NewViewUpdateNotification.
func NewViewUpdateNotification() *ViewUpdateNotification {
var elem ViewUpdateNotification
elem.register = make(map[ViewUpdateSubscriber]int, 0)
return &elem
}
// UnNotify removes the given subscriber from the notification's list if found from future events.
func (sn *ViewUpdateNotification) UnNotify(sub ViewUpdateSubscriber) {
sn.do(func() {
index, ok := sn.register[sub]
if !ok {
return
}
sn.subs = append(sn.subs[:index], sn.subs[index+1:]...)
})
}
// Notify adds the given subscriber into the notification list and will await an update of
// a new event of the given ViewUpdate type.
func (sn *ViewUpdateNotification) Notify(sub ViewUpdateSubscriber) {
sn.do(func() {
sn.register[sub] = len(sn.subs)
sn.subs = append(sn.subs, sub)
})
}
// Handle takes the giving value and asserts the expected value to be of
// the type and pass on to it's underline subscribers else ignoring the event.
func (sn *ViewUpdateNotification) Handle(elem interface{}) {
if elemEvent, ok := elem.(ViewUpdate); ok {
if sn.validation != nil && sn.validation(elemEvent) {
sn.do(func() {
for _, sub := range sn.subs {
sub.Receive(elemEvent)
}
})
return
}
sn.do(func() {
for _, sub := range sn.subs {
sub.Receive(elemEvent)
}
})
}
}
// do performs action with the mutex locked and unlocked appropriately, ensuring safe
// concurrent access.
func (sn *ViewUpdateNotification) do(fn func()) {
if fn == nil {
return
}
sn.sml.Lock()
defer sn.sml.Unlock()
fn()
}