Files
Gitea/services/websocket/stopwatch.go
T
silverwind 7308c5fb97 enhance(websocket): improve client synchronization (#38847)
Keep notification counts and stopwatches in sync when tabs open or
reconnect, closing the gap behind [this
flake](https://github.com/go-gitea/gitea/actions/runs/31312525323/job/93242429394).

1. Send current notification counts and stopwatch state on each
WebSocket connection, replacing the separate state fetches tabs made on
every connect.
2. Tabs joining an already open connection fetch current state once,
since its snapshot went out before they attached.
3. Send events queued during the handshake before the snapshot, so older
events can't roll it back.
4. Wait for the Redis SUBSCRIBE acknowledgement, so the snapshot read
after subscribing can't miss an event.
2026-10-29 05:18:17 +00:00

42 lines
1.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package websocket
import (
"context"
"gitea.dev/models/db"
issues_model "gitea.dev/models/issues"
user_model "gitea.dev/models/user"
"gitea.dev/modules/log"
"gitea.dev/modules/setting"
"gitea.dev/modules/util"
"gitea.dev/services/convert"
)
const eventStopwatches = "stopwatches" // keep in sync with web_src/js/types.ts
func (n *wsNotifier) StopwatchChanged(ctx context.Context, user *user_model.User) {
publishUserEvent(user.ID, func() []byte { return stopwatchesEvent(ctx, user) })
}
func stopwatchesEvent(ctx context.Context, user *user_model.User) []byte {
if !setting.Service.EnableTimetracking {
return nil
}
sws, err := issues_model.GetUserStopwatches(ctx, user.ID, db.ListOptions{})
if err != nil {
log.Error("websocket: GetUserStopwatches %d: %v", user.ID, err)
return nil
}
apiStopWatches, err := convert.ToStopWatches(ctx, user, sws)
if err != nil {
if !issues_model.IsErrIssueNotExist(err) {
log.Error("websocket: ToStopWatches: %v", err)
}
return nil
}
return makeEventMessage(eventStopwatches, "", util.SliceNilAsEmpty(apiStopWatches))
}