1010using Orleans . Streams ;
1111using Orleans . Runtime ;
1212
13+ using Microsoft . Extensions . DependencyInjection ;
14+
1315namespace Orleankka
1416{
1517 using Utility ;
@@ -18,13 +20,20 @@ namespace Orleankka
1820 [ DebuggerDisplay ( "s->{ToString()}" ) ]
1921 public class StreamRef < TItem > : IEquatable < StreamRef < TItem > > , IEquatable < StreamPath >
2022 {
21- [ NonSerialized ]
22- readonly IStreamProvider provider ;
23+ [ NonSerialized ] readonly IStreamProvider provider ;
24+ [ NonSerialized ] readonly IStreamRefMiddleware middleware ;
2325
24- protected internal StreamRef ( StreamPath path , IStreamProvider provider = null )
26+ protected StreamRef ( StreamPath path )
2527 {
2628 Path = path ;
29+ }
30+
31+ internal StreamRef ( StreamPath path , IStreamProvider provider , IStreamRefMiddleware middleware )
32+ : this ( path )
33+ {
34+
2735 this . provider = provider ;
36+ this . middleware = middleware ;
2837 }
2938
3039 [ NonSerialized ]
@@ -88,16 +97,28 @@ public virtual async Task Publish<TMessage>(TMessage message) where TMessage : P
8897 switch ( message )
8998 {
9099 case NextItem < TItem > next :
91- await Endpoint . OnNextAsync ( next . Item , next . Token ) ;
100+ await middleware . Publish ( Path , next , async x =>
101+ {
102+ await Endpoint . OnNextAsync ( x . Item , x . Token ) ;
103+ } ) ;
92104 break ;
93105 case NextItemBatch < TItem > next :
94- await Endpoint . OnNextBatchAsync ( next . Items , next . Token ) ;
106+ await middleware . Publish ( Path , next , async x =>
107+ {
108+ await Endpoint . OnNextBatchAsync ( x . Items , x . Token ) ;
109+ } ) ;
95110 break ;
96111 case NotifyStreamError error :
97- await Endpoint . OnErrorAsync ( error . Exception ) ;
112+ await middleware . Publish ( Path , error , async x =>
113+ {
114+ await Endpoint . OnErrorAsync ( x . Exception ) ;
115+ } ) ;
98116 break ;
99- case NotifyStreamCompleted _:
100- await Endpoint . OnCompletedAsync ( ) ;
117+ case NotifyStreamCompleted completed :
118+ await middleware . Publish ( Path , completed , async _ =>
119+ {
120+ await Endpoint . OnCompletedAsync ( ) ;
121+ } ) ;
101122 break ;
102123 default :
103124 throw new ArgumentOutOfRangeException ( nameof ( message ) , $ "Unsupported type of publish message: '{ message . GetType ( ) } '") ;
@@ -130,7 +151,7 @@ public virtual async Task<StreamSubscription<TItem>> Subscribe<TOptions>(Func<St
130151
131152 async Task < StreamSubscription < TItem > > Subscribe ( SubscribeReceiveItem o )
132153 {
133- var observer = new Observer ( this , callback ) ;
154+ var observer = CreateObserver ( callback ) ;
134155
135156 var predicate = o . Filter != null
136157 ? StreamFilter . Internal . Predicate
@@ -142,7 +163,7 @@ async Task<StreamSubscription<TItem>> Subscribe(SubscribeReceiveItem o)
142163
143164 async Task < StreamSubscription < TItem > > SubscribeBatch ( SubscribeReceiveBatch o )
144165 {
145- var observer = new BatchObserver ( this , callback ) ;
166+ var observer = CreateBatchObserver ( callback ) ;
146167 var handle = await Endpoint . SubscribeAsync ( observer , o . Token ) ;
147168 return new StreamSubscription < TItem > ( this , handle ) ;
148169 }
@@ -198,46 +219,66 @@ static object Deserialize(Type t, IDeserializationContext context)
198219 {
199220 var reader = context . StreamReader ;
200221 var path = StreamPath . Parse ( reader . ReadString ( ) ) ;
201- var provider = context . ServiceProvider . GetServiceByName < IStreamProvider > ( path . Provider ) ;
202- return new StreamRef < TItem > ( path , provider ) ;
222+ var system = context . ServiceProvider . GetRequiredService < IActorSystem > ( ) ;
223+ return system . StreamOf < TItem > ( path ) ;
203224 }
204225
205226 #endregion
206227
228+ internal BatchObserver CreateBatchObserver ( Func < StreamMessage , Task > callback )
229+ {
230+ return new BatchObserver ( this , callback , middleware ) ;
231+ }
232+
233+ internal Observer CreateObserver ( Func < StreamMessage , Task > callback )
234+ {
235+ return new Observer ( this , callback , middleware ) ;
236+ }
237+
207238 internal class BatchObserver : IAsyncBatchObserver < TItem >
208239 {
209240 readonly StreamRef < TItem > stream ;
210241 readonly Func < StreamMessage , Task > callback ;
242+ readonly IStreamRefMiddleware middleware ;
211243
212- public BatchObserver ( StreamRef < TItem > stream , Func < StreamMessage , Task > callback )
244+ public BatchObserver ( StreamRef < TItem > stream , Func < StreamMessage , Task > callback , IStreamRefMiddleware middleware )
213245 {
214246 this . stream = stream ;
215247 this . callback = callback ;
248+ this . middleware = middleware ;
216249 }
217250
218- public Task OnNextAsync ( IList < SequentialItem < TItem > > items ) =>
219- callback ( new StreamItemBatch < TItem > ( stream , items ) ) ;
251+ public Task OnNextAsync ( IList < SequentialItem < TItem > > items ) =>
252+ middleware . Receive ( stream . Path , new StreamItemBatch < TItem > ( stream , items ) , x => callback ( x ) ) ;
253+
254+ public Task OnCompletedAsync ( ) =>
255+ middleware . Receive ( stream . Path , new StreamCompleted ( stream ) , x => callback ( x ) ) ;
220256
221- public Task OnCompletedAsync ( ) => callback ( new StreamCompleted ( stream ) ) ;
222- public Task OnErrorAsync ( Exception ex ) => callback ( new StreamError ( stream , ex ) ) ;
257+ public Task OnErrorAsync ( Exception ex ) =>
258+ middleware . Receive ( stream . Path , new StreamError ( stream , ex ) , x => callback ( x ) ) ;
223259 }
224260
225261 internal class Observer : IAsyncObserver < TItem >
226262 {
227263 readonly StreamRef < TItem > stream ;
228264 readonly Func < StreamMessage , Task > callback ;
265+ readonly IStreamRefMiddleware middleware ;
229266
230- public Observer ( StreamRef < TItem > stream , Func < StreamMessage , Task > callback )
267+ public Observer ( StreamRef < TItem > stream , Func < StreamMessage , Task > callback , IStreamRefMiddleware middleware )
231268 {
232269 this . stream = stream ;
233270 this . callback = callback ;
271+ this . middleware = middleware ;
234272 }
235273
236- public Task OnNextAsync ( TItem item , StreamSequenceToken token = null ) =>
237- callback ( new StreamItem < TItem > ( stream , item , token ) ) ;
274+ public Task OnNextAsync ( TItem item , StreamSequenceToken token = null ) =>
275+ middleware . Receive ( stream . Path , new StreamItem < TItem > ( stream , item , token ) , x => callback ( x ) ) ;
276+
277+ public Task OnCompletedAsync ( ) =>
278+ middleware . Receive ( stream . Path , new StreamCompleted ( stream ) , x => callback ( x ) ) ;
238279
239- public Task OnCompletedAsync ( ) => callback ( new StreamCompleted ( stream ) ) ;
240- public Task OnErrorAsync ( Exception ex ) => callback ( new StreamError ( stream , ex ) ) ;
280+ public Task OnErrorAsync ( Exception ex ) =>
281+ middleware . Receive ( stream . Path , new StreamError ( stream , ex ) , x => callback ( x ) ) ;
241282 }
242283 }
243284}
0 commit comments