Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

message.gno

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