Commit 9f10743365

9f10743365560be9781b6172da2f7bdd15d5bf6b

parent: 8c57d1eae9

Unsigned

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

connection: Add a spawn helper method to the connection.

Layout: unified · split

src/connection.rs +42 −60
@@ -1,6 +1,6 @@
1use std::{ 1use std::{
2 cell::RefCell, collections::BTreeMap, path::PathBuf, rc::Weak, sync::Arc, 2 cell::RefCell, collections::BTreeMap, future::Future, path::PathBuf,
3 time::Duration, 3 rc::Rc, rc::Weak, sync::Arc, time::Duration,
4}; 4};
5 5
6use async_std::sync::{channel as async_channel, Receiver, Sender}; 6use async_std::sync::{channel as async_channel, Receiver, Sender};
@@ -91,6 +91,17 @@ pub struct Connection {
91} 91}
92 92
93impl Connection { 93impl Connection {
94 pub async fn spawn<F>(&self, future: F) -> F::Output
95 where
96 F: Future + Send + 'static,
97 F::Output: Send + 'static,
98 {
99 self.runtime
100 .spawn(future)
101 .await
102 .expect("Tokio error while sending a message")
103 }
104
94 pub fn new(server: &MatrixServer, client: &Client) -> Self { 105 pub fn new(server: &MatrixServer, client: &Client) -> Self {
95 let (tx, rx) = async_channel(1000); 106 let (tx, rx) = async_channel(1000);
96 107
@@ -143,23 +154,16 @@ impl Connection {
143 let room_id = room_id.to_owned(); 154 let room_id = room_id.to_owned();
144 let client = self.client.clone(); 155 let client = self.client.clone();
145 156
146 let handle = self 157 self.spawn(async move {
147 .runtime 158 client
148 .spawn(async move { 159 .room_send(
149 client 160 &room_id,
150 .room_send( 161 content,
151 &room_id, 162 Some(transaction_id.unwrap_or_else(Uuid::new_v4)),
152 content, 163 )
153 Some(transaction_id.unwrap_or_else(Uuid::new_v4)), 164 .await
154 ) 165 })
155 .await 166 .await
156 })
157 .await;
158
159 match handle {
160 Ok(response) => response,
161 Err(e) => panic!("Tokio error while sending a message {:?}", e),
162 }
163 } 167 }
164 168
165 pub async fn delete_devices( 169 pub async fn delete_devices(
@@ -168,36 +172,21 @@ impl Connection {
168 auth_info: Option<InteractiveAuthInfo>, 172 auth_info: Option<InteractiveAuthInfo>,
169 ) -> MatrixResult<DeleteDevicesResponse> { 173 ) -> MatrixResult<DeleteDevicesResponse> {
170 let client = self.client.clone(); 174 let client = self.client.clone();
171 let handle = self 175 self.spawn(async move {
172 .runtime 176 if let Some(info) = auth_info {
173 .spawn(async move { 177 let auth = Some(info.as_auth_data());
174 if let Some(info) = auth_info { 178 client.delete_devices(&devices, auth).await
175 let auth = Some(info.as_auth_data()); 179 } else {
176 client.delete_devices(&devices, auth).await 180 client.delete_devices(&devices, None).await
177 } else { 181 }
178 client.delete_devices(&devices, None).await 182 })
179 } 183 .await
180 })
181 .await;
182
183 match handle {
184 Ok(response) => response,
185 Err(e) => panic!("Tokio error while sending a message {:?}", e),
186 }
187 } 184 }
188 185
189 /// Get the list of our own devices. 186 /// Get the list of our own devices.
190 pub async fn devices(&self) -> MatrixResult<DevicesResponse> { 187 pub async fn devices(&self) -> MatrixResult<DevicesResponse> {
191 let client = self.client.clone(); 188 let client = self.client.clone();
192 let handle = self 189 self.spawn(async move { client.devices().await }).await
193 .runtime
194 .spawn(async move { client.devices().await })
195 .await;
196
197 match handle {
198 Ok(response) => response,
199 Err(e) => panic!("Tokio error while sending a message {:?}", e),
200 }
201 } 190 }
202 191
203 /// Set or reset a typing notice. 192 /// Set or reset a typing notice.
@@ -216,23 +205,16 @@ impl Connection {
216 let room_id = room_id.to_owned(); 205 let room_id = room_id.to_owned();
217 let client = self.client.clone(); 206 let client = self.client.clone();
218 207
219 let handle = self 208 self.spawn(async move {
220 .runtime 209 let typing = if typing {
221 .spawn(async move { 210 Typing::Yes(TYPING_NOTICE_TIMEOUT)
222 let typing = if typing { 211 } else {
223 Typing::Yes(TYPING_NOTICE_TIMEOUT) 212 Typing::No
224 } else { 213 };
225 Typing::No
226 };
227
228 client.typing_notice(&room_id, typing).await
229 })
230 .await;
231 214
232 match handle { 215 client.typing_notice(&room_id, typing).await
233 Ok(response) => response, 216 })
234 Err(e) => panic!("Tokio error while sending a message {:?}", e), 217 .await
235 }
236 } 218 }
237 219
238 fn save_device_id( 220 fn save_device_id(