fix(app): a speech speed saved while the services start reaches them once they run
The start may have read the settings before the save, and the controller dropped a speed saved with nothing running yet. It now keeps the latest speed and hands it to the services when they come up. Version 0.1.5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -66,6 +66,9 @@ struct Inner {
|
||||
feeder: Option<tokio::task::JoinHandle<()>>,
|
||||
/// Stop was asked for while starting.
|
||||
stop_asked: bool,
|
||||
/// The latest speech speed saved (equal to the settings file's): a start may have read the
|
||||
/// settings before it was saved, so the services get it again once they run.
|
||||
tts_speed: Option<f64>,
|
||||
}
|
||||
|
||||
pub struct Controller {
|
||||
@@ -77,7 +80,7 @@ pub struct Controller {
|
||||
impl Controller {
|
||||
pub fn new(host: impl Host) -> Arc<Self> {
|
||||
Arc::new(Self { host: Box::new(host), inner: Mutex::new(Inner { state: State::Stopped, error: None, running: None,
|
||||
dashboard: Arc::default(), feeder: None, stop_asked: false }), usage: tokio::sync::Mutex::new(None) })
|
||||
dashboard: Arc::default(), feeder: None, stop_asked: false, tts_speed: None }), usage: tokio::sync::Mutex::new(None) })
|
||||
}
|
||||
|
||||
pub fn state(&self) -> State {
|
||||
@@ -133,6 +136,9 @@ impl Controller {
|
||||
d2.lock().unwrap().on_event(&ev, Instant::now());
|
||||
}
|
||||
});
|
||||
if let Some(speed) = i.tts_speed {
|
||||
running.set_tts_speed(speed);
|
||||
}
|
||||
i.state = State::Running;
|
||||
i.running = Some(running);
|
||||
i.dashboard = dash;
|
||||
@@ -169,10 +175,14 @@ impl Controller {
|
||||
Err("cancelled".into())
|
||||
}
|
||||
|
||||
/// Give the running services a new speech speed. False when nothing runs (the next start reads it
|
||||
/// from the settings).
|
||||
/// Give the running services a new speech speed. False when nothing runs yet; a start in
|
||||
/// progress passes it on once the services run.
|
||||
pub fn set_tts_speed(&self, speed: f64) -> bool {
|
||||
let running = self.inner.lock().unwrap().running.clone();
|
||||
let running = {
|
||||
let mut i = self.inner.lock().unwrap();
|
||||
i.tts_speed = Some(speed);
|
||||
i.running.clone()
|
||||
};
|
||||
running.map(|r| r.set_tts_speed(speed)).is_some()
|
||||
}
|
||||
|
||||
@@ -495,10 +505,24 @@ mod tests {
|
||||
assert!(!c.set_tts_speed(1.7), "nothing runs: the next start reads the settings");
|
||||
c.start().await.unwrap();
|
||||
assert!(c.set_tts_speed(1.8));
|
||||
assert_eq!(*counts.speeds.lock().unwrap(), [1.8]);
|
||||
assert_eq!(counts.speeds.lock().unwrap().last(), Some(&1.8), "the latest speed is the one they use");
|
||||
assert_eq!(counts.starts.load(Ordering::SeqCst), 1, "no restart");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_speech_speed_saved_while_starting_reaches_the_services_once_they_run() {
|
||||
// The start read the settings before the save, so the new speed must follow it.
|
||||
let counts = Arc::new(Counts::default());
|
||||
let c = Controller::new(FakeHost { counts: counts.clone(), fail_first: AtomicU32::new(0), events: Mutex::new(None) });
|
||||
c.start_in_background().unwrap();
|
||||
assert_eq!(c.state(), State::Starting);
|
||||
assert!(!c.set_tts_speed(1.9), "not running yet");
|
||||
tokio::time::timeout(Duration::from_secs(2), async {
|
||||
while c.state() != State::Running { tokio::time::sleep(Duration::from_millis(5)).await; }
|
||||
}).await.unwrap();
|
||||
assert_eq!(*counts.speeds.lock().unwrap(), [1.9]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_stop_while_starting_cancels_the_start_or_stops_what_came_up() {
|
||||
for honours_cancel in [true, false] {
|
||||
|
||||
Reference in New Issue
Block a user