-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimeout.rs
54 lines (48 loc) · 1.57 KB
/
timeout.rs
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
//! Timeout example - this demonstrates running one task with a timeout continuously.
use async_std::{future::pending, task::sleep};
use bevy::{app::PanicHandlerPlugin, log::LogPlugin, prelude::*};
use bevy_async_task::{AsyncTask, AsyncTaskRunner, Duration, TaskError};
use std::task::Poll;
fn system_does_timeout(mut task_executor: AsyncTaskRunner<'_, ()>) {
if task_executor.is_idle() {
let timeout_task = AsyncTask::new_with_timeout(Duration::from_secs(1), pending());
task_executor.start(timeout_task);
info!("Started A!");
}
match task_executor.poll() {
Poll::Ready(Err(TaskError::Timeout(_))) => {
info!("Timeout on A!");
}
Poll::Pending => {
// Waiting...
}
_ => unreachable!(),
}
}
fn system_doesnt_timeout(mut task_executor: AsyncTaskRunner<'_, u32>) {
if task_executor.is_idle() {
let timeout_task = AsyncTask::new_with_timeout(Duration::from_secs(10), async {
sleep(Duration::from_secs(2)).await;
5
});
task_executor.start(timeout_task);
info!("Started B!");
}
match task_executor.poll() {
Poll::Ready(Ok(v)) => {
info!("Received B: {v}!");
}
Poll::Pending => {
// Waiting...
}
_ => unreachable!(),
}
}
/// Entry point
pub fn main() {
App::new()
.add_plugins((MinimalPlugins, LogPlugin::default(), PanicHandlerPlugin))
.add_systems(Update, system_doesnt_timeout)
.add_systems(Update, system_does_timeout)
.run();
}