Crates.io | observatory |
lib.rs | observatory |
version | 0.1.7 |
source | src |
created_at | 2021-03-10 20:58:06.074456 |
updated_at | 2021-03-16 17:59:05.836619 |
description | MobX style observables |
homepage | |
repository | https://github.com/joshua-maros/astro |
max_upload_size | |
id | 366933 |
size | 31,906 |
Provides MobX style observables. Example:
use observatory as o;
o::init();
let first_name = o::observable("William");
let last_name = o::observable("Riker");
let nickname = o::observable::<Option<&'static str>>(None);
// A derivation is run the first time it is created, and the guts of the Derivation type will
// detect that the function borrows nickname, first_name, and last_name during that time.
let display_name = o::derivation_with_ptrs!(
first_name, last_name, nickname;
if let Some(name) = *nickname.borrow() {
format!("{}", name)
} else {
format!("{} {}", *first_name.borrow(), *last_name.borrow())
}
);
// Prints "William Riker"
let logger = o::derivation_with_ptrs!(
display_name;
println!("{}", *display_name.borrow())
);
// Prints "Will of Yam Riker"
first_name.set("Will of Yam");
// Prints "Number One"
// After executing this function the library will detect that `display_name` didn't need to
// borrow first_name or last_name to update its value.
nickname.set(Some("Number One"));
// Causes no updates, display_name has automatically unsubscribed from updates to last_name.
last_name.set("Something else");