aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/GrpcApi/RecordingObserver.cs
blob: 8ba3787905a28bb06dd38f24854b9761456d5625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace math
{
    public class RecordingObserver<T> : IObserver<T>
    {
        TaskCompletionSource<List<T>> tcs = new TaskCompletionSource<List<T>>();
        List<T> data = new List<T>();

        public void OnCompleted()
        {
            tcs.SetResult(data);
        }

        public void OnError(Exception error)
        {
            tcs.SetException(error);
        }

        public void OnNext(T value)
        {
            data.Add(value);
        }

        public Task<List<T>> ToList() {
            return tcs.Task;
        }
    }
}