forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariadicTemplates.tt
More file actions
184 lines (170 loc) · 8.73 KB
/
VariadicTemplates.tt
File metadata and controls
184 lines (170 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<#@ 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;
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 = 12;
#>
namespace ReactiveUI
{
public static class WhenAnyMixin
{
/// <summary>
/// WhenAnyValue allows you to observe whenever the value of a
/// property on an object has 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> WhenAnyValue<TSender, TRet>(this TSender This,
Expression<Func<TSender, TRet>> property1)
{
return This.WhenAny(property1, (IObservedChange<TSender, TRet> c1) => c1.Value);
}
<# 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 valuePropertyParams = String.Join(", ", Enumerable.Range(1, length).Select(x => String.Format("property{0}", x))); #>
<# string valueSelectorParams = String.Join(", ", Enumerable.Range(1, length).Select(x => "c" + x)); #>
<# string valueSelectorArgs = String.Join(", ", Enumerable.Range(1, length).Select(x => String.Format("c{0}.Value", x))); #>
<# string dynamicSelectorTypeParams = String.Join(", ", templParams.Select(x => "IObservedChange<TSender, object>")); #>
<# string selectorCall = "selector(" + String.Join(", ", Enumerable.Range(1, length).Select(x => "islot" + x.ToString())) + ")"; #>
<# if (length != 1 && length <= 7) { #>
/// <summary>
/// WhenAnyValue allows you to observe whenever the value of 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<Tuple<<#= String.Join(",", templParams) #>>> WhenAnyValue<TSender, <#= String.Join(",", templParams) #>>(this TSender This,
<# for(int i=1; i <= length; i++) { #>
Expression<Func<TSender, T<#=i#>>> property<#=i#><# if (i != length) { #>,<# } #>
<# } #>)
{
return This.WhenAny(<#= valuePropertyParams #>,
(<#= valueSelectorParams #>) =>
Tuple.Create(<#= valueSelectorArgs #>));
}
<# } #>
/// <summary>
/// WhenAnyValue allows you to observe whenever the value of 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> WhenAnyValue<TSender, TRet, <#= String.Join(",", templParams) #>>(this TSender This,
<# for(int i=1; i <= length; i++) { #>
Expression<Func<TSender, T<#=i#>>> property<#=i#>,
<# } #>
Func<<#= String.Join(",", templParams) #>, TRet> selector)
{
return This.WhenAny(<#= valuePropertyParams #>,
(<#= valueSelectorParams #>) =>
selector(<#= valueSelectorArgs #>));
}
/// <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)
{
<# if (length == 1){ #>
return This.ObservableForProperty(property<#=1#>, false, false).Select(selector);
<# }else{ #>
return Observable.CombineLatest(
<# for(int i=1; i <= length; i++) { #>
This.ObservableForProperty(property<#=i#>, false, false),
<# } #>
selector
);
<# } #>
}
/// <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> WhenAnyDynamic<TSender, TRet>(this TSender This,
<# for(int i=1; i <= length; i++) { #>
Expression property<#=i#>,
<# } #>
Func<<#= dynamicSelectorTypeParams #>, TRet> selector)
{
<# if (length == 1){ #>
return ReactiveNotifyPropertyChangedMixin
.SubscribeToExpressionChain<TSender,object>(This, property<#=1#>, false, false).Select(selector);
<# }else{ #>
return Observable.CombineLatest(
<# for(int i=1; i <= length; i++) { #>
ReactiveNotifyPropertyChangedMixin
.SubscribeToExpressionChain<TSender,object>(This, property<#=i#>, false, false),
<# } #>
selector
);
<# } #>
}
<# } #>
}
public static class WhenAnyObservableMixin
{
public static IObservable<TRet> WhenAnyObservable<TSender, TRet>(this TSender This, Expression<Func<TSender, IObservable<TRet>>> obs1)
{
return This.WhenAny(obs1, x => x.Value.EmptyIfNull()).Switch();
}
<# for(int length=2; length <= maxFuncLength; length++) { #>
<# string paramsStr = String.Join(", ", Enumerable.Range(1, length).Select(x => "Expression<Func<TSender, IObservable<TRet>>> obs" + x.ToString())); #>
<# string varsStr = String.Join(", ", Enumerable.Range(1, length).Select(x => "obs" + x.ToString())); #>
<# string valsStr = String.Join(", ", Enumerable.Range(1, length).Select(x => "o" + x.ToString() + ".Value.EmptyIfNull()")); #>
public static IObservable<TRet> WhenAnyObservable<TSender, TRet>(this TSender This, <#= paramsStr #>)
{
return This.WhenAny(<#= varsStr #>, (<#=varsStr.Replace("obs", "o")#>) => new[] {<#= valsStr #>})
.Select(x => x.Merge()).Switch();
}
<# } #>
<# for(int length=2; length <= maxFuncLength; length++) { #>
<# var templParams = Enumerable.Range(1, length).Select(x => "T" + x.ToString()); #>
<# string varsStr = String.Join(", ", Enumerable.Range(1, length).Select(x => "obs" + x.ToString())); #>
<# string valsStr = String.Join(", ", Enumerable.Range(1, length).Select(x => "o" + x.ToString() + ".Value.EmptyIfNull()")); #>
<# string selectorTypeParams = String.Join(", ", templParams); #>
public static IObservable<TRet> WhenAnyObservable<TSender, TRet, <#= String.Join(",", templParams) #>>(this TSender This,
<# for(int i=1; i <= length; i++) { #>
Expression<Func<TSender, IObservable<T<#=i#>>>> obs<#=i#>,
<# } #>
Func<<#= selectorTypeParams #>, TRet> selector)
{
return This.WhenAny(<#= varsStr #>, (<#=varsStr.Replace("obs", "o")#>) => Observable.CombineLatest(<#= valsStr #>, selector))
.Switch();
}
<# } #>
}
internal static class ObservableExtensions
{
public static IObservable<T> EmptyIfNull<T>(this IObservable<T> @this)
{
return @this ?? Observable<T>.Empty;
}
}
}