#future #change #reactive #notify #watch

async-change-tracker

reactive change notifications using futures

8 releases

0.3.5 Jan 15, 2024
0.3.4 Jan 12, 2024
0.3.3 Dec 29, 2023
0.3.2 Jan 31, 2022
0.1.0 Oct 11, 2018

#246 in Asynchronous

Download history 56/week @ 2024-08-23 66/week @ 2024-08-30 54/week @ 2024-09-06 59/week @ 2024-09-13 67/week @ 2024-09-20 118/week @ 2024-09-27 114/week @ 2024-10-04 198/week @ 2024-10-11 175/week @ 2024-10-18 208/week @ 2024-10-25 253/week @ 2024-11-01 160/week @ 2024-11-08 128/week @ 2024-11-15 124/week @ 2024-11-22 397/week @ 2024-11-29 438/week @ 2024-12-06

1,095 downloads per month
Used in bui-backend

MIT/Apache

10KB
58 lines

async-change-tracker

Crates.io Documentation Crate License Dependency status build

Reactive change notifications using futures.

The ChangeTracker<T> type wraps an owned value T. Changes to T are done within a function or closure implementing FnOnce(&mut T). When this returns, any changes are sent to listeners using a futures::Stream.

In slightly more detail, create a ChangeTracker<T> with ChangeTracker::new(value: T). This 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, the functionality of ChangeTracker is shown.

use futures::stream::StreamExt;

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

// Create an receiver that fires when the value changes. The channel size
// is 1, meaning at most one change can be buffered before backpressure
// is applied.
let rx = change_tracker.get_changes(1);

// In this example take a single change and check that the old and new value
// are correct.
let rx_printer = rx.take(1).for_each(|(old_value, new_value)| {
    assert_eq!( old_value, 123);
    assert_eq!( new_value, 124);
    futures::future::ready(())
});

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

// Wait until the stream is done. In this example, the stream ends due to
// the use of `.take(1)` prior to `for_each` above. In normal usage,
// typically the stream would finish for a different reason.
futures::executor::block_on(rx_printer);

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

Testing

To test:

cargo test

License: MIT/Apache-2.0

Dependencies

~1–1.2MB
~20K SLoC