Allow subscription/notification to be cancelled by dropping the returned Subscription

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld
2021-08-23 15:54:24 -07:00
co-authored by Nathan Sobo
parent f4847bd38f
commit 6df80d94ad
8 changed files with 228 additions and 110 deletions
+4 -3
View File
@@ -324,9 +324,10 @@ impl Editor {
) -> Self {
let display_map =
cx.add_model(|cx| DisplayMap::new(buffer.clone(), settings.borrow().clone(), None, cx));
cx.observe(&buffer, Self::on_buffer_changed);
cx.subscribe(&buffer, Self::on_buffer_event);
cx.observe(&display_map, Self::on_display_map_changed);
cx.observe(&buffer, Self::on_buffer_changed).detach();
cx.subscribe(&buffer, Self::on_buffer_event).detach();
cx.observe(&display_map, Self::on_display_map_changed)
.detach();
let mut next_selection_id = 0;
let selection_set_id = buffer.update(cx, |buffer, cx| {
+10 -5
View File
@@ -2943,11 +2943,13 @@ mod tests {
let buffer_1_events = buffer_1_events.clone();
cx.subscribe(&buffer1, move |_, _, event, _| {
buffer_1_events.borrow_mut().push(event.clone())
});
})
.detach();
let buffer_2_events = buffer_2_events.clone();
cx.subscribe(&buffer2, move |_, _, event, _| {
buffer_2_events.borrow_mut().push(event.clone())
});
})
.detach();
// An edit emits an edited event, followed by a dirtied event,
// since the buffer was previously in a clean state.
@@ -3382,7 +3384,8 @@ mod tests {
cx.subscribe(&buffer1, {
let events = events.clone();
move |_, _, event, _| events.borrow_mut().push(event.clone())
});
})
.detach();
assert!(!buffer.is_dirty());
assert!(events.borrow().is_empty());
@@ -3438,7 +3441,8 @@ mod tests {
cx.subscribe(&buffer2, {
let events = events.clone();
move |_, _, event, _| events.borrow_mut().push(event.clone())
});
})
.detach();
});
fs::remove_file(dir.path().join("file2")).unwrap();
@@ -3458,7 +3462,8 @@ mod tests {
cx.subscribe(&buffer3, {
let events = events.clone();
move |_, _, event, _| events.borrow_mut().push(event.clone())
});
})
.detach();
});
tree.flush_fs_events(&cx).await;
+1 -1
View File
@@ -32,7 +32,7 @@ impl DisplayMap {
let (fold_map, snapshot) = FoldMap::new(buffer.clone(), cx);
let (tab_map, snapshot) = TabMap::new(snapshot, settings.tab_size);
let wrap_map = cx.add_model(|cx| WrapMap::new(snapshot, settings, wrap_width, cx));
cx.observe(&wrap_map, |_, _, cx| cx.notify());
cx.observe(&wrap_map, |_, _, cx| cx.notify()).detach();
DisplayMap {
buffer,
fold_map,
+4 -3
View File
@@ -252,7 +252,7 @@ impl FileFinder {
workspace.toggle_modal(cx, |cx, workspace| {
let handle = cx.handle();
let finder = cx.add_view(|cx| Self::new(workspace.settings.clone(), handle, cx));
cx.subscribe(&finder, Self::on_event);
cx.subscribe(&finder, Self::on_event).detach();
finder
});
}
@@ -281,10 +281,11 @@ impl FileFinder {
workspace: ViewHandle<Workspace>,
cx: &mut ViewContext<Self>,
) -> Self {
cx.observe(&workspace, Self::workspace_updated);
cx.observe(&workspace, Self::workspace_updated).detach();
let query_buffer = cx.add_view(|cx| Editor::single_line(settings.clone(), cx));
cx.subscribe(&query_buffer, Self::on_query_editor_event);
cx.subscribe(&query_buffer, Self::on_query_editor_event)
.detach();
Self {
handle: cx.handle().downgrade(),
+2 -1
View File
@@ -190,7 +190,8 @@ impl<T: Entity> Observer<T> {
let observer = cx.add_model(|cx| {
cx.observe(handle, move |_, _, _| {
let _ = notify_tx.try_send(());
});
})
.detach();
Observer(PhantomData)
});
(observer, notify_rx)
+3 -2
View File
@@ -61,7 +61,8 @@ impl ThemeSelector {
cx: &mut ViewContext<Self>,
) -> Self {
let query_buffer = cx.add_view(|cx| Editor::single_line(settings.clone(), cx));
cx.subscribe(&query_buffer, Self::on_query_editor_event);
cx.subscribe(&query_buffer, Self::on_query_editor_event)
.detach();
let mut this = Self {
settings,
@@ -86,7 +87,7 @@ impl ThemeSelector {
cx,
)
});
cx.subscribe(&selector, Self::on_event);
cx.subscribe(&selector, Self::on_event).detach();
selector
});
}
+8 -5
View File
@@ -290,7 +290,8 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
cx.notify()
}
})
})
.detach();
});
}
fn save(&self, cx: &mut MutableAppContext) -> Result<Task<Result<()>>> {
@@ -360,7 +361,8 @@ impl Workspace {
let pane_id = pane.id();
cx.subscribe(&pane, move |me, _, event, cx| {
me.handle_pane_event(pane_id, event, cx)
});
})
.detach();
cx.focus(&pane);
let mut left_sidebar = Sidebar::new(Side::Left);
@@ -526,7 +528,7 @@ impl Workspace {
cx.spawn(|this, mut cx| async move {
let worktree = Worktree::open_local(path, languages, fs, &mut cx).await?;
this.update(&mut cx, |this, cx| {
cx.observe(&worktree, |_, _, cx| cx.notify());
cx.observe(&worktree, |_, _, cx| cx.notify()).detach();
this.worktrees.insert(worktree.clone());
cx.notify();
});
@@ -835,7 +837,7 @@ impl Workspace {
Worktree::open_remote(rpc.clone(), worktree_id, access_token, languages, &mut cx)
.await?;
this.update(&mut cx, |workspace, cx| {
cx.observe(&worktree, |_, _, cx| cx.notify());
cx.observe(&worktree, |_, _, cx| cx.notify()).detach();
workspace.worktrees.insert(worktree);
cx.notify();
});
@@ -856,7 +858,8 @@ impl Workspace {
let pane_id = pane.id();
cx.subscribe(&pane, move |me, _, event, cx| {
me.handle_pane_event(pane_id, event, cx)
});
})
.detach();
self.panes.push(pane.clone());
self.activate_pane(pane.clone(), cx);
pane