forked from dymensionxyz/dymint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
37 lines (32 loc) · 1.02 KB
/
events.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
package utils
import (
"context"
"github.com/dymensionxyz/dymint/log"
"github.com/tendermint/tendermint/libs/pubsub"
)
// SubscribeAndHandleEvents subscribes to events and sends back a callback
func SubscribeAndHandleEvents(ctx context.Context, pubsubServer *pubsub.Server, clientID string, eventQuery pubsub.Query, callback func(event pubsub.Message), logger log.Logger, outCapacity ...int) {
subscription, err := pubsubServer.Subscribe(ctx, clientID, eventQuery, outCapacity...)
if err != nil {
logger.Error("failed to subscribe to events")
panic(err)
}
for {
select {
case <-ctx.Done():
return
case event := <-subscription.Out():
callback(event)
case <-subscription.Cancelled():
logger.Info(clientID + " subscription canceled")
return
}
}
}
// SubmitEventOrPanic submits an event or panics
func SubmitEventOrPanic(ctx context.Context, pubsubServer *pubsub.Server, msg interface{}, events map[string][]string) {
err := pubsubServer.PublishWithEvents(ctx, msg, events)
if err != nil {
panic(err)
}
}