File tree 3 files changed +20
-14
lines changed
3 files changed +20
-14
lines changed Original file line number Diff line number Diff line change @@ -6,12 +6,12 @@ wit_bindgen::generate!({
6
6
struct MySpinTimer ;
7
7
8
8
impl SpinTimer for MySpinTimer {
9
- fn handle_timer_request ( ) -> bool {
9
+ fn handle_timer_request ( ) -> ContinueTimer {
10
10
let text = spin_sdk:: config:: get ( "message" ) . unwrap ( ) ;
11
11
println ! ( "{text}" ) ;
12
12
13
- // Return false if you want to exit the timer loop calling this funtion subsequently.
14
- true
13
+ // Return ContinueTimer::True if you want to continue the timer loop calling this component/function subsequently.
14
+ ContinueTimer :: True
15
15
}
16
16
}
17
17
Original file line number Diff line number Diff line change 1
1
default world spin-timer {
2
- export handle-timer-request : func () -> bool
2
+ // Timer execution loop exit variant
3
+ variant continue-timer {
4
+ true ,
5
+ false
6
+ }
7
+
8
+ // Get the value of a key.
9
+ export handle-timer-request : func () -> continue-timer
3
10
}
Original file line number Diff line number Diff line change @@ -109,15 +109,14 @@ impl TriggerExecutor for TimerTrigger {
109
109
let duration = tokio:: time:: Duration :: from_millis ( * d * 1000 / speedup) ;
110
110
loop {
111
111
tokio:: time:: sleep ( duration) . await ;
112
- self . handle_timer_event ( c) . await . unwrap ( ) ;
113
-
114
- // Inverse the control of breaking out of loop, let the component decide this by returning false.
115
- let exit = !self . handle_timer_event ( c) . await . unwrap ( ) ;
116
-
117
- // Exit the loop if component asks for it.
118
- if exit
119
- {
120
- break ;
112
+
113
+ // Inverse the control of breaking out of loop, let the component decide this by returning ContinueTimer enum.
114
+ let exit_condition = self . handle_timer_event ( c) . await . unwrap ( ) ;
115
+
116
+ // Exit the loop if the component asks for it.
117
+ match exit_condition {
118
+ ContinueTimer :: True => continue ,
119
+ ContinueTimer :: False => break ,
121
120
}
122
121
}
123
122
} ) ;
@@ -129,7 +128,7 @@ impl TriggerExecutor for TimerTrigger {
129
128
}
130
129
131
130
impl TimerTrigger {
132
- async fn handle_timer_event ( & self , component_id : & str ) -> anyhow:: Result < bool > {
131
+ async fn handle_timer_event ( & self , component_id : & str ) -> anyhow:: Result < ContinueTimer > {
133
132
// Load the guest...
134
133
let ( instance, mut store) = self . engine . prepare_instance ( component_id) . await ?;
135
134
let EitherInstance :: Component ( instance) = instance else {
You can’t perform that action at this time.
0 commit comments