Skip to content

Commit

Permalink
Added timer loop exit feature.
Browse files Browse the repository at this point in the history
Signed-off-by: Suneet Nangia <[email protected]>
  • Loading branch information
suneetnangia committed Apr 14, 2023
1 parent a1944a3 commit 609168f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
13 changes: 13 additions & 0 deletions examples/spin-timer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/spin-timer/app-example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ wit_bindgen::generate!({
struct MySpinTimer;

impl SpinTimer for MySpinTimer {
fn handle_timer_request() {
fn handle_timer_request() -> bool {
let text = spin_sdk::config::get("message").unwrap();
println!("{text}");

// Return false if you want to exit the timer loop calling this funtion subsequently.
true
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/spin-timer/spin-timer.wit
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
default world spin-timer {
export handle-timer-request: func()
export handle-timer-request: func() -> bool
}
11 changes: 10 additions & 1 deletion examples/spin-timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ impl TriggerExecutor for TimerTrigger {
loop {
tokio::time::sleep(duration).await;
self.handle_timer_event(c).await.unwrap();

// Inverse the control of breaking out of loop, let the component decide this by returning false.
let exit = !self.handle_timer_event(c).await.unwrap();

// Exit the loop if component asks for it.
if exit
{
break;
}
}
});
}
Expand All @@ -120,7 +129,7 @@ impl TriggerExecutor for TimerTrigger {
}

impl TimerTrigger {
async fn handle_timer_event(&self, component_id: &str) -> anyhow::Result<()> {
async fn handle_timer_event(&self, component_id: &str) -> anyhow::Result<bool> {
// Load the guest...
let (instance, mut store) = self.engine.prepare_instance(component_id).await?;
let EitherInstance::Component(instance) = instance else {
Expand Down

0 comments on commit 609168f

Please sign in to comment.