-
Notifications
You must be signed in to change notification settings - Fork 2
/
subscribe.go
50 lines (40 loc) · 1.19 KB
/
subscribe.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
package magicbus
import (
"github.com/grrtrr/magicbus/event"
uuid "github.com/satori/go.uuid"
)
// Each subscription is identified by a cluster-unique ID
type SubscriptionID uuid.UUID
// NewSubscriptionID returns a new, cluster-unique subscription ID
func NewSubscriptionID() SubscriptionID {
return SubscriptionID(uuid.NewV1())
}
func (s SubscriptionID) String() string {
return uuid.UUID(s).String()
}
func (s SubscriptionID) IsZero() bool {
return uuid.Equal(uuid.UUID(s), uuid.Nil)
}
// Observer subscribes @hdlr to receive immediate notification of events.
func Observer(hdlr event.Handler) (SubscriptionID, error) {
return localBus.observer(hdlr)
}
// Unsubscribe removes subscription @id from the local bus.
func Unsubscribe(id SubscriptionID) error {
return localBus.unsubscribe(id)
}
// Add new observer to @m
func (m *MagicBus) observer(hdlr event.Handler) (SubscriptionID, error) {
var id = NewSubscriptionID()
return id, <-m.Action(func() error {
m.observers[id.String()] = hdlr
return nil
})
}
// Remove subscription records of @id
func (m *MagicBus) unsubscribe(id SubscriptionID) error {
return <-m.Action(func() error {
delete(m.observers, id.String())
return nil
})
}