diff --git a/src/context.rs b/src/context.rs index a9bbd01ad..9b0476480 100644 --- a/src/context.rs +++ b/src/context.rs @@ -233,12 +233,6 @@ pub struct InnerContext { creation_time: SystemTime, - /// The text of the last error logged and emitted as an event. - /// If the ui wants to display an error after a failure, - /// `last_error` should be used to avoid races with the event thread. - pub(crate) last_error: std::sync::RwLock, - - /// If debug logging is enabled, this contains all necessary information pub(crate) debug_logging: RwLock>, } @@ -382,7 +376,6 @@ impl Context { server_id: RwLock::new(None), creation_time: std::time::SystemTime::now(), last_full_folder_scan: Mutex::new(None), - last_error: std::sync::RwLock::new("".to_string()), debug_logging: RwLock::new(None), }; diff --git a/src/events.rs b/src/events.rs index ebb1d299f..92e528852 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,6 +1,7 @@ //! # Events specification. use std::path::PathBuf; +use std::sync::{Arc, RwLock}; use async_channel::{self as channel, Receiver, Sender, TrySendError}; use serde::Serialize; @@ -16,6 +17,11 @@ use crate::webxdc::StatusUpdateSerial; pub struct Events { receiver: Receiver, sender: Sender, + + /// The text of the last error logged and emitted as an event. + /// If the ui wants to display an error after a failure, + /// `last_error` should be used to avoid races with the event thread. + pub(crate) last_error: Arc>, } impl Default for Events { @@ -29,7 +35,11 @@ impl Events { pub fn new() -> Self { let (sender, receiver) = channel::bounded(1_000); - Self { receiver, sender } + Self { + receiver, + sender, + last_error: Arc::new(RwLock::new("".to_string())), + } } /// Emits an event. diff --git a/src/log.rs b/src/log.rs index c755484e6..ec8f98200 100644 --- a/src/log.rs +++ b/src/log.rs @@ -50,13 +50,13 @@ impl Context { /// Set last error string. /// Implemented as blocking as used from macros in different, not always async blocks. pub fn set_last_error(&self, error: &str) { - let mut last_error = self.last_error.write().unwrap(); + let mut last_error = self.events.last_error.write().unwrap(); *last_error = error.to_string(); } /// Get last error string. pub fn get_last_error(&self) -> String { - let last_error = &*self.last_error.read().unwrap(); + let last_error = &*self.events.last_error.read().unwrap(); last_error.clone() } }