From 4cc1556ca4010af1abc58ffad4720296866a7ebb Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 29 Nov 2021 14:12:53 -0800 Subject: [PATCH] Introduce weak_handle methods on ModelContext and ViewContext Co-Authored-By: Nathan Sobo --- crates/chat_panel/src/lib.rs | 2 +- crates/client/src/lib.rs | 4 ++-- crates/editor/src/lib.rs | 2 +- crates/file_finder/src/lib.rs | 2 +- crates/gpui/src/app.rs | 20 ++++++++++++++------ crates/gpui/src/views/select.rs | 2 +- crates/project_panel/src/lib.rs | 2 +- 7 files changed, 21 insertions(+), 13 deletions(-) diff --git a/crates/chat_panel/src/lib.rs b/crates/chat_panel/src/lib.rs index d9d3ce64fa9590a0c643fdc3bdea014064a291c8..44c4bd6295adb4e43025ee1e2e00854761ec97b0 100644 --- a/crates/chat_panel/src/lib.rs +++ b/crates/chat_panel/src/lib.rs @@ -96,7 +96,7 @@ impl ChatPanel { }); let mut message_list = ListState::new(0, Orientation::Bottom, 1000., { - let this = cx.handle().downgrade(); + let this = cx.weak_handle(); move |ix, cx| { let this = this.upgrade(cx).unwrap().read(cx); let message = this.active_channel.as_ref().unwrap().0.read(cx).message(ix); diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 1f3e2c008bc6c6a48965405c73f775f88d419464..652278d785116484bc5275e22427b87652963c32 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -272,7 +272,7 @@ impl Client { let subscription_id = (TypeId::of::(), Default::default()); let client = self.clone(); let mut state = self.state.write(); - let model = cx.handle().downgrade(); + let model = cx.weak_handle(); let prev_extractor = state .entity_id_extractors .insert(subscription_id.0, Box::new(|_| Default::default())); @@ -317,7 +317,7 @@ impl Client { let subscription_id = (TypeId::of::(), remote_id); let client = self.clone(); let mut state = self.state.write(); - let model = cx.handle().downgrade(); + let model = cx.weak_handle(); state .entity_id_extractors .entry(subscription_id.0) diff --git a/crates/editor/src/lib.rs b/crates/editor/src/lib.rs index 4566badd9859b6ccb1bde33a34c975e7656a4829..2a893a25b0198f0b1759a012ce9c70ef28a6ee59 100644 --- a/crates/editor/src/lib.rs +++ b/crates/editor/src/lib.rs @@ -487,7 +487,7 @@ impl Editor { ) }); Self { - handle: cx.handle().downgrade(), + handle: cx.weak_handle(), buffer, display_map, selection_set_id, diff --git a/crates/file_finder/src/lib.rs b/crates/file_finder/src/lib.rs index 21280c2be502f33edfe599b02b2c12897bd5a868..8fef0b6bdf7e29692a08a233b883350e63275b20 100644 --- a/crates/file_finder/src/lib.rs +++ b/crates/file_finder/src/lib.rs @@ -286,7 +286,7 @@ impl FileFinder { .detach(); Self { - handle: cx.handle().downgrade(), + handle: cx.weak_handle(), settings, project, query_editor, diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 0e2e313db04a9393a85ad5e58fec3b3977559929..90dcb5365bc0401fd11a0606730d2cf3d9781644 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -2110,7 +2110,7 @@ impl<'a, T: Entity> ModelContext<'a, T> { S::Event: 'static, F: 'static + FnMut(&mut T, ModelHandle, &S::Event, &mut ModelContext), { - let subscriber = self.handle().downgrade(); + let subscriber = self.weak_handle(); self.app .subscribe_internal(handle, move |emitter, event, cx| { if let Some(subscriber) = subscriber.upgrade(cx) { @@ -2129,7 +2129,7 @@ impl<'a, T: Entity> ModelContext<'a, T> { S: Entity, F: 'static + FnMut(&mut T, ModelHandle, &mut ModelContext), { - let observer = self.handle().downgrade(); + let observer = self.weak_handle(); self.app.observe_internal(handle, move |observed, cx| { if let Some(observer) = observer.upgrade(cx) { observer.update(cx, |observer, cx| { @@ -2146,6 +2146,10 @@ impl<'a, T: Entity> ModelContext<'a, T> { ModelHandle::new(self.model_id, &self.app.cx.ref_counts) } + pub fn weak_handle(&self) -> WeakModelHandle { + WeakModelHandle::new(self.model_id) + } + pub fn spawn(&self, f: F) -> Task where F: FnOnce(ModelHandle, AsyncAppContext) -> Fut, @@ -2162,7 +2166,7 @@ impl<'a, T: Entity> ModelContext<'a, T> { Fut: 'static + Future, S: 'static, { - let handle = self.handle().downgrade(); + let handle = self.weak_handle(); self.app.spawn(|cx| f(handle, cx)) } } @@ -2241,6 +2245,10 @@ impl<'a, T: View> ViewContext<'a, T> { ViewHandle::new(self.window_id, self.view_id, &self.app.cx.ref_counts) } + pub fn weak_handle(&self) -> WeakViewHandle { + WeakViewHandle::new(self.window_id, self.view_id) + } + pub fn window_id(&self) -> usize { self.window_id } @@ -2336,7 +2344,7 @@ impl<'a, T: View> ViewContext<'a, T> { H: Handle, F: 'static + FnMut(&mut T, H, &E::Event, &mut ViewContext), { - let subscriber = self.handle().downgrade(); + let subscriber = self.weak_handle(); self.app .subscribe_internal(handle, move |emitter, event, cx| { if let Some(subscriber) = subscriber.upgrade(cx) { @@ -2356,7 +2364,7 @@ impl<'a, T: View> ViewContext<'a, T> { H: Handle, F: 'static + FnMut(&mut T, H, &mut ViewContext), { - let observer = self.handle().downgrade(); + let observer = self.weak_handle(); self.app.observe_internal(handle, move |observed, cx| { if let Some(observer) = observer.upgrade(cx) { observer.update(cx, |observer, cx| { @@ -2400,7 +2408,7 @@ impl<'a, T: View> ViewContext<'a, T> { Fut: 'static + Future, S: 'static, { - let handle = self.handle().downgrade(); + let handle = self.weak_handle(); self.app.spawn(|cx| f(handle, cx)) } } diff --git a/crates/gpui/src/views/select.rs b/crates/gpui/src/views/select.rs index a76f156ebfe0fd31ec842b351680063d6a2d1e50..d7deea6d553da0360f327ca560343f65e167bf24 100644 --- a/crates/gpui/src/views/select.rs +++ b/crates/gpui/src/views/select.rs @@ -42,7 +42,7 @@ impl Select { render_item: F, ) -> Self { Self { - handle: cx.handle().downgrade(), + handle: cx.weak_handle(), render_item: Box::new(render_item), selected_item_ix: 0, item_count, diff --git a/crates/project_panel/src/lib.rs b/crates/project_panel/src/lib.rs index 4d05ed317a8e5fd0091f1f5b936ecbbcf18e9255..d8ce1df3c7ee2af1a713cdd17a048ce12f862cfc 100644 --- a/crates/project_panel/src/lib.rs +++ b/crates/project_panel/src/lib.rs @@ -108,7 +108,7 @@ impl ProjectPanel { visible_entries: Default::default(), expanded_dir_ids: Default::default(), selection: None, - handle: cx.handle().downgrade(), + handle: cx.weak_handle(), }; this.update_visible_entries(None, cx); this