Crate async_observable

source ·
Expand description

Async & reactive synchronization model to keep multiple async tasks / threads partially synchronized.

§Differentiation From Traditional Asnyc Streams

Important: An observable is not a clonable Stream<T> – versions may be skipped on the receiving side, if it doesnt ask for updates anymore or if updates are published to quickly the receiving observable just retrieves the latest value.

This is a powerful concept since it allows you to just skip the versions which are outdated by a newer version anyway and hence gain some performance advantage through the lazyness implied by this concept. Although the performance aspect is probably unimportant in most usecases it allows you to write simpler code since you dont need to take your position in the stream into account.

§Examples

§Sharing A Counter Between Tasks

use async_std::task::spawn;
use async_observable::Observable;

#[async_std::main]
async fn main() {
    let mut observable = Observable::new(0);
    let mut tasks = vec![];

    for i in 0..10 {
        let mut fork = observable.clone();

        tasks.push(spawn(async move {
            let update = fork.next().await;

            println!(
                "Task {} was notified about updated observable {}",
                i, update
            );
        }));
    }

    observable.publish(1);

    for t in tasks {
        t.await
    }
}

Structs§

  • Wraps a value and lets you fork the state to synchronize it between tasks and threads.