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