Add public method for connecting to RPC server with a given address

Max Brunsfeld and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

gpui/src/app.rs      | 12 ++++++++++--
zed/src/rpc.rs       | 26 +++++++++++++++++++-------
zed/src/workspace.rs |  6 ++----
3 files changed, 31 insertions(+), 13 deletions(-)

Detailed changes

gpui/src/app.rs 🔗

@@ -366,6 +366,14 @@ impl TestAppContext {
         self.cx.borrow().platform.clone()
     }
 
+    pub fn foreground(&self) -> Rc<executor::Foreground> {
+        self.cx.borrow().foreground().clone()
+    }
+
+    pub fn background_executor(&self) -> Arc<executor::Background> {
+        self.cx.borrow().background_executor().clone()
+    }
+
     pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option<PathBuf>) {
         self.foreground_platform.simulate_new_path_selection(result);
     }
@@ -606,7 +614,7 @@ impl MutableAppContext {
         &self.cx.font_cache
     }
 
-    pub fn foreground_executor(&self) -> &Rc<executor::Foreground> {
+    pub fn foreground(&self) -> &Rc<executor::Foreground> {
         &self.foreground
     }
 
@@ -1793,7 +1801,7 @@ impl<'a, T: View> ViewContext<'a, T> {
     }
 
     pub fn foreground(&self) -> &Rc<executor::Foreground> {
-        self.app.foreground_executor()
+        self.app.foreground()
     }
 
     pub fn background_executor(&self) -> &Arc<executor::Background> {

zed/src/rpc.rs 🔗

@@ -65,16 +65,12 @@ impl Client {
         .detach();
     }
 
-    pub async fn connect_to_server(
-        &self,
-        cx: &AsyncAppContext,
-        executor: &Arc<Background>,
-    ) -> surf::Result<ConnectionId> {
+    pub async fn log_in_and_connect(&self, cx: &AsyncAppContext) -> surf::Result<ConnectionId> {
         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<Background>,
+    ) -> surf::Result<ConnectionId> {
         // 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,
                 },
             )

zed/src/workspace.rs 🔗

@@ -708,11 +708,10 @@ impl Workspace {
 
     fn share_worktree(&mut self, _: &(), cx: &mut ViewContext<Self>) {
         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<Self>) {
         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()