test.rs

 1use gpui::{Entity, ModelHandle};
 2use smol::channel;
 3use std::marker::PhantomData;
 4
 5#[cfg(test)]
 6#[ctor::ctor]
 7fn init_logger() {
 8    // std::env::set_var("RUST_LOG", "info");
 9    env_logger::init();
10}
11
12pub struct Observer<T>(PhantomData<T>);
13
14impl<T: 'static> Entity for Observer<T> {
15    type Event = ();
16}
17
18impl<T: Entity> Observer<T> {
19    pub fn new(
20        handle: &ModelHandle<T>,
21        cx: &mut gpui::TestAppContext,
22    ) -> (ModelHandle<Self>, channel::Receiver<()>) {
23        let (notify_tx, notify_rx) = channel::unbounded();
24        let observer = cx.add_model(|cx| {
25            cx.observe(handle, move |_, _, _| {
26                let _ = notify_tx.try_send(());
27            })
28            .detach();
29            Observer(PhantomData)
30        });
31        (observer, notify_rx)
32    }
33}