Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/PushToPullAdapter.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Reactive.Disposables;
7
8 namespace System.Reactive.Linq.ObservableImpl
9 {
10 abstract class PushToPullAdapter<TSource, TResult> : IEnumerable<TResult>
11 {
12 private readonly IObservable<TSource> _source;
13
14 public PushToPullAdapter(IObservable<TSource> source)
15 {
16 _source = source;
17 }
18
19 IEnumerator IEnumerable.GetEnumerator()
20 {
21 return GetEnumerator();
22 }
23
24 public IEnumerator<TResult> GetEnumerator()
25 {
26 var d = new SingleAssignmentDisposable();
27 var res = Run(d);
28 d.Disposable = _source.SubscribeSafe(res);
29 return res;
30 }
31
32 protected abstract PushToPullSink<TSource, TResult> Run(IDisposable subscription);
33 }
34
35 abstract class PushToPullSink<TSource, TResult> : IObserver<TSource>, IEnumerator<TResult>, IDisposable
36 {
37 private readonly IDisposable _subscription;
38
39 public PushToPullSink(IDisposable subscription)
40 {
41 _subscription = subscription;
42 }
43
44 public abstract void OnNext(TSource value);
45 public abstract void OnError(Exception error);
46 public abstract void OnCompleted();
47
48 public abstract bool TryMoveNext(out TResult current);
49
50 private bool _done;
51
52 public bool MoveNext()
53 {
54 if (!_done)
55 {
56 var current = default(TResult);
57 if (TryMoveNext(out current))
58 {
59 Current = current;
60 return true;
61 }
62 else
63 {
64 _done = true;
65 _subscription.Dispose();
66 }
67 }
68
69 return false;
70 }
71
72 public TResult Current
73 {
74 get;
75 private set;
76 }
77
78 object IEnumerator.Current
79 {
80 get { return Current; }
81 }
82
83 public void Reset()
84 {
85 throw new NotSupportedException();
86 }
87
88 public void Dispose()
89 {
90 _subscription.Dispose();
91 }
92 }
93 }