use std::marker::PhantomData; use futures::future::try_join_all; use mlua::{FromLua, IntoLuaMulti}; #[derive(Debug, Clone)] pub struct ActionCallback
{
callbacks: Vec ,
}
// NOTE: For some reason the derive macro combined with PhantomData leads to issues where it
// requires all types part of P to implement default, even if they never actually get constructed.
// By manually implemented Default it works fine.
impl Default for ActionCallback {
fn default() -> Self {
Self {
callbacks: Default::default(),
_parameters: Default::default(),
}
}
}
impl FromLua for ActionCallback {
fn from_lua(value: mlua::Value, _lua: &mlua::Lua) -> mlua::Result ,
})
}
}
// TODO: Return proper error here
impl ActionCallback
where
P: IntoLuaMulti + Sync + Clone,
{
pub async fn call(&self, parameters: P) {
try_join_all(
self.callbacks
.iter()
.map(async |f| f.call_async::<()>(parameters.clone()).await),
)
.await
.unwrap();
}
pub fn is_empty(&self) -> bool {
self.callbacks.is_empty()
}
}