diff --git a/crates/gpui2/src/app/test_context.rs b/crates/gpui2/src/app/test_context.rs index d4a63c4e37c937a8451fd6361bd00f31278bfb63..d7bf2b7087a486a8d92698b6ddca3c84ffccf737 100644 --- a/crates/gpui2/src/app/test_context.rs +++ b/crates/gpui2/src/app/test_context.rs @@ -3,8 +3,8 @@ use crate::{ ForegroundExecutor, Model, ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext, }; -use anyhow::anyhow; -use futures::{SinkExt, StreamExt}; +use anyhow::{anyhow, bail}; +use futures::{SinkExt, Stream, StreamExt}; use std::{cell::RefCell, future::Future, rc::Rc, sync::Arc, time::Duration}; #[derive(Clone)] @@ -140,7 +140,25 @@ impl TestAppContext { } } - pub fn subscribe( + pub fn notifications(&mut self, entity: &Model) -> impl Stream { + let (tx, rx) = futures::channel::mpsc::unbounded(); + + entity.update(self, move |_, cx: &mut ModelContext| { + cx.observe(entity, { + let tx = tx.clone(); + move |_, _, _| { + let _ = tx.unbounded_send(()); + } + }) + .detach(); + + cx.on_release(move |_, _| tx.close_channel()).detach(); + }); + + rx + } + + pub fn events( &mut self, entity: &Model, ) -> futures::channel::mpsc::UnboundedReceiver @@ -160,36 +178,24 @@ impl TestAppContext { rx } - pub async fn condition( + pub async fn condition( &mut self, model: &Model, mut predicate: impl FnMut(&mut T, &mut ModelContext) -> bool, ) { - let (mut tx, mut rx) = futures::channel::mpsc::unbounded::<()>(); let timer = self.executor().timer(Duration::from_secs(3)); - - let subscriptions = model.update(self, move |_, cx| { - ( - cx.observe(model, move |_, _, _| { - // let _ = tx.send(()); - }), - cx.subscribe(model, move |_, _, _, _| { - let _ = tx.send(()); - }), - ) - }); + let mut notifications = self.notifications(model); use futures::FutureExt as _; use smol::future::FutureExt as _; async { - while rx.next().await.is_some() { + while notifications.next().await.is_some() { if model.update(self, &mut predicate) { return Ok(()); } } - drop(subscriptions); - unreachable!() + bail!("model dropped") } .race(timer.map(|_| Err(anyhow!("condition timed out")))) .await diff --git a/crates/project2/src/project_tests.rs b/crates/project2/src/project_tests.rs index ca6cdbccfc7b2bf58bd9fc5fc5cc295bb92c4706..5a2f82c3758d215a018d154ae4fe53b56eefd504 100644 --- a/crates/project2/src/project_tests.rs +++ b/crates/project2/src/project_tests.rs @@ -947,7 +947,7 @@ async fn test_disk_based_diagnostics_progress(cx: &mut gpui2::TestAppContext) { .await .unwrap(); - let mut events = cx.subscribe(&project); + let mut events = cx.events(&project); let fake_server = fake_servers.next().await.unwrap(); assert_eq!( @@ -1078,7 +1078,7 @@ async fn test_restarting_server_with_diagnostics_running(cx: &mut gpui2::TestApp project.update(cx, |project, cx| { project.restart_language_servers_for_buffers([buffer], cx); }); - let mut events = cx.subscribe(&project); + let mut events = cx.events(&project); // Simulate the newly started server sending more diagnostics. let fake_server = fake_servers.next().await.unwrap(); @@ -2788,6 +2788,7 @@ async fn test_rescan_and_remote_updates(cx: &mut gpui2::TestAppContext) { }); let remote = cx.update(|cx| Worktree::remote(1, 1, metadata, rpc.clone(), cx)); + cx.executor().run_until_parked(); cx.update(|cx| { diff --git a/crates/project2/src/worktree.rs b/crates/project2/src/worktree.rs index f146bf79489196d0144a16dbfd37441817b513fa..060fefe6b3aa70a86df98335d733157482ac4d1b 100644 --- a/crates/project2/src/worktree.rs +++ b/crates/project2/src/worktree.rs @@ -4065,6 +4065,7 @@ impl WorktreeModelHandle for Model { fs.create_file(&root_path.join(file_name), Default::default()) .await .unwrap(); + cx.condition(&tree, |tree, _| tree.entry_for_path(file_name).is_some()) .await; diff --git a/crates/ui2/src/components/panes.rs b/crates/ui2/src/components/panes.rs index 854786ebaa6cbd9476b9a6fcb3ce40574d91f16f..5318e3f3bb00504f50ac805ae2a55c95d57e7d6f 100644 --- a/crates/ui2/src/components/panes.rs +++ b/crates/ui2/src/components/panes.rs @@ -51,7 +51,7 @@ impl Pane { .id("drag-target") .drag_over::(|d| d.bg(red())) .on_drop(|_, files: View, cx| { - dbg!("dropped files!", files.read(cx)); + eprintln!("dropped files! {:?}", files.read(cx)); }) .absolute() .inset_0(), diff --git a/crates/ui2/src/components/tab.rs b/crates/ui2/src/components/tab.rs index d784ec017485aa58701215cc8033b70ffc0c47a2..c89ed2d7eb8fd34fce159f5726b1ea2d9159eee3 100644 --- a/crates/ui2/src/components/tab.rs +++ b/crates/ui2/src/components/tab.rs @@ -130,7 +130,7 @@ impl Tab { .on_drag(move |_view, cx| cx.build_view(|cx| drag_state.clone())) .drag_over::(|d| d.bg(black())) .on_drop(|_view, state: View, cx| { - dbg!(state.read(cx)); + eprintln!("{:?}", state.read(cx)); }) .px_2() .py_0p5()