fix(events): manipulate listeners in a task to avoid deadlocks
This commit is contained in:
parent
168ac1bb46
commit
2ea7ec9e7e
@ -9,9 +9,9 @@ pub async fn register_functions(lua: &Lua, globals: &Table, state: State) -> Res
|
||||
"add_listener",
|
||||
lua.create_function(
|
||||
move |_, (event_type, callback, id): (String, Function, Option<String>)| {
|
||||
let mut l = block_on(l.lock());
|
||||
|
||||
l.entry(event_type).or_default().push((
|
||||
let l = l.clone();
|
||||
tokio::spawn(async move {
|
||||
l.lock().await.entry(event_type).or_default().push((
|
||||
id.unwrap_or(callback.info().name.unwrap_or(format!(
|
||||
"anonymous @ {}",
|
||||
SystemTime::now()
|
||||
@ -21,6 +21,7 @@ pub async fn register_functions(lua: &Lua, globals: &Table, state: State) -> Res
|
||||
))),
|
||||
callback,
|
||||
));
|
||||
});
|
||||
Ok(())
|
||||
},
|
||||
)?,
|
||||
@ -30,8 +31,9 @@ pub async fn register_functions(lua: &Lua, globals: &Table, state: State) -> Res
|
||||
globals.set(
|
||||
"remove_listener",
|
||||
lua.create_function(move |_, (event_type, target_id): (String, String)| {
|
||||
let mut l = block_on(l.lock());
|
||||
|
||||
let l = l.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut l = l.lock().await;
|
||||
let empty = if let Some(listeners) = l.get_mut(&event_type) {
|
||||
listeners.retain(|(id, _)| target_id != *id);
|
||||
listeners.is_empty()
|
||||
@ -41,7 +43,7 @@ pub async fn register_functions(lua: &Lua, globals: &Table, state: State) -> Res
|
||||
if empty {
|
||||
l.remove(&event_type);
|
||||
}
|
||||
|
||||
});
|
||||
Ok(())
|
||||
})?,
|
||||
)?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user