Pass a handle to the current view model when spawning

Most of the time, we'll want a way to get a reference back to the current view or model, so this facilitates that common case.
This commit is contained in:
Nathan Sobo
2021-05-12 15:28:59 -06:00
parent fa6bd1f926
commit a3be5595dd
6 changed files with 41 additions and 44 deletions
+6 -4
View File
@@ -1654,11 +1654,12 @@ impl<'a, T: Entity> ModelContext<'a, T> {
pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
where
F: FnOnce(AsyncAppContext) -> Fut,
F: FnOnce(ModelHandle<T>, AsyncAppContext) -> Fut,
Fut: 'static + Future<Output = S>,
S: 'static,
{
self.app.spawn(f)
let handle = self.handle();
self.app.spawn(|ctx| f(handle, ctx))
}
}
@@ -1909,11 +1910,12 @@ impl<'a, T: View> ViewContext<'a, T> {
pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
where
F: FnOnce(AsyncAppContext) -> Fut,
F: FnOnce(ViewHandle<T>, AsyncAppContext) -> Fut,
Fut: 'static + Future<Output = S>,
S: 'static,
{
self.app.spawn(f)
let handle = self.handle();
self.app.spawn(|ctx| f(handle, ctx))
}
}