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