// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Threading.Tasks; namespace System.Reactive.Linq { partial class AsyncObservable { public static IAsyncObservable SkipWhile(this IAsyncObservable source, Func predicate) { if (source == null) throw new ArgumentNullException(nameof(source)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); return Create(observer => source.SubscribeSafeAsync(AsyncObserver.SkipWhile(observer, predicate))); } public static IAsyncObservable SkipWhile(this IAsyncObservable source, Func> predicate) { if (source == null) throw new ArgumentNullException(nameof(source)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); return Create(observer => source.SubscribeSafeAsync(AsyncObserver.SkipWhile(observer, predicate))); } public static IAsyncObservable SkipWhile(this IAsyncObservable source, Func predicate) { if (source == null) throw new ArgumentNullException(nameof(source)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); return Create(observer => source.SubscribeSafeAsync(AsyncObserver.SkipWhile(observer, predicate))); } public static IAsyncObservable SkipWhile(this IAsyncObservable source, Func> predicate) { if (source == null) throw new ArgumentNullException(nameof(source)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); return Create(observer => source.SubscribeSafeAsync(AsyncObserver.SkipWhile(observer, predicate))); } } partial class AsyncObserver { public static IAsyncObserver SkipWhile(IAsyncObserver observer, Func predicate) { if (observer == null) throw new ArgumentNullException(nameof(observer)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); bool open = false; return Create( async x => { if (!open) { try { open = !predicate(x); } catch (Exception ex) { await observer.OnErrorAsync(ex).ConfigureAwait(false); return; } } if (open) { await observer.OnNextAsync(x).ConfigureAwait(false); } }, observer.OnErrorAsync, observer.OnCompletedAsync ); } public static IAsyncObserver SkipWhile(IAsyncObserver observer, Func> predicate) { if (observer == null) throw new ArgumentNullException(nameof(observer)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); bool open = false; return Create( async x => { if (!open) { try { open = !await predicate(x).ConfigureAwait(false); } catch (Exception ex) { await observer.OnErrorAsync(ex).ConfigureAwait(false); return; } } if (open) { await observer.OnNextAsync(x).ConfigureAwait(false); } }, observer.OnErrorAsync, observer.OnCompletedAsync ); } public static IAsyncObserver SkipWhile(IAsyncObserver observer, Func predicate) { if (observer == null) throw new ArgumentNullException(nameof(observer)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); bool open = false; int i = 0; return Create( async x => { if (!open) { try { open = !predicate(x, checked(i++)); } catch (Exception ex) { await observer.OnErrorAsync(ex).ConfigureAwait(false); return; } } if (open) { await observer.OnNextAsync(x).ConfigureAwait(false); } }, observer.OnErrorAsync, observer.OnCompletedAsync ); } public static IAsyncObserver SkipWhile(IAsyncObserver observer, Func> predicate) { if (observer == null) throw new ArgumentNullException(nameof(observer)); if (predicate == null) throw new ArgumentNullException(nameof(predicate)); bool open = false; int i = 0; return Create( async x => { if (!open) { try { open = !await predicate(x, checked(i++)).ConfigureAwait(false); } catch (Exception ex) { await observer.OnErrorAsync(ex).ConfigureAwait(false); return; } } if (open) { await observer.OnNextAsync(x).ConfigureAwait(false); } }, observer.OnErrorAsync, observer.OnCompletedAsync ); } } }