refactor: Store callback function directly instead of in the registry

This commit is contained in:
2025-09-08 02:09:11 +02:00
parent e2fb680cd6
commit e880efe4cf

View File

@@ -5,7 +5,7 @@ use serde::Serialize;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct Internal { struct Internal {
uuid: uuid::Uuid, value: mlua::Value,
lua: mlua::Lua, lua: mlua::Lua,
} }
@@ -28,12 +28,9 @@ impl<T, S> Default for ActionCallback<T, S> {
impl<T, S> FromLua for ActionCallback<T, S> { impl<T, S> FromLua for ActionCallback<T, S> {
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> { fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
let uuid = uuid::Uuid::new_v4();
lua.set_named_registry_value(&uuid.to_string(), value)?;
Ok(ActionCallback { Ok(ActionCallback {
internal: Some(Internal { internal: Some(Internal {
uuid, value,
lua: lua.clone(), lua: lua.clone(),
}), }),
_this: PhantomData::<T>, _this: PhantomData::<T>,
@@ -55,11 +52,7 @@ where
let state = internal.lua.to_value(state).unwrap(); let state = internal.lua.to_value(state).unwrap();
let callback: mlua::Value = internal match &internal.value {
.lua
.named_registry_value(&internal.uuid.to_string())
.unwrap();
match callback {
mlua::Value::Function(f) => f.call_async::<()>((this.clone(), state)).await.unwrap(), mlua::Value::Function(f) => f.call_async::<()>((this.clone(), state)).await.unwrap(),
_ => todo!("Only functions are currently supported"), _ => todo!("Only functions are currently supported"),
} }