Skip to content

Commit 03d9e9b

Browse files
committed
refactor(widget): avoid complicated combinators and make decisions more local and explicit
1 parent 75c4af5 commit 03d9e9b

File tree

1 file changed

+32
-19
lines changed
  • crates/matrix-sdk/src/widget/machine

1 file changed

+32
-19
lines changed

crates/matrix-sdk/src/widget/machine/mod.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
//! No I/O logic of the [`WidgetDriver`].
1616
17-
use std::{iter, time::Duration};
17+
use std::time::Duration;
1818

1919
use driver_req::UpdateDelayedEventRequest;
2020
use from_widget::UpdateDelayedEventResponse;
@@ -248,7 +248,10 @@ impl WidgetMachine {
248248
.unwrap_or_default(),
249249

250250
FromWidgetRequest::GetOpenId {} => {
251-
let request_action = self.send_matrix_driver_request(RequestOpenId).map(
251+
let mut actions =
252+
vec![Self::send_from_widget_response(raw_request, Ok(OpenIdResponse::Pending))];
253+
254+
if let Some(request_action) = self.send_matrix_driver_request(RequestOpenId).map(
252255
|(request, request_action)| {
253256
request.then(|res, machine| {
254257
let response = match res {
@@ -269,11 +272,11 @@ impl WidgetMachine {
269272

270273
request_action
271274
},
272-
);
275+
) {
276+
actions.push(request_action);
277+
}
273278

274-
let response =
275-
Self::send_from_widget_response(raw_request, Ok(OpenIdResponse::Pending));
276-
iter::once(response).chain(request_action).collect()
279+
actions
277280
}
278281

279282
FromWidgetRequest::DelayedEventUpdate(req) => {
@@ -613,11 +616,15 @@ impl WidgetMachine {
613616
}
614617

615618
fn negotiate_capabilities(&mut self) -> Vec<Action> {
616-
let unsubscribe_required =
617-
matches!(&self.capabilities, CapabilitiesState::Negotiated(c) if !c.read.is_empty());
619+
let mut actions = Vec::new();
620+
621+
if matches!(&self.capabilities, CapabilitiesState::Negotiated(c) if !c.read.is_empty()) {
622+
actions.push(Action::Unsubscribe);
623+
}
624+
618625
self.capabilities = CapabilitiesState::Negotiating;
619626

620-
let action =
627+
if let Some(action) =
621628
self.send_to_widget_request(RequestCapabilities {}).map(|(request, action)| {
622629
request.then(|response, machine| {
623630
let requested_capabilities = response.capabilities;
@@ -633,7 +640,11 @@ impl WidgetMachine {
633640
Capabilities::default()
634641
});
635642

636-
let subscribe_required = !approved_capabilities.read.is_empty();
643+
let mut actions = Vec::new();
644+
if !approved_capabilities.read.is_empty() {
645+
actions.push(Action::Subscribe);
646+
}
647+
637648
machine.capabilities =
638649
CapabilitiesState::Negotiated(approved_capabilities.clone());
639650

@@ -642,15 +653,14 @@ impl WidgetMachine {
642653
requested: requested_capabilities,
643654
};
644655

645-
let action = machine
656+
if let Some(action) = machine
646657
.send_to_widget_request(notify_caps_changed)
647-
.map(|(_request, action)| action);
658+
.map(|(_request, action)| action)
659+
{
660+
actions.push(action);
661+
}
648662

649-
subscribe_required
650-
.then_some(Action::Subscribe)
651-
.into_iter()
652-
.chain(action)
653-
.collect()
663+
actions
654664
});
655665

656666
vec![action]
@@ -660,9 +670,12 @@ impl WidgetMachine {
660670
});
661671

662672
action
663-
});
673+
})
674+
{
675+
actions.push(action);
676+
}
664677

665-
unsubscribe_required.then_some(Action::Unsubscribe).into_iter().chain(action).collect()
678+
actions
666679
}
667680
}
668681

0 commit comments

Comments
 (0)