Skip to content
Snippets Groups Projects
Commit 9291dc9f authored by capossele's avatar capossele
Browse files

:bug: added RWMutex

parent 12c0145c
No related branches found
No related tags found
No related merge requests found
package events package events
import "sync"
type Event struct { type Event struct {
triggerFunc func(handler interface{}, params ...interface{}) triggerFunc func(handler interface{}, params ...interface{})
callbacks map[uintptr]interface{} callbacks map[uintptr]interface{}
mutex sync.RWMutex
} }
func (this *Event) Attach(closure *Closure) { func (this *Event) Attach(closure *Closure) {
this.mutex.Lock()
defer this.mutex.Unlock()
this.callbacks[closure.Id] = closure.Fnc this.callbacks[closure.Id] = closure.Fnc
} }
func (this *Event) Detach(closure *Closure) { func (this *Event) Detach(closure *Closure) {
this.mutex.Lock()
defer this.mutex.Unlock()
delete(this.callbacks, closure.Id) delete(this.callbacks, closure.Id)
} }
func (this *Event) Trigger(params ...interface{}) { func (this *Event) Trigger(params ...interface{}) {
this.mutex.RLock()
defer this.mutex.RUnlock()
for _, handler := range this.callbacks { for _, handler := range this.callbacks {
this.triggerFunc(handler, params...) this.triggerFunc(handler, params...)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment