1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
mod connection;
mod room;
use std::{cell::RefCell, rc::Rc};
use futures::FutureExt as _;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
use crate::{
media::{MediaManager, MediaManagerHandle},
rpc::{
ClientDisconnect, RpcSession, RpcTransport, WebSocketRpcClient,
WebSocketRpcSession, WebSocketRpcTransport,
},
set_panic_hook,
};
#[doc(inline)]
pub use self::{
connection::{Connection, ConnectionHandle, Connections},
room::{
ConstraintsUpdateException, Room, RoomCloseReason, RoomError,
RoomHandle, WeakRoom,
},
};
#[wasm_bindgen]
pub struct Jason(Rc<RefCell<Inner>>);
struct Inner {
media_manager: Rc<MediaManager>,
rooms: Vec<Room>,
rpc: Rc<WebSocketRpcClient>,
}
#[wasm_bindgen]
impl Jason {
#[must_use]
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
set_panic_hook();
wasm_logger::init(wasm_logger::Config::default());
Self::with_rpc_client(Rc::new(WebSocketRpcClient::new(Box::new(
|url| {
Box::pin(async move {
let ws = WebSocketRpcTransport::new(url)
.await
.map_err(|e| tracerr::new!(e))?;
Ok(Rc::new(ws) as Rc<dyn RpcTransport>)
})
},
))))
}
#[must_use]
pub fn init_room(&self) -> RoomHandle {
let rpc = Rc::clone(&self.0.borrow().rpc);
self.inner_init_room(WebSocketRpcSession::new(rpc))
}
#[must_use]
pub fn media_manager(&self) -> MediaManagerHandle {
self.0.borrow().media_manager.new_handle()
}
#[allow(clippy::needless_pass_by_value)]
pub fn close_room(&self, room_to_delete: RoomHandle) {
self.0.borrow_mut().rooms.retain(|room| {
let should_be_closed = room.inner_ptr_eq(&room_to_delete);
if should_be_closed {
room.set_close_reason(ClientDisconnect::RoomClosed.into());
}
!should_be_closed
});
}
pub fn dispose(self) {
self.0.borrow_mut().rooms.drain(..).for_each(|room| {
room.close(ClientDisconnect::RoomClosed.into());
});
}
}
impl Jason {
#[inline]
pub fn with_rpc_client(rpc: Rc<WebSocketRpcClient>) -> Self {
Self(Rc::new(RefCell::new(Inner {
rpc,
rooms: Vec::new(),
media_manager: Rc::new(MediaManager::default()),
})))
}
pub fn inner_init_room(&self, rpc: Rc<dyn RpcSession>) -> RoomHandle {
let on_normal_close = rpc.on_normal_close();
let room = Room::new(rpc, Rc::clone(&self.0.borrow().media_manager));
let weak_room = room.downgrade();
let weak_inner = Rc::downgrade(&self.0);
spawn_local(on_normal_close.map(move |reason| {
(|| {
let room = weak_room.upgrade()?;
let inner = weak_inner.upgrade()?;
let mut inner = inner.borrow_mut();
let index = inner.rooms.iter().position(|r| r.ptr_eq(&room));
if let Some(index) = index {
inner.rooms.remove(index).close(reason);
}
if inner.rooms.is_empty() {
inner.media_manager = Rc::default();
}
Some(())
})();
}));
let handle = room.new_handle();
self.0.borrow_mut().rooms.push(room);
handle
}
}
impl Default for Jason {
#[inline]
fn default() -> Self {
Self::new()
}
}