private static TSource FirstOrDefaultInternal <TSource>(IObservable <TSource> source, bool throwOnEmpty) { using var consumer = new FirstBlocking <TSource>(); using (source.Subscribe(consumer)) { consumer.Wait(); } consumer._error?.Throw(); if (throwOnEmpty && !consumer._hasValue) { throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS); } return(consumer._value); }
private static TSource FirstOrDefaultInternal <TSource>(IObservable <TSource> source, bool throwOnEmpty) { using (var consumer = new FirstBlocking <TSource>()) { using (var d = source.Subscribe(consumer)) { consumer.SetUpstream(d); if (consumer.CurrentCount != 0) { consumer.Wait(); } } consumer._error.ThrowIfNotNull(); if (throwOnEmpty && !consumer._hasValue) { throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS); } return(consumer._value); } }