oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// Package present is the fan-out behind a live presentation: one
2// session moved, told to every page watching it.
3//
4// It is Docs' hub (its internal/docs/hub.go) with one difference worth
5// stating, because it is the difference that decides everything else
6// here: Docs' notification is ADVISORY — a seq that means "there is
7// something at least this new", so a subscriber that misses ten is
8// still correct after fetching once. A presentation event is not
9// advisory in the same way: `{slide, blackout, ended}` is the whole
10// state, so what a subscriber must never do is apply a STALE one.
11//
12// Hence the two decisions below. The channel is buffered eight rather
13// than one, because a burst of arrow-key presses is a real thing and
14// eight covers it without the sender ever waiting. And a subscriber
15// that falls behind that is DROPPED — unsubscribed, its channel closed
16// — rather than having events silently skipped: the handler holding it
17// sees the close, ends the response, and the browser reconnects, which
18// re-reads the session's real state from the row on connect (the
19// stream handler's one event on connect). A skipped event, by
20// contrast, would leave a projector showing slide 4 for the rest of a
21// talk with nothing to notice it.
22//
23// Scope, like Docs': one process. Slides is one instance per team, so
24// there is no second process to reach; this type is the seam that
25// changes on the day there is.
26package present
27
28import "sync"
29
30// Event is a presentation's whole visible state at one moment — never
31// a delta, so a subscriber that applies one is exactly right rather
32// than approximately.
33//
34// The JSON tags are the wire format of the stream's `event: slide`
35// data (presentation.go), and static/present.js reads the first three
36// names. Changing one is changing the protocol.
37type Event struct {
38 // Slide is the 1-based number of the slide being shown
39 // (presentation.go's eventFor).
40 Slide int `json:"slide"`
41 // Blackout is whether the screen is blacked out. Carried
42 // separately from the slide because coming back from a blackout
43 // must not move the deck.
44 Blackout bool `json:"blackout"`
45 // Ended says the session is over. A page that hears it stops
46 // following rather than sitting on a slide that will never move
47 // again.
48 Ended bool `json:"ended"`
49
50 // The absolute position, for a session that has one
51 // (presenting_position.go). Nil for a session begun before
52 // positions existed, whose events stay the three fields above:
53 // an embedded pointer's fields are left out of the JSON while it is
54 // nil, and are all there, empty or not, when it is set.
55 *Position
56}
57
58// Position is a session's absolute position on the wire: the slide by
59// id ("" for an empty talk, which is a position too), the press on it,
60// the mode, the session's sequence, which a window compares so a late
61// event never moves it backwards, and when the talk's timer started
62// (Unix ms), null while it waits for the first press. Every window
63// shows the timer from this, whoever pressed.
64type Position struct {
65 SlideID string `json:"slideId"`
66 Step int `json:"step"`
67 Mode string `json:"mode"`
68 Seq int64 `json:"seq"`
69 TimerAt *int64 `json:"timerAt"`
70}
71
72// subscriberBuffer is how many events a subscriber may fall behind by
73// before it is dropped. Eight is a burst of arrow presses; a page that
74// has not read eight is not reading at all.
75const subscriberBuffer = 8
76
77// Hub is the per-process fan-out, keyed by presentation sqid.
78type Hub struct {
79 mu sync.Mutex
80 subs map[string]map[chan Event]struct{}
81}
82
83// New builds an empty hub. One per app (handlers.go).
84func New() *Hub {
85 return &Hub{subs: make(map[string]map[chan Event]struct{})}
86}
87
88// Subscribe registers interest in one session and returns the channel
89// to wait on plus the function that unregisters it.
90//
91// The returned cancel is idempotent — the stream handler defers it and
92// may also have reason to stop early — and it never closes the
93// channel. Only Publish closes one, and only when it is dropping the
94// subscriber, so "the channel is closed" carries exactly one meaning
95// for a reader: you fell behind, reconnect. A cancel that closed too
96// would make that signal ambiguous at the one moment it matters.
97func (h *Hub) Subscribe(key string) (<-chan Event, func()) {
98 ch := make(chan Event, subscriberBuffer)
99
100 h.mu.Lock()
101 if h.subs[key] == nil {
102 h.subs[key] = make(map[chan Event]struct{})
103 }
104 h.subs[key][ch] = struct{}{}
105 h.mu.Unlock()
106
107 var once sync.Once
108 return ch, func() {
109 once.Do(func() {
110 h.mu.Lock()
111 defer h.mu.Unlock()
112 h.remove(key, ch)
113 })
114 }
115}
116
117// Publish sends e to everyone watching key.
118//
119// It runs on whichever request goroutine just moved the session (a
120// phone's POST, the presenter's keyboard), so it must never block: a
121// browser on a wedged connection would otherwise hold a stranger's
122// command open. A subscriber whose buffer is full is therefore removed
123// and its channel closed, inside the same lock that found it, so no
124// send can race the close.
125func (h *Hub) Publish(key string, e Event) {
126 h.mu.Lock()
127 defer h.mu.Unlock()
128
129 for ch := range h.subs[key] {
130 select {
131 case ch <- e:
132 default:
133 h.remove(key, ch)
134 close(ch)
135 }
136 }
137}
138
139// remove takes ch out of key's set and drops the set with its last
140// member — otherwise this map keeps one empty entry per session ever
141// presented for the life of the process. Callers hold h.mu.
142func (h *Hub) remove(key string, ch chan Event) {
143 subs, ok := h.subs[key]
144 if !ok {
145 return
146 }
147 delete(subs, ch)
148 if len(subs) == 0 {
149 delete(h.subs, key)
150 }
151}
152