Commit cf4168417d

cf4168417d7d9917c674e9464bb3122f2cab9845

parent: 9f10743365

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-10-17 18:43 UTC

connection: Make the connection cloneable.

Layout: unified · split

src/connection.rs +5 −5
@@ -82,12 +82,12 @@ pub enum ClientMessage {
8282///
8383/// While this struct is alive a sync loop will be going on. To cancel the sync
8484/// loop drop the object.
85#[derive(Debug, Clone)]
8586pub struct Connection {
8687 #[used]
87 receiver_task: Task<()>,
88 receiver_task: Rc<Task<()>>,
8889 client: Client,
89 #[used]
90 runtime: Runtime,
90 pub runtime: Rc<Runtime>,
9191}
9292
9393impl Connection {
@@ -129,8 +129,8 @@ impl Connection {
129129
130130 Self {
131131 client: client.clone(),
132 runtime,
133 receiver_task,
132 runtime: Rc::new(runtime),
133 receiver_task: Rc::new(receiver_task),
134134 }
135135 }
136136
src/server.rs +7 −9
@@ -180,12 +180,11 @@ impl MatrixServer {
180180 Rc::downgrade(&self.inner)
181181 }
182182
183 pub fn connection(&self) -> Rc<RefCell<Option<Connection>>> {
184 self.inner().connection.clone()
183 pub fn connection(&self) -> Option<Connection> {
184 self.inner().connection.borrow().clone()
185185 }
186186
187187 pub async fn delete_devices(&self, devices: Vec<DeviceIdBox>) {
188 let connection = self.connection();
189188 let formatted = devices
190189 .iter()
191190 .map(|d| d.to_string())
@@ -193,7 +192,7 @@ impl MatrixServer {
193192 .join(", ");
194193
195194 let print_success = || {
196 self.print(&format!(
195 self.print_network(&format!(
197196 "Successfully deleted device(s) {}",
198197 formatted
199198 ));
@@ -206,7 +205,7 @@ impl MatrixServer {
206205 ));
207206 };
208207
209 if let Some(c) = &*connection.borrow() {
208 if let Some(c) = self.connection() {
210209 match c.delete_devices(devices.clone(), None).await {
211210 Ok(_) => print_success(),
212211 Err(e) => {
@@ -234,9 +233,8 @@ impl MatrixServer {
234233 }
235234
236235 pub async fn devices(&self) {
237 let connection = self.connection();
238236
239 if let Some(c) = &*connection.borrow() {
237 if let Some(c) = self.connection() {
240238 let response = match c.devices().await {
241239 Ok(r) => r,
242240 Err(e) => {
@@ -475,7 +473,7 @@ impl MatrixServer {
475473 return Ok(());
476474 }
477475
478 let client = self.inner.borrow_mut().get_or_creaet_client()?;
476 let client = self.inner.borrow_mut().get_or_create_client()?;
479477 let connection = Connection::new(&self, &client);
480478
481479 *self.inner.borrow_mut().connection.borrow_mut() = Some(connection);
@@ -677,7 +675,7 @@ impl InnerServer {
677675 buffer_handle
678676 }
679677
680 fn get_or_creaet_client(&mut self) -> Result<Client, ServerError> {
678 fn get_or_create_client(&mut self) -> Result<Client, ServerError> {
681679 let client = if let Some(c) = self.client.as_ref() {
682680 c.clone()
683681 } else {