You've already forked AstralRinth
forked from didirus/AstralRinth
Updating + Profile Repairs + Performance Improvements (#97)
* repairing * Main framework for updating * add jsconfig * more work * Improve performance * Finish updating * run lint
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::event::{
|
||||
EventError, LoadingBar, LoadingBarId, LoadingBarType, ProcessPayloadType,
|
||||
EventError, LoadingBar, LoadingBarType, ProcessPayloadType,
|
||||
ProfilePayloadType,
|
||||
};
|
||||
use futures::prelude::*;
|
||||
@@ -11,6 +11,7 @@ use crate::event::{
|
||||
};
|
||||
#[cfg(feature = "tauri")]
|
||||
use tauri::Manager;
|
||||
use uuid::Uuid;
|
||||
|
||||
/*
|
||||
Events are a way we can communciate with the Tauri frontend from the Rust backend.
|
||||
@@ -39,22 +40,23 @@ use tauri::Manager;
|
||||
// Initialize a loading bar for use in emit_loading
|
||||
// This will generate a LoadingBarId, which is used to refer to the loading bar uniquely.
|
||||
// total is the total amount of work to be done- all emissions will be considered a fraction of this value (should be 1 or 100 for simplicity)
|
||||
// default_message is the message to display on the loading bar if no message is passed to emit_loading
|
||||
// title is the title of the loading bar
|
||||
pub async fn init_loading(
|
||||
bar_type: LoadingBarType,
|
||||
total: f64,
|
||||
default_message: &str,
|
||||
) -> crate::Result<LoadingBarId> {
|
||||
title: &str,
|
||||
) -> crate::Result<Uuid> {
|
||||
let event_state = crate::EventState::get().await?;
|
||||
let key = LoadingBarId::new(bar_type);
|
||||
let key = Uuid::new_v4();
|
||||
|
||||
event_state.loading_bars.write().await.insert(
|
||||
key.clone(),
|
||||
key,
|
||||
LoadingBar {
|
||||
loading_bar_id: key.clone(),
|
||||
message: default_message.to_string(),
|
||||
loading_bar_id: key,
|
||||
message: title.to_string(),
|
||||
total,
|
||||
current: 0.0,
|
||||
bar_type,
|
||||
},
|
||||
);
|
||||
// attempt an initial loading_emit event to the frontend
|
||||
@@ -62,6 +64,40 @@ pub async fn init_loading(
|
||||
Ok(key)
|
||||
}
|
||||
|
||||
pub async fn init_or_edit_loading(
|
||||
id: Option<Uuid>,
|
||||
bar_type: LoadingBarType,
|
||||
total: f64,
|
||||
title: &str,
|
||||
) -> crate::Result<Uuid> {
|
||||
if let Some(id) = id {
|
||||
edit_loading(id, bar_type, total, title).await?;
|
||||
|
||||
Ok(id)
|
||||
} else {
|
||||
init_loading(bar_type, total, title).await
|
||||
}
|
||||
}
|
||||
|
||||
// Edits a loading bar's type
|
||||
pub async fn edit_loading(
|
||||
id: Uuid,
|
||||
bar_type: LoadingBarType,
|
||||
total: f64,
|
||||
title: &str,
|
||||
) -> crate::Result<()> {
|
||||
let event_state = crate::EventState::get().await?;
|
||||
|
||||
if let Some(bar) = event_state.loading_bars.write().await.get_mut(&id) {
|
||||
bar.bar_type = bar_type;
|
||||
bar.total = total;
|
||||
bar.message = title.to_string();
|
||||
};
|
||||
|
||||
emit_loading(&id, 0.0, None).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// emit_loading emits a loading event to the frontend
|
||||
// key refers to the loading bar to update
|
||||
// increment refers to by what relative increment to the loading struct's total to update
|
||||
@@ -69,7 +105,7 @@ pub async fn init_loading(
|
||||
// By convention, fraction is the fraction of the progress bar that is filled
|
||||
#[allow(unused_variables)]
|
||||
pub async fn emit_loading(
|
||||
key: &LoadingBarId,
|
||||
key: &Uuid,
|
||||
increment_frac: f64,
|
||||
message: Option<&str>,
|
||||
) -> crate::Result<()> {
|
||||
@@ -79,14 +115,14 @@ pub async fn emit_loading(
|
||||
let loading_bar = match loading_bar.get_mut(key) {
|
||||
Some(f) => f,
|
||||
None => {
|
||||
return Err(EventError::NoLoadingBar(key.clone()).into());
|
||||
return Err(EventError::NoLoadingBar(*key).into());
|
||||
}
|
||||
};
|
||||
|
||||
// Tick up loading bar
|
||||
loading_bar.current += increment_frac;
|
||||
let display_frac = loading_bar.current / loading_bar.total;
|
||||
let display_frac = if display_frac > 1.0 {
|
||||
let display_frac = if display_frac >= 1.0 {
|
||||
None // by convention, when its done, we submit None
|
||||
// any further updates will be ignored (also sending None)
|
||||
} else {
|
||||
@@ -101,8 +137,8 @@ pub async fn emit_loading(
|
||||
LoadingPayload {
|
||||
fraction: display_frac,
|
||||
message: message.unwrap_or(&loading_bar.message).to_string(),
|
||||
event: key.key.clone(),
|
||||
loader_uuid: key.uuid,
|
||||
event: loading_bar.bar_type.clone(),
|
||||
loader_uuid: loading_bar.loading_bar_id,
|
||||
},
|
||||
)
|
||||
.map_err(EventError::from)?;
|
||||
@@ -132,7 +168,7 @@ pub async fn emit_warning(message: &str) -> crate::Result<()> {
|
||||
// emit_process(uuid, pid, event, message)
|
||||
#[allow(unused_variables)]
|
||||
pub async fn emit_process(
|
||||
uuid: uuid::Uuid,
|
||||
uuid: Uuid,
|
||||
pid: u32,
|
||||
event: ProcessPayloadType,
|
||||
message: &str,
|
||||
@@ -159,7 +195,7 @@ pub async fn emit_process(
|
||||
// emit_profile(path, event)
|
||||
#[allow(unused_variables)]
|
||||
pub async fn emit_profile(
|
||||
uuid: uuid::Uuid,
|
||||
uuid: Uuid,
|
||||
path: PathBuf,
|
||||
name: &str,
|
||||
event: ProfilePayloadType,
|
||||
@@ -253,7 +289,7 @@ macro_rules! loading_join {
|
||||
pub async fn loading_try_for_each_concurrent<I, F, Fut, T>(
|
||||
stream: I,
|
||||
limit: Option<usize>,
|
||||
key: Option<&LoadingBarId>,
|
||||
key: Option<&Uuid>,
|
||||
total: f64,
|
||||
num_futs: usize, // num is in here as we allow Iterator to be passed in, which doesn't have a size
|
||||
message: Option<&str>,
|
||||
@@ -285,7 +321,7 @@ where
|
||||
pub async fn loading_try_for_each_concurrent<I, F, Fut, T>(
|
||||
stream: I,
|
||||
limit: Option<usize>,
|
||||
_key: Option<&LoadingBarId>,
|
||||
_key: Option<&Uuid>,
|
||||
_total: f64,
|
||||
_num_futs: usize, // num is in here as we allow Iterator to be passed in, which doesn't have a size
|
||||
_message: Option<&str>,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Theseus state management system
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashMap, fmt, path::PathBuf, sync::Arc};
|
||||
use std::{collections::HashMap, path::PathBuf, sync::Arc};
|
||||
use tokio::sync::OnceCell;
|
||||
use tokio::sync::RwLock;
|
||||
use uuid::Uuid;
|
||||
@@ -14,7 +14,7 @@ pub struct EventState {
|
||||
/// Tauri app
|
||||
#[cfg(feature = "tauri")]
|
||||
pub app: tauri::AppHandle,
|
||||
pub loading_bars: RwLock<HashMap<LoadingBarId, LoadingBar>>,
|
||||
pub loading_bars: RwLock<HashMap<Uuid, LoadingBar>>,
|
||||
}
|
||||
|
||||
impl EventState {
|
||||
@@ -48,6 +48,13 @@ impl EventState {
|
||||
Ok(EVENT_STATE.get().ok_or(EventError::NotInitialized)?.clone())
|
||||
}
|
||||
|
||||
pub async fn list_progress_bars() -> crate::Result<HashMap<Uuid, LoadingBar>>
|
||||
{
|
||||
let value = Self::get().await?;
|
||||
let read = value.loading_bars.read().await;
|
||||
Ok(read.clone())
|
||||
}
|
||||
|
||||
// Initialization requires no app handle in non-tauri mode, so we can just use the same function
|
||||
#[cfg(not(feature = "tauri"))]
|
||||
pub async fn get() -> crate::Result<Arc<Self>> {
|
||||
@@ -55,35 +62,13 @@ impl EventState {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct LoadingBar {
|
||||
pub loading_bar_id: LoadingBarId,
|
||||
pub loading_bar_id: Uuid,
|
||||
pub message: String,
|
||||
pub total: f64,
|
||||
pub current: f64,
|
||||
}
|
||||
|
||||
// Loading Bar Id lets us uniquely identify loading bars stored in the state
|
||||
// the uuid lets us identify loading bars across threads
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, Hash, PartialEq, Eq)]
|
||||
pub struct LoadingBarId {
|
||||
pub key: LoadingBarType,
|
||||
pub uuid: Uuid,
|
||||
}
|
||||
|
||||
impl LoadingBarId {
|
||||
pub fn new(key: LoadingBarType) -> Self {
|
||||
Self {
|
||||
key,
|
||||
uuid: Uuid::new_v4(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LoadingBarId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}-{}", self.key, self.uuid)
|
||||
}
|
||||
pub bar_type: LoadingBarType,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, Hash, PartialEq, Eq)]
|
||||
@@ -98,7 +83,10 @@ pub enum LoadingBarType {
|
||||
profile_uuid: Uuid,
|
||||
profile_name: String,
|
||||
},
|
||||
ProfileSync,
|
||||
ProfileUpdate {
|
||||
profile_uuid: Uuid,
|
||||
profile_name: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
@@ -139,6 +127,7 @@ pub struct ProfilePayload {
|
||||
pub enum ProfilePayloadType {
|
||||
Created,
|
||||
Added, // also triggered when Created
|
||||
Synced,
|
||||
Edited,
|
||||
Removed,
|
||||
}
|
||||
@@ -149,7 +138,7 @@ pub enum EventError {
|
||||
NotInitialized,
|
||||
|
||||
#[error("Non-existent loading bar of key: {0}")]
|
||||
NoLoadingBar(LoadingBarId),
|
||||
NoLoadingBar(Uuid),
|
||||
|
||||
#[cfg(feature = "tauri")]
|
||||
#[error("Tauri error: {0}")]
|
||||
|
||||
Reference in New Issue
Block a user