Crate observable_property

Source
Expand description

§Observable Property

A thread-safe observable property implementation for Rust that allows you to observe changes to values across multiple threads.

§Features

  • Thread-safe: Uses Arc<RwLock<>> for safe concurrent access
  • Observer pattern: Subscribe to property changes with callbacks
  • Filtered observers: Only notify when specific conditions are met
  • Async notifications: Non-blocking observer notifications with background threads
  • Panic isolation: Observer panics don’t crash the system
  • Type-safe: Generic implementation works with any Clone + Send + Sync type

§Quick Start

use observable_property::ObservableProperty;
use std::sync::Arc;

// Create an observable property
let property = ObservableProperty::new(42);

// Subscribe to changes
let observer_id = property.subscribe(Arc::new(|old_value, new_value| {
    println!("Value changed from {} to {}", old_value, new_value);
})).unwrap();

// Change the value (triggers observer)
property.set(100).unwrap();

// Unsubscribe when done
property.unsubscribe(observer_id).unwrap();

§Multi-threading Example

use observable_property::ObservableProperty;
use std::sync::Arc;
use std::thread;

let property = Arc::new(ObservableProperty::new(0));
let property_clone = property.clone();

// Subscribe from one thread
property.subscribe(Arc::new(|old, new| {
    println!("Value changed: {} -> {}", old, new);
})).unwrap();

// Modify from another thread
thread::spawn(move || {
    property_clone.set(42).unwrap();
}).join().unwrap();

Structs§

ObservableProperty
A thread-safe observable property that notifies observers when its value changes

Enums§

PropertyError
Errors that can occur when working with ObservableProperty

Type Aliases§

Observer
Function type for observers that get called when property values change
ObserverId
Unique identifier for registered observers