Skip to content

Instantly share code, notes, and snippets.

@Protonz
Last active August 3, 2018 19:05
Show Gist options
  • Save Protonz/fa00a65a1b48a33527429deeda0943fa to your computer and use it in GitHub Desktop.
Save Protonz/fa00a65a1b48a33527429deeda0943fa 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