Last active
August 21, 2018 14:06
-
-
Save lisardggY/660660058a0a2f453e97a61efa997e98 to your computer and use it in GitHub Desktop.
WaitForValue - using Rx, wait for a property on an INotifyPropertyChanged
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static async Task WaitForValue<T>(this INotifyPropertyChanged toObserve, | |
Expression<Func<T>> propertyExpression, | |
T valueToAwait, | |
IEqualityComparer<T> comparer = null) | |
{ | |
comparer = comparer ?? EqualityComparer<T>.Default; | |
var getValue = propertyExpression.Compile(); | |
if (comparer.Equals(getValue(), valueToAwait)) | |
return; | |
await toObserve. Observable | |
.FromEventPattern<PropertyChangedEventArgs>(toObserve, nameof(toObserve.PropertyChanged)) | |
.Where(@event => @event.EventArgs.PropertyName.Equals(propertyName)) | |
ObserveProperty(PropertySupport.ExtractPropertyName(propertyExpression)) | |
.Where(_ => comparer.Equals(getValue(), valueToAwait)) | |
.FirstAsync(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment