Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RPC/EchoAction/
EchoActionServer.rs

1//! Singleton submission gate for every Cocoon→Mountain request. Wraps the
2//! Echo scheduler with a per-method priority lane.
3
4use std::sync::Arc;
5
6use Echo::{Scheduler::Scheduler::Scheduler, Task::Priority::Priority as EchoPriority};
7use tokio::sync::oneshot;
8
9use crate::RPC::EchoAction::{ExtensionHostRegistry, ResolveMethodPriority};
10
11#[derive(Clone)]
12pub struct Struct {
13	Registry:Arc<ExtensionHostRegistry::Struct>,
14}
15
16impl Default for Struct {
17	fn default() -> Self { Self::new() }
18}
19
20impl Struct {
21	pub fn new() -> Self { Self { Registry:Arc::new(ExtensionHostRegistry::Struct::new()) } }
22
23	/// Registry accessor so tonic handlers can pass it into per-extension
24	/// logic without threading it through the scheduler.
25	pub fn Registry(&self) -> Arc<ExtensionHostRegistry::Struct> { self.Registry.clone() }
26
27	/// Submit `Task` to the Echo scheduler on the lane chosen for `Method`,
28	/// wait for completion, and return the result.
29	pub async fn Dispatch<F, T>(&self, Scheduler:&Scheduler, Method:&str, Task:F) -> Result<T, String>
30	where
31		F: std::future::Future<Output = T> + Send + 'static,
32		T: Send + 'static, {
33		let Priority = ResolveMethodPriority::Fn(Method);
34
35		let (Sender, Receiver) = oneshot::channel::<T>();
36
37		Scheduler.Submit(
38			async move {
39				let Output = Task.await;
40				let _ = Sender.send(Output);
41			},
42			Priority,
43		);
44
45		Receiver
46			.await
47			.map_err(|_| "EchoAction task cancelled before completion".to_string())
48	}
49}
50
51// Allow `EchoPriority` import below to satisfy clippy unused warning when
52// the inner Scheduler import is feature-gated in future revisions.
53fn _Priority(P:EchoPriority) -> EchoPriority { P }