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 @@
11use std::{
2 cell::RefCell, collections::BTreeMap, path::PathBuf, rc::Weak, sync::Arc,
3 time::Duration,
2 cell::RefCell, collections::BTreeMap, future::Future, path::PathBuf,
3 rc::Rc, rc::Weak, sync::Arc, time::Duration,
44};
55
66use async_std::sync::{channel as async_channel, Receiver, Sender};
@@ -91,6 +91,17 @@ pub struct Connection {
9191}
9292
9393impl 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
94105 pub fn new(server: &MatrixServer, client: &Client) -> Self {
95106 let (tx, rx) = async_channel(1000);
96107
@@ -143,23 +154,16 @@ impl Connection {
143154 let room_id = room_id.to_owned();
144155 let client = self.client.clone();
145156
146 let handle = self
147 .runtime
148 .spawn(async move {
149 client
150 .room_send(
151 &room_id,
152 content,
153 Some(transaction_id.unwrap_or_else(Uuid::new_v4)),
154 )
155 .await
156 })
157 .await;
158
159 match handle {
160 Ok(response) => response,
161 Err(e) => panic!("Tokio error while sending a message {:?}", e),
162 }
157 self.spawn(async move {
158 client
159 .room_send(
160 &room_id,
161 content,
162 Some(transaction_id.unwrap_or_else(Uuid::new_v4)),
163 )
164 .await
165 })
166 .await
163167 }
164168
165169 pub async fn delete_devices(
@@ -168,36 +172,21 @@ impl Connection {
168172 auth_info: Option<InteractiveAuthInfo>,
169173 ) -> MatrixResult<DeleteDevicesResponse> {
170174 let client = self.client.clone();
171 let handle = self
172 .runtime
173 .spawn(async move {
174 if let Some(info) = auth_info {
175 let auth = Some(info.as_auth_data());
176 client.delete_devices(&devices, auth).await
177 } else {
178 client.delete_devices(&devices, None).await
179 }
180 })
181 .await;
182
183 match handle {
184 Ok(response) => response,
185 Err(e) => panic!("Tokio error while sending a message {:?}", e),
186 }
175 self.spawn(async move {
176 if let Some(info) = auth_info {
177 let auth = Some(info.as_auth_data());
178 client.delete_devices(&devices, auth).await
179 } else {
180 client.delete_devices(&devices, None).await
181 }
182 })
183 .await
187184 }
188185
189186 /// Get the list of our own devices.
190187 pub async fn devices(&self) -> MatrixResult<DevicesResponse> {
191188 let client = self.client.clone();
192 let handle = self
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 }
189 self.spawn(async move { client.devices().await }).await
201190 }
202191
203192 /// Set or reset a typing notice.
@@ -216,23 +205,16 @@ impl Connection {
216205 let room_id = room_id.to_owned();
217206 let client = self.client.clone();
218207
219 let handle = self
220 .runtime
221 .spawn(async move {
222 let typing = if typing {
223 Typing::Yes(TYPING_NOTICE_TIMEOUT)
224 } else {
225 Typing::No
226 };
227
228 client.typing_notice(&room_id, typing).await
229 })
230 .await;
208 self.spawn(async move {
209 let typing = if typing {
210 Typing::Yes(TYPING_NOTICE_TIMEOUT)
211 } else {
212 Typing::No
213 };
231214
232 match handle {
233 Ok(response) => response,
234 Err(e) => panic!("Tokio error while sending a message {:?}", e),
235 }
215 client.typing_notice(&room_id, typing).await
216 })
217 .await
236218 }
237219
238220 fn save_device_id(