Exemple #1
0
 public ConsumableResultCursor(IInternalResultCursor cursor)
 {
     _cursor = cursor;
 }
Exemple #2
0
        private IDisposable StartStreaming(IInternalResultCursor cursor,
                                           IObserver <IRecord> recordObserver = null, IObserver <IResultSummary> summaryObserver = null)
        {
            var cancellation = new CompositeDisposable(2);

            if (summaryObserver != null)
            {
                cancellation.Add(_summary.Subscribe(summaryObserver));
            }

            if (StartStreaming())
            {
                if (recordObserver != null)
                {
                    cancellation.Add(_records.Subscribe(recordObserver));
                }

                var streamingCancellation = new CancellationDisposable();
                cancellation.Add(streamingCancellation);

                Task.Run(async() =>
                {
                    try
                    {
                        // Ensure that we propagate any errors from the KeysAsync call
                        await cursor.KeysAsync().ConfigureAwait(false);

                        if (!_records.HasObservers)
                        {
                            cursor.Cancel();
                        }
                        else
                        {
                            streamingCancellation.Token.Register(cursor.Cancel);
                        }

                        while (await cursor.FetchAsync().ConfigureAwait(false))
                        {
                            _records.OnNext(cursor.Current);
                        }

                        _records.OnCompleted();
                    }
                    catch (Exception exc)
                    {
                        _records.OnError(exc);

                        if (summaryObserver != null)
                        {
                            _summary.OnError(exc);
                        }
                    }

                    try
                    {
                        _summary.OnNext(await cursor.ConsumeAsync().ConfigureAwait(false));
                        _summary.OnCompleted();
                    }
                    catch (Exception exc)
                    {
                        _summary.OnError(exc);
                    }

                    CompleteStreaming();
                }, streamingCancellation.Token);
            }
            else
            {
                recordObserver?.OnError(new ResultConsumedException(
                                            "Streaming has already started and/or finished with a previous Records or Summary subscription."));
            }

            return(cancellation);
        }