Only alert on change of state.
parent
b5ce5395d8
commit
17a310ba4a
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
10
xmpp/xmpp.go
10
xmpp/xmpp.go
|
@ -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!"
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue