Skip to main content

ReactiveObject

Struct ReactiveObject 

Source
pub struct ReactiveObject { /* private fields */ }
Expand description

The part of a view model that makes it a reactive object (ReactiveObject): its PropertyChanging and PropertyChanged events, and the state of its change notifications (suppression, delay, the Changing, Changed and ThrownExceptions observables), created when first needed.

Deviation (DEVIATIONS.md, ReactiveUI): upstream’s base class is a part that a view model holds as a field, named reactive by convention. The macro reactive_object! writes the traits of a reactive object for the type, and reactive_properties! its notifying properties. The view model is shared as an Rc, whose handle gives the observables with the sender (IReactiveNotifyPropertyChanged).

use ferroui_reactiveui::{reactive_object, IReactiveNotifyPropertyChanged, IReactiveObjectExt, ReactiveObject};
use ferroui_reactive::ObservableExt;
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Default)]
struct Person {
    reactive: ReactiveObject,
    name: RefCell<String>,
}
reactive_object!(Person);

impl Person {
    fn name(&self) -> String {
        self.name.borrow().clone()
    }
    fn set_name(&self, value: String) {
        self.raise_and_set_if_changed(&self.name, value, "Name");
    }
}

let person = Rc::new(Person::default());
let names = Rc::new(RefCell::new(Vec::new()));
let seen = names.clone();
let _subscription = person.changed().subscribe_next(move |change| seen.borrow_mut().push(change.property_name));
person.set_name("Ada".into());
person.set_name("Ada".into());
assert_eq!(person.name(), "Ada");
assert_eq!(*names.borrow(), vec!["Name".to_owned()]);

Implementations§

Source§

impl ReactiveObject

Source

pub fn new() -> Self

The part of a reactive object without handlers or state.

Source

pub fn property_changing(&self) -> &Event<str>

The event raised when a property is about to change (PropertyChanging). It is raised once it has a handler, and follows the suppression and the delay of the notifications.

Source

pub fn property_changed(&self) -> &Event<str>

The event raised when a property has changed (PropertyChanged).

Source

pub fn extension_state(&self) -> Rc<dyn IExtensionState>

The state of the change notifications, created at the first call (GetState).

Source

pub fn subscribe_property_changing_events(&self)

Makes changes raise the PropertyChanging event (SubscribePropertyChangingEvents). A change does it by itself when the event has a handler.

Source

pub fn subscribe_property_changed_events(&self)

Makes changes raise the PropertyChanged event (SubscribePropertyChangedEvents). A change does it by itself when the event has a handler.

Source

pub fn suppress_change_notifications(&self) -> Rc<dyn IDisposable>

Stops all change notifications, the events and the observables, until the result is disposed (SuppressChangeNotifications). The calls nest: the notifications resume when every result is disposed.

Source

pub fn are_change_notifications_enabled(&self) -> bool

Whether change notifications are sent (AreChangeNotificationsEnabled).

Source

pub fn delay_change_notifications(&self) -> Rc<dyn IDisposable>

Keeps the change notifications until the result is disposed, then sends the last one of each property, in the order of those last notifications (DelayChangeNotifications). The calls nest.

Source

pub fn are_change_notifications_delayed(&self) -> bool

Whether change notifications are delayed (AreChangeNotificationsDelayed).

Source

pub fn thrown_exceptions(&self) -> Rc<dyn IObservable<ObservableError>>

The errors of the subscribers of the change observables (ThrownExceptions); the same observable at every call.

Trait Implementations§

Source§

impl AsRef<ReactiveObject> for ReactiveObject

Source§

fn as_ref(&self) -> &ReactiveObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<ReactiveObject> for ReactiveRecord

Source§

fn as_ref(&self) -> &ReactiveObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for ReactiveObject

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ReactiveObject

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.