Skip to main content

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