made 'block_reentrancy()' extension method for IProperty<T> (blocks reentrancy in value setter)
This commit is contained in:
1 parent
ee498f1ad0
commit
c4f29bd2cf
1 file changed
+28
@@ -47,6 +47,34 @@ namespace mbts.ui.shell {
|
||||
void on_errors_changed(object sender, DataErrorsChangedEventArgs e) =>
|
||||
o.OnNext(vm.HasErrors);
|
||||
});
|
||||
|
||||
public static IProperty<T> block_reentrancy<T>(this IProperty<T> source) =>
|
||||
new GatedPropertyWrapper<T>(source);
|
||||
|
||||
/// Blocks re-entrancy in <see cref="IProperty{T}.Value"/> setter.
|
||||
class GatedPropertyWrapper<T> : IProperty<T> {
|
||||
bool _is_updating;
|
||||
readonly IProperty<T> _source;
|
||||
|
||||
public GatedPropertyWrapper(IProperty<T> source) =>
|
||||
_source = source;
|
||||
|
||||
public event PropertyChange<T> Changing { add => _source.Changing += value; remove => _source.Changing -= value; }
|
||||
public event PropertyChange<T> Changed { add => _source.Changed += value; remove => _source.Changed -= value; }
|
||||
|
||||
public T Value {
|
||||
get => _source.Value;
|
||||
set {
|
||||
if (!_is_updating) {
|
||||
_is_updating = true;
|
||||
_source.Value = value;
|
||||
_is_updating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IDisposable Subscribe(IObserver<(T oldval, T newval)> observer) => _source.Subscribe(observer);
|
||||
}
|
||||
}
|
||||
|
||||
public class ViewModelBase : INotifyPropertyChanged, IRaiseNotifyPropertyChanged
|
||||
|
||||
Reference in new issue
Block a user