From f2848a092b36e3c7a57ecbfe63e953216ba72cf8 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 7 Mar 2022 18:45:01 -0700 Subject: [PATCH] WIP: Start on a delegation... like events, but single consumer that takes ownership of event --- crates/gpui/src/app.rs | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 6548223933..182d56b3c2 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -60,6 +60,10 @@ pub trait View: Entity + Sized { } } +pub trait Delegator: Entity { + type Delegation; +} + pub trait ReadModel { fn read_model(&self, handle: &ModelHandle) -> &T; } @@ -740,6 +744,7 @@ type ActionCallback = type GlobalActionCallback = dyn FnMut(&dyn AnyAction, &mut MutableAppContext); type SubscriptionCallback = Box bool>; +type DelegationCallback = Box, &mut MutableAppContext) -> bool>; type ObservationCallback = Box bool>; type ReleaseObservationCallback = Box; @@ -757,6 +762,7 @@ pub struct MutableAppContext { next_subscription_id: usize, frame_count: usize, subscriptions: Arc>>>, + delegations: Arc>>, observations: Arc>>>, release_observations: Arc>>>, presenters_and_platform_windows: @@ -804,6 +810,7 @@ impl MutableAppContext { next_subscription_id: 0, frame_count: 0, subscriptions: Default::default(), + delegations: Default::default(), observations: Default::default(), release_observations: Default::default(), presenters_and_platform_windows: HashMap::new(), @@ -1149,6 +1156,37 @@ impl MutableAppContext { } } + pub fn become_delegate_internal(&mut self, handle: &H, mut callback: F) -> Subscription + where + E: Entity, + E::Event: 'static, + H: Handle, + F: 'static + FnMut(H, &E::Event, &mut Self) -> bool, + { + let id = post_inc(&mut self.next_subscription_id); + let emitter = handle.downgrade(); + self.subscriptions + .lock() + .entry(handle.id()) + .or_default() + .insert( + id, + Box::new(move |payload, cx| { + if let Some(emitter) = H::upgrade_from(&emitter, cx.as_ref()) { + let payload = payload.downcast_ref().expect("downcast is type safe"); + callback(emitter, payload, cx) + } else { + false + } + }), + ); + Subscription::Subscription { + id, + entity_id: handle.id(), + subscriptions: Some(Arc::downgrade(&self.subscriptions)), + } + } + pub fn observe_release(&mut self, handle: &H, mut callback: F) -> Subscription where E: Entity, @@ -2590,6 +2628,27 @@ impl<'a, T: View> ViewContext<'a, T> { }) } + pub fn become_delegate(&mut self, handle: &H, mut callback: F) -> Subscription + where + E: Delegator, + E::Event: 'static, + H: Handle, + F: 'static + FnMut(&mut T, H, E::Event, &mut ViewContext), + { + // let subscriber = self.weak_handle(); + // self.app + // .subscribe_internal(handle, move |emitter, event, cx| { + // if let Some(subscriber) = subscriber.upgrade(cx) { + // subscriber.update(cx, |subscriber, cx| { + // callback(subscriber, emitter, event, cx); + // }); + // true + // } else { + // false + // } + // }) + } + pub fn observe_release(&mut self, handle: &H, mut callback: F) -> Subscription where E: Entity,