Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrHandlerIsNotFunc = err{Code: 10000, Msg: "handler is not a function"} ErrHandlerParamNum = err{Code: 10001, Msg: "the number of parameters of the handler must be two"} ErrHandlerFirstParam = err{Code: 10002, Msg: "the first of parameters of the handler must be a string"} ErrNoSubscriber = err{Code: 10003, Msg: "no subscriber on topic"} ErrChannelClosed = err{Code: 10004, Msg: "channel is closed"} )
Global variables that represent common errors that may be returned by the eventbus functions.
Functions ¶
func Publish ¶ added in v1.0.3
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 PublishSync ¶ added in v1.0.4
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 ResetSingleton ¶ added in v1.0.8
func 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 Subscribe ¶ added in v1.0.3
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 Unsubscribe ¶ added in v1.0.3
Unsubscribe removes handler defined for a topic. Returns error if there are no handlers subscribed to the topic.
Types ¶
type CowMap ¶
CowMap is a wrapper of Copy-On-Write map
If a fully meaningful CowMap is implemented, both sync.Map and CowMap utilize atomic.Value atomic operations to access the map during data reading, resulting in similar read performance. In reality, sync.Map is already a read-write separated structure, yet it has better write performance. Therefore, CowMap directly utilizes sync.Map as its internal structure.
type EventBus ¶
type EventBus struct {
// contains filtered or unexported fields
}
EventBus is a container for event topics. Each topic corresponds to a channel. `eventbus.Publish()` pushes a message to the channel, and the handler in `eventbus.Subscribe()` will process the message coming out of the channel.
func NewBuffered ¶
NewBuffered returns new EventBus with a buffered channel. The second argument indicate the buffer's length
func (*EventBus) Publish ¶
publish triggers the handlers defined for this channel asynchronously. The `payload` argument will be passed to the handler. It uses the channel to asynchronously call the handler. The type of the payload must correspond to the second parameter of the handler in `Subscribe()`.
func (*EventBus) PublishSync ¶ added in v1.0.4
publishSync triggers the handlers defined for this channel synchronously. The payload argument will be passed to the handler. It does not use channels and instead directly calls the handler function.
type Pipe ¶
Pipe is a wrapper for a channel that allows for asynchronous message passing to subscribers. Use Pipe.Publish() instead of `chan<-` and Pipe.Subscribe() instead of `<-chan`. To pass messages to subscribers synchronously, use Pipe.PublishSync(), which does not use a channel. If multiple subscribers exist, each subscriber will receive the message.
func NewBufferedPipe ¶
NewPipe create a buffered pipe, bufferSize is the buffer size of the pipe When create a buffered pipe. You can publish into the Pipe without a corresponding concurrent subscriber.
func (*Pipe[T]) Publish ¶
Publish triggers the handlers defined for this pipe, transferring the payload to the handlers.
func (*Pipe[T]) PublishSync ¶ added in v1.0.4
PublishSync triggers the handlers defined for this pipe synchronously, without using a channel. The payload will be passed directly to the handlers.
func (*Pipe[T]) Unsubscribe ¶
unsubscribe removes handler defined for this pipe.