-
Notifications
You must be signed in to change notification settings - Fork 27
/
DelaySubscription.cs
60 lines (56 loc) · 2.3 KB
/
DelaySubscription.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
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Xml;
using System.Xml.Serialization;
namespace Bonsai.Shaders
{
/// <summary>
/// Represents an operator that delays subscription to the observable sequence
/// by the specified time interval, using the render loop scheduler.
/// </summary>
[Description("Delays subscription to the observable sequence by the specified time interval, using the render loop scheduler.")]
public class DelaySubscription : Combinator
{
static readonly UpdateFrame updateFrame = new UpdateFrame();
/// <summary>
/// Gets or sets the time interval by which to delay subscription to the
/// sequence.
/// </summary>
[XmlIgnore]
[Description("The time interval by which to delay subscription to the sequence.")]
public TimeSpan DueTime { get; set; }
/// <summary>
/// Gets or sets an XML representation of the due time for serialization.
/// </summary>
[Browsable(false)]
[XmlElement(nameof(DueTime))]
[EditorBrowsable(EditorBrowsableState.Never)]
public string DueTimeXml
{
get { return XmlConvert.ToString(DueTime); }
set { DueTime = XmlConvert.ToTimeSpan(value); }
}
/// <summary>
/// Delays subscription to an observable sequence by the specified time
/// interval, using the render loop scheduler.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements in the <paramref name="source"/> sequence.
/// </typeparam>
/// <param name="source">
/// The source sequence to delay subscription for.
/// </param>
/// <returns>
/// The time-shifted sequence, where subscription is delayed by the
/// specified time interval, using the render loop scheduler.
/// </returns>
public override IObservable<TSource> Process<TSource>(IObservable<TSource> source)
{
var dueTime = DueTime.TotalSeconds;
var elapsedTime = updateFrame.Generate().Scan(0.0, (elapsed, evt) => elapsed + evt.TimeStep.ElapsedTime);
return elapsedTime.FirstAsync(elapsed => elapsed > dueTime).SelectMany(x => source);
}
}
}