none
A Zip that doesn't complete when one of the observable's completes

    Question

  • I have a scenario where I need to kick off three different observables and I only want the subscriber to fire once all three have produced a value or completed. 2 of them come from Observable.FromAsync's so when I use a ZIP

    Observable.zip(obs1, obs2, obs3)

    The zip just completes when any of those observable complete...  So to get around this I created this

    public static IObservable<T> AsNeverComplete<T>(this IObservable<T> observable)
    		{
    			//Hack way to make ZIP not complete if a sequence completes
    			return Observable.Create<T>(observer =>
    			{
    				bool valueProduced = false;
    
    				return observable.Subscribe(result => 
    				{
    					valueProduced = true;
    					observer.OnNext(result);
    				}
    					, observer.OnError, 
    					() =>
    				{
    					if(!valueProduced)
    					{
    						observer.OnNext(default(T));
    					}
    				});
    			});
    		}

    Which (AFAICT) works but I was just curious if there's a different/better way to go about this?

    so now in my actual code I can do 

    var zippedObs= 
    				Observable.Zip (
    					obs1.AsNeverComplete(), 
    					obs2.AsNeverComplete(), 
    					obs3.AsNeverComplete(),  
    					(p1, p2, _) => 
    					{
    						//do stuff
    						return true;
    					})
    .Take(1)
    .Subscribe()
     ; 
    
    
    

    Thursday, June 02, 2016 10:24 PM