Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Commands/
Execute.rs

1//! Wire method: `commands:execute`.
2//! Dispatches to Mountain's CommandExecutor and emits
3//! `sky://commands/executed` for `vscode.commands.onDidExecuteCommand`.
4
5use std::sync::Arc;
6
7use serde_json::{Value, json};
8use tauri::Emitter;
9
10use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
11
12pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
13	use CommonLibrary::Command::CommandExecutor::CommandExecutor;
14
15	let CommandId = Arguments
16		.first()
17		.and_then(|V| V.as_str())
18		.ok_or_else(|| "commands:execute requires string command_id as first argument".to_string())?
19		.to_string();
20
21	let CommandArgs:Vec<Value> = Arguments.into_iter().skip(1).collect();
22
23	let Argument = CommandArgs.first().cloned().unwrap_or(Value::Null);
24
25	dev_log!("ipc", "commands:execute id={}", CommandId);
26
27	let Result = RunTime
28		.Environment
29		.ExecuteCommand(CommandId.clone(), Argument)
30		.await
31		.map_err(|Error| format!("commands:execute failed: {}", Error));
32
33	let _ = RunTime.Environment.ApplicationHandle.emit(
34		"sky://commands/executed",
35		json!({ "command": CommandId, "arguments": CommandArgs }),
36	);
37
38	// Dual-emit to Cocoon via Vine so `vscode.commands.onDidExecuteCommand`
39	// callbacks fire inside extensions running in the Node.js extension host.
40	// The Tauri-emit above only reaches the renderer (Sky); Cocoon cannot
41	// listen for `sky://*` events directly because there is no Tauri runtime
42	// in Node. Cocoon's `Services/Handler/Notification/Handler.ts` maps
43	// `$acceptCommandExecuted` → `Emitter.emit("commands.executed", payload)`
44	// which the `Commands/Namespace.ts` `onDidExecuteCommand` subscriber
45	// listens to. Fire-and-forget; failure is non-fatal (the Tauri-emit
46	// already reached the renderer-side observers).
47	let _ = crate::Vine::Client::SendNotification::Fn(
48		"cocoon-main".to_string(),
49		"$acceptCommandExecuted".to_string(),
50		json!({ "command": CommandId, "arguments": CommandArgs }),
51	)
52	.await;
53
54	Result
55}