diff options
author | Aram Drevekenin <aram@poor.dev> | 2023-03-27 19:19:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-27 19:19:34 +0200 |
commit | 341f9eb8c8771a59b2e4d238ba49ba88c3720d6d (patch) | |
tree | 51205536dd0789efb770dbe0095af7210a60eed3 /zellij-server/src/background_jobs.rs | |
parent | 7b609b053f3aaf466258e12be53d57614c8884c7 (diff) |
feat(plugins): async plugin loading (#2327)
* work
* refactor(plugins): break down start plugin async function
* work
* loading messages
* nice ui
* floating panes and error handling
* cleanups and conflicting plugin/direction
* find exact pane when relayouting
* fix plugin pane titles
* kill loading tasks on exit
* refactor: move stuff around
* style(fmt): rustfmt
* various fixes and refactors
Diffstat (limited to 'zellij-server/src/background_jobs.rs')
-rw-r--r-- | zellij-server/src/background_jobs.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/zellij-server/src/background_jobs.rs b/zellij-server/src/background_jobs.rs index c612c9d57..8061b4a47 100644 --- a/zellij-server/src/background_jobs.rs +++ b/zellij-server/src/background_jobs.rs @@ -2,6 +2,10 @@ use zellij_utils::async_std::task; use zellij_utils::errors::{prelude::*, BackgroundJobContext, ContextType}; use std::collections::HashMap; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; use std::time::{Duration, Instant}; use crate::panes::PaneId; @@ -11,6 +15,8 @@ use crate::thread_bus::Bus; #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub enum BackgroundJob { DisplayPaneError(Vec<PaneId>, String), + AnimatePluginLoading(u32), // u32 - plugin_id + StopPluginLoadingAnimation(u32), // u32 - plugin_id Exit, } @@ -18,16 +24,22 @@ impl From<&BackgroundJob> for BackgroundJobContext { fn from(background_job: &BackgroundJob) -> Self { match *background_job { BackgroundJob::DisplayPaneError(..) => BackgroundJobContext::DisplayPaneError, + BackgroundJob::AnimatePluginLoading(..) => BackgroundJobContext::AnimatePluginLoading, + BackgroundJob::StopPluginLoadingAnimation(..) => { + BackgroundJobContext::StopPluginLoadingAnimation + }, BackgroundJob::Exit => BackgroundJobContext::Exit, } } } static FLASH_DURATION_MS: u64 = 1000; +static PLUGIN_ANIMATION_OFFSET_DURATION_MD: u64 = 500; pub(crate) fn background_jobs_main(bus: Bus<BackgroundJob>) -> Result<()> { let err_context = || "failed to write to pty".to_string(); let mut running_jobs: HashMap<BackgroundJob, Instant> = HashMap::new(); + let mut loading_plugins: HashMap<u32, Arc<AtomicBool>> = HashMap::new(); // u32 - plugin_id loop { let (event, mut err_ctx) = bus.recv().with_context(err_context)?; @@ -54,7 +66,37 @@ pub(crate) fn background_jobs_main(bus: Bus<BackgroundJob>) -> Result<()> { } }); }, + BackgroundJob::AnimatePluginLoading(pid) => { + let loading_plugin = Arc::new(AtomicBool::new(true)); + if job_already_running(job, &mut running_jobs) { + continue; + } + task::spawn({ + let senders = bus.senders.clone(); + let loading_plugin = loading_plugin.clone(); + async move { + while loading_plugin.load(Ordering::SeqCst) { + let _ = senders.send_to_screen( + ScreenInstruction::ProgressPluginLoadingOffset(pid), + ); + task::sleep(std::time::Duration::from_millis( + PLUGIN_ANIMATION_OFFSET_DURATION_MD, + )) + .await; + } + } + }); + loading_plugins.insert(pid, loading_plugin); + }, + BackgroundJob::StopPluginLoadingAnimation(pid) => { + if let Some(loading_plugin) = loading_plugins.remove(&pid) { + loading_plugin.store(false, Ordering::SeqCst); + } + }, BackgroundJob::Exit => { + for loading_plugin in loading_plugins.values() { + loading_plugin.store(false, Ordering::SeqCst); + } return Ok(()); }, } |