1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-11 15:14:16 +03:00

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
This commit is contained in:
Wez Furlong 2023-03-19 07:22:14 -07:00
parent 4d471807c3
commit 8e0f9947d2
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387
3 changed files with 30 additions and 6 deletions

View File

@ -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<bool> {
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<bool> {
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 {

View File

@ -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

View File

@ -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<Rc<mlua::Lua>>) -> mlua::Result<()> {
if let Some(lua) = lua {
let scheduled_events: Vec<ScheduledEvent> = lua.named_registry_value(SCHEDULED_EVENTS)?;
lua.set_named_registry_value(SCHEDULED_EVENTS, Vec::<ScheduledEvent>::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<ScheduledEvent> =
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<ScheduledEvent> =
lua.named_registry_value(SCHEDULED_EVENTS)?;
scheduled_events.push(event);
lua.set_named_registry_value(SCHEDULED_EVENTS, scheduled_events)?;
}
Ok(())
})?,
)?;