refactor: minor improvements
This commit is contained in:
parent
5691afaf2d
commit
2b9bf1987b
@ -96,7 +96,10 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
|
||||
)?;
|
||||
call_listeners(&state, "init", ()).await;
|
||||
|
||||
if let Some(address) = state.http_address {
|
||||
let Some(address) = state.http_address else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let listener = TcpListener::bind(address).await.map_err(|error| {
|
||||
error!("failed to listen on {address}: {error:?}");
|
||||
error
|
||||
@ -123,7 +126,6 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
@ -132,9 +134,9 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
|
||||
|
||||
async fn call_listeners<T: Clone + IntoLuaMulti>(state: &State, event_type: &str, data: T) {
|
||||
if let Some(listeners) = state.event_listeners.read().await.get(event_type) {
|
||||
for (_, listener) in listeners {
|
||||
if let Err(error) = listener.call_async::<()>(data.clone()).await {
|
||||
error!("failed to call lua event listener for {event_type}: {error:?}");
|
||||
for (id, callback) in listeners {
|
||||
if let Err(error) = callback.call_async::<()>(data.clone()).await {
|
||||
error!("failed to call lua event listener {id} for {event_type}: {error:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,19 +8,23 @@ pub fn register_functions(lua: &Lua, globals: &Table, event_listeners: ListenerM
|
||||
globals.set(
|
||||
"add_listener",
|
||||
lua.create_function(
|
||||
move |_, (event_type, callback, id): (String, Function, Option<String>)| {
|
||||
move |_, (event_type, callback, optional_id): (String, Function, Option<String>)| {
|
||||
let m = m.clone();
|
||||
tokio::spawn(async move {
|
||||
m.write().await.entry(event_type).or_default().push((
|
||||
id.unwrap_or(callback.info().name.unwrap_or(format!(
|
||||
let id = optional_id.unwrap_or_else(|| {
|
||||
callback.info().name.unwrap_or(format!(
|
||||
"anonymous @ {}",
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_millis()
|
||||
))),
|
||||
callback,
|
||||
));
|
||||
))
|
||||
});
|
||||
tokio::spawn(async move {
|
||||
m.write()
|
||||
.await
|
||||
.entry(event_type)
|
||||
.or_default()
|
||||
.push((id, callback));
|
||||
});
|
||||
Ok(())
|
||||
},
|
||||
|
@ -75,7 +75,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(log_file)
|
||||
.expect("should have been able to open log file"),
|
||||
.expect("log file should be accessible"),
|
||||
)
|
||||
.boxed()
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user