https://github.com/zahash/reactivate
Thread Safe Reactive Data Structure. Made with โค๏ธ for ๐ฆ
https://github.com/zahash/reactivate
observer-pattern reactive reactive-programming rust thread threads threadsafe
Last synced: about 1 month ago
JSON representation
Thread Safe Reactive Data Structure. Made with โค๏ธ for ๐ฆ
- Host: GitHub
- URL: https://github.com/zahash/reactivate
- Owner: zahash
- License: mit
- Created: 2023-07-13T12:15:32.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-25T17:58:30.000Z (about 1 year ago)
- Last Synced: 2025-04-13T00:48:39.267Z (about 2 months ago)
- Topics: observer-pattern, reactive, reactive-programming, rust, thread, threads, threadsafe
- Language: Rust
- Homepage: https://crates.io/crates/reactivate
- Size: 49.8 KB
- Stars: 41
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
โโโโโโโ โโโโโโโโ โโโโโโ โโโโโโโโโโโโโโโโโโโโโโ โโโ โโโโโโ โโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโ โโโโโโโโโโโ โโโ โโโโโโ โโโโโโโโโโโ โโโ โโโโโโ
โโโโโโโโโโโโโโ โโโโโโโโโโโ โโโ โโโโโโโ โโโโโโโโโโโโ โโโ โโโโโโ
โโโ โโโโโโโโโโโโโโ โโโโโโโโโโโ โโโ โโโ โโโโโโโ โโโ โโโ โโโ โโโโโโโโ
โโโ โโโโโโโโโโโโโโ โโโ โโโโโโโ โโโ โโโ โโโโโ โโโ โโโ โโโ โโโโโโโโ
------------------------------------------------------------------------------
Thread Safe Reactive Data Structure. Made with โค๏ธ for ๐ฆ[](https://crates.io/crates/reactivate)
[](https://opensource.org/licenses/MIT)## ๐ Installation
include it in your `Cargo.toml` under `[dependencies]`
```toml
reactivate = { version = "*", features = ["threadsafe"] }
```## ๐งโ๐ป Usage examples
### ๐๏ธ Construction
```rust
use reactivate::Reactive;fn main() {
let r = Reactive::new(10);println!("{:?}", r); // Reactive(10)
println!("{:?}", r.value()); // 10
}
```### ๐ฅ Derive
```rust
use reactivate::Reactive;fn main() {
let r = Reactive::new(10);
let d = r.derive(|val| val + 5);println!("{:?}", r); // Reactive(10)
println!("{:?}", d); // Reactive(15)
}
```### โจ Update
```rust
use reactivate::Reactive;fn main() {
let r = Reactive::new(10);
let d = r.derive(|val| val + 5);r.update(|_| 20);
println!("{:?}", r); // Reactive(20)
println!("{:?}", d); // Reactive(25)
}
```### โก Update Inplace
```rust
use reactivate::Reactive;fn main() {
let r = Reactive::new(vec![1, 2, 3]);
let d = r.derive(|nums| nums.iter().sum::());r.update_inplace(|nums| {
nums.push(4);
nums.push(5);
nums.push(6);
});println!("{:?}", r); // Reactive([1, 2, 3, 4, 5, 6])
println!("{:?}", d); // Reactive(21)
}
```### ๐ค Merge and Derive
```rust
use reactivate::{Merge, Reactive};fn main() {
let a = Reactive::new(String::from("hazash"));
let b = Reactive::new(0);
let d = (&a, &b)
.merge()
.derive(|(a_val, b_val)| a_val.len() + b_val);println!("{:?}", a); // Reactive("hazash")
println!("{:?}", b); // Reactive(0)
println!("{:?}", d); // Reactive(6)b.update(|_| 5);
println!("{:?}", a); // Reactive("hazash")
println!("{:?}", b); // Reactive(5)
println!("{:?}", d); // Reactive(11)a.update(|_| String::from("mouse"));
println!("{:?}", a); // Reactive("mouse")
println!("{:?}", b); // Reactive(5)
println!("{:?}", d); // Reactive(10)
}
```### ๐ Add Observers
```rust
use reactivate::Reactive;
use std::sync::{Arc, Mutex};fn main() {
let r: Reactive = Reactive::default();// Arc> is used to make the vector thread safe
// because Reactive as a whole must be thread safe
let changes: Arc>> = Default::default();r.add_observer({
let changes = changes.clone();
move |val| changes.lock().unwrap().push(val.clone())
});r.update(|_| String::from("a"));
r.update_inplace(|s| {
s.push('b');
});println!("{:?}", r); // Reactive("ab")
println!("{:?}", changes.lock().unwrap().clone()); // ["a", "ab"]
}
```### ๐งต With Threads (features = ["threadsafe"])
```rust
use reactivate::Reactive;
use std::{thread, time::Duration};fn main() {
let r: Reactive = Reactive::default();
let d = r.derive(|s| s.len());let handle = thread::spawn({
let r = r.clone();move || {
for _ in 0..10 {
r.update_inplace(|s| s.push('a'));
thread::sleep(Duration::from_millis(1));
}
}
});for _ in 0..10 {
r.update_inplace(|s| s.push('b'));
thread::sleep(Duration::from_millis(1));
}handle.join().unwrap();
println!("{:?}", r); // Reactive("babababababababababa")
println!("{:?}", d); // Reactive(20)
}
```## ๐ Connect with Us
M. Zahash โ [email protected]
Distributed under the MIT license. See `LICENSE` for more information.
[https://github.com/zahash/](https://github.com/zahash/)
## ๐ค Contribute to Reactivate!
1. Fork it ()
2. Create your feature branch (`git checkout -b feature/fooBar`)
3. Commit your changes (`git commit -am 'Add some fooBar'`)
4. Push to the branch (`git push origin feature/fooBar`)
5. Create a new Pull Request## โค๏ธ Show Some Love!
If you find Reactivate helpful and enjoy using it, consider giving it a [โญ on GitHub!](https://github.com/zahash/reactivate/stargazers) Your star is a gesture of appreciation and encouragement for the continuous improvement of Reactivate.