Skip to content

Instantly share code, notes, and snippets.

@hickford
Created May 28, 2012 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hickford/2819921 to your computer and use it in GitHub Desktop.
Save hickford/2819921 to your computer and use it in GitHub Desktop.
Reactive Extension's Where method reimplemented
public static class ObservableExtensions
{
public static IObservable<TSource> Where<TSource> (this IObservable<TSource> source, Func<TSource,bool> predicate)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (predicate == null)
{
throw new ArgumentNullException("predicate");
}
// consider whether this should be wrapped in Observable.Defer( () => {} )
return Observable.Create<TSource>(observer =>
{
return source.Subscribe(x=>
{
bool flag = false;
try
{
flag = predicate(x);
}
catch (Exception e)
{
observer.OnError(e);
return;
}
if (flag)
{
observer.OnNext(x);
}
},
observer.OnError,
observer.OnCompleted
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment