mirror of
https://github.com/go-gitea/gitea
synced 2024-11-12 21:24:24 +00:00
e46dbec294
* Move EventSource to SharedWorker (#12095) Backport #12095 Move EventSource to use a SharedWorker. This prevents issues with HTTP/1.1 open browser connections from preventing gitea from opening multiple tabs. Also allow setting EVENT_SOURCE_UPDATE_TIME to disable EventSource updating Fix #11978 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: techknowlogick <techknowlogick@gitea.io> * Bugfix for shared event source For some reason our eslint configuration is not working correctly and a bug has become apparent when trying to backport this to 1.12. Signed-off-by: Andrew Thornton <art27@cantab.net> * Re-fix #12095 again Unfortunately some of the suggested changes to #12095 introduced bugs which due to caching behaviour of sharedworkers were not caught on simple tests. These are as follows: * Changing from simple for loop to use includes here: ```js register(port) { if (!this.clients.includes(port)) return; this.clients.push(port); port.postMessage({ type: 'status', message: `registered to ${this.url}`, }); } ``` The additional `!` prevents any clients from being added and should read: ```js if (this.clients.includes(port)) return; ``` * Dropping the use of jQuery `$(...)` selection and using DOM `querySelector` here: ```js async function receiveUpdateCount(event) { try { const data = JSON.parse(event.data); const notificationCount = document.querySelector('.notification_count'); if (data.Count > 0) { notificationCount.classList.remove('hidden'); } else { notificationCount.classList.add('hidden'); } notificationCount.text() = `${data.Count}`; await updateNotificationTable(); } catch (error) { console.error(error, event); } } ``` Requires that `notificationCount.text()` be changed to use `textContent` instead. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package eventsource
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
)
|
|
|
|
// Init starts this eventsource
|
|
func (m *Manager) Init() {
|
|
if setting.UI.Notification.EventSourceUpdateTime <= 0 {
|
|
return
|
|
}
|
|
go graceful.GetManager().RunWithShutdownContext(m.Run)
|
|
}
|
|
|
|
// Run runs the manager within a provided context
|
|
func (m *Manager) Run(ctx context.Context) {
|
|
then := timeutil.TimeStampNow().Add(-2)
|
|
timer := time.NewTicker(setting.UI.Notification.EventSourceUpdateTime)
|
|
loop:
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
break loop
|
|
case <-timer.C:
|
|
now := timeutil.TimeStampNow().Add(-2)
|
|
|
|
uidCounts, err := models.GetUIDsAndNotificationCounts(then, now)
|
|
if err != nil {
|
|
log.Error("Unable to get UIDcounts: %v", err)
|
|
}
|
|
for _, uidCount := range uidCounts {
|
|
m.SendMessage(uidCount.UserID, &Event{
|
|
Name: "notification-count",
|
|
Data: uidCount,
|
|
})
|
|
}
|
|
then = now
|
|
}
|
|
}
|
|
m.UnregisterAll()
|
|
}
|