Require a context when constructing a worktree

This commit is contained in:
Nathan Sobo 2021-04-12 18:42:19 -06:00
parent 24cdfd2471
commit 41f50cdb61
2 changed files with 16 additions and 18 deletions

View file

@ -117,7 +117,7 @@ impl Workspace {
} }
} }
let worktree = ctx.add_model(|ctx| Worktree::new(ctx.model_id(), path, Some(ctx))); let worktree = ctx.add_model(|ctx| Worktree::new(ctx.model_id(), path, ctx));
ctx.observe(&worktree, Self::on_worktree_updated); ctx.observe(&worktree, Self::on_worktree_updated);
self.worktrees.insert(worktree); self.worktrees.insert(worktree);
ctx.notify(); ctx.notify();

View file

@ -49,7 +49,7 @@ struct DirToScan {
} }
impl Worktree { impl Worktree {
pub fn new<T>(id: usize, path: T, ctx: Option<&mut ModelContext<Self>>) -> Self pub fn new<T>(id: usize, path: T, ctx: &mut ModelContext<Self>) -> Self
where where
T: Into<PathBuf>, T: Into<PathBuf>,
{ {
@ -60,27 +60,25 @@ impl Worktree {
entries: HashMap::new(), entries: HashMap::new(),
file_paths: Vec::new(), file_paths: Vec::new(),
histories: HashMap::new(), histories: HashMap::new(),
scanning: ctx.is_some(), scanning: true,
}))); })));
if let Some(ctx) = ctx { let done_scanning = {
tree.0.write().scanning = true;
let tree = tree.clone(); let tree = tree.clone();
let task = ctx.background_executor().spawn(async move { ctx.background_executor().spawn(async move {
tree.scan_dirs()?; tree.scan_dirs()?;
Ok(()) Ok(())
}); })
};
ctx.spawn(task, Self::done_scanning).detach(); ctx.spawn(done_scanning, Self::done_scanning).detach();
ctx.spawn_stream( ctx.spawn_stream(
timer::repeat(Duration::from_millis(100)).map(|_| ()), timer::repeat(Duration::from_millis(100)).map(|_| ()),
Self::scanning, Self::scanning,
|_, _| {}, |_, _| {},
) )
.detach(); .detach();
}
tree tree
} }
@ -690,7 +688,7 @@ mod test {
let root_link_path = dir.path().join("root_link"); let root_link_path = dir.path().join("root_link");
unix::fs::symlink(&dir.path().join("root"), &root_link_path).unwrap(); unix::fs::symlink(&dir.path().join("root"), &root_link_path).unwrap();
let tree = app.add_model(|ctx| Worktree::new(1, root_link_path, Some(ctx))); let tree = app.add_model(|ctx| Worktree::new(1, root_link_path, ctx));
app.finish_pending_tasks().await; app.finish_pending_tasks().await;
app.read(|ctx| { app.read(|ctx| {
@ -719,7 +717,7 @@ mod test {
"file1": "the old contents", "file1": "the old contents",
})); }));
let tree = app.add_model(|ctx| Worktree::new(1, dir.path(), Some(ctx))); let tree = app.add_model(|ctx| Worktree::new(1, dir.path(), ctx));
app.finish_pending_tasks().await; app.finish_pending_tasks().await;
let buffer = Buffer::new(1, "a line of text.\n".repeat(10 * 1024)); let buffer = Buffer::new(1, "a line of text.\n".repeat(10 * 1024));