2021-06-28 16:48:38 +04:00
|
|
|
use helix_view::Editor;
|
|
|
|
|
|
|
|
use crate::compositor::Compositor;
|
|
|
|
|
|
|
|
use futures_util::future::{self, BoxFuture, Future, FutureExt};
|
2021-07-01 21:41:20 +04:00
|
|
|
use futures_util::stream::{FuturesUnordered, StreamExt};
|
2021-06-28 16:48:38 +04:00
|
|
|
|
|
|
|
pub type Callback = Box<dyn FnOnce(&mut Editor, &mut Compositor) + Send>;
|
|
|
|
pub type JobFuture = BoxFuture<'static, anyhow::Result<Option<Callback>>>;
|
|
|
|
|
|
|
|
pub struct Job {
|
|
|
|
pub future: BoxFuture<'static, anyhow::Result<Option<Callback>>>,
|
|
|
|
/// Do we need to wait for this job to finish before exiting?
|
|
|
|
pub wait: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Jobs {
|
2021-07-03 07:30:13 +04:00
|
|
|
pub futures: FuturesUnordered<JobFuture>,
|
2021-06-28 16:48:38 +04:00
|
|
|
/// These are the ones that need to complete before we exit.
|
2021-07-03 07:30:13 +04:00
|
|
|
pub wait_futures: FuturesUnordered<JobFuture>,
|
2021-06-28 16:48:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Job {
|
2022-01-14 07:25:59 +04:00
|
|
|
pub fn new<F: Future<Output = anyhow::Result<()>> + Send + 'static>(f: F) -> Self {
|
|
|
|
Self {
|
2021-06-28 16:48:38 +04:00
|
|
|
future: f.map(|r| r.map(|()| None)).boxed(),
|
|
|
|
wait: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_callback<F: Future<Output = anyhow::Result<Callback>> + Send + 'static>(
|
|
|
|
f: F,
|
2022-01-14 07:25:59 +04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2021-06-28 17:00:44 +04:00
|
|
|
future: f.map(|r| r.map(Some)).boxed(),
|
2021-06-28 16:48:38 +04:00
|
|
|
wait: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 07:25:59 +04:00
|
|
|
pub fn wait_before_exiting(mut self) -> Self {
|
2021-06-28 16:48:38 +04:00
|
|
|
self.wait = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Jobs {
|
2022-01-14 07:25:59 +04:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
2021-06-28 16:48:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spawn<F: Future<Output = anyhow::Result<()>> + Send + 'static>(&mut self, f: F) {
|
|
|
|
self.add(Job::new(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn callback<F: Future<Output = anyhow::Result<Callback>> + Send + 'static>(
|
|
|
|
&mut self,
|
|
|
|
f: F,
|
|
|
|
) {
|
|
|
|
self.add(Job::with_callback(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_callback(
|
2021-09-02 05:49:23 +04:00
|
|
|
&self,
|
2021-06-28 16:48:38 +04:00
|
|
|
editor: &mut Editor,
|
|
|
|
compositor: &mut Compositor,
|
|
|
|
call: anyhow::Result<Option<Callback>>,
|
|
|
|
) {
|
|
|
|
match call {
|
|
|
|
Ok(None) => {}
|
|
|
|
Ok(Some(call)) => {
|
|
|
|
call(editor, compositor);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
editor.set_error(format!("Async job failed: {}", e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 07:30:13 +04:00
|
|
|
pub async fn next_job(&mut self) -> Option<anyhow::Result<Option<Callback>>> {
|
|
|
|
tokio::select! {
|
|
|
|
event = self.futures.next() => { event }
|
|
|
|
event = self.wait_futures.next() => { event }
|
|
|
|
}
|
2021-06-28 16:48:38 +04:00
|
|
|
}
|
|
|
|
|
2021-09-02 05:49:23 +04:00
|
|
|
pub fn add(&self, j: Job) {
|
2021-06-28 16:48:38 +04:00
|
|
|
if j.wait {
|
|
|
|
self.wait_futures.push(j.future);
|
|
|
|
} else {
|
|
|
|
self.futures.push(j.future);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Blocks until all the jobs that need to be waited on are done.
|
2022-02-05 10:05:19 +04:00
|
|
|
pub async fn finish(&mut self) {
|
2021-06-28 16:48:38 +04:00
|
|
|
let wait_futures = std::mem::take(&mut self.wait_futures);
|
2022-04-19 09:21:31 +04:00
|
|
|
log::debug!("waiting on jobs...");
|
2022-02-05 10:05:19 +04:00
|
|
|
wait_futures.for_each(|_| future::ready(())).await
|
2021-06-28 16:48:38 +04:00
|
|
|
}
|
|
|
|
}
|