Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 2.5.2
Fetching contributors…

Cannot retrieve contributors at this time

71 lines (58 sloc) 2.839 kb
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core.dll" #>
<#@ import namespace="System.Linq" #>
<#@ output extension=".cs" #>
using System;
using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
<#
// NB: maxFuncLength is 4 on WP7, 12 on every other platform.
// VariadicTemplates_WP7.tt should always be a copy of VariadicTemplates.tt
// except for this section
int maxFuncLength = 4;
#>
namespace ReactiveUI
{
public static class WhenAnyMixin
{
<# for(int length=1; length <= maxFuncLength; length++) { #>
<# var templParams = Enumerable.Range(1, length).Select(x => "T" + x.ToString()); #>
<# string selectorTypeParams = String.Join(", ", templParams.Select(x => String.Format("IObservedChange<TSender, {0}>", x))); #>
<# string selectorCall = "selector(" + String.Join(", ", Enumerable.Range(1, length).Select(x => "islot" + x.ToString())) + ")"; #>
/// <summary>
/// WhenAny allows you to observe whenever one or more properties on an
/// object have changed, providing an initial value when the Observable
/// is set up, unlike ObservableForProperty(). Use this method in
/// constructors to set up bindings between properties that also need an
/// initial setup.
/// </summary>
public static IObservable<TRet> WhenAny<TSender, TRet, <#= String.Join(",", templParams) #>>(this TSender This,
<# for(int i=1; i <= length; i++) { #>
Expression<Func<TSender, T<#=i#>>> property<#=i#>,
<# } #>
Func<<#= selectorTypeParams #>, TRet> selector)
where TSender : IReactiveNotifyPropertyChanged
{
<# for(int i=1; i <= length; i++) { #>
var slot<#=i#> = new ObservedChange<TSender, T<#=i#>>() {
Sender = This,
PropertyName = String.Join(".", RxApp.expressionToPropertyNames(property<#=i#>)),
};
T<#=i#> slot<#=i#>Value = default(T<#=i#>); slot<#=i#>.TryGetValue(out slot<#=i#>Value); slot<#=i#>.Value = slot<#=i#>Value;
IObservedChange<TSender, T<#=i#>> islot<#=i#> = slot<#=i#>;
<# } #>
return Observable.Create<TRet>(subject => {
subject.OnNext(<#= selectorCall #>);
return Observable.Merge(<# for(int i=1; i <= length; i++) { #>
This.ObservableForProperty(property<#=i#>).Do(x => { lock (slot<#=i#>) { islot<#=i#> = x.fillInValue(); } }).Select(x => <#= selectorCall #>)<#= (i==length?"":",") #> <# } #>
).Subscribe(subject);
});
}
<# } #>
}
}
Jump to Line
Something went wrong with that request. Please try again.