Fix keystroke observer leak in vim crate (#17913)

Release Notes:

- Fixed a performance problem that happened when using vim mode after
opening and closing many editors

Co-authored-by: Antonio <antonio@zed.dev>
Co-authored-by: Nathan <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-09-16 15:50:12 -07:00 committed by GitHub
parent 67f149a4bc
commit 243629cce8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 79 additions and 43 deletions

View file

@ -204,7 +204,8 @@ impl App {
type Handler = Box<dyn FnMut(&mut AppContext) -> bool + 'static>;
type Listener = Box<dyn FnMut(&dyn Any, &mut AppContext) -> bool + 'static>;
type KeystrokeObserver = Box<dyn FnMut(&KeystrokeEvent, &mut WindowContext) + 'static>;
pub(crate) type KeystrokeObserver =
Box<dyn FnMut(&KeystrokeEvent, &mut WindowContext) -> bool + 'static>;
type QuitHandler = Box<dyn FnOnce(&mut AppContext) -> LocalBoxFuture<'static, ()> + 'static>;
type ReleaseListener = Box<dyn FnOnce(&mut dyn Any, &mut AppContext) + 'static>;
type NewViewListener = Box<dyn FnMut(AnyView, &mut WindowContext) + 'static>;
@ -1050,7 +1051,7 @@ impl AppContext {
/// and that this API will not be invoked if the event's propagation is stopped.
pub fn observe_keystrokes(
&mut self,
f: impl FnMut(&KeystrokeEvent, &mut WindowContext) + 'static,
mut f: impl FnMut(&KeystrokeEvent, &mut WindowContext) + 'static,
) -> Subscription {
fn inner(
keystroke_observers: &mut SubscriberSet<(), KeystrokeObserver>,
@ -1060,7 +1061,14 @@ impl AppContext {
activate();
subscription
}
inner(&mut self.keystroke_observers, Box::new(f))
inner(
&mut self.keystroke_observers,
Box::new(move |event, cx| {
f(event, cx);
true
}),
)
}
/// Register key bindings.