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

Creates new runtime

Poll the runtime to update futures. This should be called on every frame until all futures managed by the runtime have resolved.

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.

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.

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.