| index 1aec305..f0791fe 100644 |
| --- a/internal/slides/presentation.go |
| +++ b/internal/slides/presentation.go |
| @@ -1251,22 +1251,33 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese |
| fl.Flush() |
| return true |
| } |
| - // lastSeq is the newest position sequence this stream has sent. Two |
| - // presses commit in order but publish from their own goroutines, |
| - // so the older one's event can reach the hub second; an event at or |
| - // below lastSeq is that, or a repeat, and is not sent. Sequence 0 |
| - // is a session begun before positions existed, or the ended event |
| - // endPresentationsOf publishes, and always goes through. |
| + // deliver is the one place a position is written to this stream: |
| + // the connect event, the catch-up after a hold, and every event the |
| + // hub sends all go through it. |
| + // |
| + // lastSeq is the newest position sequence it has written. Two |
| + // presses commit in order but publish from their own goroutines, so |
| + // the older one's event can reach the hub second, and a catch-up |
| + // can write the row before an event published earlier arrives. An |
| + // event at or below lastSeq is one of those, or a repeat of what |
| + // the page has, and is not written. lastSeq moves only when an |
| + // event is written, so nothing newer than the page has is lost. |
| + // Sequence 0 is a session begun before positions existed, or the |
| + // ended event endPresentationsOf publishes, and is always written. |
| var lastSeq int64 |
| - stale := func(e present.Event) bool { |
| - if e.Seq == 0 { |
| + deliver := func(e present.Event) bool { |
| + if e.Seq != 0 && e.Seq <= lastSeq { |
| + return true |
| + } |
| + body, err := json.Marshal(e) |
| + if err != nil { |
| return false |
| } |
| - if e.Seq <= lastSeq { |
| - return true |
| + if !send("event: slide\ndata: " + string(body) + "\n\n") { |
| + return false |
| } |
| - lastSeq = e.Seq |
| - return false |
| + lastSeq = max(lastSeq, e.Seq) |
| + return true |
| } |
| // catchUp is what a held stream owes when the presenter's standing |
| // returns: the talk as it is now, read afresh. |
| @@ -1275,15 +1286,12 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese |
| if err := a.db.G.WithContext(r.Context()).Take(&now, p.ID).Error; err != nil { |
| return false |
| } |
| - // Sent whatever lastSeq says: an event dropped while the stream |
| - // was held may have moved lastSeq without reaching the page, |
| - // and the row is the newest there is. |
| - e := eventFor(now) |
| - lastSeq = max(lastSeq, e.Seq) |
| - body, err := json.Marshal(e) |
| - return err == nil && send("event: slide\ndata: "+string(body)+"\n\n") |
| + return deliver(eventFor(now)) |
| } |
| - write := func(text string) bool { |
| + // open runs the checks before any write: false ends the stream. A |
| + // stream that was held and stands again is caught up first, so |
| + // whatever the caller writes next is compared with the row. |
| + open := func() bool { |
| if alive != nil && !alive() { |
| return false |
| } |
| @@ -1291,23 +1299,19 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese |
| if !stands() { |
| return false |
| } |
| - if held { |
| - return !strings.HasPrefix(text, ":") || send(text) |
| - } |
| - if wasHeld && !catchUp() { |
| - return false |
| - } |
| - return send(text) |
| + return held || !wasHeld || catchUp() |
| + } |
| + // ping is the heartbeat, which a held stream still sends. |
| + ping := func() bool { |
| + return open() && send(": ping\n\n") |
| } |
| + // event is one of the hub's events. A held stream drops it; the |
| + // catch-up writes the row when the hold ends. |
| event := func(e present.Event) bool { |
| - if stale(e) { |
| - return true |
| - } |
| - body, err := json.Marshal(e) |
| - if err != nil { |
| + if !open() { |
| return false |
| } |
| - return write("event: slide\ndata: " + string(body) + "\n\n") |
| + return held || deliver(e) |
| } |
| |
| if !event(eventFor(current)) { |
| @@ -1346,7 +1350,7 @@ func (a *app) streamPresentation(w http.ResponseWriter, r *http.Request, p Prese |
| } |
| heartbeat.Reset(streamHeartbeat) |
| case <-heartbeat.C: |
| - if !write(": ping\n\n") { |
| + if !ping() { |
| return |
| } |
| heartbeat.Reset(streamHeartbeat) |