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