-
Notifications
You must be signed in to change notification settings - Fork 2
/
singleton.go
52 lines (44 loc) · 1.75 KB
/
singleton.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
package eventbus
var (
// singleton is a pointer to a unbuffered EventBus instance, which will be created when necessary.
singleton *EventBus
)
func init() {
ResetSingleton()
}
// ResetSingleton resets the singleton object. If the singleton object is not nil,
// it first closes the old singleton, and then creates a new singleton instance.
func ResetSingleton() {
if singleton != nil {
singleton.Close()
}
singleton = New()
}
// Unsubscribe removes handler defined for a topic.
// Returns error if there are no handlers subscribed to the topic.
func Unsubscribe(topic string, handler any) error {
return singleton.Unsubscribe(topic, handler)
}
// Subscribe subscribes to a topic, return an error if the handler is not a function.
// The handler must have two parameters: the first parameter must be a string,
// and the type of the handler's second parameter must be consistent with the type of the payload in `Publish()`
func Subscribe(topic string, handler any) error {
return singleton.Subscribe(topic, handler)
}
// Publish triggers the handlers defined for a topic. The `payload` argument will be passed to the handler.
// The type of the payload must correspond to the second parameter of the handler in `Subscribe()`.
func Publish(topic string, payload any) error {
return singleton.Publish(topic, payload)
}
// PublishSync is a synchronous version of Publish that triggers the handlers defined for a topic with the given payload.
// The type of the payload must correspond to the second parameter of the handler in `Subscribe()`.
func PublishSync(topic string, payload any) error {
return singleton.Publish(topic, payload)
}
// Close closes the singleton instance of EventBus.
func Close() {
if singleton != nil {
singleton.Close()
singleton = nil
}
}