Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Switch.cs « Observable « Linq « Reactive « System.Reactive.Linq « Source « NET « Rx - github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 794b0d423f1cc918b37d0d80ec98432e8eaf4ef2 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#if !NO_PERF
using System;
using System.Reactive.Disposables;

namespace System.Reactive.Linq.Observαble
{
    class Switch<TSource> : Producer<TSource>
    {
        private readonly IObservable<IObservable<TSource>> _sources;

        public Switch(IObservable<IObservable<TSource>> sources)
        {
            _sources = sources;
        }

        protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
        {
            var sink = new _(this, observer, cancel);
            setSink(sink);
            return sink.Run();
        }

        class _ : Sink<TSource>, IObserver<IObservable<TSource>>
        {
            private readonly Switch<TSource> _parent;

            public _(Switch<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
                : base(observer, cancel)
            {
                _parent = parent;
            }

            private object _gate;
            private IDisposable _subscription;
            private SerialDisposable _innerSubscription;
            private bool _isStopped;
            private ulong _latest;
            private bool _hasLatest;

            public IDisposable Run()
            {
                _gate = new object();
                _innerSubscription = new SerialDisposable();
                _isStopped = false;
                _latest = 0UL;
                _hasLatest = false;

                var subscription = new SingleAssignmentDisposable();
                _subscription = subscription;
                subscription.Disposable = _parent._sources.SubscribeSafe(this);

                return new CompositeDisposable(_subscription, _innerSubscription);
            }

            public void OnNext(IObservable<TSource> value)
            {
                var id = default(ulong);
                lock (_gate)
                {
                    id = unchecked(++_latest);
                    _hasLatest = true;
                }

                var d = new SingleAssignmentDisposable();
                _innerSubscription.Disposable = d;
                d.Disposable = value.SubscribeSafe(new ι(this, id, d));
            }

            public void OnError(Exception error)
            {
                lock (_gate)
                    base._observer.OnError(error);

                base.Dispose();
            }

            public void OnCompleted()
            {
                lock (_gate)
                {
                    _subscription.Dispose();

                    _isStopped = true;
                    if (!_hasLatest)
                    {
                        base._observer.OnCompleted();
                        base.Dispose();
                    }
                }
            }

            class ι : IObserver<TSource>
            {
                private readonly _ _parent;
                private readonly ulong _id;
                private readonly IDisposable _self;

                public ι(_ parent, ulong id, IDisposable self)
                {
                    _parent = parent;
                    _id = id;
                    _self = self;
                }

                public void OnNext(TSource value)
                {
                    lock (_parent._gate)
                    {
                        if (_parent._latest == _id)
                            _parent._observer.OnNext(value);
                    }
                }

                public void OnError(Exception error)
                {
                    lock (_parent._gate)
                    {
                        _self.Dispose();

                        if (_parent._latest == _id)
                        {
                            _parent._observer.OnError(error);
                            _parent.Dispose();
                        }
                    }
                }

                public void OnCompleted()
                {
                    lock (_parent._gate)
                    {
                        _self.Dispose();

                        if (_parent._latest == _id)
                        {
                            _parent._hasLatest = false;

                            if (_parent._isStopped)
                            {
                                _parent._observer.OnCompleted();
                                _parent.Dispose();
                            }
                        }
                    }
                }
            }
        }
    }
}
#endif