From 30b68945b4aac0dc37d570d3b0ecfc881f39e712 Mon Sep 17 00:00:00 2001 From: Azareal Date: Thu, 12 Mar 2020 11:08:56 +1000 Subject: [PATCH] guarantee more space for alerts --- common/alerts.go | 4 ++-- public/global.js | 12 ++++++------ routes.go | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/common/alerts.go b/common/alerts.go index 62481446..0d0533ec 100644 --- a/common/alerts.go +++ b/common/alerts.go @@ -56,7 +56,7 @@ func init() { }) } -const AlertsGrowHint = len(`{"msgs":[],"count":,"tc":}`) + 1 + 7 +const AlertsGrowHint = len(`{"msgs":[],"count":,"tc":}`) + 1 + 10 // TODO: See if we can json.Marshal instead? func escapeTextInJson(in string) string { @@ -167,7 +167,7 @@ func buildAlertString(msg string, sub []string, path, avatar string, asid int) s return sb.String() } -const AlertsGrowHint2 = len(`{"msg":"","sub":[],"path":"","avatar":"","id":}`) + 3 + 3 + 1 + 1 + 1 +const AlertsGrowHint2 = len(`{"msg":"","sub":[],"path":"","avatar":"","id":}`) + 5 + 3 + 1 + 1 + 1 // TODO: Use a string builder? func buildAlertSb(sb *strings.Builder, msg string, sub []string, path, avatar string, asid int) { diff --git a/public/global.js b/public/global.js index b01d27e5..d93766ab 100644 --- a/public/global.js +++ b/public/global.js @@ -236,7 +236,7 @@ function runWebSockets(resume = false) { setTimeout(() => { if(!noAlerts) { - var alertMenuList = document.getElementsByClassName("menu_alerts"); + let alertMenuList = document.getElementsByClassName("menu_alerts"); for(var i=0; i < alertMenuList.length; i++) loadAlerts(alertMenuList[i]); } runWebSockets(true); @@ -282,14 +282,14 @@ function runWebSockets(resume = false) { delete alertMapping[key]; // TODO: Add support for other alert feeds like PM Alerts - var generalAlerts = document.getElementById("general_alerts"); + let generalAlerts = document.getElementById("general_alerts"); if(alertList.length < 8) loadAlerts(generalAlerts); else updateAlertList(generalAlerts); }); } } else if("Topics" in data) { console.log("topic in data"); - console.log("data", data); + console.log("data",data); let topic = data.Topics[0]; if(topic === undefined){ console.log("empty topic list"); @@ -321,10 +321,10 @@ function runWebSockets(resume = false) { } } - var messages = event.data.split('\r'); + let messages = event.data.split('\r'); for(var i=0; i < messages.length; i++) { let message = messages[i]; - //console.log("Message: ",message); + //console.log("message",message); let msgblocks = SplitN(message," ",3); if(msgblocks.length < 3) continue; if(message.startsWith("set ")) { @@ -348,7 +348,7 @@ function runWebSockets(resume = false) { // We can only get away with this because template_alert has no phrases, otherwise it too would have to be part of the "dance", I miss Go concurrency :( if(!noAlerts) { notifyOnScriptW("template_alert", e => { - if(e!=undefined) console.log("failed alert? why?", e) + if(e!=undefined) console.log("failed alert? why?",e) }, () => { if(!Template_alert) throw("template function not found"); addInitHook("after_phrases", () => { diff --git a/routes.go b/routes.go index 135626c4..a5c879c4 100644 --- a/routes.go +++ b/routes.go @@ -175,7 +175,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError var ok bool var sb strings.Builder - sb.Grow(c.AlertsGrowHint + (len(alerts) * c.AlertsGrowHint2)) + sb.Grow(c.AlertsGrowHint + (len(alerts) * (c.AlertsGrowHint2 + 1)) - 1) sb.WriteString(`{"msgs":[`) for i, alert := range alerts { if i != 0 {