Repository navigation
Expand file tree
/
Copy pathlive_text.rs
More file actions
189 lines (165 loc) · 5.4 KB
/
Copy pathlive_text.rs
File metadata and controls
189 lines (165 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! What the dictation being recorded has said so far, for the HUDs (the
//! Labs feature live text) and the first run's practice field, which
//! [`demand`]s it while it shows and reads it with [`current`].
//!
//! The capture loop publishes through its recording's [`Publisher`], which
//! the recording owns: dropping the recording (release, Escape, a short tap,
//! a lost microphone) clears the text, so no ending can leave it on screen.
//! A HUD registers a [`watch`]er and is told each change from the capture
//! loop's thread; it must hand the text to its own thread and never wait.
//! The text is provisional: it is only ever shown, never pasted or kept.
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use crate::transcription_engine::Partial;
type Watcher = Arc<dyn Fn(Option<&Partial>) + Send + Sync>;
struct Board {
/// The publisher whose text is current, or 0 when none is.
owner: u64,
/// The current publisher's latest text.
text: Option<Partial>,
watchers: Vec<Watcher>,
}
static BOARD: Mutex<Board> = Mutex::new(Board {
owner: 0,
text: None,
watchers: Vec::new(),
});
static NEXT_PUBLISHER: AtomicU64 = AtomicU64::new(1);
static DEMANDS: AtomicUsize = AtomicUsize::new(0);
fn board() -> MutexGuard<'static, Board> {
BOARD.lock().unwrap_or_else(|error| error.into_inner())
}
/// Calls `watcher` with each new text, and with `None` once the recording
/// that published it ends.
pub(crate) fn watch(watcher: impl Fn(Option<&Partial>) + Send + Sync + 'static) {
board().watchers.push(Arc::new(watcher));
}
/// The text the recording in progress has shown, if any.
pub(crate) fn current() -> Option<Partial> {
board().text.clone()
}
/// Asks dictation streams to publish their text while it is held, without
/// the experiment (the first run's practice field). The HUD plate still
/// shows only with the experiment.
pub(crate) struct Demand(());
pub(crate) fn demand() -> Demand {
DEMANDS.fetch_add(1, Ordering::AcqRel);
Demand(())
}
impl Drop for Demand {
fn drop(&mut self) {
DEMANDS.fetch_sub(1, Ordering::AcqRel);
}
}
/// Whether anything holds a [`Demand`].
pub(crate) fn demanded() -> bool {
DEMANDS.load(Ordering::Acquire) > 0
}
/// One recording's hold on the live text.
pub(crate) struct Publisher {
id: u64,
}
impl Publisher {
pub(crate) fn new() -> Self {
Self {
id: NEXT_PUBLISHER.fetch_add(1, Ordering::Relaxed),
}
}
/// Shows `partial`, replacing any other recording's text.
pub(crate) fn show(&self, partial: &Partial) {
let watchers = {
let mut board = board();
board.owner = self.id;
board.text = Some(partial.clone());
board.watchers.clone()
};
for watcher in watchers {
watcher(Some(partial));
}
}
}
impl Drop for Publisher {
/// Clears the text if it is still this recording's.
fn drop(&mut self) {
let watchers = {
let mut board = board();
if board.owner != self.id {
return;
}
board.owner = 0;
board.text = None;
board.watchers.clone()
};
for watcher in watchers {
watcher(None);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The board is process-wide; these tests take turns with it.
static TURN: Mutex<()> = Mutex::new(());
fn turn() -> MutexGuard<'static, ()> {
TURN.lock().unwrap_or_else(|error| error.into_inner())
}
fn said(stable: &str) -> Partial {
Partial {
stable: stable.into(),
tentative: String::new(),
}
}
/// Records every call a watcher gets, as the stable text or `None`.
fn watched() -> Arc<Mutex<Vec<Option<String>>>> {
let seen = Arc::new(Mutex::new(Vec::new()));
let log = seen.clone();
watch(move |partial| {
log.lock()
.unwrap()
.push(partial.map(|partial| partial.stable.clone()));
});
seen
}
#[test]
fn a_recording_shows_its_text_and_clears_it_when_it_ends() {
let _turn = turn();
let seen = watched();
let recording = Publisher::new();
recording.show(&said("so the plan"));
recording.show(&said("so the plan for today"));
assert_eq!(current().unwrap().stable, "so the plan for today");
drop(recording);
assert_eq!(current(), None);
assert_eq!(
*seen.lock().unwrap(),
[
Some("so the plan".into()),
Some("so the plan for today".into()),
None
]
);
}
#[test]
fn an_older_recording_ending_never_clears_a_newer_ones_text() {
let _turn = turn();
let seen = watched();
let older = Publisher::new();
older.show(&said("old"));
let newer = Publisher::new();
newer.show(&said("new"));
drop(older);
assert_eq!(
*seen.lock().unwrap(),
[Some("old".into()), Some("new".into())]
);
drop(newer);
assert_eq!(seen.lock().unwrap().last(), Some(&None));
}
#[test]
fn a_recording_that_never_showed_text_clears_nothing() {
let _turn = turn();
let seen = watched();
drop(Publisher::new());
assert!(seen.lock().unwrap().is_empty());
}
}