Start on subscribing to messages in channel entity instances
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com> Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
co-authored by
Max Brunsfeld
Antonio Scandurra
parent
5b599a32b8
commit
541f58e12c
+58
-8
@@ -70,6 +70,11 @@ pub trait UpdateModel {
|
||||
F: FnOnce(&mut T, &mut ModelContext<T>) -> S;
|
||||
}
|
||||
|
||||
pub trait UpgradeModelHandle {
|
||||
fn upgrade_model_handle<T: Entity>(&self, handle: WeakModelHandle<T>)
|
||||
-> Option<ModelHandle<T>>;
|
||||
}
|
||||
|
||||
pub trait ReadView {
|
||||
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T;
|
||||
}
|
||||
@@ -457,6 +462,15 @@ impl UpdateModel for AsyncAppContext {
|
||||
}
|
||||
}
|
||||
|
||||
impl UpgradeModelHandle for AsyncAppContext {
|
||||
fn upgrade_model_handle<T: Entity>(
|
||||
&self,
|
||||
handle: WeakModelHandle<T>,
|
||||
) -> Option<ModelHandle<T>> {
|
||||
self.0.borrow_mut().upgrade_model_handle(handle)
|
||||
}
|
||||
}
|
||||
|
||||
impl ReadModelWith for AsyncAppContext {
|
||||
fn read_model_with<E: Entity, F: FnOnce(&E, &AppContext) -> T, T>(
|
||||
&self,
|
||||
@@ -1434,6 +1448,15 @@ impl UpdateModel for MutableAppContext {
|
||||
}
|
||||
}
|
||||
|
||||
impl UpgradeModelHandle for MutableAppContext {
|
||||
fn upgrade_model_handle<T: Entity>(
|
||||
&self,
|
||||
handle: WeakModelHandle<T>,
|
||||
) -> Option<ModelHandle<T>> {
|
||||
self.cx.upgrade_model_handle(handle)
|
||||
}
|
||||
}
|
||||
|
||||
impl ReadView for MutableAppContext {
|
||||
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
|
||||
if let Some(view) = self.cx.views.get(&(handle.window_id, handle.view_id)) {
|
||||
@@ -1565,6 +1588,19 @@ impl ReadModel for AppContext {
|
||||
}
|
||||
}
|
||||
|
||||
impl UpgradeModelHandle for AppContext {
|
||||
fn upgrade_model_handle<T: Entity>(
|
||||
&self,
|
||||
handle: WeakModelHandle<T>,
|
||||
) -> Option<ModelHandle<T>> {
|
||||
if self.models.contains_key(&handle.model_id) {
|
||||
Some(ModelHandle::new(handle.model_id, &self.ref_counts))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ReadView for AppContext {
|
||||
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
|
||||
if let Some(view) = self.views.get(&(handle.window_id, handle.view_id)) {
|
||||
@@ -1858,6 +1894,15 @@ impl<M> UpdateModel for ModelContext<'_, M> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<M> UpgradeModelHandle for ModelContext<'_, M> {
|
||||
fn upgrade_model_handle<T: Entity>(
|
||||
&self,
|
||||
handle: WeakModelHandle<T>,
|
||||
) -> Option<ModelHandle<T>> {
|
||||
self.cx.upgrade_model_handle(handle)
|
||||
}
|
||||
}
|
||||
|
||||
impl<M> Deref for ModelContext<'_, M> {
|
||||
type Target = MutableAppContext;
|
||||
|
||||
@@ -2175,6 +2220,15 @@ impl<V> ReadModel for ViewContext<'_, V> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> UpgradeModelHandle for ViewContext<'_, V> {
|
||||
fn upgrade_model_handle<T: Entity>(
|
||||
&self,
|
||||
handle: WeakModelHandle<T>,
|
||||
) -> Option<ModelHandle<T>> {
|
||||
self.cx.upgrade_model_handle(handle)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: View> UpdateModel for ViewContext<'_, V> {
|
||||
fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
|
||||
where
|
||||
@@ -2372,7 +2426,6 @@ impl<T> Handle<T> for ModelHandle<T> {
|
||||
EntityLocation::Model(self.model_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WeakModelHandle<T> {
|
||||
model_id: usize,
|
||||
model_type: PhantomData<T>,
|
||||
@@ -2386,13 +2439,8 @@ impl<T: Entity> WeakModelHandle<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn upgrade(&self, cx: impl AsRef<AppContext>) -> Option<ModelHandle<T>> {
|
||||
let cx = cx.as_ref();
|
||||
if cx.models.contains_key(&self.model_id) {
|
||||
Some(ModelHandle::new(self.model_id, &cx.ref_counts))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
pub fn upgrade(self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<T>> {
|
||||
cx.upgrade_model_handle(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2419,6 +2467,8 @@ impl<T> Clone for WeakModelHandle<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Copy for WeakModelHandle<T> {}
|
||||
|
||||
pub struct ViewHandle<T> {
|
||||
window_id: usize,
|
||||
view_id: usize,
|
||||
|
||||
Reference in New Issue
Block a user