diff --git a/rust/examples/videoroom.rs b/rust/examples/videoroom.rs index 369ae4f0da..8b94a7c06c 100644 --- a/rust/examples/videoroom.rs +++ b/rust/examples/videoroom.rs @@ -232,7 +232,6 @@ mod rooms_registry { use crate::room::{Room, RoomId, WeakRoom}; use async_lock::Mutex; use mediasoup::prelude::*; - use std::collections::hash_map::Entry; use std::collections::HashMap; use std::sync::Arc; @@ -250,47 +249,13 @@ mod rooms_registry { worker_manager: &WorkerManager, room_id: RoomId, ) -> Result { - let mut rooms = self.rooms.lock().await; - match rooms.entry(room_id) { - Entry::Occupied(mut entry) => match entry.get().upgrade() { + if let Some(weak_room) = self.rooms.lock().await.get(&room_id) { + match weak_room.upgrade() { Some(room) => Ok(room), - None => { - let room = Room::new_with_id(worker_manager, room_id).await?; - entry.insert(room.downgrade()); - room.on_close({ - let room_id = room.id(); - let rooms = Arc::clone(&self.rooms); - - move || { - std::thread::spawn(move || { - futures_lite::future::block_on(async move { - rooms.lock().await.remove(&room_id); - }); - }); - } - }) - .detach(); - Ok(room) - } - }, - Entry::Vacant(entry) => { - let room = Room::new_with_id(worker_manager, room_id).await?; - entry.insert(room.downgrade()); - room.on_close({ - let room_id = room.id(); - let rooms = Arc::clone(&self.rooms); - - move || { - std::thread::spawn(move || { - futures_lite::future::block_on(async move { - rooms.lock().await.remove(&room_id); - }); - }); - } - }) - .detach(); - Ok(room) + None => self.create_room(worker_manager).await, } + } else { + self.create_room(worker_manager).await } } @@ -298,9 +263,9 @@ mod rooms_registry { pub async fn create_room(&self, worker_manager: &WorkerManager) -> Result { let mut rooms = self.rooms.lock().await; let room = Room::new(worker_manager).await?; - rooms.insert(room.id(), room.downgrade()); + let room_id = room.id(); + rooms.insert(room_id, room.downgrade()); room.on_close({ - let room_id = room.id(); let rooms = Arc::clone(&self.rooms); move || {