From cc0af18c565bfed9986e3d9af98b212b18ae25b6 Mon Sep 17 00:00:00 2001 From: Evgeniy Ivakha Date: Thu, 9 Mar 2017 01:24:57 +0500 Subject: [PATCH] Fixed events handling order --- ami.go | 2 +- amigo.go | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ami.go b/ami.go index 170edcd..804e119 100644 --- a/ami.go +++ b/ami.go @@ -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 { diff --git a/amigo.go b/amigo.go index fa78e2c..ccefaf9 100644 --- a/amigo.go +++ b/amigo.go @@ -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) @@ -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 { @@ -211,6 +213,7 @@ func (a *Amigo) Connect() { agiCommandsMutex.Unlock() } } + a.handlerMutex.RUnlock() } }()