From ac9ccf633a99ac707e694b91bb45e0c591d3077f Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 11 Jan 2022 17:04:16 -0500 Subject: [PATCH] Setnewstates as method, sort, check and set to skip noop --- main.go | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index cebd918..f305dc8 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "local/truckstop/message" "log" "regexp" + "sort" "strings" "sync" "time" @@ -66,19 +67,30 @@ func matrixrecv() error { states[state] = struct{}{} } } - if len(states) == 0 { - return nil - } - conf := *config.Get() - conf.States = []config.State{} - for k := range states { - conf.States = append(conf.States, k) - } - log.Printf("%+v, %+v", states, conf) - config.Set(conf) + setNewStates(states) return nil } +func setNewStates(states map[config.State]struct{}) { + if len(states) == 0 { + return + } + newstates := []config.State{} + for k := range states { + newstates = append(newstates, k) + } + sort.Slice(newstates, func(i, j int) bool { + return newstates[i] < newstates[j] + }) + conf := *config.Get() + if fmt.Sprint(newstates) == fmt.Sprint(conf.States) { + return + } + conf.States = newstates + log.Printf("updating config new states: %+v", conf) + config.Set(conf) +} + func email() error { log.Printf("checking email...") ch, err := config.Get().Emailer.ReadIMAP() @@ -101,16 +113,7 @@ func email() error { states[state] = struct{}{} } } - if len(states) == 0 { - return nil - } - conf := *config.Get() - conf.States = []config.State{} - for k := range states { - conf.States = append(conf.States, k) - } - log.Printf("%+v, %+v", states, conf) - config.Set(conf) + setNewStates(states) return nil }