Crate async_change_tracker[][src]

Reactive change notifications using futures.

This is the successor of raii-change-tracker.

The main item of interest is ChangeTracker. Creating a new with ChangeTracker::new(value: T) will take ownership of the value of type T. You can then create a futures::Stream (with get_changes()) that emits a tuple (old_value, new_value) of type (T, T) upon every change to the owned value. The value can be changed with the modify() method of ChangeTracker and read using the as_ref() method from the AsRef trait.

Example

In this example, all the functionality of ChangeTracker is shown. A tokio runtime is used to execute the futures.

extern crate async_change_tracker;
extern crate futures;
extern crate tokio;

use futures::Stream;

// Wrap an integer `with ChangeTracker`
let mut change_tracker = async_change_tracker::ChangeTracker::new( 123 );

// Create an receiver that fires when the value changes
let rx = change_tracker.get_changes(1);

// In this example, upon change, check the old and new value are correct.
let rx_printer = rx.for_each(|(old_value, new_value)| {
    assert_eq!( old_value, 123);
    assert_eq!( new_value, 124);
    // Here in this example, we return error to abort the stream.
    // In normal usage, typically an `Ok` value would be returned.
    futures::future::err(())
});

// Now, check and then change the value.
change_tracker.modify(|scoped_store| {
    assert_eq!(*scoped_store, 123);
    *scoped_store += 1;
});

// Wait until the stream is done. In this example, the stream ends due to
// the error we return in the `for_each` closure above. In normal usage,
// typically the stream would finish for a different reason.
match tokio::runtime::current_thread::block_on_all(rx_printer) {
    Ok(_) => panic!("should not get here"),
    Err(()) => (),
}

// Finally, check that the final value is as expected.
assert!(*change_tracker.as_ref() == 124);

Structs

ChangeTracker

Tracks changes to data. Notifies listeners via a futures::Stream.