diff --git a/server/src/tests.rs b/server/src/tests.rs index 1cc7bf18ba..3846f68794 100644 --- a/server/src/tests.rs +++ b/server/src/tests.rs @@ -549,7 +549,11 @@ impl TestServer { } } - async fn create_client(&mut self, cx: &mut TestAppContext, name: &str) -> (UserId, Client) { + async fn create_client( + &mut self, + cx: &mut TestAppContext, + name: &str, + ) -> (UserId, Arc) { let user_id = self.app_state.db.create_user(name, false).await.unwrap(); let client = Client::new(); let (client_conn, server_conn) = Channel::bidirectional(); diff --git a/zed/src/channel.rs b/zed/src/channel.rs index 60ff50489c..5fc21206b2 100644 --- a/zed/src/channel.rs +++ b/zed/src/channel.rs @@ -64,7 +64,7 @@ impl Channel { fn handle_message_sent( &mut self, message: TypedEnvelope, - rpc: rpc::Client, + rpc: Arc, cx: &mut ModelContext, ) -> Result<()> { Ok(()) diff --git a/zed/src/lib.rs b/zed/src/lib.rs index 90e68c698d..492eaaf104 100644 --- a/zed/src/lib.rs +++ b/zed/src/lib.rs @@ -30,7 +30,7 @@ pub struct AppState { pub settings: watch::Receiver, pub languages: Arc, pub themes: Arc, - pub rpc: rpc::Client, + pub rpc: Arc, pub fs: Arc, } diff --git a/zed/src/rpc.rs b/zed/src/rpc.rs index 1c0d6d0894..902f17eb9e 100644 --- a/zed/src/rpc.rs +++ b/zed/src/rpc.rs @@ -23,14 +23,13 @@ lazy_static! { std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev:443".to_string()); } -#[derive(Clone)] pub struct Client { peer: Arc, - state: Arc>, + state: RwLock, } #[derive(Default)] -pub struct ClientState { +struct ClientState { connection_id: Option, entity_id_extractors: HashMap u64>>, model_handlers: HashMap< @@ -40,28 +39,33 @@ pub struct ClientState { } pub struct Subscription { - state: Weak>, + client: Weak, id: (TypeId, u64), } impl Drop for Subscription { fn drop(&mut self) { - if let Some(state) = self.state.upgrade() { - let _ = state.write().model_handlers.remove(&self.id).unwrap(); + if let Some(client) = self.client.upgrade() { + client + .state + .write() + .model_handlers + .remove(&self.id) + .unwrap(); } } } impl Client { - pub fn new() -> Self { - Self { + pub fn new() -> Arc { + Arc::new(Self { peer: Peer::new(), state: Default::default(), - } + }) } pub fn subscribe_from_model( - &self, + self: &Arc, remote_id: u64, cx: &mut ModelContext, mut handler: F, @@ -72,7 +76,7 @@ impl Client { F: 'static + Send + Sync - + FnMut(&mut M, TypedEnvelope, Client, &mut ModelContext) -> Result<()>, + + FnMut(&mut M, TypedEnvelope, Arc, &mut ModelContext) -> Result<()>, { let subscription_id = (TypeId::of::(), remote_id); let client = self.clone(); @@ -108,12 +112,12 @@ impl Client { } Subscription { - state: Arc::downgrade(&self.state), + client: Arc::downgrade(self), id: subscription_id, } } - pub async fn log_in_and_connect(&self, cx: AsyncAppContext) -> surf::Result<()> { + pub async fn log_in_and_connect(self: &Arc, cx: AsyncAppContext) -> surf::Result<()> { if self.state.read().connection_id.is_some() { return Ok(()); } @@ -144,7 +148,11 @@ impl Client { Ok(()) } - pub async fn add_connection(&self, conn: Conn, cx: AsyncAppContext) -> surf::Result<()> + pub async fn add_connection( + self: &Arc, + conn: Conn, + cx: AsyncAppContext, + ) -> surf::Result<()> where Conn: 'static + futures::Sink @@ -155,11 +163,11 @@ impl Client { let (connection_id, handle_io, mut incoming) = self.peer.add_connection(conn).await; { let mut cx = cx.clone(); - let state = self.state.clone(); + let this = self.clone(); cx.foreground() .spawn(async move { while let Some(message) = incoming.recv().await { - let mut state = state.write(); + let mut state = this.state.write(); if let Some(extract_entity_id) = state.entity_id_extractors.get(&message.payload_type_id()) { diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 8dfa7fc4da..663ca5f75e 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -312,7 +312,7 @@ pub struct State { pub struct Workspace { pub settings: watch::Receiver, languages: Arc, - rpc: rpc::Client, + rpc: Arc, fs: Arc, modal: Option, center: PaneGroup, diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index 723487ad4a..d9a8aa889b 100644 --- a/zed/src/worktree.rs +++ b/zed/src/worktree.rs @@ -107,7 +107,7 @@ impl Worktree { } pub async fn open_remote( - rpc: rpc::Client, + rpc: Arc, id: u64, access_token: String, languages: Arc, @@ -125,7 +125,7 @@ impl Worktree { async fn remote( open_response: proto::OpenWorktreeResponse, - rpc: rpc::Client, + rpc: Arc, languages: Arc, cx: &mut AsyncAppContext, ) -> Result> { @@ -283,7 +283,7 @@ impl Worktree { pub fn handle_add_peer( &mut self, envelope: TypedEnvelope, - _: rpc::Client, + _: Arc, cx: &mut ModelContext, ) -> Result<()> { match self { @@ -295,7 +295,7 @@ impl Worktree { pub fn handle_remove_peer( &mut self, envelope: TypedEnvelope, - _: rpc::Client, + _: Arc, cx: &mut ModelContext, ) -> Result<()> { match self { @@ -307,7 +307,7 @@ impl Worktree { pub fn handle_update( &mut self, envelope: TypedEnvelope, - _: rpc::Client, + _: Arc, cx: &mut ModelContext, ) -> anyhow::Result<()> { self.as_remote_mut() @@ -318,7 +318,7 @@ impl Worktree { pub fn handle_open_buffer( &mut self, envelope: TypedEnvelope, - rpc: rpc::Client, + rpc: Arc, cx: &mut ModelContext, ) -> anyhow::Result<()> { let receipt = envelope.receipt(); @@ -341,7 +341,7 @@ impl Worktree { pub fn handle_close_buffer( &mut self, envelope: TypedEnvelope, - _: rpc::Client, + _: Arc, cx: &mut ModelContext, ) -> anyhow::Result<()> { self.as_local_mut() @@ -397,7 +397,7 @@ impl Worktree { pub fn handle_update_buffer( &mut self, envelope: TypedEnvelope, - _: rpc::Client, + _: Arc, cx: &mut ModelContext, ) -> Result<()> { let payload = envelope.payload.clone(); @@ -444,7 +444,7 @@ impl Worktree { pub fn handle_save_buffer( &mut self, envelope: TypedEnvelope, - rpc: rpc::Client, + rpc: Arc, cx: &mut ModelContext, ) -> Result<()> { let sender_id = envelope.original_sender_id()?; @@ -488,7 +488,7 @@ impl Worktree { pub fn handle_buffer_saved( &mut self, envelope: TypedEnvelope, - _: rpc::Client, + _: Arc, cx: &mut ModelContext, ) -> Result<()> { let payload = envelope.payload.clone(); @@ -966,7 +966,7 @@ impl LocalWorktree { pub fn share( &mut self, - rpc: rpc::Client, + rpc: Arc, cx: &mut ModelContext, ) -> Task> { let snapshot = self.snapshot(); @@ -1068,7 +1068,7 @@ impl fmt::Debug for LocalWorktree { } struct ShareState { - rpc: rpc::Client, + rpc: Arc, remote_id: u64, snapshots_tx: Sender, _subscriptions: Vec, @@ -1078,7 +1078,7 @@ pub struct RemoteWorktree { remote_id: u64, snapshot: Snapshot, snapshot_rx: watch::Receiver, - rpc: rpc::Client, + rpc: Arc, updates_tx: postage::mpsc::Sender, replica_id: ReplicaId, open_buffers: HashMap,