-
Notifications
You must be signed in to change notification settings - Fork 0
/
center.go
71 lines (60 loc) · 1.85 KB
/
center.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
//
// @project geniusrabbit.com 2016 – 2017, 2019 - 2022
// @author Dmitry Ponomarev <[email protected]> 2016 – 2017, 2019 - 2022
//
package notificationcenter
import (
"context"
)
// DefaultRegistry is global registry
var DefaultRegistry = NewRegistry()
// PublisherByName returns pub interface by name if exists or Nil otherwise
func PublisherByName(name string) Publisher {
return DefaultRegistry.Publisher(name)
}
// SubscriberByName returns sub interface by name if exists or Nil otherwise
func SubscriberByName(name string) Subscriber {
return DefaultRegistry.Subscriber(name)
}
// Register one or more Publisher or Subscriber services.
// As input parameters must be order of parameters {Name, interface}
//
// Example:
// ```
// nc.Register(
// "events", kafka.MustNewSubscriber(),
// "notifications", nats.MustNewSubscriber(),
// )
// ```
func Register(params ...any) error {
return DefaultRegistry.Register(params...)
}
// Publish one or more messages to the pub-service
func Publish(ctx context.Context, name string, messages ...any) error {
return DefaultRegistry.Publish(ctx, name, messages...)
}
// Subscribe new handler on some particular subscriber interface by name
func Subscribe(ctx context.Context, name string, receiver any) error {
return DefaultRegistry.Subscribe(ctx, name, receiver)
}
// Listen runs subscribers listen interface
func Listen(ctx context.Context) error {
return DefaultRegistry.Listen(ctx)
}
// Close notification center
func Close() error {
return DefaultRegistry.Close()
}
// OnClose event will be executed only after closing all interfaces
//
// Usecases in the application makes subsribing for the finishing event very convinient
//
// ```go
// func myDatabaseObserver() {
// <- notificationcenter.OnClose()
// // ... Do something
// }
// ```
func OnClose() <-chan bool {
return DefaultRegistry.OnClose()
}