From b299f1a172388e73f0c3405f82b08ca4d3cc8884 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Fri, 10 Apr 2026 05:57:34 +0200 Subject: [PATCH] chore: add methods to `Controller` to pause and unpause futures - Use `Controller::until_paused` as a signal in a select to pause futures - Use `Controller::until_unpaused` to block futures in a select loop --- src/operation/controller.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/operation/controller.rs b/src/operation/controller.rs index f4b26fe..960a28d 100644 --- a/src/operation/controller.rs +++ b/src/operation/controller.rs @@ -86,6 +86,35 @@ impl Controller { self.set_state(ControllerState::Paused); } + /// Returns when the state is paused. + /// + /// Use this to pause futures. + pub async fn until_paused(&self) { + loop { + if matches!(self.state(), ControllerState::Paused) { + return; + } + + self.inner.notify.notified().await; + } + } + + /// Returns when state is neither paused, cancelled, nor failed. + /// + /// Use this to resume futures. + pub async fn until_unpaused(&self) { + loop { + if !matches!( + self.state(), + ControllerState::Paused | ControllerState::Cancelled | ControllerState::Failed + ) { + return; + } + + self.inner.notify.notified().await; + } + } + pub fn unpause(&self) { if !self.is_cancelled() | !self.is_failed() { self.set_state(ControllerState::Running);