Add the entity trait and implement for models, views, subscriptions, and observations

This commit is contained in:
Mikayla
2023-10-30 17:50:21 -07:00
parent 58446c2715
commit 327a2f9967
13 changed files with 183 additions and 101 deletions
+4 -4
View File
@@ -32,7 +32,7 @@ impl Context for AsyncAppContext {
Ok(lock.build_model(build_model))
}
fn update_entity<T: 'static, R>(
fn update_model<T: 'static, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
@@ -42,7 +42,7 @@ impl Context for AsyncAppContext {
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock(); // Need this to compile
Ok(lock.update_entity(handle, update))
Ok(lock.update_model(handle, update))
}
}
@@ -230,13 +230,13 @@ impl Context for AsyncWindowContext {
.update_window(self.window, |cx| cx.build_model(build_model))
}
fn update_entity<T: 'static, R>(
fn update_model<T: 'static, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Result<R> {
self.app
.update_window(self.window, |cx| cx.update_entity(handle, update))
.update_window(self.window, |cx| cx.update_model(handle, update))
}
}
+39 -13
View File
@@ -1,4 +1,4 @@
use crate::{AnyBox, AppContext, Context};
use crate::{private::Sealed, AnyBox, AppContext, Context, Entity};
use anyhow::{anyhow, Result};
use derive_more::{Deref, DerefMut};
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
@@ -253,6 +253,32 @@ pub struct Model<T> {
unsafe impl<T> Send for Model<T> {}
unsafe impl<T> Sync for Model<T> {}
impl<T> Sealed for Model<T> {}
impl<T: 'static> Entity<T> for Model<T> {
type Weak = WeakModel<T>;
fn entity_id(&self) -> EntityId {
self.any_model.entity_id
}
fn downgrade(&self) -> Self::Weak {
WeakModel {
any_model: self.any_model.downgrade(),
entity_type: self.entity_type,
}
}
fn upgrade_from(weak: &Self::Weak) -> Option<Self>
where
Self: Sized,
{
Some(Model {
any_model: weak.any_model.upgrade()?,
entity_type: weak.entity_type,
})
}
}
impl<T: 'static> Model<T> {
fn new(id: EntityId, entity_map: Weak<RwLock<EntityRefCounts>>) -> Self
@@ -265,11 +291,12 @@ impl<T: 'static> Model<T> {
}
}
/// Downgrade the this to a weak model reference
pub fn downgrade(&self) -> WeakModel<T> {
WeakModel {
any_model: self.any_model.downgrade(),
entity_type: self.entity_type,
}
// Delegate to the trait implementation to keep behavior in one place.
// This method was included to improve method resolution in the presence of
// the Model's deref
Entity::downgrade(self)
}
/// Convert this into a dynamically typed model.
@@ -294,7 +321,7 @@ impl<T: 'static> Model<T> {
where
C: Context,
{
cx.update_entity(self, update)
cx.update_model(self, update)
}
}
@@ -334,7 +361,7 @@ impl<T> Eq for Model<T> {}
impl<T> PartialEq<WeakModel<T>> for Model<T> {
fn eq(&self, other: &WeakModel<T>) -> bool {
self.entity_id() == other.entity_id()
self.any_model.entity_id() == other.entity_id()
}
}
@@ -415,11 +442,10 @@ impl<T> Clone for WeakModel<T> {
}
impl<T: 'static> WeakModel<T> {
/// Upgrade this weak model reference into a strong model reference
pub fn upgrade(&self) -> Option<Model<T>> {
Some(Model {
any_model: self.any_model.upgrade()?,
entity_type: self.entity_type,
})
// Delegate to the trait implementation to keep behavior in one place.
Model::upgrade_from(self)
}
/// Update the entity referenced by this model with the given function if
@@ -441,7 +467,7 @@ impl<T: 'static> WeakModel<T> {
crate::Flatten::flatten(
self.upgrade()
.ok_or_else(|| anyhow!("entity release"))
.map(|this| cx.update_entity(&this, update)),
.map(|this| cx.update_model(&this, update)),
)
}
}
@@ -462,6 +488,6 @@ impl<T> Eq for WeakModel<T> {}
impl<T> PartialEq<Model<T>> for WeakModel<T> {
fn eq(&self, other: &Model<T>) -> bool {
self.entity_id() == other.entity_id()
self.entity_id() == other.any_model.entity_id()
}
}
+39 -30
View File
@@ -1,6 +1,6 @@
use crate::{
AppContext, AsyncAppContext, Context, Effect, EntityId, EventEmitter, MainThread, Model,
Reference, Subscription, Task, WeakModel,
AppContext, AsyncAppContext, Context, Effect, Entity, EntityId, EventEmitter, MainThread,
Model, Reference, Subscription, Task, WeakModel,
};
use derive_more::{Deref, DerefMut};
use futures::FutureExt;
@@ -31,29 +31,32 @@ impl<'a, T: 'static> ModelContext<'a, T> {
}
pub fn handle(&self) -> Model<T> {
self.weak_handle()
self.weak_model()
.upgrade()
.expect("The entity must be alive if we have a model context")
}
pub fn weak_handle(&self) -> WeakModel<T> {
pub fn weak_model(&self) -> WeakModel<T> {
self.model_state.clone()
}
pub fn observe<T2: 'static>(
pub fn observe<T2, E>(
&mut self,
handle: &Model<T2>,
mut on_notify: impl FnMut(&mut T, Model<T2>, &mut ModelContext<'_, T>) + Send + 'static,
entity: &E,
mut on_notify: impl FnMut(&mut T, E, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: 'static + Send,
T2: 'static,
E: Entity<T2>,
{
let this = self.weak_handle();
let handle = handle.downgrade();
let this = self.weak_model();
let entity_id = entity.entity_id();
let handle = entity.downgrade();
self.app.observers.insert(
handle.entity_id,
entity_id,
Box::new(move |cx| {
if let Some((this, handle)) = this.upgrade().zip(handle.upgrade()) {
if let Some((this, handle)) = this.upgrade().zip(E::upgrade_from(&handle)) {
this.update(cx, |this, cx| on_notify(this, handle, cx));
true
} else {
@@ -63,21 +66,24 @@ impl<'a, T: 'static> ModelContext<'a, T> {
)
}
pub fn subscribe<E: 'static + EventEmitter>(
pub fn subscribe<T2, E>(
&mut self,
handle: &Model<E>,
mut on_event: impl FnMut(&mut T, Model<E>, &E::Event, &mut ModelContext<'_, T>) + Send + 'static,
entity: &E,
mut on_event: impl FnMut(&mut T, E, &T2::Event, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: 'static + Send,
T2: 'static + EventEmitter,
E: Entity<T2>,
{
let this = self.weak_handle();
let handle = handle.downgrade();
let this = self.weak_model();
let entity_id = entity.entity_id();
let entity = entity.downgrade();
self.app.event_listeners.insert(
handle.entity_id,
entity_id,
Box::new(move |event, cx| {
let event: &E::Event = event.downcast_ref().expect("invalid event type");
if let Some((this, handle)) = this.upgrade().zip(handle.upgrade()) {
let event: &T2::Event = event.downcast_ref().expect("invalid event type");
if let Some((this, handle)) = this.upgrade().zip(E::upgrade_from(&entity)) {
this.update(cx, |this, cx| on_event(this, handle, event, cx));
true
} else {
@@ -103,17 +109,20 @@ impl<'a, T: 'static> ModelContext<'a, T> {
)
}
pub fn observe_release<E: 'static>(
pub fn observe_release<T2, E>(
&mut self,
handle: &Model<E>,
mut on_release: impl FnMut(&mut T, &mut E, &mut ModelContext<'_, T>) + Send + 'static,
entity: &E,
mut on_release: impl FnMut(&mut T, &mut T2, &mut ModelContext<'_, T>) + Send + 'static,
) -> Subscription
where
T: Any + Send,
T2: 'static,
E: Entity<T2>,
{
let this = self.weak_handle();
let entity_id = entity.entity_id();
let this = self.weak_model();
self.app.release_listeners.insert(
handle.entity_id,
entity_id,
Box::new(move |entity, cx| {
let entity = entity.downcast_mut().expect("invalid entity type");
if let Some(this) = this.upgrade() {
@@ -130,7 +139,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
where
T: 'static + Send,
{
let handle = self.weak_handle();
let handle = self.weak_model();
self.global_observers.insert(
TypeId::of::<G>(),
Box::new(move |cx| handle.update(cx, |view, cx| f(view, cx)).is_ok()),
@@ -145,7 +154,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
Fut: 'static + Future<Output = ()> + Send,
T: 'static + Send,
{
let handle = self.weak_handle();
let handle = self.weak_model();
self.app.quit_observers.insert(
(),
Box::new(move |cx| {
@@ -191,7 +200,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
Fut: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
let this = self.weak_handle();
let this = self.weak_model();
self.app.spawn(|cx| f(this, cx))
}
@@ -203,7 +212,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
Fut: Future<Output = R> + 'static,
R: Send + 'static,
{
let this = self.weak_handle();
let this = self.weak_model();
self.app.spawn_on_main(|cx| f(this, cx))
}
}
@@ -235,12 +244,12 @@ impl<'a, T> Context for ModelContext<'a, T> {
self.app.build_model(build_model)
}
fn update_entity<U: 'static, R>(
fn update_model<U: 'static, R>(
&mut self,
handle: &Model<U>,
update: impl FnOnce(&mut U, &mut Self::ModelContext<'_, U>) -> R,
) -> R {
self.app.update_entity(handle, update)
self.app.update_model(handle, update)
}
}
+2 -2
View File
@@ -26,13 +26,13 @@ impl Context for TestAppContext {
lock.build_model(build_model)
}
fn update_entity<T: 'static, R>(
fn update_model<T: 'static, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Self::Result<R> {
let mut lock = self.app.lock();
lock.update_entity(handle, update)
lock.update_model(handle, update)
}
}