Skip to content

Commit 2ad1032

Browse files
feat(Track): add handler registry for RPC method management
- Introduces `HandlerRegistry` struct in Mountain backend to manage registration and lookup of RPC handlers - Uses HashMap to store handler functions keyed by method names (e.g. "fs_stat", "config_getConfiguration") - Enables centralized registration of native capability handlers required by Cocoon extension host - Supports async handler functions with Tauri runtime context and AppState access - Provides foundation for implementing extension-facing APIs through Track dispatcher This implementation directly supports Land's Path A strategy by creating a structured way to handle RPC requests from VS Code extensions running in Cocoon. The registry pattern aligns with Mountain's role as central dispatcher, enabling scalable addition of new native capabilities while maintaining type safety across IPC boundaries.
1 parent 718d53d commit 2ad1032

2 files changed

Lines changed: 61 additions & 47 deletions

File tree

Source/Library.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ pub mod handlers {
3232
pub mod protocol;
3333

3434
pub mod proxy;
35+
36+
pub mod registry;
3537
}

Source/handlers/registry.rs

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,74 @@
11
// Mountain/src/handlers/registry.rs
2-
use crate::runtime::AppRuntime; // Use the common runtime
3-
use crate::vine::VineError;
4-
use serde_json::Value;
52
use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc};
6-
use tauri::{AppHandle, Runtime, Window}; // Use common error type
3+
4+
use serde_json::Value;
5+
use tauri::{AppHandle, Runtime, Window};
6+
7+
// Use the common runtime
8+
use crate::runtime::AppRuntime;
9+
// Use common error type
10+
use crate::vine::VineError;
711

812
// Define the signature for a handler function
913
pub type SidecarRequestHandler<R> = Arc<
10-
dyn Fn(
11-
AppHandle<R>,
12-
Window<R>,
13-
Arc<AppRuntime>,
14-
String,
15-
Value,
16-
) -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>>
17-
+ Send
18-
+ Sync,
14+
dyn Fn(
15+
AppHandle<R>,
16+
17+
Window<R>,
18+
19+
Arc<AppRuntime>,
20+
21+
String,
22+
23+
Value,
24+
) -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>>
25+
+ Send
26+
+ Sync,
1927
>;
2028

2129
// Simple registry using a HashMap
22-
pub struct HandlerRegistry<R: Runtime> {
23-
handlers: HashMap<String, SidecarRequestHandler<R>>,
30+
pub struct HandlerRegistry<R:Runtime> {
31+
handlers:HashMap<String, SidecarRequestHandler<R>>,
2432
}
2533

26-
impl<R: Runtime> HandlerRegistry<R> {
27-
pub fn new() -> Self {
28-
Self {
29-
handlers: HashMap::new(),
30-
}
31-
}
32-
33-
// Register a handler for a specific method name
34-
pub fn register<F>(&mut self, method: &str, handler: F)
35-
where
36-
F: Fn(
37-
AppHandle<R>,
38-
Window<R>,
39-
Arc<AppRuntime>,
40-
String,
41-
Value,
42-
) -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>>
43-
+ Send
44-
+ Sync
45-
+ 'static,
46-
{
47-
println!("[Handler Registry] Registering handler for '{}'", method);
48-
self.handlers.insert(method.to_string(), Arc::new(handler));
49-
}
50-
51-
// Get a handler for a method name
52-
pub fn get(&self, method: &str) -> Option<SidecarRequestHandler<R>> {
53-
self.handlers.get(method).cloned()
54-
}
34+
impl<R:Runtime> HandlerRegistry<R> {
35+
pub fn new() -> Self { Self { handlers:HashMap::new() } }
36+
37+
// Register a handler for a specific method name
38+
pub fn register<F>(&mut self, method:&str, handler:F)
39+
where
40+
F: Fn(
41+
AppHandle<R>,
42+
43+
Window<R>,
44+
45+
Arc<AppRuntime>,
46+
47+
String,
48+
49+
Value,
50+
) -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>>
51+
+ Send
52+
+ Sync
53+
+ 'static, {
54+
println!("[Handler Registry] Registering handler for '{}'", method);
55+
56+
self.handlers.insert(method.to_string(), Arc::new(handler));
57+
}
58+
59+
// Get a handler for a method name
60+
pub fn get(&self, method:&str) -> Option<SidecarRequestHandler<R>> { self.handlers.get(method).cloned() }
5561
}
5662

5763
// --- Example Registration in main.rs or handlers module ---
5864
// fn register_handlers<R: Runtime>(registry: &mut HandlerRegistry<R>) {
59-
// registry.register("fs_stat", |app, win, rt, sid, params| Box::pin(handlers::native_fs::handle_fs_stat(app, win, rt, sid, params)));
60-
// registry.register("config_getConfiguration", |app, win, rt, sid, params| Box::pin(handlers::config::handle_get_configuration(app, win, rt, sid, params)));
61-
// // ... register all other handlers ...
65+
// registry.register("fs_stat", |app, win, rt, sid, params|
66+
// Box::pin(handlers::native_fs::handle_fs_stat(app, win, rt, sid, params)));
67+
68+
// registry.register("config_getConfiguration", |app, win, rt, sid, params|
69+
// Box::pin(handlers::config::handle_get_configuration(app, win, rt, sid,
70+
71+
// ... register all other handlers ...
72+
// params)));
73+
6274
// }

0 commit comments

Comments
 (0)