Consolidate server's rpc state into the rpc::Server struct

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-08-19 12:17:52 -07:00
parent 266867b516
commit 3631fbd874
4 changed files with 569 additions and 609 deletions

View file

@ -2,7 +2,7 @@ use super::{
db::{self, UserId},
errors::TideResultExt,
};
use crate::{github, rpc, AppState, Request, RequestExt as _};
use crate::{github, AppState, Request, RequestExt as _};
use anyhow::{anyhow, Context};
use async_trait::async_trait;
pub use oauth2::basic::BasicClient as Client;
@ -19,7 +19,7 @@ use serde::{Deserialize, Serialize};
use std::{borrow::Cow, convert::TryFrom, sync::Arc};
use surf::Url;
use tide::Server;
use zrpc::{auth as zed_auth, proto, Peer};
use zrpc::auth as zed_auth;
static CURRENT_GITHUB_USER: &'static str = "current_github_user";
static GITHUB_AUTH_URL: &'static str = "https://github.com/login/oauth/authorize";
@ -100,43 +100,6 @@ impl RequestExt for Request {
}
}
#[async_trait]
pub trait PeerExt {
async fn sign_out(
self: &Arc<Self>,
connection_id: zrpc::ConnectionId,
state: &AppState,
) -> tide::Result<()>;
}
#[async_trait]
impl PeerExt for Peer {
async fn sign_out(
self: &Arc<Self>,
connection_id: zrpc::ConnectionId,
state: &AppState,
) -> tide::Result<()> {
self.disconnect(connection_id).await;
let worktree_ids = state.rpc.write().await.remove_connection(connection_id);
for worktree_id in worktree_ids {
let state = state.rpc.read().await;
if let Some(worktree) = state.worktrees.get(&worktree_id) {
rpc::broadcast(connection_id, worktree.connection_ids(), |conn_id| {
self.send(
conn_id,
proto::RemovePeer {
worktree_id,
peer_id: connection_id.0,
},
)
})
.await?;
}
}
Ok(())
}
}
pub fn build_client(client_id: &str, client_secret: &str) -> Client {
Client::new(
ClientId::new(client_id.to_string()),

View file

@ -14,7 +14,7 @@ mod tests;
use self::errors::TideResultExt as _;
use anyhow::{Context, Result};
use async_std::{net::TcpListener, sync::RwLock as AsyncRwLock};
use async_std::net::TcpListener;
use async_trait::async_trait;
use auth::RequestExt as _;
use db::{Db, DbOptions};
@ -51,7 +51,6 @@ pub struct AppState {
auth_client: auth::Client,
github_client: Arc<github::AppClient>,
repo_client: github::RepoClient,
rpc: AsyncRwLock<rpc::State>,
config: Config,
}
@ -76,7 +75,6 @@ impl AppState {
auth_client: auth::build_client(&config.github_client_id, &config.github_client_secret),
github_client,
repo_client,
rpc: Default::default(),
config,
};
this.register_partials();

File diff suppressed because it is too large Load diff

View file

@ -540,7 +540,7 @@ impl TestServer {
let db_name = format!("zed-test-{}", rng.gen::<u128>());
let app_state = Self::build_app_state(&db_name).await;
let peer = Peer::new();
let server = rpc::build_server(&app_state, &peer);
let server = rpc::Server::new(app_state.clone(), peer.clone());
Self {
peer,
app_state,
@ -595,7 +595,6 @@ impl TestServer {
auth_client: auth::build_client("", ""),
repo_client: github::RepoClient::test(&github_client),
github_client,
rpc: Default::default(),
config,
})
}