Move last_error from Context to Events

This commit is contained in:
link2xt
2023-01-16 22:01:26 +00:00
parent fb093253c6
commit dab288936a
3 changed files with 13 additions and 10 deletions

View File

@@ -233,12 +233,6 @@ pub struct InnerContext {
creation_time: SystemTime, 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<String>,
/// If debug logging is enabled, this contains all necessary information
pub(crate) debug_logging: RwLock<Option<DebugLogging>>, pub(crate) debug_logging: RwLock<Option<DebugLogging>>,
} }
@@ -382,7 +376,6 @@ impl Context {
server_id: RwLock::new(None), server_id: RwLock::new(None),
creation_time: std::time::SystemTime::now(), creation_time: std::time::SystemTime::now(),
last_full_folder_scan: Mutex::new(None), last_full_folder_scan: Mutex::new(None),
last_error: std::sync::RwLock::new("".to_string()),
debug_logging: RwLock::new(None), debug_logging: RwLock::new(None),
}; };

View File

@@ -1,6 +1,7 @@
//! # Events specification. //! # Events specification.
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use async_channel::{self as channel, Receiver, Sender, TrySendError}; use async_channel::{self as channel, Receiver, Sender, TrySendError};
use serde::Serialize; use serde::Serialize;
@@ -16,6 +17,11 @@ use crate::webxdc::StatusUpdateSerial;
pub struct Events { pub struct Events {
receiver: Receiver<Event>, receiver: Receiver<Event>,
sender: Sender<Event>, sender: Sender<Event>,
/// 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<RwLock<String>>,
} }
impl Default for Events { impl Default for Events {
@@ -29,7 +35,11 @@ impl Events {
pub fn new() -> Self { pub fn new() -> Self {
let (sender, receiver) = channel::bounded(1_000); let (sender, receiver) = channel::bounded(1_000);
Self { receiver, sender } Self {
receiver,
sender,
last_error: Arc::new(RwLock::new("".to_string())),
}
} }
/// Emits an event. /// Emits an event.

View File

@@ -50,13 +50,13 @@ impl Context {
/// Set last error string. /// Set last error string.
/// Implemented as blocking as used from macros in different, not always async blocks. /// Implemented as blocking as used from macros in different, not always async blocks.
pub fn set_last_error(&self, error: &str) { 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(); *last_error = error.to_string();
} }
/// Get last error string. /// Get last error string.
pub fn get_last_error(&self) -> 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() last_error.clone()
} }
} }