perf(network): Improved performance and reactiveness of the UI by speeding up network requests and clearing the channel whenever a request is cancelled/the UI is routing
This commit is contained in:
+8
-4
@@ -40,7 +40,7 @@ pub trait NetworkTrait {
|
||||
#[derive(Clone)]
|
||||
pub struct Network<'a, 'b> {
|
||||
client: Client,
|
||||
cancellation_token: CancellationToken,
|
||||
pub cancellation_token: CancellationToken,
|
||||
pub app: &'a Arc<Mutex<App<'b>>>,
|
||||
}
|
||||
|
||||
@@ -74,6 +74,13 @@ impl<'a, 'b> Network<'a, 'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn reset_cancellation_token(&mut self) {
|
||||
let mut app = self.app.lock().await;
|
||||
self.cancellation_token = app.reset_cancellation_token();
|
||||
app.should_refresh = true;
|
||||
app.is_loading = false;
|
||||
}
|
||||
|
||||
async fn handle_request<B, R>(
|
||||
&mut self,
|
||||
request_props: RequestProps<B>,
|
||||
@@ -89,9 +96,6 @@ impl<'a, 'b> Network<'a, 'b> {
|
||||
select! {
|
||||
_ = self.cancellation_token.cancelled() => {
|
||||
warn!("Received Cancel request. Cancelling request to: {request_uri}");
|
||||
let mut app = self.app.lock().await;
|
||||
self.cancellation_token = app.reset_cancellation_token();
|
||||
app.is_loading = false;
|
||||
Ok(R::default())
|
||||
}
|
||||
resp = self.call_api(request_props).await.send() => {
|
||||
|
||||
@@ -181,11 +181,31 @@ mod tests {
|
||||
|
||||
assert!(!async_server.matched_async().await);
|
||||
assert!(app_arc.lock().await.error.text.is_empty());
|
||||
assert!(!network.cancellation_token.is_cancelled());
|
||||
assert!(resp.is_ok());
|
||||
assert_eq!(resp.unwrap(), Test::default());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reset_cancellation_token() {
|
||||
let cancellation_token = CancellationToken::new();
|
||||
let (tx, _) = mpsc::channel::<NetworkEvent>(500);
|
||||
let app_arc = Arc::new(Mutex::new(App::new(
|
||||
tx,
|
||||
AppConfig::default(),
|
||||
cancellation_token.clone(),
|
||||
)));
|
||||
app_arc.lock().await.should_refresh = false;
|
||||
app_arc.lock().await.is_loading = true;
|
||||
let mut network = Network::new(&app_arc, cancellation_token, Client::new());
|
||||
network.cancellation_token.cancel();
|
||||
|
||||
network.reset_cancellation_token().await;
|
||||
|
||||
assert!(!network.cancellation_token.is_cancelled());
|
||||
assert!(app_arc.lock().await.should_refresh);
|
||||
assert!(!app_arc.lock().await.is_loading);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_request_get_invalid_body() {
|
||||
let mut server = Server::new_async().await;
|
||||
|
||||
Reference in New Issue
Block a user