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::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::CreateEffectForRequest};
60use crate::dev_log;
61
62/// The primary Tauri command handler for requests originating from the `Sky`
63/// frontend. This is the general-purpose entry point for commands that are
64/// defined abstractly in the `Common` crate.
65#[command]
66pub async fn DispatchFrontendCommand<R:Runtime>(
67 ApplicationHandle:AppHandle<R>,
68
69 RunTime:State<'_, Arc<ApplicationRunTime>>,
70
71 Command:String,
72
73 Argument:Value,
74) -> Result<Value, String> {
75 dev_log!("ipc", "[Track/FrontendCommand] Dispatching frontend command: {}", Command);
76
77 match CreateEffectForRequest(&ApplicationHandle, &Command, Argument) {
78 Ok(EffectFn) => {
79 let runtime_clone = RunTime.inner().clone();
80
81 EffectFn(runtime_clone).await
82 },
83
84 Err(Error) => {
85 dev_log!("ipc", "error: [Track/FrontendCommand] Failed to create effect for command '{}': {}",
86 Command, Error);
87
88 Err(Error)
89 },
90 }
91}