Skip to content

Commit c92bf62

Browse files
committed
refactor(widget): Extract method for processing requested capabilities
1 parent a3ea524 commit c92bf62

File tree

1 file changed

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

1 file changed

+28
-19
lines changed

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,30 @@ impl WidgetMachine {
688688
actions
689689
}
690690

691+
/// Attempts to acquire capabilities that have been requested by the widget
692+
/// during the initial capability negotiation handshake.
693+
fn process_requested_capabilities(&mut self, requested: Capabilities) -> Vec<Action> {
694+
match self.send_matrix_driver_request(AcquireCapabilities {
695+
desired_capabilities: requested.clone(),
696+
}) {
697+
None => Vec::new(),
698+
Some((request, action)) => {
699+
request.add_response_handler(|result, machine| {
700+
machine.process_acquired_capabilities(result, requested)
701+
});
702+
vec![action]
703+
}
704+
}
705+
}
706+
707+
/// Performs an initial capability negotiation handshake.
708+
///
709+
/// The sequence is as follows: the machine sends a [`RequestCapabilities`]
710+
/// `toWidget` action, the widget responds with its requested
711+
/// capabilities, the machine attempts to acquire the
712+
/// requested capabilities from the driver, then it sends a
713+
/// [`NotifyCapabilitiesChanged`] `toWidget` action to tell the widget
714+
/// which capabilities were approved.
691715
fn negotiate_capabilities(&mut self) -> Vec<Action> {
692716
let mut actions = Vec::new();
693717

@@ -697,28 +721,13 @@ impl WidgetMachine {
697721

698722
self.capabilities = CapabilitiesState::Negotiating;
699723

700-
let Some((request, action)) = self.send_to_widget_request(RequestCapabilities {}) else {
701-
// We're done, return early.
702-
return actions;
703-
};
704-
705-
request.add_response_handler(|response, machine| {
706-
let requested_capabilities = response.capabilities;
707-
708-
let Some((request, action)) = machine.send_matrix_driver_request(AcquireCapabilities {
709-
desired_capabilities: requested_capabilities.clone(),
710-
}) else {
711-
// We're done, return early.
712-
return Vec::new();
713-
};
714-
724+
if let Some((request, action)) = self.send_to_widget_request(RequestCapabilities {}) {
715725
request.add_response_handler(|result, machine| {
716-
machine.process_acquired_capabilities(result, requested_capabilities)
726+
machine.process_requested_capabilities(result.capabilities)
717727
});
718-
vec![action]
719-
});
728+
actions.push(action);
729+
}
720730

721-
actions.push(action);
722731
actions
723732
}
724733
}

0 commit comments

Comments
 (0)