Skip to content

Instantly share code, notes, and snippets.

@chiuan
Forked from Protonz/SwitchExtensions.cs
Created March 22, 2017 08:09
Show Gist options
  • Save chiuan/e14393dc487a4a3f2bef137148492005 to your computer and use it in GitHub Desktop.
Save chiuan/e14393dc487a4a3f2bef137148492005 to your computer and use it in GitHub Desktop.
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