Skip to content

Commit 0922998

Browse files
authored
Move the stopwatches to the eventsource stream (#14588)
Move the stopwatches to the eventsource stream Use the /user/events eventsource to update the stopwatches instead of polling /api/v1/user/stopwatches if the eventsource is enabled. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 430b3b7 commit 0922998

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

routers/events/events.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
package events
66

77
import (
8+
"encoding/json"
89
"net/http"
910
"time"
1011

12+
"code.gitea.io/gitea/models"
1113
"code.gitea.io/gitea/modules/context"
14+
"code.gitea.io/gitea/modules/convert"
1215
"code.gitea.io/gitea/modules/eventsource"
1316
"code.gitea.io/gitea/modules/graceful"
1417
"code.gitea.io/gitea/modules/log"
18+
"code.gitea.io/gitea/modules/setting"
1519
"code.gitea.io/gitea/routers/user"
1620
)
1721

@@ -55,6 +59,8 @@ func Events(ctx *context.Context) {
5559

5660
timer := time.NewTicker(30 * time.Second)
5761

62+
stopwatchTimer := time.NewTicker(setting.UI.Notification.MinTimeout)
63+
5864
loop:
5965
for {
6066
select {
@@ -75,6 +81,32 @@ loop:
7581
case <-shutdownCtx.Done():
7682
go unregister()
7783
break loop
84+
case <-stopwatchTimer.C:
85+
sws, err := models.GetUserStopwatches(ctx.User.ID, models.ListOptions{})
86+
if err != nil {
87+
log.Error("Unable to GetUserStopwatches: %v", err)
88+
continue
89+
}
90+
apiSWs, err := convert.ToStopWatches(sws)
91+
if err != nil {
92+
log.Error("Unable to APIFormat stopwatches: %v", err)
93+
continue
94+
}
95+
dataBs, err := json.Marshal(apiSWs)
96+
if err != nil {
97+
log.Error("Unable to marshal stopwatches: %v", err)
98+
continue
99+
}
100+
_, err = (&eventsource.Event{
101+
Name: "stopwatches",
102+
Data: string(dataBs),
103+
}).WriteTo(ctx.Resp)
104+
if err != nil {
105+
log.Error("Unable to write to EventStream for user %s: %v", ctx.User.Name, err)
106+
go unregister()
107+
break loop
108+
}
109+
ctx.Resp.Flush()
78110
case event, ok := <-messageChan:
79111
if !ok {
80112
break loop

web_src/js/features/eventsource.sharedworker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Source {
1212
this.listen('open');
1313
this.listen('logout');
1414
this.listen('notification-count');
15+
this.listen('stopwatches');
1516
this.listen('error');
1617
}
1718

web_src/js/features/stopwatch.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,58 @@ export async function initStopwatch() {
1717
$(this).parent().trigger('submit');
1818
});
1919

20-
if (!stopwatchEl || NotificationSettings.MinTimeout <= 0) {
20+
if (!stopwatchEl) {
21+
return;
22+
}
23+
24+
if (NotificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) {
25+
// Try to connect to the event source via the shared worker first
26+
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
27+
worker.addEventListener('error', (event) => {
28+
console.error(event);
29+
});
30+
worker.port.onmessageerror = () => {
31+
console.error('Unable to deserialize message');
32+
};
33+
worker.port.postMessage({
34+
type: 'start',
35+
url: `${window.location.origin}${AppSubUrl}/user/events`,
36+
});
37+
worker.port.addEventListener('message', (event) => {
38+
if (!event.data || !event.data.type) {
39+
console.error(event);
40+
return;
41+
}
42+
if (event.data.type === 'stopwatches') {
43+
updateStopwatchData(JSON.parse(event.data.data));
44+
} else if (event.data.type === 'error') {
45+
console.error(event.data);
46+
} else if (event.data.type === 'logout') {
47+
if (event.data !== 'here') {
48+
return;
49+
}
50+
worker.port.postMessage({
51+
type: 'close',
52+
});
53+
worker.port.close();
54+
window.location.href = AppSubUrl;
55+
}
56+
});
57+
worker.port.addEventListener('error', (e) => {
58+
console.error(e);
59+
});
60+
worker.port.start();
61+
window.addEventListener('beforeunload', () => {
62+
worker.port.postMessage({
63+
type: 'close',
64+
});
65+
worker.port.close();
66+
});
67+
68+
return;
69+
}
70+
71+
if (NotificationSettings.MinTimeout <= 0) {
2172
return;
2273
}
2374

@@ -59,6 +110,10 @@ async function updateStopwatch() {
59110
updateTimeInterval = null;
60111
}
61112

113+
return updateStopwatchData(data);
114+
}
115+
116+
async function updateStopwatchData(data) {
62117
const watch = data[0];
63118
const btnEl = $('.active-stopwatch-trigger');
64119
if (!watch) {

0 commit comments

Comments
 (0)