From 8e0f9947d2560cccb1b68ba29ae84055570cce8a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 19 Mar 2023 07:22:14 -0700 Subject: [PATCH] fix time.call_after when used inside an event callback ```lua local wezterm = require 'wezterm' function user_callback_test(window, pane) print("user_callback_test()") wezterm.time.call_after(3, function() print("Hello again, later") end) end return { keys = { { mods = 'CTRL|ALT', key = 'u', action = wezterm.action_callback(user_callback_test) } } } ``` refs: https://github.com/wez/wezterm/issues/3026 --- config/src/lua.rs | 10 ++++++++++ docs/changelog.md | 3 +++ lua-api-crates/time-funcs/src/lib.rs | 23 +++++++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/config/src/lua.rs b/config/src/lua.rs index 9ba4f3ab2..9c73f5114 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -735,6 +735,14 @@ pub fn register_event<'lua>( } } +const IS_EVENT: &str = "wezterm-is-event-emission"; + +/// Returns true if the current lua context is being called as part +/// of an emit_event call. +pub fn is_event_emission<'lua>(lua: &'lua Lua) -> mlua::Result { + lua.named_registry_value(IS_EVENT) +} + /// This implements `wezterm.emit`. /// The first parameter to emit is the name of a signal that may or may not /// have previously been registered via `wezterm.on`. @@ -751,6 +759,8 @@ pub async fn emit_event<'lua>( lua: &'lua Lua, (name, args): (String, mlua::MultiValue<'lua>), ) -> mlua::Result { + lua.set_named_registry_value(IS_EVENT, true)?; + let decorated_name = format!("wezterm-event-{}", name); let tbl: mlua::Value = lua.named_registry_value(&decorated_name)?; match tbl { diff --git a/docs/changelog.md b/docs/changelog.md index a3e7f28cc..ad9c8b76b 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -147,6 +147,9 @@ As features stabilize some brief notes about them will accumulate here. [#3263](https://github.com/wez/wezterm/issues/3263) * Improved compatiblity with the Kitty Image Protocol [#2716](https://github.com/wez/wezterm/issues/2716) +* [wezterm.time.call_after](config/lua/wezterm.time/call_after.md) would not + work when used in an event callback. + [#3026](https://github.com/wez/wezterm/issues/3026) #### Changed * `CTRL-SHIFT-P` now activates the new [command diff --git a/lua-api-crates/time-funcs/src/lib.rs b/lua-api-crates/time-funcs/src/lib.rs index 90011a8f7..deda28089 100644 --- a/lua-api-crates/time-funcs/src/lib.rs +++ b/lua-api-crates/time-funcs/src/lib.rs @@ -1,6 +1,8 @@ use chrono::prelude::*; use config::lua::mlua::{self, Lua, MetaMethod, UserData, UserDataMethods}; -use config::lua::{emit_event, get_or_create_module, get_or_create_sub_module, wrap_callback}; +use config::lua::{ + emit_event, get_or_create_module, get_or_create_sub_module, is_event_emission, wrap_callback, +}; use config::ConfigSubscription; use std::rc::Rc; use std::sync::Mutex; @@ -16,6 +18,7 @@ lazy_static::lazy_static! { fn schedule_all(lua: Option>) -> mlua::Result<()> { if let Some(lua) = lua { let scheduled_events: Vec = lua.named_registry_value(SCHEDULED_EVENTS)?; + lua.set_named_registry_value(SCHEDULED_EVENTS, Vec::::new())?; let generation = config::configuration().generation(); for event in scheduled_events { event.schedule(generation); @@ -147,13 +150,21 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { "call_after", lua.create_function(|lua, (interval_seconds, func): (u64, mlua::Function)| { let user_event_id = wrap_callback(lua, func)?; - let mut scheduled_events: Vec = - lua.named_registry_value(SCHEDULED_EVENTS)?; - scheduled_events.push(ScheduledEvent { + + let event = ScheduledEvent { user_event_id, interval_seconds, - }); - lua.set_named_registry_value(SCHEDULED_EVENTS, scheduled_events)?; + }; + + if is_event_emission(lua)? { + let generation = config::configuration().generation(); + event.schedule(generation); + } else { + let mut scheduled_events: Vec = + lua.named_registry_value(SCHEDULED_EVENTS)?; + scheduled_events.push(event); + lua.set_named_registry_value(SCHEDULED_EVENTS, scheduled_events)?; + } Ok(()) })?, )?;