6
6
//-----------------------------------------------------------------------
7
7
8
8
using Akka . Actor ;
9
- using Akka . Actor . Internal ;
10
9
using Akka . Annotations ;
11
- using Akka . Dispatch ;
12
- using Akka . Util . Internal ;
13
10
14
11
namespace Akka . Event
15
12
{
@@ -26,7 +23,7 @@ namespace Akka.Event
26
23
/// watching a few actors too much - we opt for the 2nd choice here.
27
24
/// </summary>
28
25
[ InternalApi ]
29
- class EventStreamUnsubscriber : ActorBase
26
+ internal class EventStreamUnsubscriber : ActorBase
30
27
{
31
28
private readonly EventStream _eventStream ;
32
29
private readonly bool _debug ;
@@ -45,140 +42,83 @@ public EventStreamUnsubscriber(EventStream eventStream, ActorSystem system, bool
45
42
_debug = debug ;
46
43
47
44
}
48
-
49
- /// <summary>
50
- /// TBD
51
- /// </summary>
52
- /// <param name="message">TBD</param>
53
- /// <returns>TBD</returns>
45
+
54
46
protected override bool Receive ( object message )
55
47
{
56
- return message . Match ( ) . With < Register > ( register =>
48
+ switch ( message )
57
49
{
58
- if ( _debug )
59
- _eventStream . Publish ( new Debug ( this . GetType ( ) . Name , GetType ( ) ,
60
- string . Format ( "watching {0} in order to unsubscribe from EventStream when it terminates" , register . Actor ) ) ) ;
61
- Context . Watch ( register . Actor ) ;
62
- } ) . With < UnregisterIfNoMoreSubscribedChannels > ( unregister =>
63
- {
64
- if ( _debug )
65
- _eventStream . Publish ( new Debug ( this . GetType ( ) . Name , GetType ( ) ,
66
- string . Format ( "unwatching {0} since has no subscriptions" , unregister . Actor ) ) ) ;
67
- Context . Unwatch ( unregister . Actor ) ;
68
- } ) . With < Terminated > ( terminated =>
69
- {
70
- if ( _debug )
71
- _eventStream . Publish ( new Debug ( this . GetType ( ) . Name , GetType ( ) ,
72
- string . Format ( "unsubscribe {0} from {1}, because it was terminated" , terminated . Actor , _eventStream ) ) ) ;
73
- _eventStream . Unsubscribe ( terminated . Actor ) ;
74
- } )
75
- . WasHandled ;
50
+ case Register register :
51
+ {
52
+ if ( _debug )
53
+ _eventStream . Publish ( new Debug ( GetType ( ) . Name , GetType ( ) ,
54
+ $ "watching { register . Actor } in order to unsubscribe from EventStream when it terminates") ) ;
55
+ Context . Watch ( register . Actor ) ;
56
+ break ;
57
+ }
58
+ case UnregisterIfNoMoreSubscribedChannels unregister :
59
+ {
60
+ if ( _debug )
61
+ _eventStream . Publish ( new Debug ( GetType ( ) . Name , GetType ( ) ,
62
+ $ "unwatching { unregister . Actor } since has no subscriptions") ) ;
63
+ Context . Unwatch ( unregister . Actor ) ;
64
+ break ;
65
+ }
66
+ case Terminated terminated :
67
+ {
68
+ if ( _debug )
69
+ _eventStream . Publish ( new Debug ( GetType ( ) . Name , GetType ( ) ,
70
+ $ "unsubscribe { terminated . ActorRef } from { _eventStream } , because it was terminated") ) ;
71
+ _eventStream . Unsubscribe ( terminated . ActorRef ) ;
72
+ break ;
73
+ }
74
+ default :
75
+ return false ;
76
+ }
77
+
78
+ return true ;
76
79
}
77
80
78
- /// <summary>
79
- /// TBD
80
- /// </summary>
81
81
protected override void PreStart ( )
82
82
{
83
83
if ( _debug )
84
- _eventStream . Publish ( new Debug ( this . GetType ( ) . Name , GetType ( ) ,
84
+ _eventStream . Publish ( new Debug ( GetType ( ) . Name , GetType ( ) ,
85
85
string . Format ( "registering unsubscriber with {0}" , _eventStream ) ) ) ;
86
- _eventStream . InitUnsubscriber ( Self , _system ) ;
87
86
}
88
87
89
88
/// <summary>
90
- /// TBD
89
+ /// INTERNAL API
90
+ ///
91
+ /// Registers a new subscriber to be death-watched and automatically unsubscribed.
91
92
/// </summary>
92
93
internal class Register
93
94
{
94
- /// <summary>
95
- /// TBD
96
- /// </summary>
97
- /// <param name="actor">TBD</param>
98
95
public Register ( IActorRef actor )
99
96
{
100
97
Actor = actor ;
101
98
}
102
99
103
100
/// <summary>
104
- /// TBD
105
- /// </summary>
106
- public IActorRef Actor { get ; private set ; }
107
- }
108
-
109
-
110
- /// <summary>
111
- /// TBD
112
- /// </summary>
113
- internal class Terminated
114
- {
115
- /// <summary>
116
- /// TBD
117
- /// </summary>
118
- /// <param name="actor">TBD</param>
119
- public Terminated ( IActorRef actor )
120
- {
121
- Actor = actor ;
122
- }
123
-
124
- /// <summary>
125
- /// TBD
101
+ /// The actor we're going to deathwatch and automatically unsubscribe
126
102
/// </summary>
127
103
public IActorRef Actor { get ; private set ; }
128
104
}
129
105
130
106
/// <summary>
131
- /// TBD
107
+ /// INTERNAL API
108
+ ///
109
+ /// Unsubscribes an actor that is no longer subscribed and does not need to be death-watched any longer.
132
110
/// </summary>
133
111
internal class UnregisterIfNoMoreSubscribedChannels
134
112
{
135
- /// <summary>
136
- /// TBD
137
- /// </summary>
138
- /// <param name="actor">TBD</param>
139
113
public UnregisterIfNoMoreSubscribedChannels ( IActorRef actor )
140
114
{
141
115
Actor = actor ;
142
116
}
143
117
144
118
/// <summary>
145
- /// TBD
119
+ /// The actor we're no longer going to death watch.
146
120
/// </summary>
147
121
public IActorRef Actor { get ; private set ; }
148
122
}
149
123
}
150
-
151
-
152
-
153
- /// <summary>
154
- /// Provides factory for Akka.Event.EventStreamUnsubscriber actors with unique names.
155
- /// This is needed if someone spins up more EventStreams using the same ActorSystem,
156
- /// each stream gets it's own unsubscriber.
157
- /// </summary>
158
- class EventStreamUnsubscribersProvider
159
- {
160
- private readonly AtomicCounter _unsubscribersCounter = new AtomicCounter ( 0 ) ;
161
- private static readonly EventStreamUnsubscribersProvider _instance = new EventStreamUnsubscribersProvider ( ) ;
162
-
163
-
164
- /// <summary>
165
- /// TBD
166
- /// </summary>
167
- public static EventStreamUnsubscribersProvider Instance
168
- {
169
- get { return _instance ; }
170
- }
171
-
172
- /// <summary>
173
- /// TBD
174
- /// </summary>
175
- /// <param name="system">TBD</param>
176
- /// <param name="eventStream">TBD</param>
177
- /// <param name="debug">TBD</param>
178
- public void Start ( ActorSystemImpl system , EventStream eventStream , bool debug )
179
- {
180
- system . SystemActorOf ( Props . Create < EventStreamUnsubscriber > ( eventStream , system , debug ) . WithDispatcher ( Dispatchers . InternalDispatcherId ) ,
181
- string . Format ( "EventStreamUnsubscriber-{0}" , _unsubscribersCounter . IncrementAndGet ( ) ) ) ;
182
- }
183
- }
184
124
}
0 commit comments