Skip to main content

dex_sdk/
timer.rs

1// Copyright (c) 2026 Super Durable, Inc.
2//
3// Licensed under the Sustainable Use License 1.0.
4// You may not use this file except in compliance with the License.
5// See the LICENSE file in the repository root.
6//
7// SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
8
9use std::time::Duration;
10
11use crate::Condition;
12
13/// Creates durable timer conditions for Step waits.
14///
15/// Returning a timer Condition from WaitFor lets Dex wait without keeping a Worker handler
16/// running. Creating the Condition itself does not block or start a local timer.
17///
18/// ```
19/// use dex_sdk::{Timer, Wait};
20/// use std::time::Duration;
21/// let wait = Wait::until(Timer::by_duration(Duration::from_secs(30)));
22/// ```
23pub struct Timer;
24
25impl Timer {
26    /// Returns a condition that becomes true after `duration` elapses.
27    ///
28    /// Timer duration is measured by Dex workflow time and survives Worker restarts.
29    /// Durations must fit signed 32-bit whole seconds when the Worker submits the wait.
30    /// Invalid durations fail wait-result mapping. Use [`crate::Context::has_timer_fired`] during
31    /// Execute to distinguish a timer from another winning Condition.
32    pub fn by_duration(duration: Duration) -> Condition {
33        Condition::timer(duration)
34    }
35}
36
37#[derive(Clone, Debug, Eq, PartialEq)]
38/// Selects one timer from a Step execution for [`crate::Client::skip_timer`].
39pub enum TimerId {
40    /// Targets the condition carrying this explicit condition ID.
41    ConditionId(String),
42    /// Targets the zero-based timer-condition index within the wait result.
43    ConditionIndex(usize),
44}
45
46impl TimerId {
47    /// Selects a timer by the ID assigned with [`Condition::with_id`].
48    pub fn by_condition_id(id: impl Into<String>) -> Self {
49        Self::ConditionId(id.into())
50    }
51
52    /// Selects a timer by its zero-based condition index.
53    pub fn by_condition_index(index: usize) -> Self {
54        Self::ConditionIndex(index)
55    }
56}