@@ -21,62 +21,68 @@ public IObservable<IChangeSet<TDestination, TDestinationKey>> Run() => Observabl
2121 {
2222 var locker = new object ( ) ;
2323 var cache = new Cache < ChangeSetCache < TDestination , TDestinationKey > , TKey > ( ) ;
24- var updateCounter = 0 ;
25-
26- // Transformation Function:
27- // Create the Child Observable by invoking the async selector, appending the counter and the synchronize
28- // Pass the result to a new ChangeSetCache instance.
29- ChangeSetCache < TDestination , TDestinationKey > Transform_ ( TSource obj , TKey key ) => new (
30- Observable . Defer ( ( ) => selector ( obj , key ) )
31- . Do ( _ => Interlocked . Increment ( ref updateCounter ) )
32- . Synchronize ( locker ! ) ) ;
24+ var parentUpdate = false ;
3325
3426 // This is manages all of the changes
3527 var changeTracker = new ChangeSetMergeTracker < TDestination , TDestinationKey > ( ( ) => cache . Items , comparer , equalityComparer ) ;
3628
29+ // Transform Helper
30+ async Task < IObservable < IChangeSet < TDestination , TDestinationKey > > > InvokeSelector ( TSource obj , TKey key )
31+ {
32+ if ( errorHandler != null )
33+ {
34+ try
35+ {
36+ return await selector ( obj , key ) . ConfigureAwait ( false ) ;
37+ }
38+ catch ( Exception e )
39+ {
40+ errorHandler . Invoke ( new Error < TSource , TKey > ( e , obj , key ) ) ;
41+ return Observable . Empty < IChangeSet < TDestination , TDestinationKey > > ( ) ;
42+ }
43+ }
44+
45+ return await selector ( obj , key ) . ConfigureAwait ( false ) ;
46+ }
47+
48+ // Transformation Function:
49+ // Create the Child Observable by invoking the async selector, appending the synchronize, and creating a new ChangeSetCache instance.
50+ ChangeSetCache < TDestination , TDestinationKey > Transform_ ( TSource obj , TKey key ) =>
51+ new ( Observable . Defer ( ( ) => InvokeSelector ( obj , key ) ) . Synchronize ( locker ! ) ) ;
52+
3753 // Transform to a cache changeset of child caches, synchronize, clone changes to the local copy, and publish.
38- // Increment updateCounter BEFORE the lock so that incoming changesets will cause the downstream changeset to be delayed
39- // until all pending changesets have been handled.
40- var shared =
41- ( errorHandler is null ? source . Transform ( Transform_ ) : source . TransformSafe ( Transform_ , errorHandler ) )
42- . Do ( _ => Interlocked . Increment ( ref updateCounter ) )
43- . Synchronize ( locker )
44- . Do ( cache . Clone )
45- . Publish ( ) ;
54+ var shared = source
55+ . Transform ( Transform_ )
56+ . Synchronize ( locker )
57+ . Do (
58+ changes =>
59+ {
60+ cache . Clone ( changes ) ;
61+ parentUpdate = true ;
62+ } )
63+ . Publish ( ) ;
4664
4765 // Merge the child changeset changes together and apply to the tracker
48- // Emit the changeset if there are no other pending changes
66+ // Emit the changeset if not currently handling a parent stream update
4967 var subMergeMany = shared
5068 . MergeMany ( cacheChangeSet => cacheChangeSet . Source )
5169 . SubscribeSafe (
52- changes => changeTracker . ProcessChangeSet ( changes , Interlocked . Decrement ( ref updateCounter ) == 0 ? observer : null ) ,
70+ changes => changeTracker . ProcessChangeSet ( changes , ! parentUpdate ? observer : null ) ,
5371 observer . OnError ) ;
5472
5573 // When a source item is removed, all of its sub-items need to be removed
56- // Emit the changeset if there are no other pending changes
74+ // Emit any pending changes
5775 var subRemove = shared
5876 . OnItemRemoved ( changeSetCache => changeTracker . RemoveItems ( changeSetCache . Cache . KeyValues ) , invokeOnUnsubscribe : false )
5977 . OnItemUpdated ( ( _ , prev ) => changeTracker . RemoveItems ( prev . Cache . KeyValues ) )
6078 . SubscribeSafe (
6179 _ =>
6280 {
63- if ( Interlocked . Decrement ( ref updateCounter ) == 0 )
64- {
65- changeTracker . EmitChanges ( observer ) ;
66- }
81+ changeTracker . EmitChanges ( observer ) ;
82+ parentUpdate = false ;
6783 } ,
6884 observer . OnError ,
69- ( ) =>
70- {
71- if ( Volatile . Read ( ref updateCounter ) == 0 )
72- {
73- observer . OnCompleted ( ) ;
74- }
75- else
76- {
77- changeTracker . MarkComplete ( ) ;
78- }
79- } ) ;
85+ observer . OnCompleted ) ;
8086
8187 return new CompositeDisposable ( shared . Connect ( ) , subMergeMany , subRemove ) ;
8288 } ) ;
0 commit comments