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

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