Mountain/Track/FrontendCommand/DispatchFrontendCommand.rs
1//! # DispatchFrontendCommand (Track)
2//!
3//! ## RESPONSIBILITIES
4//!
5//! This module provides the primary Tauri command handler for requests
6//! originating from the Sky frontend. It serves as the general-purpose entry
7//! point for commands that are defined abstractly in the Common crate.
8//!
9//! ### Core Functions:
10//! - Receive frontend commands via Tauri IPC
11//! - Route commands to the effect creation system
12//! - Execute effects through the ApplicationRunTime
13//! - Return results or errors to the frontend
14//!
15//! ## ARCHITECTURAL ROLE
16//!
17//! DispatchFrontendCommand acts as the **frontend gateway** in Track's dispatch
18//! layer:
19//!
20//! ```text
21//! Sky (Frontend) ──► DispatchFrontendCommand ──► CreateEffectForRequest ──► ApplicationRunTime ──► Providers
22//! ```
23//!
24//! ## KEY COMPONENTS
25//!
26//! - **Fn**: Main dispatch function (public async fn Fn<R:Runtime>)
27//!
28//! ## ERROR HANDLING
29//!
30//! - Effect creation failures are caught and logged
31//! - Unknown commands are reported with context
32//! - Errors are propagated to the frontend with descriptive messages
33//!
34//! ## LOGGING
35//!
36//! - All incoming commands are logged at debug level
37//! - Effect creation failures are logged at error level
38//! - Log format: "[Track/FrontendCommand] Dispatching frontend command: {}"
39//!
40//! ## PERFORMANCE CONSIDERATIONS
41//!
42//! - Direct effect execution without intermediate overhead
43//! - Minimal locking to avoid blocking the UI thread
44//! - Async operations to prevent blocking
45//!
46//! ## TODO
47//!
48//! - [ ] Add request timeout handling
49//! - [ ] Implement request cancellation support (VS Code compatibility)
50//! - [ ] Add request metrics and telemetry
51
52use std::sync::Arc;
53
54use log::{debug, error};
55use serde_json::Value;
56use tauri::{AppHandle, Manager, Runtime, State, command};
57
58use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::CreateEffectForRequest};
59
60/// The primary Tauri command handler for requests originating from the `Sky`
61/// frontend. This is the general-purpose entry point for commands that are
62/// defined abstractly in the `Common` crate.
63#[command]
64pub async fn DispatchFrontendCommand<R:Runtime>(
65 ApplicationHandle:AppHandle<R>,
66
67 RunTime:State<'_, Arc<ApplicationRunTime>>,
68
69 Command:String,
70
71 Argument:Value,
72) -> Result<Value, String> {
73 debug!("[Track/FrontendCommand] Dispatching frontend command: {}", Command);
74
75 match CreateEffectForRequest(&ApplicationHandle, &Command, Argument) {
76 Ok(EffectFn) => {
77 let runtime_clone = RunTime.inner().clone();
78
79 EffectFn(runtime_clone).await
80 },
81
82 Err(Error) => {
83 error!(
84 "[Track/FrontendCommand] Failed to create effect for command '{}': {}",
85 Command, Error
86 );
87
88 Err(Error)
89 },
90 }
91}