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

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-06-23 16:40:43 -07:00
parent 54c4b31249
commit 6a166554e8
3 changed files with 31 additions and 13 deletions

View file

@ -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> {

View file

@ -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,
},
)

View file

@ -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()