Skip to content

Instantly share code, notes, and snippets.

@danielmoore
Created May 8, 2011 17:43
Show Gist options
  • Save danielmoore/961532 to your computer and use it in GitHub Desktop.
Save danielmoore/961532 to your computer and use it in GitHub Desktop.
Testing light compile for INPC to RX
public static class NotifyPropertyChangedExtensions
{
public static IObservable<TProp> GetPropertyChanges<TSource, TProp>(this TSource source, Expression<Func<TSource, TProp>> propertySelector, bool lightCompile = true)
where TSource : INotifyPropertyChanged
{
var propertyName = GetPropertyName(propertySelector);
return Observable
.FromEvent<PropertyChangedEventHandler, PropertyChangedEventArgs>(
h => new PropertyChangedEventHandler((s, e) => h(e)),
h => source.PropertyChanged += h,
h => source.PropertyChanged -= h)
.Where(e => string.Equals(propertyName, e.PropertyName, StringComparison.Ordinal))
.Let(o =>
{
var selector = lightCompile ? propertySelector.LightCompile() : propertySelector.Compile();
return o.Select(e => selector(source));
});
}
}
[TestClass]
public class NotifyPropertyChangedExtensionTest
{
[TestMethod]
public void TimeTestLightCompile()
{
const int IterCount = 10000;
var testObj = new TestBindableType();
var stream = testObj
.GetPropertyChanges(t => t.MyValue, lightCompile: true)
.Select(x => new Unit())
.StartWith(new Unit())
.Timestamp()
.Select(t => t.Timestamp.Ticks);
using (stream.Subscribe(Console.WriteLine))
{
for (int i = 0; i < IterCount; i++)
testObj.MyValue = i.ToString();
}
}
[TestMethod]
public void TimeTestCompile()
{
const int IterCount = 10000;
var testObj = new TestBindableType();
var stream = testObj.GetPropertyChanges(t => t.MyValue, lightCompile: false)
.Select(x => new Unit())
.StartWith(new Unit())
.Timestamp()
.Select(t => t.Timestamp.Ticks);
using (stream.Subscribe(Console.WriteLine))
{
for (int i = 0; i < IterCount; i++)
testObj.MyValue = i.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment