fix(plugins): only listen to hd if a plugin is subscribed to hd events (#2529)
* fix(plugins): only listen to hd if a plugin is subscribed to hd events * style(fmt): rustfmt * fix(tests): give time for fs watcher to do its thing * fix(tests): increase timeout
This commit is contained in:
parent
9e69bea434
commit
0b831cfee5
6 changed files with 49 additions and 20 deletions
|
|
@ -5,7 +5,11 @@ mod wasm_bridge;
|
||||||
mod watch_filesystem;
|
mod watch_filesystem;
|
||||||
mod zellij_exports;
|
mod zellij_exports;
|
||||||
use log::info;
|
use log::info;
|
||||||
use std::{collections::HashMap, fs, path::PathBuf};
|
use std::{
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
fs,
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
use wasmer::Store;
|
use wasmer::Store;
|
||||||
|
|
||||||
use crate::screen::ScreenInstruction;
|
use crate::screen::ScreenInstruction;
|
||||||
|
|
@ -14,7 +18,7 @@ use crate::{pty::PtyInstruction, thread_bus::Bus, ClientId, ServerInstruction};
|
||||||
use wasm_bridge::WasmBridge;
|
use wasm_bridge::WasmBridge;
|
||||||
|
|
||||||
use zellij_utils::{
|
use zellij_utils::{
|
||||||
data::{Event, PluginCapabilities},
|
data::{Event, EventType, PluginCapabilities},
|
||||||
errors::{prelude::*, ContextType, PluginContext},
|
errors::{prelude::*, ContextType, PluginContext},
|
||||||
input::{
|
input::{
|
||||||
command::TerminalAction,
|
command::TerminalAction,
|
||||||
|
|
@ -74,6 +78,7 @@ pub enum PluginInstruction {
|
||||||
String, // serialized message
|
String, // serialized message
|
||||||
String, // serialized payload
|
String, // serialized payload
|
||||||
),
|
),
|
||||||
|
PluginSubscribedToEvents(PluginId, ClientId, HashSet<EventType>),
|
||||||
Exit,
|
Exit,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,6 +102,9 @@ impl From<&PluginInstruction> for PluginContext {
|
||||||
PluginContext::PostMessageToPluginWorker
|
PluginContext::PostMessageToPluginWorker
|
||||||
},
|
},
|
||||||
PluginInstruction::PostMessageToPlugin(..) => PluginContext::PostMessageToPlugin,
|
PluginInstruction::PostMessageToPlugin(..) => PluginContext::PostMessageToPlugin,
|
||||||
|
PluginInstruction::PluginSubscribedToEvents(..) => {
|
||||||
|
PluginContext::PluginSubscribedToEvents
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -267,6 +275,17 @@ pub(crate) fn plugin_thread_main(
|
||||||
)];
|
)];
|
||||||
wasm_bridge.update_plugins(updates)?;
|
wasm_bridge.update_plugins(updates)?;
|
||||||
},
|
},
|
||||||
|
PluginInstruction::PluginSubscribedToEvents(_plugin_id, _client_id, events) => {
|
||||||
|
for event in events {
|
||||||
|
if let EventType::FileSystemCreate
|
||||||
|
| EventType::FileSystemRead
|
||||||
|
| EventType::FileSystemUpdate
|
||||||
|
| EventType::FileSystemDelete = event
|
||||||
|
{
|
||||||
|
wasm_bridge.start_fs_watcher_if_not_started();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
PluginInstruction::Exit => {
|
PluginInstruction::Exit => {
|
||||||
wasm_bridge.cleanup();
|
wasm_bridge.cleanup();
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -553,7 +553,7 @@ pub fn can_subscribe_to_hd_events() {
|
||||||
received_screen_instructions,
|
received_screen_instructions,
|
||||||
ScreenInstruction::PluginBytes,
|
ScreenInstruction::PluginBytes,
|
||||||
screen_receiver,
|
screen_receiver,
|
||||||
3
|
2
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = plugin_thread_sender.send(PluginInstruction::AddClient(client_id));
|
let _ = plugin_thread_sender.send(PluginInstruction::AddClient(client_id));
|
||||||
|
|
@ -565,12 +565,8 @@ pub fn can_subscribe_to_hd_events() {
|
||||||
client_id,
|
client_id,
|
||||||
size,
|
size,
|
||||||
));
|
));
|
||||||
let _ = plugin_thread_sender.send(PluginInstruction::Update(vec![(
|
// extra long time because we only start the fs watcher on plugin load
|
||||||
None,
|
std::thread::sleep(std::time::Duration::from_millis(5000));
|
||||||
Some(client_id),
|
|
||||||
Event::InputReceived,
|
|
||||||
)])); // will be cached and sent to the plugin once it's loaded
|
|
||||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
|
||||||
std::fs::OpenOptions::new()
|
std::fs::OpenOptions::new()
|
||||||
.create(true)
|
.create(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
|
|
|
||||||
|
|
@ -75,13 +75,7 @@ impl WasmBridge {
|
||||||
let connected_clients: Arc<Mutex<Vec<ClientId>>> = Arc::new(Mutex::new(vec![]));
|
let connected_clients: Arc<Mutex<Vec<ClientId>>> = Arc::new(Mutex::new(vec![]));
|
||||||
let plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>> =
|
let plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>> =
|
||||||
Arc::new(Mutex::new(HashMap::new()));
|
Arc::new(Mutex::new(HashMap::new()));
|
||||||
let watcher = match watch_filesystem(senders.clone(), &zellij_cwd) {
|
let watcher = None;
|
||||||
Ok(watcher) => Some(watcher),
|
|
||||||
Err(e) => {
|
|
||||||
log::error!("Failed to watch filesystem: {:?}", e);
|
|
||||||
None
|
|
||||||
},
|
|
||||||
};
|
|
||||||
WasmBridge {
|
WasmBridge {
|
||||||
connected_clients,
|
connected_clients,
|
||||||
plugins,
|
plugins,
|
||||||
|
|
@ -697,6 +691,17 @@ impl WasmBridge {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
pub fn start_fs_watcher_if_not_started(&mut self) {
|
||||||
|
if self.watcher.is_none() {
|
||||||
|
self.watcher = match watch_filesystem(self.senders.clone(), &self.zellij_cwd) {
|
||||||
|
Ok(watcher) => Some(watcher),
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Failed to watch filesystem: {:?}", e);
|
||||||
|
None
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_plugin_successful_loading(senders: &ThreadSenders, plugin_id: PluginId) {
|
fn handle_plugin_successful_loading(senders: &ThreadSenders, plugin_id: PluginId) {
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ use zellij_utils::{
|
||||||
input::{
|
input::{
|
||||||
actions::Action,
|
actions::Action,
|
||||||
command::{RunCommand, RunCommandAction, TerminalAction},
|
command::{RunCommand, RunCommandAction, TerminalAction},
|
||||||
layout::{Layout, RunPlugin, RunPluginLocation},
|
layout::Layout,
|
||||||
plugins::PluginType,
|
plugins::PluginType,
|
||||||
},
|
},
|
||||||
serde,
|
serde,
|
||||||
|
|
@ -146,8 +146,17 @@ impl ForeignFunctionEnv {
|
||||||
fn host_subscribe(env: &ForeignFunctionEnv) {
|
fn host_subscribe(env: &ForeignFunctionEnv) {
|
||||||
wasi_read_object::<HashSet<EventType>>(&env.plugin_env.wasi_env)
|
wasi_read_object::<HashSet<EventType>>(&env.plugin_env.wasi_env)
|
||||||
.and_then(|new| {
|
.and_then(|new| {
|
||||||
env.subscriptions.lock().to_anyhow()?.extend(new);
|
env.subscriptions.lock().to_anyhow()?.extend(new.clone());
|
||||||
Ok(())
|
Ok(new)
|
||||||
|
})
|
||||||
|
.and_then(|new| {
|
||||||
|
env.plugin_env
|
||||||
|
.senders
|
||||||
|
.send_to_plugin(PluginInstruction::PluginSubscribedToEvents(
|
||||||
|
env.plugin_env.plugin_id,
|
||||||
|
env.plugin_env.client_id,
|
||||||
|
new,
|
||||||
|
))
|
||||||
})
|
})
|
||||||
.with_context(|| format!("failed to subscribe for plugin {}", env.plugin_env.name()))
|
.with_context(|| format!("failed to subscribe for plugin {}", env.plugin_env.name()))
|
||||||
.fatal();
|
.fatal();
|
||||||
|
|
|
||||||
|
|
@ -929,7 +929,6 @@ impl Screen {
|
||||||
self.character_cell_size.clone(),
|
self.character_cell_size.clone(),
|
||||||
);
|
);
|
||||||
let mut tabs_to_close = vec![];
|
let mut tabs_to_close = vec![];
|
||||||
let size = self.size;
|
|
||||||
for (tab_index, tab) in &mut self.tabs {
|
for (tab_index, tab) in &mut self.tabs {
|
||||||
if tab.has_selectable_tiled_panes() {
|
if tab.has_selectable_tiled_panes() {
|
||||||
tab.render(&mut output).context(err_context)?;
|
tab.render(&mut output).context(err_context)?;
|
||||||
|
|
|
||||||
|
|
@ -370,6 +370,7 @@ pub enum PluginContext {
|
||||||
ApplyCachedWorkerMessages,
|
ApplyCachedWorkerMessages,
|
||||||
PostMessageToPluginWorker,
|
PostMessageToPluginWorker,
|
||||||
PostMessageToPlugin,
|
PostMessageToPlugin,
|
||||||
|
PluginSubscribedToEvents,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stack call representations corresponding to the different types of [`ClientInstruction`]s.
|
/// Stack call representations corresponding to the different types of [`ClientInstruction`]s.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue