WIP - Allow RpcClient to register handlers for incoming messages

This commit is contained in:
Max Brunsfeld 2021-06-15 17:22:48 -07:00
parent d3a3ad2820
commit 8b66e0aa7e
6 changed files with 190 additions and 29 deletions

View file

@ -1,10 +1,16 @@
use futures::Future;
use gpui::MutableAppContext;
use rpc_client::RpcClient;
use std::sync::Arc;
use zed_rpc::proto::RequestMessage;
pub mod assets;
pub mod editor;
pub mod file_finder;
pub mod language;
pub mod menus;
mod operation_queue;
mod rpc_client;
pub mod rpc_client;
pub mod settings;
mod sum_tree;
#[cfg(test)]
@ -18,6 +24,28 @@ mod worktree;
pub struct AppState {
pub settings: postage::watch::Receiver<settings::Settings>,
pub language_registry: std::sync::Arc<language::LanguageRegistry>,
pub rpc_client: Arc<RpcClient>,
}
impl AppState {
pub async fn on_rpc_request<Req, F, Fut>(
&self,
cx: &mut MutableAppContext,
handler: F,
) where
Req: RequestMessage,
F: 'static + Send + Sync + Fn(Req, &AppState, &mut MutableAppContext) -> Fut,
Fut: 'static + Send + Sync + Future<Output = Req::Response>,
{
let app_state = self.clone();
let cx = cx.to_background();
app_state
.rpc_client
.on_request(move |req| cx.update(|cx| async move {
handler(req, &app_state, cx)
})
.await
}
}
pub fn init(cx: &mut gpui::MutableAppContext) {