2023-12-01 00:03:26 +01:00
|
|
|
use helix_event::status::StatusMessage;
|
|
|
|
use helix_event::{runtime_local, send_blocking};
|
2021-06-28 14:48:38 +02:00
|
|
|
use helix_view::Editor;
|
2023-12-01 00:03:26 +01:00
|
|
|
use once_cell::sync::OnceCell;
|
2021-06-28 14:48:38 +02:00
|
|
|
|
|
|
|
use crate::compositor::Compositor;
|
|
|
|
|
2022-03-31 16:58:50 +02:00
|
|
|
use futures_util::future::{BoxFuture, Future, FutureExt};
|
2021-07-01 19:41:20 +02:00
|
|
|
use futures_util::stream::{FuturesUnordered, StreamExt};
|
2023-12-01 00:03:26 +01:00
|
|
|
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
2021-06-28 14:48:38 +02:00
|
|
|
|
2023-02-09 03:24:31 +01:00
|
|
|
pub type EditorCompositorCallback = Box<dyn FnOnce(&mut Editor, &mut Compositor) + Send>;
|
|
|
|
pub type EditorCallback = Box<dyn FnOnce(&mut Editor) + Send>;
|
|
|
|
|
2023-12-01 00:03:26 +01:00
|
|
|
runtime_local! {
|
|
|
|
static JOB_QUEUE: OnceCell<Sender<Callback>> = OnceCell::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn dispatch_callback(job: Callback) {
|
|
|
|
let _ = JOB_QUEUE.wait().send(job).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn dispatch(job: impl FnOnce(&mut Editor, &mut Compositor) + Send + 'static) {
|
|
|
|
let _ = JOB_QUEUE
|
|
|
|
.wait()
|
|
|
|
.send(Callback::EditorCompositor(Box::new(job)))
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dispatch_blocking(job: impl FnOnce(&mut Editor, &mut Compositor) + Send + 'static) {
|
|
|
|
let jobs = JOB_QUEUE.wait();
|
|
|
|
send_blocking(jobs, Callback::EditorCompositor(Box::new(job)))
|
|
|
|
}
|
|
|
|
|
2022-07-12 05:38:26 +02:00
|
|
|
pub enum Callback {
|
2023-02-09 03:24:31 +01:00
|
|
|
EditorCompositor(EditorCompositorCallback),
|
|
|
|
Editor(EditorCallback),
|
2022-07-12 05:38:26 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 14:48:38 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Jobs {
|
2023-12-01 00:03:26 +01:00
|
|
|
/// jobs that need to complete before we exit.
|
2021-07-03 05:30:13 +02:00
|
|
|
pub wait_futures: FuturesUnordered<JobFuture>,
|
2023-12-01 00:03:26 +01:00
|
|
|
pub callbacks: Receiver<Callback>,
|
|
|
|
pub status_messages: Receiver<StatusMessage>,
|
2021-06-28 14:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Job {
|
2022-01-14 04:25:59 +01:00
|
|
|
pub fn new<F: Future<Output = anyhow::Result<()>> + Send + 'static>(f: F) -> Self {
|
|
|
|
Self {
|
2021-06-28 14:48:38 +02: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 04:25:59 +01:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2021-06-28 15:00:44 +02:00
|
|
|
future: f.map(|r| r.map(Some)).boxed(),
|
2021-06-28 14:48:38 +02:00
|
|
|
wait: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:25:59 +01:00
|
|
|
pub fn wait_before_exiting(mut self) -> Self {
|
2021-06-28 14:48:38 +02:00
|
|
|
self.wait = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Jobs {
|
2023-12-01 00:03:26 +01:00
|
|
|
#[allow(clippy::new_without_default)]
|
2022-01-14 04:25:59 +01:00
|
|
|
pub fn new() -> Self {
|
2023-12-01 00:03:26 +01:00
|
|
|
let (tx, rx) = channel(1024);
|
|
|
|
let _ = JOB_QUEUE.set(tx);
|
|
|
|
let status_messages = helix_event::status::setup();
|
|
|
|
Self {
|
|
|
|
wait_futures: FuturesUnordered::new(),
|
|
|
|
callbacks: rx,
|
|
|
|
status_messages,
|
|
|
|
}
|
2021-06-28 14:48:38 +02: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 03:49:23 +02:00
|
|
|
&self,
|
2021-06-28 14:48:38 +02:00
|
|
|
editor: &mut Editor,
|
|
|
|
compositor: &mut Compositor,
|
|
|
|
call: anyhow::Result<Option<Callback>>,
|
|
|
|
) {
|
|
|
|
match call {
|
|
|
|
Ok(None) => {}
|
2022-07-12 05:38:26 +02:00
|
|
|
Ok(Some(call)) => match call {
|
|
|
|
Callback::EditorCompositor(call) => call(editor, compositor),
|
|
|
|
Callback::Editor(call) => call(editor),
|
|
|
|
},
|
2021-06-28 14:48:38 +02:00
|
|
|
Err(e) => {
|
|
|
|
editor.set_error(format!("Async job failed: {}", e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 03:49:23 +02:00
|
|
|
pub fn add(&self, j: Job) {
|
2021-06-28 14:48:38 +02:00
|
|
|
if j.wait {
|
|
|
|
self.wait_futures.push(j.future);
|
|
|
|
} else {
|
2023-12-01 00:03:26 +01:00
|
|
|
tokio::spawn(async move {
|
|
|
|
match j.future.await {
|
|
|
|
Ok(Some(cb)) => dispatch_callback(cb).await,
|
|
|
|
Ok(None) => (),
|
|
|
|
Err(err) => helix_event::status::report(err).await,
|
|
|
|
}
|
|
|
|
});
|
2021-06-28 14:48:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Blocks until all the jobs that need to be waited on are done.
|
2022-07-12 05:38:26 +02:00
|
|
|
pub async fn finish(
|
|
|
|
&mut self,
|
2022-09-03 05:38:38 +02:00
|
|
|
editor: &mut Editor,
|
2022-07-12 05:38:26 +02:00
|
|
|
mut compositor: Option<&mut Compositor>,
|
|
|
|
) -> anyhow::Result<()> {
|
2022-04-19 07:21:31 +02:00
|
|
|
log::debug!("waiting on jobs...");
|
2022-03-31 16:58:50 +02:00
|
|
|
let mut wait_futures = std::mem::take(&mut self.wait_futures);
|
2022-08-31 05:08:15 +02:00
|
|
|
|
2022-03-31 16:58:50 +02:00
|
|
|
while let (Some(job), tail) = wait_futures.into_future().await {
|
|
|
|
match job {
|
2022-07-12 05:38:26 +02:00
|
|
|
Ok(callback) => {
|
2022-03-31 16:58:50 +02:00
|
|
|
wait_futures = tail;
|
2022-07-12 05:38:26 +02:00
|
|
|
|
|
|
|
if let Some(callback) = callback {
|
|
|
|
// clippy doesn't realize this is an error without the derefs
|
|
|
|
#[allow(clippy::needless_option_as_deref)]
|
|
|
|
match callback {
|
2022-09-03 05:38:38 +02:00
|
|
|
Callback::EditorCompositor(call) if compositor.is_some() => {
|
|
|
|
call(editor, compositor.as_deref_mut().unwrap())
|
2022-07-12 05:38:26 +02:00
|
|
|
}
|
2022-09-03 05:38:38 +02:00
|
|
|
Callback::Editor(call) => call(editor),
|
2022-07-12 05:38:26 +02:00
|
|
|
|
|
|
|
// skip callbacks for which we don't have the necessary references
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 16:58:50 +02:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
self.wait_futures = tail;
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2021-06-28 14:48:38 +02:00
|
|
|
}
|
|
|
|
}
|