Delete old Peer implementation and adapt previous code paths

This still doesn't compile but should be close.
This commit is contained in:
Antonio Scandurra
2021-08-19 15:02:30 +02:00
parent 5dee7ecf5b
commit d6412fdbde
10 changed files with 171 additions and 997 deletions
-2
View File
@@ -24,14 +24,12 @@ pub use settings::Settings;
use parking_lot::Mutex;
use postage::watch;
use std::sync::Arc;
use zrpc::ForegroundRouter;
pub struct AppState {
pub settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
pub settings: watch::Receiver<Settings>,
pub languages: Arc<language::LanguageRegistry>,
pub themes: Arc<settings::ThemeRegistry>,
pub rpc_router: Arc<ForegroundRouter>,
pub rpc: rpc::Client,
pub fs: Arc<dyn fs::Fs>,
}
+6 -35
View File
@@ -13,7 +13,7 @@ use zrpc::proto::EntityMessage;
pub use zrpc::{proto, ConnectionId, PeerId, TypedEnvelope};
use zrpc::{
proto::{EnvelopedMessage, RequestMessage},
ForegroundRouter, Peer, Receipt,
Peer, Receipt,
};
lazy_static! {
@@ -43,25 +43,6 @@ impl Client {
}
}
pub fn on_message<H, M>(
&self,
router: &mut ForegroundRouter,
handler: H,
cx: &mut gpui::MutableAppContext,
) where
H: 'static + Clone + for<'a> MessageHandler<'a, M>,
M: proto::EnvelopedMessage,
{
let this = self.clone();
let cx = cx.to_async();
router.add_message_handler(move |message| {
let this = this.clone();
let mut cx = cx.clone();
let handler = handler.clone();
async move { handler.handle(message, &this, &mut cx).await }
});
}
pub fn subscribe_from_model<T, M, F>(
&self,
remote_id: u64,
@@ -90,11 +71,7 @@ impl Client {
})
}
pub async fn log_in_and_connect(
&self,
router: Arc<ForegroundRouter>,
cx: AsyncAppContext,
) -> surf::Result<()> {
pub async fn log_in_and_connect(&self, cx: AsyncAppContext) -> surf::Result<()> {
if self.state.read().await.connection_id.is_some() {
return Ok(());
}
@@ -111,13 +88,13 @@ impl Client {
.await
.context("websocket handshake")?;
log::info!("connected to rpc address {}", *ZED_SERVER_URL);
self.add_connection(stream, router, cx).await?;
self.add_connection(stream, cx).await?;
} else if let Some(host) = ZED_SERVER_URL.strip_prefix("http://") {
let stream = smol::net::TcpStream::connect(host).await?;
let request = request.uri(format!("ws://{}/rpc", host)).body(())?;
let (stream, _) = async_tungstenite::client_async(request, stream).await?;
log::info!("connected to rpc address {}", *ZED_SERVER_URL);
self.add_connection(stream, router, cx).await?;
self.add_connection(stream, cx).await?;
} else {
return Err(anyhow!("invalid server url: {}", *ZED_SERVER_URL))?;
};
@@ -125,12 +102,7 @@ impl Client {
Ok(())
}
pub async fn add_connection<Conn>(
&self,
conn: Conn,
router: Arc<ForegroundRouter>,
cx: AsyncAppContext,
) -> surf::Result<()>
pub async fn add_connection<Conn>(&self, conn: Conn, cx: AsyncAppContext) -> surf::Result<()>
where
Conn: 'static
+ futures::Sink<WebSocketMessage, Error = WebSocketError>
@@ -138,8 +110,7 @@ impl Client {
+ Unpin
+ Send,
{
let (connection_id, handle_io, handle_messages) =
self.peer.add_connection(conn, router).await;
let (connection_id, handle_io, handle_messages) = self.peer.add_connection(conn).await;
cx.foreground().spawn(handle_messages).detach();
cx.background()
.spawn(async move {
-2
View File
@@ -15,7 +15,6 @@ use std::{
sync::Arc,
};
use tempdir::TempDir;
use zrpc::ForegroundRouter;
#[cfg(feature = "test-support")]
pub use zrpc::test::Channel;
@@ -163,7 +162,6 @@ pub fn build_app_state(cx: &AppContext) -> Arc<AppState> {
settings,
themes,
languages: languages.clone(),
rpc_router: Arc::new(ForegroundRouter::new()),
rpc: rpc::Client::new(languages),
fs: Arc::new(RealFs),
})
+2 -4
View File
@@ -728,10 +728,9 @@ impl Workspace {
fn share_worktree(&mut self, app_state: &Arc<AppState>, cx: &mut ViewContext<Self>) {
let rpc = self.rpc.clone();
let platform = cx.platform();
let router = app_state.rpc_router.clone();
let task = cx.spawn(|this, mut cx| async move {
rpc.log_in_and_connect(router, cx.clone()).await?;
rpc.log_in_and_connect(cx.clone()).await?;
let share_task = this.update(&mut cx, |this, cx| {
let worktree = this.worktrees.iter().next()?;
@@ -761,10 +760,9 @@ impl Workspace {
fn join_worktree(&mut self, app_state: &Arc<AppState>, cx: &mut ViewContext<Self>) {
let rpc = self.rpc.clone();
let languages = self.languages.clone();
let router = app_state.rpc_router.clone();
let task = cx.spawn(|this, mut cx| async move {
rpc.log_in_and_connect(router, cx.clone()).await?;
rpc.log_in_and_connect(cx.clone()).await?;
let worktree_url = cx
.platform()