This commit is contained in:
Antonio Scandurra
2023-10-21 18:21:14 +02:00
parent b7d30fca2b
commit aa3fb28f81
9 changed files with 161 additions and 42 deletions
+37 -3
View File
@@ -13,6 +13,16 @@ impl Context for AsyncAppContext {
type EntityContext<'a, 'w, T: 'static + Send + Sync> = ModelContext<'a, T>;
type Result<T> = Result<T>;
fn refresh(&mut self) -> Self::Result<()> {
let app = self
.0
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock(); // Need this to compile
lock.refresh();
Ok(())
}
fn entity<T: Send + Sync + 'static>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
@@ -21,7 +31,7 @@ impl Context for AsyncAppContext {
.0
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock();
let mut lock = app.lock(); // Need this to compile
Ok(lock.entity(build_entity))
}
@@ -34,7 +44,7 @@ impl Context for AsyncAppContext {
.0
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock();
let mut lock = app.lock(); // Need this to compile
Ok(lock.update_entity(handle, update))
}
@@ -46,9 +56,21 @@ impl Context for AsyncAppContext {
.0
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock();
let lock = app.lock(); // Need this to compile
Ok(lock.read_global(read))
}
fn update_global<G: 'static + Send + Sync, R>(
&mut self,
update: impl FnOnce(&mut G, &mut Self::BorrowedContext<'_, '_>) -> R,
) -> Self::Result<R> {
let app = self
.0
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock(); // Need this to compile
Ok(lock.update_global(update))
}
}
impl AsyncAppContext {
@@ -106,6 +128,10 @@ impl Context for AsyncWindowContext {
type EntityContext<'a, 'w, T: 'static + Send + Sync> = ViewContext<'a, 'w, T>;
type Result<T> = Result<T>;
fn refresh(&mut self) -> Self::Result<()> {
self.app.refresh()
}
fn entity<R: Send + Sync + 'static>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, R>) -> R,
@@ -129,4 +155,12 @@ impl Context for AsyncWindowContext {
) -> Result<R> {
self.app.read_window(self.window, |cx| cx.read_global(read))
}
fn update_global<G: 'static + Send + Sync, R>(
&mut self,
update: impl FnOnce(&mut G, &mut Self::BorrowedContext<'_, '_>) -> R,
) -> Result<R> {
self.app
.update_window(self.window, |cx| cx.update_global(update))
}
}
+14
View File
@@ -136,6 +136,10 @@ impl<'a, T: 'static> Context for ModelContext<'a, T> {
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ModelContext<'b, U>;
type Result<U> = U;
fn refresh(&mut self) {
self.app.refresh();
}
fn entity<U: Send + Sync + 'static>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, U>) -> U,
@@ -157,4 +161,14 @@ impl<'a, T: 'static> Context for ModelContext<'a, T> {
) -> R {
read(self.app.global(), self)
}
fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
where
G: 'static + Send + Sync,
{
let mut global = self.app.pop_global::<G>();
let result = f(global.as_mut(), self);
self.app.push_global(global);
result
}
}