This commit is contained in:
Antonio Scandurra
2023-11-01 18:34:51 +01:00
parent c3a8bab4d2
commit 337a79e35f
5 changed files with 56 additions and 72 deletions
+2 -6
View File
@@ -504,16 +504,12 @@ impl AppContext {
/// Spawns the future returned by the given function on the thread pool. The closure will be invoked
/// with AsyncAppContext, which allows the application state to be accessed across await points.
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut) -> Task<R>
where
Fut: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
let cx = self.to_async();
self.executor.spawn(async move {
let future = f(cx);
future.await
})
self.executor.spawn(f(self.to_async()))
}
/// Schedules the given function to be run at the end of the current effect cycle, allowing entities
+1 -4
View File
@@ -189,10 +189,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
result
}
pub fn spawn<Fut, R>(
&self,
f: impl FnOnce(WeakModel<T>, AsyncAppContext) -> Fut + Send + 'static,
) -> Task<R>
pub fn spawn<Fut, R>(&self, f: impl FnOnce(WeakModel<T>, AsyncAppContext) -> Fut) -> Task<R>
where
T: 'static,
Fut: Future<Output = R> + Send + 'static,
+4 -8
View File
@@ -484,7 +484,7 @@ impl<'a> WindowContext<'a> {
/// use within your future.
pub fn spawn<Fut, R>(
&mut self,
f: impl FnOnce(AnyWindowHandle, AsyncWindowContext) -> Fut + Send + 'static,
f: impl FnOnce(AnyWindowHandle, AsyncWindowContext) -> Fut,
) -> Task<R>
where
R: Send + 'static,
@@ -493,8 +493,7 @@ impl<'a> WindowContext<'a> {
let window = self.window.handle;
self.app.spawn(move |app| {
let cx = AsyncWindowContext::new(app, window);
let future = f(window, cx);
async move { future.await }
f(window, cx)
})
}
@@ -1872,17 +1871,14 @@ impl<'a, V: 'static> ViewContext<'a, V> {
pub fn spawn<Fut, R>(
&mut self,
f: impl FnOnce(WeakView<V>, AsyncWindowContext) -> Fut + Send + 'static,
f: impl FnOnce(WeakView<V>, AsyncWindowContext) -> Fut,
) -> Task<R>
where
R: Send + 'static,
Fut: Future<Output = R> + Send + 'static,
{
let view = self.view().downgrade();
self.window_cx.spawn(move |_, cx| {
let result = f(view, cx);
async move { result.await }
})
self.window_cx.spawn(move |_, cx| f(view, cx))
}
pub fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R