package ui type EventFn func(Context) bool type EventStateFn func(Context, interface{}) func NewEvents() *Events { return &Events{events: map[uint]EventStateFn{}} } type Events struct { nextID uint events map[uint]EventStateFn } type EventHandler interface { AddHandler(EventFn) uint AddHandlerState(EventStateFn) uint RemoveHandler(uint) } func (e *Events) Notify(ctx Context, state interface{}) bool { if e.events == nil { return false } for _, handler := range e.events { handler(ctx, state) } return len(e.events) > 0 } func (e *Events) AddHandler(handler EventFn) uint { return e.AddHandlerState(func(ctx Context, _ interface{}) { handler(ctx) }) } func (e *Events) AddHandlerState(handler EventStateFn) uint { if e.events == nil { e.events = map[uint]EventStateFn{} } id := e.nextID e.nextID++ e.events[id] = handler return id } func (e *Events) RemoveHandler(id uint) { delete(e.events, id) }