From 6a166554e89bd7f2b82afae6d32599fb63c9eb0a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 23 Jun 2021 16:40:43 -0700 Subject: [PATCH] Add public method for connecting to RPC server with a given address Co-Authored-By: Nathan Sobo --- gpui/src/app.rs | 12 ++++++++++-- zed/src/rpc.rs | 26 +++++++++++++++++++------- zed/src/workspace.rs | 6 ++---- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index decf659561764a8b53eceeccfb22a401ea1c8e3e..9985d6217a5f60f710474e8d659a2ef4e2f62cea 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -366,6 +366,14 @@ impl TestAppContext { self.cx.borrow().platform.clone() } + pub fn foreground(&self) -> Rc { + self.cx.borrow().foreground().clone() + } + + pub fn background_executor(&self) -> Arc { + self.cx.borrow().background_executor().clone() + } + pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option) { self.foreground_platform.simulate_new_path_selection(result); } @@ -606,7 +614,7 @@ impl MutableAppContext { &self.cx.font_cache } - pub fn foreground_executor(&self) -> &Rc { + pub fn foreground(&self) -> &Rc { &self.foreground } @@ -1793,7 +1801,7 @@ impl<'a, T: View> ViewContext<'a, T> { } pub fn foreground(&self) -> &Rc { - self.app.foreground_executor() + self.app.foreground() } pub fn background_executor(&self) -> &Arc { diff --git a/zed/src/rpc.rs b/zed/src/rpc.rs index 2fd0d47e3a59224e0c82b675d1e17c6c54557b06..0388869d6669771a8b9c6c36b706c44c1c148a18 100644 --- a/zed/src/rpc.rs +++ b/zed/src/rpc.rs @@ -65,16 +65,12 @@ impl Client { .detach(); } - pub async fn connect_to_server( - &self, - cx: &AsyncAppContext, - executor: &Arc, - ) -> surf::Result { + pub async fn log_in_and_connect(&self, cx: &AsyncAppContext) -> surf::Result { if let Some(connection_id) = self.state.lock().await.connection_id { return Ok(connection_id); } - let (user_id, access_token) = Self::login(cx.platform(), executor).await?; + let (user_id, access_token) = Self::login(cx.platform(), &cx.background_executor()).await?; let mut response = surf::get(format!( "{}{}", @@ -93,6 +89,22 @@ impl Client { .await .context("failed to parse rpc address response")?; + self.connect( + &address, + user_id.parse()?, + access_token, + &cx.background_executor(), + ) + .await + } + + pub async fn connect( + &self, + address: &str, + user_id: i32, + access_token: String, + executor: &Arc, + ) -> surf::Result { // TODO - If the `ZED_SERVER_URL` uses https, then wrap this stream in // a TLS stream using `native-tls`. let stream = smol::net::TcpStream::connect(&address).await?; @@ -108,7 +120,7 @@ impl Client { .request( connection_id, proto::Auth { - user_id: user_id.parse()?, + user_id, access_token, }, ) diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 6ae6f43229840c4536d84bd616d0f91819faf1ca..e0a873fc380dbf48ef7e4e08ebe77dd7a3630300 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -708,11 +708,10 @@ impl Workspace { fn share_worktree(&mut self, _: &(), cx: &mut ViewContext) { let rpc = self.rpc.clone(); - let executor = cx.background_executor().clone(); let platform = cx.platform(); let task = cx.spawn(|this, mut cx| async move { - let connection_id = rpc.connect_to_server(&cx, &executor).await?; + let connection_id = rpc.log_in_and_connect(&cx).await?; let share_task = this.update(&mut cx, |this, cx| { let worktree = this.worktrees.iter().next()?; @@ -741,10 +740,9 @@ impl Workspace { fn join_worktree(&mut self, _: &(), cx: &mut ViewContext) { let rpc = self.rpc.clone(); - let executor = cx.background_executor().clone(); let task = cx.spawn(|this, mut cx| async move { - let connection_id = rpc.connect_to_server(&cx, &executor).await?; + let connection_id = rpc.log_in_and_connect(&cx).await?; let worktree_url = cx .platform()