pub struct Runtime { /* private fields */ }Expand description
Simple single-threaded futures runtime for Wasm
This is only available if building with the with_async feature flag:
[dependencies]
ark-api = { version = "*", features = ["with_async"] }§Usage
To use this runtime to run async code call the spawn method on a future and
then call poll on every frame.
If you are writing an applet you may want to create a new Runtime in your
new method and call poll on every frame in the update method.
struct Module {
runtime: Runtime,
channel: Receiver<String>,
}
impl Applet for Module {
fn new() -> Self {
let mut runtime = Runtime::new();
// Call an async function to get a future
let future = some_async_function();
// Run the future on the runtime, getting back a channel for the final result
let channel = runtime.spawn(future);
Self { channel, runtime }
}
fn update(&mut self) {
// Poll the runtime to run the future
self.runtime.poll();
// Check the channel to see if the future has resolved yet
match self.channel.try_recv() {
Ok(Some(value)) => println!("The future resolved returning {}", value),
_ => println!("Still waiting for the future"),
};
}
}Here we are adding the future to the runtime with the spawn method in the
new method, but futures can be added to the runtime at any point in your
Ark module.
Implementations§
Source§impl Runtime
impl Runtime
Sourcepub fn poll(&mut self)
pub fn poll(&mut self)
Poll the runtime to update futures. This should be called on every frame until all futures managed by the runtime have resolved.
Sourcepub fn spawn<T: Send + 'static>(
&mut self,
future: impl Future<Output = T> + 'static + Send,
) -> Receiver<T>
pub fn spawn<T: Send + 'static>( &mut self, future: impl Future<Output = T> + 'static + Send, ) -> Receiver<T>
Spawn a future, returning a channel that will receive the result of the future once it resolves.
Ensure to use .try_recv rather than .recv when checking if the
channel contains the result of the future as .recv will block until the
future resolves, resulting in poor player experience and performance.
Sourcepub fn block_on<T>(&mut self, future: impl Future<Output = T>) -> T
pub fn block_on<T>(&mut self, future: impl Future<Output = T>) -> T
Drives a future to completion and returns the result.
Note this function is asynchronous and will block until the future has resolved. Using this function in your game’s update loop will likely result in poor player experience and performance.