Only alert on change of state.

change-name
Robert Jacob 2018-04-17 23:28:01 +02:00
parent b5ce5395d8
commit 17a310ba4a
2 changed files with 12 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import (
"os"
)
type Listener func(status SpaceStatus)
type Listener func(old, new SpaceStatus)
type Storage struct {
filePath string
@ -31,19 +31,20 @@ func (s *Storage) Status() SpaceStatus {
}
func (s *Storage) Modify(modifiers ...func(*SpaceStatus)) {
old := s.status
for _, m := range modifiers {
m(&s.status)
}
s.notify()
s.notify(old, s.status)
}
func (s *Storage) AddListener(l Listener) {
s.listeners = append(s.listeners, l)
}
func (s *Storage) notify() {
func (s *Storage) notify(old, new SpaceStatus) {
for _, l := range s.listeners {
l(s.status)
l(old, new)
}
}

View File

@ -70,13 +70,17 @@ func AddXMPPListener(storage *spaceapi.Storage, jid, password, target, handle st
}
}()
storage.AddListener(func(status spaceapi.SpaceStatus) {
if status.State.Open == nil {
storage.AddListener(func(old, new spaceapi.SpaceStatus) {
if new.State.Open == nil {
return
}
if old.State.Open == new.State.Open {
return
}
msg := "Space is now OPEN!"
if !*status.State.Open {
if !*new.State.Open {
msg = "Space is now CLOSED!"
}