@@ -35,6 +35,7 @@ class timer final {
35
35
}
36
36
}
37
37
38
+ // First, `function` is called once, and then `function` is called every interval specified by `interval`.
38
39
void start (std::function<void (void )> function,
39
40
duration interval) {
40
41
enabled_ = true ;
@@ -62,6 +63,25 @@ class timer final {
62
63
return enabled_;
63
64
}
64
65
66
+ // Update the interval.
67
+ // Any `function` call reserved before calling this method will be canceled, and the `function` will be called after `interval` duration.
68
+ //
69
+ // Special cases:.
70
+ // - If `interval` == duration(0), this method works same as `stop`.
71
+ // - If `interval` is same as the current interval, this method does nothing.
72
+ void set_interval (duration interval) {
73
+ if (interval == duration (0 )) {
74
+ stop ();
75
+ } else if (interval != interval_) {
76
+ dispatcher_client_.enqueue_to_dispatcher ([this , interval] {
77
+ ++current_function_id_;
78
+ interval_ = interval;
79
+
80
+ enqueue (current_function_id_);
81
+ });
82
+ }
83
+ }
84
+
65
85
private:
66
86
// This method is executed in the dispatcher thread.
67
87
void call_function (int function_id) {
@@ -79,6 +99,10 @@ class timer final {
79
99
});
80
100
}
81
101
102
+ enqueue (function_id);
103
+ }
104
+
105
+ void enqueue (int function_id) {
82
106
dispatcher_client_.enqueue_to_dispatcher (
83
107
[this , function_id] {
84
108
call_function (function_id);
0 commit comments