Note: need to think about function signatures for Before and After options. Receiving multiple events in the callbacks only makes sense if the Debounce option will be implemented (#153)
package example
func example(bus event.Bus) {
h := handler.New(
bus,
handler.Before(func(_ context.Context, events []event.Event) error {
log.Printf("Handling events:\n%v", events)
return nil
}),
handler.After(func(_ context.Context, r handler.Result) error {
log.Printf("Failed to handle events:\n%v", r.Events)
log.Printf("Failed event: %v", r.Failed)
log.Printf("Error: %v", f.Err)
// either return an error so it will be returned in the handler's error channel
return fmt.Errorf("wrapped error: %w", f.Err)
// or return nil to not report the error in the error channel
return nil
}),
)
}
handler.WithLock and handler.WithReadLock
handler.WithLock and handler.WithReadLock are wrappers for handler.Before() and handler.After() to lock/unlock a Mutex (or some type with Lock + Unlock or RLock + RUnlock methods) before/after handling events:
package example
func example1(bus event.Bus) {
var mux sync.Mutex
h := handler.New(bus, handler.WithLock(&mux))
}
func example2(bus event.Bus) {
var mux sync.RWMutex
h := handler.New(bus, handler.WithReadLock(&mux))
}
func example3(bus event.Bus) {
var mux sync.RWMutex
h := handler.New(bus, handler.WithLock(&mux))
}
handler.Before
andhandler.After
handler.WithLock
andhandler.WithReadLock
handler.WithLock
andhandler.WithReadLock
are wrappers forhandler.Before()
andhandler.After()
to lock/unlock aMutex
(or some type withLock + Unlock
orRLock + RUnlock
methods) before/after handling events:WithLock
implementation: