forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservedChangedMixin.cs
More file actions
112 lines (102 loc) · 4.59 KB
/
Copy pathObservedChangedMixin.cs
File metadata and controls
112 lines (102 loc) · 4.59 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive.Linq;
using System.Reflection;
namespace ReactiveUI
{
public static class ObservedChangedMixin
{
/// <summary>
/// Returns the current value of a property given a notification that
/// it has changed.
/// </summary>
/// <returns>The current value of the property</returns>
public static TValue GetValue<TSender, TValue>(this IObservedChange<TSender, TValue> This)
{
TValue ret;
if (!This.TryGetValue(out ret)) {
throw new Exception(String.Format("One of the properties in the expression '{0}' was null", This.PropertyName));
}
return ret;
}
/// <summary>
/// Attempts to return the current value of a property given a
/// notification that it has changed. If any property in the
/// property expression is null, false is returned.
/// </summary>
/// <param name="changeValue">The value of the property
/// expression.</param>
/// <returns>True if the entire expression was able to be followed,
/// false otherwise</returns>
public static bool TryGetValue<TSender, TValue>(this IObservedChange<TSender, TValue> This, out TValue changeValue)
{
if (!Equals(This.Value, default(TValue))) {
changeValue = This.Value;
return true;
}
object current = This.Sender;
string fullPropName = This.PropertyName;
return Reflection.TryGetValueForPropertyChain(out changeValue, current, fullPropName.Split('.'));
}
internal static IObservedChange<TSender, TValue> fillInValue<TSender, TValue>(this IObservedChange<TSender, TValue> This)
{
// XXX: This is an internal method because I'm unsafely upcasting,
// but in certain cases it's needed.
var ret = (ObservedChange<TSender, TValue>)This;
var val = default(TValue);
This.TryGetValue(out val);
ret.Value = val;
return ret;
}
/// <summary>
/// Given a fully filled-out IObservedChange object, SetValueToProperty
/// will apply it to the specified object (i.e. it will ensure that
/// target.property == This.GetValue() and "replay" the observed change
/// onto another object)
/// </summary>
/// <param name="target">The target object to apply the change to.</param>
/// <param name="property">The target property to apply the change to.</param>
public static void SetValueToProperty<TSender, TValue, TTarget>(
this IObservedChange<TSender, TValue> This,
TTarget target,
Expression<Func<TTarget, TValue>> property)
{
Reflection.SetValueToPropertyChain(target, Reflection.ExpressionToPropertyNames(property), This.GetValue());
}
/// <summary>
/// Given a stream of notification changes, this method will convert
/// the property changes to the current value of the property.
/// </summary>
/// <returns>An Observable representing the stream of current values of
/// the given change notification stream.</returns>
public static IObservable<TValue> Value<TSender, TValue>(
this IObservable<IObservedChange<TSender, TValue>> This)
{
return This.Select(GetValue);
}
/// <summary>
/// ValueIfNotDefault is similar to Value(), but filters out null values
/// from the stream.
/// </summary>
/// <returns>An Observable representing the stream of current values of
/// the given change notification stream.</returns>
public static IObservable<TValue> ValueIfNotDefault<TSender, TValue>(
this IObservable<IObservedChange<TSender, TValue>> This)
{
return This.Value().Where(x => EqualityComparer<TValue>.Default.Equals(x, default(TValue)) == false);
}
/// <summary>
/// Given a stream of notification changes, this method will convert
/// the property changes to the current value of the property.
/// </summary>
public static IObservable<TRet> Value<TSender, TValue, TRet>(
this IObservable<IObservedChange<TSender, TValue>> This)
{
// XXX: There is almost certainly a non-retarded way to do this
return This.Select(x => (TRet)((object)GetValue(x)));
}
}
}
// vim: tw=120 ts=4 sw=4 et :