Introduce weak_handle methods on ModelContext and ViewContext

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-11-29 14:12:53 -08:00
parent 29b616f4cc
commit 4cc1556ca4
7 changed files with 21 additions and 13 deletions

View File

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

View File

@ -272,7 +272,7 @@ impl Client {
let subscription_id = (TypeId::of::<T>(), 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::<T>(), 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)

View File

@ -487,7 +487,7 @@ impl Editor {
)
});
Self {
handle: cx.handle().downgrade(),
handle: cx.weak_handle(),
buffer,
display_map,
selection_set_id,

View File

@ -286,7 +286,7 @@ impl FileFinder {
.detach();
Self {
handle: cx.handle().downgrade(),
handle: cx.weak_handle(),
settings,
project,
query_editor,

View File

@ -2110,7 +2110,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
S::Event: 'static,
F: 'static + FnMut(&mut T, ModelHandle<S>, &S::Event, &mut ModelContext<T>),
{
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<S>, &mut ModelContext<T>),
{
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<T> {
WeakModelHandle::new(self.model_id)
}
pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
where
F: FnOnce(ModelHandle<T>, AsyncAppContext) -> Fut,
@ -2162,7 +2166,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
Fut: 'static + Future<Output = S>,
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<T> {
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<E>,
F: 'static + FnMut(&mut T, H, &E::Event, &mut ViewContext<T>),
{
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<E>,
F: 'static + FnMut(&mut T, H, &mut ViewContext<T>),
{
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<Output = S>,
S: 'static,
{
let handle = self.handle().downgrade();
let handle = self.weak_handle();
self.app.spawn(|cx| f(handle, cx))
}
}

View File

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

View File

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