Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added wildcard support #3

Merged
merged 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func main(){

// register
emitter.On("myevent", fn)

// listen to all events
emitter.On("**", fn)

// listen to events based on widlcard
emitter.On("my*", fn)

// now remove it
emitter.RemoveListener("myevent", fn)
Expand Down
44 changes: 41 additions & 3 deletions goemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@ package Emitter

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

// wildcard helper
func eventMatchPattern(eventName, pattern []rune) bool {
for len(pattern) > 0 {
switch pattern[0] {
case '*':
return eventMatchPattern(eventName, pattern[1:]) || (len(eventName) > 0 && eventMatchPattern(eventName[1:], pattern))

default:
if len(eventName) == 0 || eventName[0] != pattern[0] {
return false
}
}

eventName = eventName[1:]
pattern = pattern[1:]
}

return len(eventName) == 0 && len(pattern) == 0
}

// Emitter - our listeners container
type Emitter struct {
listeners map[interface{}][]Listener
Expand Down Expand Up @@ -117,10 +138,27 @@ func (self *Emitter) Listeners(event string) []Listener {
self.mutex.Lock()
defer self.mutex.Unlock()

if _, ok := self.listeners[event]; !ok {
return nil
listeners := make([]Listener, 0)

// add the ones that follow pattern
for eventPattern, lis := range self.listeners {
shouldAdd := false

// add generic "**" bound listeners
shouldAdd = shouldAdd || eventPattern.(string) == "**"
// add listener bound on full name event
shouldAdd = shouldAdd || eventPattern.(string) == event
// add listeners that have matching wildcard pattern
shouldAdd = shouldAdd ||
(strings.Contains(eventPattern.(string), "*") &&
eventMatchPattern([]rune(event), []rune(eventPattern.(string))))

if shouldAdd {
listeners = append(listeners, lis...)
}
}
return self.listeners[event]

return listeners
}

// ListenersCount() - return the count of listeners in the speicifed event
Expand Down
25 changes: 23 additions & 2 deletions goemitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ func TestOnce(t *testing.T) {
expect(t, 1, counter)
}

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

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

emitter.On("testevent", fn1)
emitter.On("test*", fn1)
emitter.On("t*", fn1)
emitter.On("nomatch", fn1)

emitter.EmitSync("testevent")

listenersCount := emitter.ListenersCount("testevent")

expect(t, 3, listenersCount, "wrong listeners count")
expect(t, 3, counter, "wrong fn execution")
}

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

Expand Down Expand Up @@ -104,8 +125,8 @@ func TestRandomConcurrentCalls(t *testing.T) {
expect(t, nil, err)
}

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