1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! This library implements multiple and periodic timers on the Internet Computer.
//!
//! # Example
//!
//! ```rust,no_run
//! # use std::time::Duration;
//! # fn main() {
//! ic_cdk_timers::set_timer(Duration::from_secs(1), async { ic_cdk::println!("Hello from the future!") });
//! # }
//! ```
//!
//! # Details
//!
//! Timers internally use a bounded-wait self-call for error handling purposes. This is not guaranteed to
//! remain the case in the future, but means that if the system is under heavy load, timers may begin to
//! slow down by a lot as the self-calls begin to time out and the timers are rescheduled for the next global
//! timer tick. This also means that each executed timer incurs the cycle cost of a canister call.
//!
//! <div class="warning">
//!
//! Timers are not persisted across canister upgrades.
//!
//! </div>
use ;
use crate;
/// Type returned by the [`set_timer`] and [`set_timer_interval`] functions. Pass to [`clear_timer`] to remove the timer.
pub use crateTaskId as TimerId;
/// Sets `future` to be executed later, after `delay`. Panics if `delay` + [`time()`] is more than [`u64::MAX`] nanoseconds.
///
/// To cancel the timer before it executes, pass the returned `TimerId` to [`clear_timer`].
///
/// <div class="warning">
///
/// Timers are not persisted across canister upgrades.
///
/// </div>
///
/// # Examples
///
/// ```no_run
/// # use std::time::Duration;
/// ic_cdk_timers::set_timer(Duration::from_secs(1), async {
/// ic_cdk::println!("Hello from the future!");
/// });
/// ```
///
/// [`time()`]: https://docs.rs/ic-cdk/0.18.5/ic_cdk/api/fn.time.html
/// Sets `func` to be executed every `interval`. Panics if `interval` + [`time()`] is more than [`u64::MAX`] nanoseconds.
///
/// To cancel the interval timer, pass the returned `TimerId` to [`clear_timer`].
///
/// This is a closure returning a future (`|| async {`), not an async closure (`async || {`). The two syntaxes
/// are interchangeable *if* the closure does not capture anything. If it does, you will need the former syntax,
/// and it is almost certain that either your captures must be Copy or you must use e.g. Rc to share them, because
/// you cannot capture by reference, and referencing an owned capture in a returned async block is not possible.
///
/// <div class="warning">
///
/// Interval timers should be *idempotent* with respect to the canister's state, as during heavy network load,
/// timeouts may result in duplicate execution.
///
/// </div>
///
/// <div class="warning">
///
/// Timers are not persisted across canister upgrades.
///
/// </div>
///
/// # Examples
///
/// ```no_run
/// # use std::time::Duration;
/// ic_cdk_timers::set_timer_interval(Duration::from_secs(5), || async {
/// ic_cdk::println!("This will run every five seconds forever!");
/// });
/// ```
///
/// [`time()`]: https://docs.rs/ic-cdk/0.18.5/ic_cdk/api/fn.time.html
/// Sets `func` to be executed every `interval`. Panics if `interval` + [`time()`] is more than [`u64::MAX`] nanoseconds.
///
/// To cancel the interval timer, pass the returned `TimerId` to [`clear_timer`].
///
/// Unlike [`set_timer_interval`], this function takes an async closure (`async || {`). This is simpler to use
/// with captured variables, but also means that invocations cannot be run concurrently; if the interval is up
/// but the previous invocation is still running, the new invocation will be skipped.
///
/// <div class="warning">
///
/// Interval timers should be *idempotent* with respect to the canister's state, as during heavy network load,
/// timeouts may result in duplicate execution.
///
/// </div>
///
/// <div class="warning">
///
/// Timers are not persisted across canister upgrades.
///
/// </div>
///
/// # Examples
///
/// ```no_run
/// # use std::time::Duration;
/// ic_cdk_timers::set_timer_interval_serial(Duration::from_secs(5), async || {
/// ic_cdk::println!("This will run every five seconds forever!");
/// });
/// ```
///
/// [`time()`]: https://docs.rs/ic-cdk/0.18.5/ic_cdk/api/fn.time.html
/// Cancels an existing timer. Does nothing if the timer has already been canceled.
///
/// # Examples
///
/// ```no_run
/// # use std::time::Duration;
/// let timer_id = ic_cdk_timers::set_timer(Duration::from_secs(60), async {
/// ic_cdk::println!("This will never run, because we cancel it!");
/// });
/// ic_cdk_timers::clear_timer(timer_id);
/// ```