# base/wait (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/wait/

Asynchronous waiting. (Wait.seconds and Wait.nextFrame of the original base/wait.js)
- Awaited inside an engine task (Engine::spawn_task, spawn_scene_task). The engine polls tasks once per frame, so
  if not ready yet it wakes itself and checks again the next frame.
- seconds is wall clock time as in the original. (the original setTimeout(seconds * 1000); not multiplied by the time scale (time_scale) and keeps running while paused)
  Measured from when it is called. (the original also sets the setTimeout when seconds is called) performance.now() on the web, std::time on desktop. (web_time::Instant)
  Just as the original setTimeout calls back on the next turn even for 0 seconds, it ends only after at least one engine polling turn has passed since the call. (seconds(0) rests once and ends)
  If awaited after the time has already passed, it ends immediately as in the original. Like the original setTimeout, the time is converted to milliseconds as a WebIDL long (ToInt32), and negatives are 0.
  (NaN and infinity are 0, 2^31 milliseconds or more wraps around modulo 2^32)
- next_frame waits until the next frame like the original (awaiting a requestAnimationFrame Promise). It ends after one engine polling turn has passed since the call.
- There is one polling turn number per thread, and the engine increments it every polling turn. (poll_tasks in core/task.rs)

## WaitSeconds (struct) (Clone, Copy, Debug, Future)

Time wait future. (returned by seconds)

Example: Call it first, do other work, then wait (measured from when it was called)

```rust
use vanilla::{wait, Engine};

fn show_loading(engine: &Engine) {
	engine.spawn_task(move |mut engine_context| async move {
		let minimum_wait: wait::WaitSeconds = wait::seconds(2.0);
		engine_context.with(|engine| engine.set_title("Loading"));
		minimum_wait.await;
		engine_context.with(|engine| engine.set_title("Ready"));
	});
}
```

## NextFrame (struct) (Clone, Copy, Debug, Future)

One-frame wait future. (returned by next_frame)

Example: Wait until the next frame and read the frame number

```rust
use vanilla::{wait, Engine};

fn read_next_frame_number(engine: &Engine) {
	engine.spawn_task(move |mut engine_context| async move {
		let start_frame_number = engine_context.with(|engine| engine.get_frame_number());
		wait::next_frame().await;
		let next_frame_number = engine_context.with(|engine| engine.get_frame_number());
		println!("{} {}", start_frame_number, next_frame_number);
	});
}
```

## Functions and constants

- `pub fn seconds(seconds: f64) -> WaitSeconds` (method, since 0.4.0): Waits asynchronously for a set time. (the original Wait.seconds, wall clock seconds)
  - `seconds` f64: Time to wait. (wall clock seconds, unaffected by the time scale and pausing. After truncating to milliseconds, negatives, NaN and infinity are 0)
  - returns WaitSeconds: A future that ends once that time has passed since the call and at least one engine polling turn has passed. (await it inside an engine task)

  Example: A countdown that changes the window title every second

  ```rust
  use vanilla::{wait, Engine};
  
  fn start_countdown(engine: &Engine) {
  	engine.spawn_task(move |mut engine_context| async move {
  		for count in (1..=3).rev() {
  			engine_context.with(|engine| engine.set_title(&format!("{}", count)));
  			wait::seconds(1.0).await;
  		}
  		engine_context.with(|engine| engine.set_title("Go"));
  	});
  }
  ```

- `pub fn next_frame() -> NextFrame` (method, since 0.4.0): Waits asynchronously for one frame. (the original Wait.nextFrame)
  - returns NextFrame: A future that ends after one engine polling turn has passed since the call. (await it inside an engine task)
