Skip to content

Commit

Permalink
Fixed events handling order
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeniy Ivakha committed Mar 8, 2017
1 parent 5f5194b commit cc0af18
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newAMIAdapter(ip, port, username, password string, eventEmitter func(string

a.actionsChan = make(chan map[string]string)
a.responseChans = make(map[string]chan map[string]string)
a.EventsChan = make(chan map[string]string)
a.EventsChan = make(chan map[string]string, 1000)

go func() {
for {
Expand Down
21 changes: 12 additions & 9 deletions amigo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var (
version = "0.1.5"
version = "0.1.6"

// TODO: implement function to clear old data in handlers.
agiCommandsHandlers = make(map[string]agiCommand)
Expand Down Expand Up @@ -163,38 +163,40 @@ func (a *Amigo) Connect() {
var e = <-a.ami.EventsChan
a.handlerMutex.RLock()

var event = strings.ToUpper(e["Event"])
if len(e["Time"]) == 0 {
e["Time"] = time.Now().Format(time.RFC3339Nano)
}

if a.defaultChannel != nil {
go func(e map[string]string) {
a.defaultChannel <- e
}(e)
a.defaultChannel <- e
}

var event = strings.ToUpper(e["Event"])
if len(event) != 0 && (a.handlers[event] != nil || a.defaultHandler != nil) {
if a.capitalizeProps {
ev := map[string]string{}
for k, v := range e {
ev[strings.ToUpper(k)] = v
}

if a.handlers[event] != nil {
go a.handlers[event](ev)
a.handlers[event](ev)
}

if a.defaultHandler != nil {
go a.defaultHandler(ev)
a.defaultHandler(ev)
}
} else {
if a.defaultHandler != nil {
go a.defaultHandler(e)
a.defaultHandler(e)
}

if a.handlers[event] != nil {
go a.handlers[event](e)
a.handlers[event](e)
}
}
}

if event == "ASYNCAGI" {
commandID, ok := e["CommandID"]
if !ok {
Expand All @@ -211,6 +213,7 @@ func (a *Amigo) Connect() {
agiCommandsMutex.Unlock()
}
}

a.handlerMutex.RUnlock()
}
}()
Expand Down

0 comments on commit cc0af18

Please sign in to comment.