Browse Source

Only alert on change of state.

change-name
Robert Jacob 1 year ago
parent
commit
17a310ba4a
2 changed files with 12 additions and 7 deletions
  1. +5
    -4
      storage.go
  2. +7
    -3
      xmpp/xmpp.go

+ 5
- 4
storage.go 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)
}
}


+ 7
- 3
xmpp/xmpp.go 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!"
}


Loading…
Cancel
Save