Stop wol rebuild from firing so frequently.

This commit is contained in:
Azareal 2019-05-13 15:13:27 +10:00
parent e57e2f69b2
commit 30722e1013
1 changed files with 16 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package common
import ( import (
"bytes" "bytes"
//"log"
"net/http/httptest" "net/http/httptest"
"github.com/Azareal/Gosora/common/phrases" "github.com/Azareal/Gosora/common/phrases"
@ -20,7 +21,7 @@ func wolInit(widget *Widget, schedule *WidgetScheduler) error {
return nil return nil
} }
func wolGetUsers() ([]*User,int) { func wolGetUsers() ([]*User, int) {
ucount := WsHub.UserCount() ucount := WsHub.UserCount()
// We don't want a ridiculously long list, so we'll show the number if it's too high and only show staff individually // We don't want a ridiculously long list, so we'll show the number if it's too high and only show staff individually
var users []*User var users []*User
@ -56,15 +57,23 @@ var wolLastUsers []*User
func wolTick(widget *Widget) error { func wolTick(widget *Widget) error {
w := httptest.NewRecorder() w := httptest.NewRecorder()
users, ucount := wolGetUsers() users, ucount := wolGetUsers()
//log.Printf("users: %+v\n",users)
//log.Printf("wolLastUsers: %+v\n",wolLastUsers) var inOld = func(id int) bool {
for _, user := range wolLastUsers {
if id == user.ID {
return true
}
}
return false
}
// Avoid rebuilding the widget, if the users are exactly the same as on the last tick // Avoid rebuilding the widget, if the users are exactly the same as on the last tick
if len(users) == len(wolLastUsers) { if len(users) == len(wolLastUsers) {
diff := false diff := false
for i, user := range users { for _, user := range users {
if user.ID != wolLastUsers[i].ID { if !inOld(user.ID) {
diff = true diff = true
break
} }
} }
if !diff { if !diff {
@ -77,6 +86,8 @@ func wolTick(widget *Widget) error {
} }
} }
} }
//log.Printf("users: %+v\n", users)
//log.Printf("wolLastUsers: %+v\n", wolLastUsers)
wol := &wolUsers{SimpleDefaultHeader(w), phrases.GetTmplPhrase("widget.online_name"), users, ucount} wol := &wolUsers{SimpleDefaultHeader(w), phrases.GetTmplPhrase("widget.online_name"), users, ucount}
err := wol.Header.Theme.RunTmpl("widget_online", wol, wol.Header.Writer) err := wol.Header.Theme.RunTmpl("widget_online", wol, wol.Header.Writer)