Skip to content
Open
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
27 changes: 15 additions & 12 deletions emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ The design goals are:
package emitter

import (
"path"
"sync"
)

Expand Down Expand Up @@ -55,13 +54,12 @@ func Sync(e *Event) { e.Flags = e.Flags | FlagSync }

// New returns just created Emitter struct. Capacity argument
// will be used to create channels with given capacity
func New(capacity uint) *Emitter {
return &Emitter{
Cap: capacity,
listeners: make(map[string][]listener),
middlewares: make(map[string][]func(*Event)),
isInit: true,
}
// optionally include a matcher to use or nil to use the default
// which implements path.Match() from the system library
func New(capacity uint, matcher Matcher) *Emitter {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about to create another function/constructor, like:

func NewWithMatcher(capacity uint, matcher Matcher) *Emitter {

or make it a variadic function, like:

func New(capacity uint, matcher ...Matcher) *Emitter {

rather than introducing a broken change?

I would prefer to don't change the existing library's API if it's not a but fix.

e := &Emitter{Cap: capacity, Matcher: matcher}
e.init()
return e
}

// Emitter is a struct that allows to emit, receive
Expand All @@ -73,6 +71,7 @@ type Emitter struct {
listeners map[string][]listener
isInit bool
middlewares map[string][]func(*Event)
Matcher Matcher
}

func newListener(capacity uint, middlewares ...func(*Event)) listener {
Expand All @@ -92,6 +91,10 @@ func (e *Emitter) init() {
e.listeners = make(map[string][]listener)
e.middlewares = make(map[string][]func(*Event))
e.isInit = true
if e.Matcher == nil {
e.Matcher = DefaultMatcher()
}

}
}

Expand Down Expand Up @@ -298,9 +301,9 @@ func pushEvent(
func (e *Emitter) getMiddlewares(topic string) []func(*Event) {
var acc []func(*Event)
for pattern, v := range e.middlewares {
if match, _ := path.Match(pattern, topic); match {
if match, _ := e.Matcher.Match(pattern, topic); match {
acc = append(acc, v...)
} else if match, _ := path.Match(topic, pattern); match {
} else if match, _ := e.Matcher.Match(topic, pattern); match {
acc = append(acc, v...)
}
}
Expand All @@ -317,12 +320,12 @@ func (e *Emitter) matched(topic string) ([]string, error) {
acc := []string{}
var err error
for k := range e.listeners {
if matched, err := path.Match(topic, k); err != nil {
if matched, err := e.Matcher.Match(topic, k); err != nil {
return []string{}, err
} else if matched {
acc = append(acc, k)
} else {
if matched, _ := path.Match(k, topic); matched {
if matched, _ := e.Matcher.Match(k, topic); matched {
acc = append(acc, k)
}
}
Expand Down
26 changes: 26 additions & 0 deletions matcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package emitter

import (
"path"
)

// Matcher interface describes a pattern matcher that determines
// if the name is included as a subset of the pattern
// returns true,nil upon success
type Matcher interface {
Match(pattern, name string) (matched bool, err error)
}

// Default matcher returns the standard Matcher (PathMatcher)
func DefaultMatcher() Matcher {
return &PathMatch{}
}

// PathMatch is a Matcher implementation of the system path.Match
// function
type PathMatch struct {
}

func (p *PathMatch) Match(pattern, name string) (matched bool, err error) {
return path.Match(pattern, name)
}