ReactiveProperty
struct · ferroui-reactiveui · v0.5.0
struct ReactiveProperty<T: Clone + PartialEq + 'static>
A property that holds a value, notifies its changes to its observers and through
PropertyChanged, and validates it (ReactiveProperty<T>).
The handle is shared: clones are the same property, as the object of .NET is a reference.
Subscribers receive the current value when they subscribe (unless the property skips it), then
each change, on the scheduler of the property. Validators turn the values into errors, which
the property aggregates and reports through INotifyDataErrorInfo, HasErrors and
observe_error_changed. Disposing the property ends its
subscriptions.
Deviation (DEVIATIONS.md, ReactiveUI): upstream derives the class from ReactiveObject. The
handle holds a ReactiveObject and implements IReactiveObject itself, so it raises its
PropertyChanged and PropertyChanging events as a reactive object does.
Deviation (DEVIATIONS.md, ReactiveUI): upstream's constructors and its Create factories take
the same arguments; both are the constructors here. A value of a type with no "null" is held as
it is, and ReactiveProperty<Option<T>> holds an optional value.
use ferroui_reactive::concurrency::ImmediateScheduler;
use ferroui_reactive::ObservableExt;
use ferroui_reactiveui::ReactiveProperty;
use std::cell::RefCell;
use std::rc::Rc;
let name = ReactiveProperty::with_scheduler(String::new(), Some(ImmediateScheduler::instance()), false, false)
.add_validation_error(|name: String| if name.is_empty() { Some("required") } else { None });
let seen = Rc::new(RefCell::new(Vec::new()));
let log = seen.clone();
let _subscription = name.subscribe_next(move |value| log.borrow_mut().push(value));
assert!(name.has_errors());
name.set_value("Ada".to_owned());
assert!(!name.has_errors());
assert_eq!(*seen.borrow(), vec![String::new(), "Ada".to_owned()]);
Members
newwith_valuewith_optionswith_scheduleris_disposedvalueset_valuehas_errorsobserve_error_changedobserve_has_errorsadd_validation_error_observableadd_validation_error_observable_withadd_validation_erroradd_validation_error_withadd_validation_error_asyncadd_validation_error_async_withdisposecheck_validationrefreshget_errorserrors_changed
new
fn new() -> Self
A property of the default value, which notifies on the task pool scheduler, gives the
current value to subscribers and skips the values equal to the last one
(ReactiveProperty(), Create()).
with_value
fn with_value(initial_value: T) -> Self
A property of initial_value, with the defaults of new
(ReactiveProperty(T), Create(T)).
with_options
fn with_options(initial_value: T, skip_current_value_on_subscribe: bool, allow_duplicate_values: bool) -> Self
A property of initial_value that notifies on the task pool scheduler
(ReactiveProperty(T, bool, bool), Create(T, bool, bool)): with
skip_current_value_on_subscribe, a subscriber does not receive the current value when it
subscribes; with allow_duplicate_values, setting a value equal to the current one notifies
it again.
with_scheduler
fn with_scheduler(initial_value: T, scheduler: Option<Rc<dyn IScheduler>>, skip_current_value_on_subscribe: bool, allow_duplicate_values: bool) -> Self
A property of initial_value that notifies on scheduler, or on the task pool scheduler
when it is None (ReactiveProperty(T, ISequencer?, bool, bool),
Create(T, ISequencer, bool, bool)).
is_disposed
fn is_disposed(self: &Self) -> bool
Whether the property was disposed (IsDisposed).
value
fn value(self: &Self) -> T
The value (Value { get; }).
set_value
fn set_value(self: &Self, value: T)
Sets the value (Value { set; }): a value equal to the current one notifies it again only
when the property allows duplicate values; another one is stored, validated, notified to
the subscribers and raises PropertyChanged for Value.
has_errors
fn has_errors(self: &Self) -> bool
Whether the property has validation errors (HasErrors).
observe_error_changed
fn observe_error_changed(self: &Self) -> Rc<dyn IObservable<Option<DataErrors>>>
The errors of the property each time they change, starting with the current ones
(ObserveErrorChanged); None when it has none. It completes when the property is
disposed.
observe_has_errors
fn observe_has_errors(self: &Self) -> Rc<dyn IObservable<bool>>
Whether the property has validation errors, each time its errors change, starting with
the current state (ObserveHasErrors).
add_validation_error_observable
fn add_validation_error_observable<R: IntoValidationErrors + 'static>(self: &Self, validator: impl Fn(Rc<dyn IObservable<T>>) -> Rc<dyn IObservable<R>> + 'static) -> Self
Adds a validator that reads the values as a sequence and reports their errors as a
sequence (AddValidationError(Func<IObservable<T>, IObservable<IEnumerable?>>) and its
string form); the current value is validated at once. Returns the property, to chain the
calls.
add_validation_error_observable_with
fn add_validation_error_observable_with<R: IntoValidationErrors + 'static>(self: &Self, validator: impl Fn(Rc<dyn IObservable<T>>) -> Rc<dyn IObservable<R>> + 'static, ignore_initial_error: bool) -> Self
Adds a validator that reads the values as a sequence and reports their errors as a
sequence; with ignore_initial_error, the current value is not validated
(AddValidationError(Func<IObservable<T>, IObservable<IEnumerable?>>, bool) and its
string form). The errors of all the validators are aggregated, and ErrorsChanged is
raised each time they are reported.
add_validation_error
fn add_validation_error<R: IntoValidationErrors + 'static>(self: &Self, validator: impl Fn(T) -> R + 'static) -> Self
Adds a validator of each value (AddValidationError(Func<T, IEnumerable?>) and
AddValidationError(Func<T, string?>)); the current value is validated at once.
add_validation_error_with
fn add_validation_error_with<R: IntoValidationErrors + 'static>(self: &Self, validator: impl Fn(T) -> R + 'static, ignore_initial_error: bool) -> Self
Adds a validator of each value; with ignore_initial_error, the current value is not
validated (AddValidationError(Func<T, IEnumerable?>, bool) and
AddValidationError(Func<T, string?>, bool)).
add_validation_error_async
fn add_validation_error_async<R: IntoValidationErrors + Clone + 'static, Fut: Future<Output = Result<R, ObservableError>> + 'static>(self: &Self, validator: impl Fn(T) -> Fut + 'static) -> Self
Adds an asynchronous validator of each value (AddValidationError(Func<T, Task<IEnumerable?>>)
and AddValidationError(Func<T, Task<string?>>)); the current value is validated at once.
The validator gives a future of a Result<R, ObservableError>, which runs as a task of the
thread (ferroui_system::task::spawn_local).
add_validation_error_async_with
fn add_validation_error_async_with<R: IntoValidationErrors + Clone + 'static, Fut: Future<Output = Result<R, ObservableError>> + 'static>(self: &Self, validator: impl Fn(T) -> Fut + 'static, ignore_initial_error: bool) -> Self
Adds an asynchronous validator of each value; with ignore_initial_error, the current
value is not validated (AddValidationError(Func<T, Task<IEnumerable?>>, bool) and
AddValidationError(Func<T, Task<string?>>, bool)). The results of the tasks are reported
as they come; a task that fails, or a validator that panics, ends the validation of the
property, as its error goes to the validation pipeline.
dispose
fn dispose(self: &Self)
Ends the subscriptions of the property and completes its sequences (Dispose). A second
call does nothing.
check_validation
fn check_validation(self: &Self)
Validates the current value again (CheckValidation).
refresh
fn refresh(self: &Self)
Notifies the current value again, to the validators and to the subscribers, even when it
did not change, and raises PropertyChanged for Value (Refresh).
get_errors
fn get_errors(self: &Self, _property_name: Option<&str>) -> Option<DataErrors>
The errors of the property, whatever property_name is (GetErrors(string?)); None when
it has none. INotifyDataErrorInfo::get_errors gives an empty list instead.
errors_changed
fn errors_changed(self: &Self) -> &Event<DataErrorsChangedEventArgs>
The event raised each time the validators report errors (ErrorsChanged).
Trait implementations
SendSyncFreezeUnsafeUnpinUnpinUnwindSafeRefUnwindSafeIReactiveObjectExtBorrow<T>BorrowMut<T>CloneToUninitInto<U>From<T>TryInto<U>TryFrom<U>AnyToOwnedSubscribeGuard<T>ClonePartialEqDebugDefaultIObservable<T>ObservableExt<T>IDisposableICancelableINotifyPropertyChangedINotifyPropertyChangingIEnableLoggerIReactiveObjectIHandleObservableErrorsINotifyDataErrorInfoIReactiveProperty<T>ReactivePropertyMixins
Availability
Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.