message.gno
1.07 Kb ยท 39 lines
1package message
2
3// TopicAll defines a topic for all types of message.
4// This topic can be used to subscribe to message for all topics.
5const TopicAll Topic = "*"
6
7type (
8 // Topic defines a type for message topics.
9 Topic string
10
11 // Callback defines a type for message callbacks.
12 Callback func(Message)
13
14 // Message defines a type for published messages.
15 Message struct {
16 // Topic is the message topic.
17 Topic Topic
18
19 // Data contains optional message data.
20 Data any
21 }
22
23 // Publisher defines an interface for message publishers.
24 Publisher interface {
25 // Publish publishes a message for a topic.
26 Publish(_ Topic, data any) error
27 }
28
29 // Subscriber defines an interface for message subscribers.
30 Subscriber interface {
31 // Subscribe subscribes to messages published for a topic.
32 // It returns the callback ID within the topic.
33 Subscribe(Topic, Callback) (id int, _ error)
34
35 // Unsubscribe unsubscribes a callback from a message topic.
36 // ID is the callback ID within the topic, returned on subscription.
37 Unsubscribe(_ Topic, id int) (unsubscribed bool, _ error)
38 }
39)