/
WorkflowProperty.cs
85 lines (77 loc) · 2.63 KB
/
WorkflowProperty.cs
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
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Xml.Serialization;
namespace Bonsai.Expressions
{
/// <summary>
/// Represents a workflow property.
/// </summary>
[DefaultProperty("Value")]
[Combinator(MethodName = "Generate")]
[XmlType(Namespace = Constants.XmlNamespace)]
[WorkflowElementCategory(ElementCategory.Source)]
public abstract class WorkflowProperty
{
internal WorkflowProperty()
{
}
internal abstract Type PropertyType { get; }
}
/// <summary>
/// Represents a strongly typed workflow property.
/// </summary>
/// <typeparam name="TValue">The type of the property value.</typeparam>
[XmlType(Namespace = Constants.XmlNamespace)]
public class WorkflowProperty<TValue> : WorkflowProperty
{
TValue value;
event Action<TValue> ValueChanged;
/// <summary>
/// Gets or sets the value of the property.
/// </summary>
[Description("The value of the property.")]
public TValue Value
{
get { return value; }
set
{
this.value = value;
OnValueChanged(value);
}
}
internal override Type PropertyType
{
get { return typeof(TValue); }
}
void OnValueChanged(TValue value)
{
ValueChanged?.Invoke(value);
}
/// <summary>
/// Generates an observable sequence that produces a value whenever the
/// workflow property changes, starting with the initial property value.
/// </summary>
/// <returns>An observable sequence of property values.</returns>
public virtual IObservable<TValue> Generate()
{
return Observable
.Defer(() => Observable.Return(value))
.Concat(Observable.FromEvent<TValue>(
handler => ValueChanged += handler,
handler => ValueChanged -= handler));
}
/// <summary>
/// Generates an observable sequence that produces a value whenever the
/// source sequence emits a new element.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">The source sequence used to generate new values.</param>
/// <returns>An observable sequence of property values.</returns>
public IObservable<TValue> Generate<TSource>(IObservable<TSource> source)
{
return source.Select(x => value);
}
}
}