mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Remove delegate support from GPUI
We added this because we thought it would save some allocations when sending operations given that we could move them to the delegate upon notifying it, but the reality is that we serialize operations and that only requires a reference.
This commit is contained in:
parent
0a9595b5fa
commit
ef1ec88523
@ -740,7 +740,6 @@ type ActionCallback =
|
||||
type GlobalActionCallback = dyn FnMut(&dyn AnyAction, &mut MutableAppContext);
|
||||
|
||||
type SubscriptionCallback = Box<dyn FnMut(&dyn Any, &mut MutableAppContext) -> bool>;
|
||||
type DelegationCallback = Box<dyn FnMut(Box<dyn Any>, &mut MutableAppContext) -> bool>;
|
||||
type ObservationCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
|
||||
type ReleaseObservationCallback = Box<dyn FnMut(&dyn Any, &mut MutableAppContext)>;
|
||||
|
||||
@ -758,7 +757,6 @@ pub struct MutableAppContext {
|
||||
next_subscription_id: usize,
|
||||
frame_count: usize,
|
||||
subscriptions: Arc<Mutex<HashMap<usize, BTreeMap<usize, SubscriptionCallback>>>>,
|
||||
delegations: Arc<Mutex<HashMap<usize, (usize, DelegationCallback)>>>,
|
||||
observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, ObservationCallback>>>>,
|
||||
release_observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, ReleaseObservationCallback>>>>,
|
||||
presenters_and_platform_windows:
|
||||
@ -806,7 +804,6 @@ 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(),
|
||||
@ -1152,35 +1149,6 @@ impl MutableAppContext {
|
||||
}
|
||||
}
|
||||
|
||||
fn become_delegate_internal<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
||||
where
|
||||
E: Entity,
|
||||
H: Handle<E>,
|
||||
F: 'static + FnMut(H, E::Event, &mut Self) -> bool,
|
||||
{
|
||||
let id = post_inc(&mut self.next_subscription_id);
|
||||
let emitter = handle.downgrade();
|
||||
self.delegations.lock().insert(
|
||||
handle.id(),
|
||||
(
|
||||
id,
|
||||
Box::new(move |payload, cx| {
|
||||
if let Some(emitter) = H::upgrade_from(&emitter, cx.as_ref()) {
|
||||
let payload = *payload.downcast().expect("downcast is type safe");
|
||||
callback(emitter, payload, cx)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}),
|
||||
),
|
||||
);
|
||||
Subscription::Delegation {
|
||||
id,
|
||||
entity_id: handle.id(),
|
||||
delegations: Some(Arc::downgrade(&self.delegations)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn observe_release<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
||||
where
|
||||
E: Entity,
|
||||
@ -1730,17 +1698,6 @@ impl MutableAppContext {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let delegate = self.delegations.lock().remove(&entity_id);
|
||||
if let Some((id, mut callback)) = delegate {
|
||||
let alive = callback(payload, self);
|
||||
if alive {
|
||||
self.delegations
|
||||
.lock()
|
||||
.entry(entity_id)
|
||||
.or_insert_with(|| (id, callback));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn notify_model_observers(&mut self, observed_id: usize) {
|
||||
@ -2387,26 +2344,6 @@ impl<'a, T: Entity> ModelContext<'a, T> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn become_delegate<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
||||
where
|
||||
E: Entity,
|
||||
H: Handle<E>,
|
||||
F: 'static + FnMut(&mut T, H, E::Event, &mut ModelContext<T>),
|
||||
{
|
||||
let delegate = self.weak_handle();
|
||||
self.app
|
||||
.become_delegate_internal(handle, move |emitter, event, cx| {
|
||||
if let Some(delegate) = delegate.upgrade(cx) {
|
||||
delegate.update(cx, |subscriber, cx| {
|
||||
callback(subscriber, emitter, event, cx);
|
||||
});
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn observe_release<S, F>(
|
||||
&mut self,
|
||||
handle: &ModelHandle<S>,
|
||||
@ -2672,26 +2609,6 @@ impl<'a, T: View> ViewContext<'a, T> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn become_delegate<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
||||
where
|
||||
E: Entity,
|
||||
H: Handle<E>,
|
||||
F: 'static + FnMut(&mut T, H, E::Event, &mut ViewContext<T>),
|
||||
{
|
||||
let delegate = self.weak_handle();
|
||||
self.app
|
||||
.become_delegate_internal(handle, move |emitter, event, cx| {
|
||||
if let Some(delegate) = delegate.upgrade(cx) {
|
||||
delegate.update(cx, |subscriber, cx| {
|
||||
callback(subscriber, emitter, event, cx);
|
||||
});
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn observe_release<E, F, H>(&mut self, handle: &H, mut callback: F) -> Subscription
|
||||
where
|
||||
E: Entity,
|
||||
@ -3845,11 +3762,6 @@ pub enum Subscription {
|
||||
entity_id: usize,
|
||||
subscriptions: Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, SubscriptionCallback>>>>>,
|
||||
},
|
||||
Delegation {
|
||||
id: usize,
|
||||
entity_id: usize,
|
||||
delegations: Option<Weak<Mutex<HashMap<usize, (usize, DelegationCallback)>>>>,
|
||||
},
|
||||
Observation {
|
||||
id: usize,
|
||||
entity_id: usize,
|
||||
@ -3869,9 +3781,6 @@ impl Subscription {
|
||||
Subscription::Subscription { subscriptions, .. } => {
|
||||
subscriptions.take();
|
||||
}
|
||||
Subscription::Delegation { delegations, .. } => {
|
||||
delegations.take();
|
||||
}
|
||||
Subscription::Observation { observations, .. } => {
|
||||
observations.take();
|
||||
}
|
||||
@ -3918,19 +3827,6 @@ impl Drop for Subscription {
|
||||
}
|
||||
}
|
||||
}
|
||||
Subscription::Delegation {
|
||||
id,
|
||||
entity_id,
|
||||
delegations,
|
||||
} => {
|
||||
if let Some(delegations) = delegations.as_ref().and_then(Weak::upgrade) {
|
||||
if let Entry::Occupied(entry) = delegations.lock().entry(*entity_id) {
|
||||
if *id == entry.get().0 {
|
||||
let _ = entry.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4197,11 +4093,6 @@ mod tests {
|
||||
let handle_1 = cx.add_model(|_| Model::default());
|
||||
let handle_2 = cx.add_model(|_| Model::default());
|
||||
handle_1.update(cx, |_, cx| {
|
||||
cx.become_delegate(&handle_2, |model, _, event, _| {
|
||||
model.events.push(event * 3);
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.subscribe(&handle_2, move |model: &mut Model, emitter, event, cx| {
|
||||
model.events.push(*event);
|
||||
|
||||
@ -4214,10 +4105,10 @@ mod tests {
|
||||
});
|
||||
|
||||
handle_2.update(cx, |_, c| c.emit(7));
|
||||
assert_eq!(handle_1.read(cx).events, vec![7, 21]);
|
||||
assert_eq!(handle_1.read(cx).events, vec![7]);
|
||||
|
||||
handle_2.update(cx, |_, c| c.emit(5));
|
||||
assert_eq!(handle_1.read(cx).events, vec![7, 21, 5, 10, 15]);
|
||||
assert_eq!(handle_1.read(cx).events, vec![7, 5, 10]);
|
||||
}
|
||||
|
||||
#[crate::test(self)]
|
||||
@ -4475,11 +4366,6 @@ mod tests {
|
||||
let handle_3 = cx.add_model(|_| Model);
|
||||
|
||||
handle_1.update(cx, |_, cx| {
|
||||
cx.become_delegate(&handle_2, |me, _, event, _| {
|
||||
me.events.push(event * 3);
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.subscribe(&handle_2, move |me, emitter, event, cx| {
|
||||
me.events.push(*event);
|
||||
|
||||
@ -4497,13 +4383,13 @@ mod tests {
|
||||
});
|
||||
|
||||
handle_2.update(cx, |_, c| c.emit(7));
|
||||
assert_eq!(handle_1.read(cx).events, vec![7, 21]);
|
||||
assert_eq!(handle_1.read(cx).events, vec![7]);
|
||||
|
||||
handle_2.update(cx, |_, c| c.emit(5));
|
||||
assert_eq!(handle_1.read(cx).events, vec![7, 21, 5, 10, 15]);
|
||||
assert_eq!(handle_1.read(cx).events, vec![7, 5, 10]);
|
||||
|
||||
handle_3.update(cx, |_, c| c.emit(9));
|
||||
assert_eq!(handle_1.read(cx).events, vec![7, 21, 5, 10, 15, 9]);
|
||||
assert_eq!(handle_1.read(cx).events, vec![7, 5, 10, 9]);
|
||||
}
|
||||
|
||||
#[crate::test(self)]
|
||||
|
@ -80,7 +80,7 @@ fn test_edit_events(cx: &mut gpui::MutableAppContext) {
|
||||
let buffer1_ops = buffer1_ops.clone();
|
||||
|buffer, cx| {
|
||||
let buffer_1_events = buffer_1_events.clone();
|
||||
cx.become_delegate(&buffer1, move |_, _, event, _| match event {
|
||||
cx.subscribe(&buffer1, move |_, _, event, _| match event.clone() {
|
||||
Event::Operation(op) => buffer1_ops.borrow_mut().push(op),
|
||||
event @ _ => buffer_1_events.borrow_mut().push(event),
|
||||
})
|
||||
@ -610,7 +610,7 @@ fn test_random_collaboration(cx: &mut MutableAppContext, mut rng: StdRng) {
|
||||
let mut buffer = Buffer::new(i as ReplicaId, base_text.as_str(), cx);
|
||||
buffer.set_group_interval(Duration::from_millis(rng.gen_range(0..=200)));
|
||||
let network = network.clone();
|
||||
cx.become_delegate(&cx.handle(), move |buffer, _, event, _| {
|
||||
cx.subscribe(&cx.handle(), move |buffer, _, event, _| {
|
||||
if let Event::Operation(op) = event {
|
||||
network
|
||||
.borrow_mut()
|
||||
@ -706,7 +706,7 @@ fn test_random_collaboration(cx: &mut MutableAppContext, mut rng: StdRng) {
|
||||
Buffer::from_proto(new_replica_id, old_buffer, None, cx).unwrap();
|
||||
new_buffer.set_group_interval(Duration::from_millis(rng.gen_range(0..=200)));
|
||||
let network = network.clone();
|
||||
cx.become_delegate(&cx.handle(), move |buffer, _, event, _| {
|
||||
cx.subscribe(&cx.handle(), move |buffer, _, event, _| {
|
||||
if let Event::Operation(op) = event {
|
||||
network.borrow_mut().broadcast(
|
||||
buffer.replica_id(),
|
||||
|
@ -942,7 +942,7 @@ impl Project {
|
||||
remote_id
|
||||
))?,
|
||||
}
|
||||
cx.become_delegate(buffer, |this, buffer, event, cx| {
|
||||
cx.subscribe(buffer, |this, buffer, event, cx| {
|
||||
this.on_buffer_event(buffer, event, cx);
|
||||
})
|
||||
.detach();
|
||||
@ -1028,7 +1028,7 @@ impl Project {
|
||||
fn on_buffer_event(
|
||||
&mut self,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
event: BufferEvent,
|
||||
event: &BufferEvent,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Option<()> {
|
||||
match event {
|
||||
|
Loading…
Reference in New Issue
Block a user