forked from cschladetsch/CsharpFlow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerator.cs
More file actions
executable file
·123 lines (93 loc) · 1.9 KB
/
Generator.cs
File metadata and controls
executable file
·123 lines (93 loc) · 1.9 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
// (C) 2012 Christian Schladetsch. See http://www.schladetsch.net/flow/license.txt for Licensing information.
using System;
using System.Text;
namespace Flow
{
internal abstract class Generator<TR> : Transient, ITypedGenerator<TR>
{
/// <inheritdoc />
public TR Value { get; protected set; }
/// <inheritdoc />
public event GeneratorHandler Suspended;
/// <inheritdoc />
public event GeneratorHandler Resumed;
/// <inheritdoc />
public event GeneratorHandler Stepped;
/// <inheritdoc />
public bool Running { get; private set; }
/// <inheritdoc />
public int StepNumber { get; private set; }
/// <inheritdoc />
public virtual void Step()
{
++StepNumber;
if (Stepped != null)
Stepped(this);
}
/// <inheritdoc />
public virtual void Post()
{
}
/// <inheritdoc />
public void Suspend()
{
if (!Running || !Active)
return;
Running = false;
if (Suspended != null)
Suspended(this);
}
/// <inheritdoc />
public void Resume()
{
if (Running || !Active)
return;
Running = true;
if (Resumed != null)
Resumed(this);
}
/// <inheritdoc />
public void SuspendAfter (ITransient other)
{
if (IsNullOrEmpty(other))
{
Suspend();
return;
}
Resume();
other.Completed += tr => Suspend();
}
/// <inheritdoc />
public bool ResumeAfter(ITransient other)
{
if (IsNullOrEmpty(other))
{
Resume();
return true;
}
Suspend();
other.Completed += tr => Resume();
return true;
}
/// <inheritdoc />
public bool ResumeAfter(TimeSpan span)
{
if (!Active)
return false;
ResumeAfter(Factory.NewTimer(span));
return true;
}
/// <inheritdoc />
public bool SuspendAfter(TimeSpan span)
{
if (!Active)
return false;
SuspendAfter(Factory.NewTimer(span));
return true;
}
internal Generator()
{
Completed += tr => Suspend();
}
}
}