2021-04-05 12:49:00 -05:00
|
|
|
//! Bookkeeping to make sure only one long-running operation is being executed
|
|
|
|
//! at a time.
|
2021-01-10 09:02:02 -06:00
|
|
|
|
2022-04-16 07:16:58 -05:00
|
|
|
pub(crate) type Cause = String;
|
|
|
|
|
2023-03-26 01:39:28 -05:00
|
|
|
pub(crate) struct OpQueue<Args = (), Output = ()> {
|
|
|
|
op_requested: Option<(Cause, Args)>,
|
2021-01-10 09:02:02 -06:00
|
|
|
op_in_progress: bool,
|
2021-04-05 12:49:00 -05:00
|
|
|
last_op_result: Output,
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
|
|
|
|
2023-03-26 01:39:28 -05:00
|
|
|
impl<Args, Output: Default> Default for OpQueue<Args, Output> {
|
2021-01-28 09:33:02 -06:00
|
|
|
fn default() -> Self {
|
2022-04-16 07:16:58 -05:00
|
|
|
Self { op_requested: None, op_in_progress: false, last_op_result: Default::default() }
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
2021-01-28 09:33:02 -06:00
|
|
|
}
|
|
|
|
|
2023-03-26 01:39:28 -05:00
|
|
|
impl<Args, Output> OpQueue<Args, Output> {
|
|
|
|
pub(crate) fn request_op(&mut self, reason: Cause, args: Args) {
|
|
|
|
self.op_requested = Some((reason, args));
|
2021-01-28 09:33:02 -06:00
|
|
|
}
|
2023-03-26 01:39:28 -05:00
|
|
|
pub(crate) fn should_start_op(&mut self) -> Option<(Cause, Args)> {
|
2021-01-28 09:33:02 -06:00
|
|
|
if self.op_in_progress {
|
2022-04-16 07:16:58 -05:00
|
|
|
return None;
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
2022-04-16 07:16:58 -05:00
|
|
|
self.op_in_progress = self.op_requested.is_some();
|
|
|
|
self.op_requested.take()
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
2021-04-05 12:49:00 -05:00
|
|
|
pub(crate) fn op_completed(&mut self, result: Output) {
|
2021-01-10 09:02:02 -06:00
|
|
|
assert!(self.op_in_progress);
|
|
|
|
self.op_in_progress = false;
|
2021-04-05 12:49:00 -05:00
|
|
|
self.last_op_result = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn last_op_result(&self) -> &Output {
|
|
|
|
&self.last_op_result
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|
2021-04-06 06:16:35 -05:00
|
|
|
pub(crate) fn op_in_progress(&self) -> bool {
|
|
|
|
self.op_in_progress
|
|
|
|
}
|
|
|
|
pub(crate) fn op_requested(&self) -> bool {
|
2022-04-16 07:16:58 -05:00
|
|
|
self.op_requested.is_some()
|
2021-04-06 06:16:35 -05:00
|
|
|
}
|
2021-01-10 09:02:02 -06:00
|
|
|
}
|