Documentation
¶
Overview ¶
Package bell implements a simple event system (bell ringing and listening)
Several listeners can be added for each ringing (handlerFunc). Listeners are called in a separate goroutine through an established channel. When the bell rings, a message is sequentially transmitted to each listener.
If a channel is closed, the goroutine for that event is terminated.
Example for usage: Listen("event_name", func(message Message) { fmt.PrintLn(message) }) - add listener on bell by name "event_name" Ring("event_name", "some_data") - Ring on bell (call event "event_name")
Example ¶
// Use via global state event := "event_name" event2 := "event_name_2" // add listener on event event_name bell.Listen(event, func(message bell.Message) { // we extend CustomStruct in message customStruct := message.(CustomStruct) fmt.Println(customStruct) }) // add listener on event event_name_2 bell.Listen(event2, func(message bell.Message) {}) // get event list list := bell.List() // only for test sort.Strings(list) fmt.Println(list) // remove listeners on event_name_2 bell.Remove(event2) // get event list again fmt.Println(bell.List()) // check if exists event_name_2 event in storage fmt.Println(bell.Has(event2)) // call event event_name _ = bell.Ring(event, CustomStruct{name: "testName", param: 12}) // wait until the event completes its work bell.Wait()
Output: [event_name event_name_2] [event_name] false {testName 12}
Example (UsingContext) ¶
// Use bell with context // create a custom struct for pass a context type Custom struct { ctx context.Context value interface{} } // add listener bell.Listen("event", func(message bell.Message) { for iterationsCount := 1; true; iterationsCount++ { select { case <-message.(*Custom).ctx.Done(): return default: fmt.Printf("Iteration #%d\n", iterationsCount) time.Sleep(10 * time.Second) } } }) // create a global context for all calls globalCtx, cancelGlobalCtx := context.WithCancel(context.Background()) // create a children context for a call with timeout ringCtx, ringCancel := context.WithTimeout(globalCtx, time.Minute) defer ringCancel() _ = bell.Ring("event", &Custom{ringCtx, "value"}) // wait a second for the handler to perform one iteration time.Sleep(time.Second) // interrupt all handlers cancelGlobalCtx()
Output: Iteration #1
Index ¶
- func Has(event string) bool
- func List() []string
- func Listen(event string, handlerFunc func(message Message))
- func ListenN(event string, handlerFunc func(message Message), copiesCount uint)
- func Queue(size uint)
- func Remove(names ...string)
- func Ring(event string, message Message) error
- func Wait()
- type Events
- func (e *Events) Has(event string) bool
- func (e *Events) List() []string
- func (e *Events) Listen(event string, handlerFunc func(message Message))
- func (e *Events) ListenN(event string, handlerFunc func(message Message), copiesCount uint)
- func (e *Events) Queue(size uint) *Events
- func (e *Events) Remove(names ...string)
- func (e *Events) Ring(event string, message Message) error
- func (e *Events) Wait()
- type Message
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Listen ¶
Listen Subscribe on event where event - the event name, handlerFunc - handler function
func ListenN ¶ added in v2.1.0
ListenN Subscribe on event where event - the event name, handlerFunc - handler function copiesCount - count handlers copies run
func Remove ¶
func Remove(names ...string)
Remove Removes listeners by event name Removing listeners closes channels and stops the goroutine.
If you call the function without the "names" parameter, all listeners of all events will be removed.
Types ¶
type Events ¶
type Events struct {
// contains filtered or unexported fields
}
Events thread safe structure stores events, their handlers and functions for management
Example ¶
// Use events object (without global state) eventName := "event_name" // make a new events store events := bell.New() // add listener on event events.Listen(eventName, func(msg bell.Message) { fmt.Println(msg) }) // call event event_name _ = events.Ring(eventName, "Hello bell!") // wait until the event completes its work events.Wait()
Output: Hello bell!
func (*Events) Listen ¶
Listen Subscribe on event where event - the event name, handlerFunc - handler function
func (*Events) ListenN ¶ added in v2.1.0
ListenN Subscribe on event where event - the event name, handlerFunc - handler function copiesCount - count handlers copies run
func (*Events) Remove ¶
Remove Removes listeners by event name Removing listeners closes channels and stops the goroutine.
If you call the function without the "names" parameter, all listeners of all events will be removed.