Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3c11659

Browse files
committedApr 17, 2023
Updated return variant and unused expression.
Signed-off-by: Suneet Nangia <[email protected]>
1 parent 609168f commit 3c11659

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed
 

‎examples/spin-timer/app-example/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ wit_bindgen::generate!({
66
struct MySpinTimer;
77

88
impl SpinTimer for MySpinTimer {
9-
fn handle_timer_request() -> bool {
9+
fn handle_timer_request() -> ContinueTimer {
1010
let text = spin_sdk::config::get("message").unwrap();
1111
println!("{text}");
1212

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
1515
}
1616
}
1717

‎examples/spin-timer/spin-timer.wit

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
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
310
}

‎examples/spin-timer/src/main.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,14 @@ impl TriggerExecutor for TimerTrigger {
109109
let duration = tokio::time::Duration::from_millis(*d * 1000 / speedup);
110110
loop {
111111
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,
121120
}
122121
}
123122
});
@@ -129,7 +128,7 @@ impl TriggerExecutor for TimerTrigger {
129128
}
130129

131130
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> {
133132
// Load the guest...
134133
let (instance, mut store) = self.engine.prepare_instance(component_id).await?;
135134
let EitherInstance::Component(instance) = instance else {

0 commit comments

Comments
 (0)
Please sign in to comment.