-
-
Save chiuan/e14393dc487a4a3f2bef137148492005 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace UniRx { | |
public static class SwitchExtensions { | |
public static IObservable<OUT> SwitchSelectWhenTrue<OUT>( this IObservable<bool> observable, Func<IObservable<OUT>> resultIfTrue ) { | |
return observable.Select(value => value == true ? resultIfTrue() : Observable.Empty<OUT>()).Switch(); | |
} | |
public static IObservable<OUT> SwitchSelectWhenFalse<OUT>( this IObservable<bool> observable, Func<IObservable<OUT>> resultIfFalse ) { | |
return observable.Select(value => value == false ? resultIfFalse() : Observable.Empty<OUT>()).Switch(); | |
} | |
public static IObservable<OUT> SwitchSelectWhenNull<OUT, IN>( this IObservable<IN> observable, Func<IObservable<OUT>> resultWhenNull ) where IN : class { | |
return observable.Select(value => value == null ? resultWhenNull() : Observable.Empty<OUT>()).Switch(); | |
} | |
public static IObservable<OUT> SwitchSelectWhenNotNull<OUT, IN>( this IObservable<IN> observable, Func<IN, IObservable<OUT>> resultWhenNotNull ) where IN : class { | |
return observable.Select(value => value != null ? resultWhenNotNull(value) : Observable.Empty<OUT>()).Switch(); | |
} | |
public static IObservable<OUT> SwitchSelect<OUT, IN>( this IObservable<IN> observable, Func<IN, IObservable<OUT>> result ) { | |
return observable.Select(result).Switch(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment