Crate observable_property_tokio

Crate observable_property_tokio 

Source
Expand description

§Observable Property with Tokio

A thread-safe, async-compatible observable property implementation for Rust that allows you to observe changes to values using Tokio for asynchronous operations.

§Features

  • Thread-safe: Uses Arc<RwLock<>> for safe concurrent access with optimized locking
  • Observer pattern: Subscribe to property changes with callbacks
  • Filtered observers: Only notify when specific conditions are met
  • Async notifications: Non-blocking observer notifications with Tokio tasks
  • 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_tokio::ObservableProperty;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), observable_property_tokio::PropertyError> {
    // 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);
    }))?;

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

    // For async notification (uses Tokio)
    property.set_async(200).await?;

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

    Ok(())
}

§Multi-threading Example with Tokio

use observable_property_tokio::ObservableProperty;
use std::sync::Arc;
use tokio::task;

#[tokio::main]
async fn main() -> Result<(), observable_property_tokio::PropertyError> {
    let property = Arc::new(ObservableProperty::new(0));
    let property_clone = property.clone();

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

    // Modify from another task
    task::spawn(async move {
        property_clone.set(42)?;
        Ok::<_, observable_property_tokio::PropertyError>(())
    }).await??;

    Ok(())
}

Structs§

BatchConfig
Configuration for batched property updates
BatchedProperty
A batched wrapper around ObservableProperty that reduces notification overhead
ObservableProperty
A thread-safe observable property that notifies observers when its value changes
ObserverId
Unique identifier for registered observers
PropertyConfig
Configuration options for ObservableProperty
PropertyHandle
ShutdownReport
Report generated after a property shutdown operation
Subscription

Enums§

PropertyError
Errors that can occur when working with ObservableProperty

Type Aliases§

Observer
Function type for observers that get called when property values change