From e0ae5c97d4dbbc33ef2e0deeee5bb0902015a353 Mon Sep 17 00:00:00 2001 From: Byson94 Date: Sat, 25 Oct 2025 15:47:04 +0530 Subject: [PATCH] feat: add lifetime flag to update command --- CHANGELOG.md | 1 + crates/ewwii/src/app.rs | 18 +++++++++++++++--- crates/ewwii/src/opts.rs | 9 +++++++-- crates/ewwii/src/server.rs | 1 + 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8d84bc..7487c97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). - An advanced widget named `flowbox`. - `focusable` property to all widget. - `widget_name` property to all widget. +- `lifetime` flag for update command. ### Fixed diff --git a/crates/ewwii/src/app.rs b/crates/ewwii/src/app.rs index e587dcd..204112c 100644 --- a/crates/ewwii/src/app.rs +++ b/crates/ewwii/src/app.rs @@ -87,6 +87,7 @@ pub enum DaemonCommand { TriggerUpdateUI { inject_vars: Option>, should_preserve_state: bool, + lifetime: Option, sender: DaemonResponseSender, }, CallRhaiFns { @@ -165,6 +166,7 @@ pub struct App { // The cached store of poll/listen handlers pub pl_handler_store: rhai_impl::updates::ReactiveVarStore, + pub clear_pl_onclose: HashMap, pub rt_engine_config: EngineConfValues, pub config_parser: Rc>, @@ -348,8 +350,8 @@ impl App { let output = format!("{:#?}", &self.pl_handler_store.read().unwrap()); sender.send_success(output)? } - DaemonCommand::TriggerUpdateUI { inject_vars, should_preserve_state, sender } => { - match self.trigger_ui_update_with(inject_vars, should_preserve_state) { + DaemonCommand::TriggerUpdateUI { inject_vars, should_preserve_state, lifetime, sender } => { + match self.trigger_ui_update_with(inject_vars, should_preserve_state, lifetime) { Ok(_) => sender.send_success(String::new())?, Err(e) => sender.send_failure(e.to_string())?, }; @@ -404,6 +406,11 @@ impl App { // let scope_index = ewwii_window.scope_index; ewwii_window.close(); + println!("VAR: {:#?}", self.clear_pl_onclose); + if let Some(var_name) = self.clear_pl_onclose.remove(instance_id) { + println!("NAME: {}", var_name); + self.pl_handler_store.write().unwrap().remove(&var_name); + } if auto_reopen { self.failed_windows.insert(instance_id.to_string()); @@ -726,9 +733,10 @@ impl App { /// Trigger a UI update with the given flags. /// Even if there are no flags, the UI will still be updated. pub fn trigger_ui_update_with( - &self, + &mut self, inject_vars: Option>, should_preserve_state: bool, + lifetime: Option ) -> Result<()> { let compiled_ast = self.ewwii_config.get_owned_compiled_ast(); let config_path = self.paths.get_rhai_path(); @@ -751,6 +759,7 @@ impl App { if let Some(vars) = inject_vars { for (name, val) in vars { scope.set_value(name.clone(), Dynamic::from(val.clone())); + let name_clone = name.clone(); // Preserving the new state. // --- @@ -758,6 +767,9 @@ impl App { // in the poll/listen variable store (or the `pl_handler_store` in self) if should_preserve_state { self.pl_handler_store.write().unwrap().insert(name, val); + if let Some(win_name) = &lifetime { + self.clear_pl_onclose.insert(win_name.clone(), name_clone); + } } } } diff --git a/crates/ewwii/src/opts.rs b/crates/ewwii/src/opts.rs index 455cca1..f80e115 100644 --- a/crates/ewwii/src/opts.rs +++ b/crates/ewwii/src/opts.rs @@ -197,9 +197,13 @@ pub enum ActionWithServer { #[arg(long = "inject", short = 'i', value_parser = parse_inject_var_map)] inject_vars: Option>, - /// Preserve the new updates. Only meaningful if used with inject. + /// Preserve the new updates. #[arg(long = "preserve", short = 'p')] should_preserve_state: bool, + + /// Tie the variable lifetime to a window lifetime. + #[arg(long = "lifetime", short = 'l')] + lifetime: Option, }, /// Call rhai functions. (NOTE: All poll/listen will default to their initial value) @@ -289,10 +293,11 @@ impl ActionWithServer { self, ) -> (app::DaemonCommand, Option) { let command = match self { - ActionWithServer::TriggerUpdateUI { inject_vars, should_preserve_state } => { + ActionWithServer::TriggerUpdateUI { inject_vars, should_preserve_state, lifetime } => { return with_response_channel(|sender| app::DaemonCommand::TriggerUpdateUI { inject_vars, should_preserve_state, + lifetime, sender, }) } diff --git a/crates/ewwii/src/server.rs b/crates/ewwii/src/server.rs index cfdc0bb..3949b32 100644 --- a/crates/ewwii/src/server.rs +++ b/crates/ewwii/src/server.rs @@ -85,6 +85,7 @@ pub fn initialize_server( window_close_timer_abort_senders: HashMap::new(), widget_reg_store: std::rc::Rc::new(std::sync::Mutex::new(None)), pl_handler_store, + clear_pl_onclose: HashMap::new(), rt_engine_config: EngineConfValues::default(), config_parser, paths,