WIP - flush_fs_events

This commit is contained in:
Max Brunsfeld 2023-11-01 17:45:38 -07:00
parent 6ee93125d0
commit 401ddc6f49
3 changed files with 51 additions and 12 deletions

View file

@ -3,8 +3,9 @@ use crate::{
ForegroundExecutor, Model, ModelContext, Result, Task, TestDispatcher, TestPlatform,
WindowContext,
};
use futures::SinkExt;
use std::{cell::RefCell, future::Future, rc::Rc, sync::Arc};
use anyhow::anyhow;
use futures::{SinkExt, StreamExt};
use std::{cell::RefCell, future::Future, rc::Rc, sync::Arc, time::Duration};
#[derive(Clone)]
pub struct TestAppContext {
@ -158,4 +159,40 @@ impl TestAppContext {
.detach();
rx
}
pub async fn condition<T: EventEmitter + 'static>(
&mut self,
model: &Model<T>,
mut predicate: impl FnMut(&mut T, &mut ModelContext<T>) -> 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(());
}),
)
});
use futures::FutureExt as _;
use smol::future::FutureExt as _;
async {
while rx.next().await.is_some() {
if model.update(self, &mut predicate) {
return Ok(());
}
}
drop(subscriptions);
unreachable!()
}
.race(timer.map(|_| Err(anyhow!("condition timed out"))))
.await
.unwrap();
}
}