Skip to content

Commit

Permalink
Merge pull request #1 from casskir/master
Browse files Browse the repository at this point in the history
Fixed listeners can't be removed.
  • Loading branch information
pentateu authored Oct 3, 2019
2 parents 0087c90 + 72e7e25 commit 81f99c6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
19 changes: 14 additions & 5 deletions goemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
// Version: v1.0.0
package Emitter

import "sync"
import (
"reflect"
"sync"
)

// Emitter - our listeners container
type Emitter struct {
Expand Down Expand Up @@ -64,13 +67,19 @@ func (self *Emitter) Once(event string, callback func(...interface{})) *Emitter

// RemoveListeners() - remove the specified callback from the specified events' listeners
func (self *Emitter) RemoveListener(event string, callback func(...interface{})) *Emitter {
return self.removeListenerInternal(event, callback, false)
}

func (self *Emitter) removeListenerInternal(event string, callback func(...interface{}), suppress bool) *Emitter {
if _, ok := self.listeners[event]; !ok {
return self
}
for k, v := range self.listeners[event] {
if &v.callback == &callback {
if reflect.ValueOf(v.callback).Pointer() == reflect.ValueOf(callback).Pointer() {
self.listeners[event] = append(self.listeners[event][:k], self.listeners[event][k+1:]...)
self.EmitSync("removeListener", []interface{}{event, callback})
if !suppress {
self.EmitSync("removeListener", []interface{}{event, callback})
}
return self
}
}
Expand Down Expand Up @@ -112,7 +121,7 @@ func (self *Emitter) EmitSync(event string, args ...interface{}) *Emitter {
defer self.mutex.Unlock()
for _, v := range self.Listeners(event) {
if v.once {
self.RemoveListener(event, v.callback)
self.removeListenerInternal(event, v.callback, true)
}
v.callback(args...)
}
Expand All @@ -126,7 +135,7 @@ func (self *Emitter) EmitAsync(event string, args []interface{}) *Emitter {
defer self.mutex.Unlock()
for _, v := range self.Listeners(event) {
if v.once {
self.RemoveListener(event, v.callback)
self.removeListenerInternal(event, v.callback, true)
}
go v.callback(args...)
}
Expand Down
51 changes: 51 additions & 0 deletions goemitter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package Emitter

import (
"reflect"
"testing"
)

func TestRemoveListener(t *testing.T) {
emitter := Construct()

counter := 0
fn1 := func(args ...interface{}) {
counter++
}
fn2 := func(args ...interface{}) {
counter++
}

emitter.On("testevent", fn1)
emitter.On("testevent", fn2)

emitter.RemoveListener("testevent", fn1)
emitter.EmitSync("testevent")

listenersCount := emitter.ListenersCount("testevent")

expect(t, 1, listenersCount)
expect(t, 1, counter)
}

func TestOnce(t *testing.T) {
emitter := Construct()

counter := 0
fn := func(args ...interface{}) {
counter++
}

emitter.Once("testevent", fn)

emitter.EmitSync("testevent")
emitter.EmitSync("testevent")

expect(t, 1, counter)
}

func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}

0 comments on commit 81f99c6

Please sign in to comment.