Skip to content

Instantly share code, notes, and snippets.

@lisardggY
Last active August 21, 2018 14:06
Show Gist options
  • Save lisardggY/660660058a0a2f453e97a61efa997e98 to your computer and use it in GitHub Desktop.
Save lisardggY/660660058a0a2f453e97a61efa997e98 to your computer and use it in GitHub Desktop.
WaitForValue - using Rx, wait for a property on an INotifyPropertyChanged
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