forked from cschladetsch/CsharpFlow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransient.cs
More file actions
115 lines (92 loc) · 2 KB
/
Transient.cs
File metadata and controls
115 lines (92 loc) · 2 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
// (C) 2012 Christian Schladetsch. See http://www.schladetsch.net/flow/license.txt for Licensing information.
using System;
namespace Flow
{
internal class Transient : ITransient
{
/// <inheritdoc />
public event TransientHandler Completed;
/// <summary>
/// Occurs when completed, with a reason why.
/// </summary>
public event TransientHandlerReason WhyCompleted;
/// <inheritdoc />
public string Name
{
get
{
return _name;
}
set
{
if (_name == value)
return;
if (NewName != null)
NewName(this, _name, value);
_name = value;
}
}
/// <inheritdoc />
public IKernel Kernel { get; /*internal*/ set; }
/// <inheritdoc />
public IFactory Factory { get { return Kernel.Factory; } }
/// <inheritdoc />
public event NamedHandler NewName;
/// <inheritdoc />
public bool Active { get; private set; }
/// <summary>
/// Return true if the given other transient is either null or does not exist
/// </summary>
/// <returns>
/// True if the given other transient is either null or does not exist
/// </returns>
/// <param name='other'>
/// The transient to consider
/// </param>
public static bool IsNullOrEmpty (ITransient other)
{
return other == null || !other.Active;
}
internal Transient()
{
Active = true;
}
/// <inheritdoc />
public void Complete()
{
if (!Active)
return;
if (Completed != null)
Completed(this);
Active = false;
}
/// <inheritdoc />
public void CompleteAfter(ITransient other)
{
if (!Active)
return;
if (other == null)
return;
if (!other.Active)
{
Complete();
return;
}
other.Completed += tr => CompletedBecause(other);
}
void CompletedBecause(ITransient other)
{
if (!Active)
return;
if (WhyCompleted != null)
WhyCompleted(this, other);
Complete();
}
/// <inheritdoc />
public void CompleteAfter(TimeSpan span)
{
CompleteAfter(Factory.NewTimer(span));
}
private string _name;
}
}