Files

50 lines
1.8 KiB
Go
Raw Permalink Normal View History

2021-07-25 15:37:30 +10:00
package runtime
import (
"context"
)
2022-10-22 22:03:37 +00:00
// EventsOn registers a listener for the given event name. It returns a function to cancel the listener
func EventsOn(ctx context.Context, eventName string, callback func(optionalData ...interface{})) func() {
2021-08-15 21:07:34 +10:00
events := getEvents(ctx)
2022-10-22 22:03:37 +00:00
return events.On(eventName, callback)
2021-07-25 15:37:30 +10:00
}
2023-11-12 12:30:49 +11:00
// EventsOff unregisters a listener for the given event name, optionally multiple listeners can be unregistered via `additionalEventNames`
func EventsOff(ctx context.Context, eventName string, additionalEventNames ...string) {
2021-08-15 21:07:34 +10:00
events := getEvents(ctx)
events.Off(eventName)
if len(additionalEventNames) > 0 {
for _, eventName := range additionalEventNames {
events.Off(eventName)
}
}
2021-08-15 21:07:34 +10:00
}
2023-11-12 12:30:49 +11:00
// EventsOff unregisters a listener for the given event name, optionally multiple listeners can be unregistered via `additionalEventNames`
2022-10-22 22:03:37 +00:00
func EventsOffAll(ctx context.Context) {
2021-08-15 21:07:34 +10:00
events := getEvents(ctx)
2022-10-22 22:03:37 +00:00
events.OffAll()
2021-07-25 15:37:30 +10:00
}
2022-10-22 22:03:37 +00:00
// EventsOnce registers a listener for the given event name. After the first callback, the
// listener is deleted. It returns a function to cancel the listener
func EventsOnce(ctx context.Context, eventName string, callback func(optionalData ...interface{})) func() {
2021-08-15 21:07:34 +10:00
events := getEvents(ctx)
2022-10-22 22:03:37 +00:00
return events.Once(eventName, callback)
}
// EventsOnMultiple registers a listener for the given event name, that may be called a maximum of 'counter' times. It returns a function
// to cancel the listener
func EventsOnMultiple(ctx context.Context, eventName string, callback func(optionalData ...interface{}), counter int) func() {
events := getEvents(ctx)
return events.OnMultiple(eventName, callback, counter)
2021-08-15 21:07:34 +10:00
}
2021-07-25 15:37:30 +10:00
2021-08-15 21:07:34 +10:00
// EventsEmit pass through
func EventsEmit(ctx context.Context, eventName string, optionalData ...interface{}) {
events := getEvents(ctx)
events.Emit(eventName, optionalData...)
2021-07-25 15:37:30 +10:00
}