From cfcf7ab5aa7e3cd626ab881cc58f976f9a050e16 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 7 May 2025 21:15:13 -0400 Subject: [PATCH] refactor events: centrally located in rust for better discoverability --- .../commands/sora/sora_image_remix_command.rs | 21 ++++++------- crates/desktop/artcraft/src/events/mod.rs | 5 ++- .../src/events/sendable_event_error.rs | 31 +++++++++++++++++++ .../src/events/sendable_event_trait.rs | 19 ++++++++++++ .../desktop/artcraft/src/events/sora/mod.rs | 4 +++ .../sora/sora_image_enqueue_failure_event.rs | 11 +++++++ .../sora/sora_image_enqueue_success_event.rs | 11 +++++++ .../sora_image_generation_complete_event.rs | 12 +++++++ .../sora_image_generation_failed_event.rs | 12 +++++++ .../src/threads/sora_task_polling_thread.rs | 26 +++++++--------- 10 files changed, 124 insertions(+), 28 deletions(-) create mode 100644 crates/desktop/artcraft/src/events/sendable_event_error.rs create mode 100644 crates/desktop/artcraft/src/events/sendable_event_trait.rs create mode 100644 crates/desktop/artcraft/src/events/sora/mod.rs create mode 100644 crates/desktop/artcraft/src/events/sora/sora_image_enqueue_failure_event.rs create mode 100644 crates/desktop/artcraft/src/events/sora/sora_image_enqueue_success_event.rs create mode 100644 crates/desktop/artcraft/src/events/sora/sora_image_generation_complete_event.rs create mode 100644 crates/desktop/artcraft/src/events/sora/sora_image_generation_failed_event.rs diff --git a/crates/desktop/artcraft/src/commands/sora/sora_image_remix_command.rs b/crates/desktop/artcraft/src/commands/sora/sora_image_remix_command.rs index 90465ae9eb..f5163345e7 100644 --- a/crates/desktop/artcraft/src/commands/sora/sora_image_remix_command.rs +++ b/crates/desktop/artcraft/src/commands/sora/sora_image_remix_command.rs @@ -1,3 +1,6 @@ +use crate::events::sendable_event_trait::SendableEvent; +use crate::events::sora::sora_image_enqueue_failure_event::SoraImageEnqueueFailureEvent; +use crate::events::sora::sora_image_enqueue_success_event::SoraImageEnqueueSuccessEvent; use crate::state::data_dir::app_data_root::AppDataRoot; use crate::state::data_dir::trait_data_subdir::DataSubdir; use crate::state::sora::read_sora_credentials_from_disk::read_sora_credentials_from_disk; @@ -28,16 +31,6 @@ use storyteller_client::media_files::get_media_file::get_media_file; use storyteller_client::utils::api_host::ApiHost; use tauri::{AppHandle, Emitter, Manager, State}; use tokens::tokens::media_files::MediaFileToken; -use crate::threads::sora_task_polling_thread::SoraImageGenerationComplete; - -#[derive(Debug, Clone, Serialize)] -pub struct SoraImageEnqueueFailure { - // TODO: Reason. -} - -#[derive(Debug, Clone, Serialize)] -pub struct SoraImageEnqueueSuccess { -} #[derive(Deserialize)] pub struct SoraImageRemixCommand { @@ -75,7 +68,9 @@ pub async fn sora_image_remix_command( if let Err(err) = result { error!("error: {:?}", err); - let result = app.emit("sora-image-enqueue-failure", SoraImageEnqueueFailure{}); + let event = SoraImageEnqueueFailureEvent {}; + let result = event.send(&app); + if let Err(err) = result { error!("Failed to emit event: {:?}", err); } @@ -83,7 +78,9 @@ pub async fn sora_image_remix_command( return Err("there was an error".to_string()); } - let result = app.emit("sora-image-enqueue-success", SoraImageEnqueueSuccess{}); + let event = SoraImageEnqueueSuccessEvent {}; + let result = event.send(&app); + if let Err(err) = result { error!("Failed to emit event: {:?}", err); } diff --git a/crates/desktop/artcraft/src/events/mod.rs b/crates/desktop/artcraft/src/events/mod.rs index 2db7fd03ce..ee9e0cd166 100644 --- a/crates/desktop/artcraft/src/events/mod.rs +++ b/crates/desktop/artcraft/src/events/mod.rs @@ -1 +1,4 @@ -pub mod notification_event; \ No newline at end of file +pub mod notification_event; +pub mod sendable_event_error; +pub mod sendable_event_trait; +pub mod sora; \ No newline at end of file diff --git a/crates/desktop/artcraft/src/events/sendable_event_error.rs b/crates/desktop/artcraft/src/events/sendable_event_error.rs new file mode 100644 index 0000000000..25d4a57433 --- /dev/null +++ b/crates/desktop/artcraft/src/events/sendable_event_error.rs @@ -0,0 +1,31 @@ +use std::error::Error; +use std::fmt::{Display, Formatter}; + +#[derive(Debug)] +pub enum SendableEventError { + TauriError(tauri::Error), + AnyhowError(anyhow::Error), +} + +impl Error for SendableEventError {} + +impl Display for SendableEventError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Self::TauriError(err) => write!(f, "SendableEventError::TauriError: {}", err), + Self::AnyhowError(err) => write!(f, "SendableEventError::AnyhowError: {}", err), + } + } +} + +impl From for SendableEventError { + fn from(err: anyhow::Error) -> Self { + SendableEventError::AnyhowError(err) + } +} + +impl From for SendableEventError { + fn from(err: tauri::Error) -> Self { + SendableEventError::TauriError(err) + } +} diff --git a/crates/desktop/artcraft/src/events/sendable_event_trait.rs b/crates/desktop/artcraft/src/events/sendable_event_trait.rs new file mode 100644 index 0000000000..eb7e466888 --- /dev/null +++ b/crates/desktop/artcraft/src/events/sendable_event_trait.rs @@ -0,0 +1,19 @@ +use crate::events::sendable_event_error::SendableEventError; +use serde::Serialize; +use tauri::{AppHandle, Emitter}; + +/// Implement this on a Serializable object to be able to send it as an event to the frontend. +pub trait SendableEvent : Serialize { + /// This is the name of the event that the frontend subscribes to. + const FRONTEND_EVENT_NAME: &'static str; + + /// Default implementation of send(). + /// This serializes and sends the event to the frontend. + fn send(&self, app: &AppHandle) -> Result<(), SendableEventError> { + let result = app.emit(Self::FRONTEND_EVENT_NAME, self); + if let Err(err) = result { + return Err(SendableEventError::from(err)); + } + Ok(()) + } +} diff --git a/crates/desktop/artcraft/src/events/sora/mod.rs b/crates/desktop/artcraft/src/events/sora/mod.rs new file mode 100644 index 0000000000..e263671af0 --- /dev/null +++ b/crates/desktop/artcraft/src/events/sora/mod.rs @@ -0,0 +1,4 @@ +pub mod sora_image_enqueue_failure_event; +pub mod sora_image_enqueue_success_event; +pub mod sora_image_generation_complete_event; +pub mod sora_image_generation_failed_event; \ No newline at end of file diff --git a/crates/desktop/artcraft/src/events/sora/sora_image_enqueue_failure_event.rs b/crates/desktop/artcraft/src/events/sora/sora_image_enqueue_failure_event.rs new file mode 100644 index 0000000000..c8317b088b --- /dev/null +++ b/crates/desktop/artcraft/src/events/sora/sora_image_enqueue_failure_event.rs @@ -0,0 +1,11 @@ +use crate::events::sendable_event_trait::SendableEvent; +use serde_derive::Serialize; + +#[derive(Debug, Clone, Serialize)] +pub struct SoraImageEnqueueFailureEvent { + // TODO: Reason. +} + +impl SendableEvent for SoraImageEnqueueFailureEvent { + const FRONTEND_EVENT_NAME: &'static str = "sora-image-enqueue-failure"; +} diff --git a/crates/desktop/artcraft/src/events/sora/sora_image_enqueue_success_event.rs b/crates/desktop/artcraft/src/events/sora/sora_image_enqueue_success_event.rs new file mode 100644 index 0000000000..7d3a8b6a9d --- /dev/null +++ b/crates/desktop/artcraft/src/events/sora/sora_image_enqueue_success_event.rs @@ -0,0 +1,11 @@ +use crate::events::sendable_event_trait::SendableEvent; +use serde_derive::Serialize; + +#[derive(Debug, Clone, Serialize)] +pub struct SoraImageEnqueueSuccessEvent { + // TODO: Token, etc. +} + +impl SendableEvent for SoraImageEnqueueSuccessEvent { + const FRONTEND_EVENT_NAME: &'static str = "sora-image-enqueue-success"; +} diff --git a/crates/desktop/artcraft/src/events/sora/sora_image_generation_complete_event.rs b/crates/desktop/artcraft/src/events/sora/sora_image_generation_complete_event.rs new file mode 100644 index 0000000000..09cddf624d --- /dev/null +++ b/crates/desktop/artcraft/src/events/sora/sora_image_generation_complete_event.rs @@ -0,0 +1,12 @@ +use serde_derive::Serialize; +use tokens::tokens::media_files::MediaFileToken; +use crate::events::sendable_event_trait::SendableEvent; + +#[derive(Debug, Clone, Serialize)] +pub struct SoraImageGenerationCompleteEvent { + pub media_file_token: MediaFileToken, +} + +impl SendableEvent for SoraImageGenerationCompleteEvent { + const FRONTEND_EVENT_NAME: &'static str = "sora-image-generation-complete"; +} diff --git a/crates/desktop/artcraft/src/events/sora/sora_image_generation_failed_event.rs b/crates/desktop/artcraft/src/events/sora/sora_image_generation_failed_event.rs new file mode 100644 index 0000000000..6a4a20f79a --- /dev/null +++ b/crates/desktop/artcraft/src/events/sora/sora_image_generation_failed_event.rs @@ -0,0 +1,12 @@ +use serde_derive::Serialize; +use crate::events::sendable_event_trait::SendableEvent; + +#[derive(Debug, Clone, Serialize)] +pub struct SoraImageGenerationFailedEvent { + pub prompt: String, +} + +impl SendableEvent for SoraImageGenerationFailedEvent { + const FRONTEND_EVENT_NAME: &'static str = "sora-image-generation-failed"; +} + diff --git a/crates/desktop/artcraft/src/threads/sora_task_polling_thread.rs b/crates/desktop/artcraft/src/threads/sora_task_polling_thread.rs index 915c5ff7ed..da8cddcbb8 100644 --- a/crates/desktop/artcraft/src/threads/sora_task_polling_thread.rs +++ b/crates/desktop/artcraft/src/threads/sora_task_polling_thread.rs @@ -1,3 +1,6 @@ +use crate::events::sendable_event_trait::SendableEvent; +use crate::events::sora::sora_image_generation_complete_event::SoraImageGenerationCompleteEvent; +use crate::events::sora::sora_image_generation_failed_event::SoraImageGenerationFailedEvent; use crate::state::data_dir::app_data_root::AppDataRoot; use crate::state::data_dir::trait_data_subdir::DataSubdir; use crate::state::sora::sora_credential_manager::SoraCredentialManager; @@ -18,16 +21,6 @@ use tauri::{AppHandle, Emitter}; use tempdir::TempDir; use tokens::tokens::media_files::MediaFileToken; -#[derive(Debug, Clone, Serialize)] -pub struct SoraImageGenerationComplete { - pub media_file_token: MediaFileToken, -} - -#[derive(Debug, Clone, Serialize)] -pub struct SoraImageGenerationFailed { - pub prompt: String, -} - pub async fn sora_task_polling_thread( app_handle: AppHandle, app_data_root: AppDataRoot, @@ -102,9 +95,10 @@ async fn polling_loop( for task in failed_tasks.iter() { if sora_task_queue.contains_key(&task.id)? { - app_handle.emit("sora-image-generation-failed", SoraImageGenerationFailed { + let event = SoraImageGenerationFailedEvent { prompt: task.prompt.clone(), - })?; + }; + event.send(&app_handle)?; } } @@ -133,9 +127,11 @@ async fn polling_loop( info!("Uploaded to API backend: {:?}", result.media_file_token); - app_handle.emit("sora-image-generation-complete", SoraImageGenerationComplete { - media_file_token: result.media_file_token - })?; + let event = SoraImageGenerationCompleteEvent { + media_file_token: result.media_file_token, + }; + + event.send(&app_handle)?; } }