diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 5a00f27ddf..62135900a3 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -18,8 +18,8 @@ use gpui::{ actions, platform::AppVersion, serde_json::{self, Value}, - AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, AppContext, - AsyncAppContext, Entity, ModelHandle, Task, View, ViewContext, ViewHandle, + AnyModelHandle, AnyWeakModelHandle, AnyWeakViewHandle, AppContext, AsyncAppContext, Entity, + ModelHandle, Task, View, ViewContext, WeakViewHandle, }; use lazy_static::lazy_static; use parking_lot::RwLock; @@ -221,7 +221,7 @@ enum WeakSubscriber { enum Subscriber { Model(AnyModelHandle), - View(AnyViewHandle), + View(AnyWeakViewHandle), } #[derive(Clone, Debug)] @@ -567,7 +567,7 @@ impl Client { H: 'static + Send + Sync - + Fn(ViewHandle, TypedEnvelope, Arc, AsyncAppContext) -> F, + + Fn(WeakViewHandle, TypedEnvelope, Arc, AsyncAppContext) -> F, F: 'static + Future>, { self.add_entity_message_handler::(move |handle, message, client, cx| { @@ -666,7 +666,7 @@ impl Client { H: 'static + Send + Sync - + Fn(ViewHandle, TypedEnvelope, Arc, AsyncAppContext) -> F, + + Fn(WeakViewHandle, TypedEnvelope, Arc, AsyncAppContext) -> F, F: 'static + Future>, { self.add_view_message_handler(move |entity, envelope, client, cx| { @@ -1273,7 +1273,15 @@ impl Client { pending.push(message); return; } - Some(weak_subscriber @ _) => subscriber = weak_subscriber.upgrade(cx), + Some(weak_subscriber @ _) => match weak_subscriber { + WeakSubscriber::Model(handle) => { + subscriber = handle.upgrade(cx).map(Subscriber::Model); + } + WeakSubscriber::View(handle) => { + subscriber = Some(Subscriber::View(handle.clone())); + } + WeakSubscriber::Pending(_) => {} + }, _ => {} } } @@ -1357,16 +1365,6 @@ impl Client { } } -impl WeakSubscriber { - fn upgrade(&self, cx: &AsyncAppContext) -> Option { - match self { - WeakSubscriber::Model(handle) => handle.upgrade(cx).map(Subscriber::Model), - WeakSubscriber::View(handle) => handle.upgrade(cx).map(Subscriber::View), - WeakSubscriber::Pending(_) => None, - } - } -} - fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option { if IMPERSONATE_LOGIN.is_some() { return None; diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index a67e28715b..3739620e3b 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -4156,9 +4156,24 @@ impl AnyWeakViewHandle { self.view_id } + fn is(&self) -> bool { + TypeId::of::() == self.view_type + } + pub fn upgrade(&self, cx: &impl BorrowAppContext) -> Option { cx.read_with(|cx| cx.upgrade_any_view_handle(self)) } + + pub fn downcast(self) -> Option> { + if self.is::() { + Some(WeakViewHandle { + any_handle: self, + view_type: PhantomData, + }) + } else { + None + } + } } impl Hash for AnyWeakViewHandle { diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index f626227989..0796c7c283 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -2219,7 +2219,7 @@ impl Workspace { // RPC handlers async fn handle_follow( - this: ViewHandle, + this: WeakViewHandle, envelope: TypedEnvelope, _: Arc, mut cx: AsyncAppContext, @@ -2267,7 +2267,7 @@ impl Workspace { } async fn handle_unfollow( - this: ViewHandle, + this: WeakViewHandle, envelope: TypedEnvelope, _: Arc, mut cx: AsyncAppContext, @@ -2282,7 +2282,7 @@ impl Workspace { } async fn handle_update_followers( - this: ViewHandle, + this: WeakViewHandle, envelope: TypedEnvelope, _: Arc, cx: AsyncAppContext,