Skip to content
Snippets Groups Projects
Commit 895dfc09 authored by capossele's avatar capossele
Browse files

:zap: improved mutex usage

parent 77c15bb8
No related branches found
No related tags found
No related merge requests found
...@@ -10,22 +10,22 @@ type Event struct { ...@@ -10,22 +10,22 @@ type Event struct {
func (this *Event) Attach(closure *Closure) { func (this *Event) Attach(closure *Closure) {
this.mutex.Lock() this.mutex.Lock()
defer this.mutex.Unlock()
this.callbacks[closure.Id] = closure.Fnc this.callbacks[closure.Id] = closure.Fnc
this.mutex.Unlock()
} }
func (this *Event) Detach(closure *Closure) { func (this *Event) Detach(closure *Closure) {
this.mutex.Lock() this.mutex.Lock()
defer this.mutex.Unlock()
delete(this.callbacks, closure.Id) delete(this.callbacks, closure.Id)
this.mutex.Unlock()
} }
func (this *Event) Trigger(params ...interface{}) { func (this *Event) Trigger(params ...interface{}) {
this.mutex.RLock() 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...)
} }
this.mutex.RUnlock()
} }
func NewEvent(triggerFunc func(handler interface{}, params ...interface{})) *Event { func NewEvent(triggerFunc func(handler interface{}, params ...interface{})) *Event {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment