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

MySubject.cs « Tests « Tests.System.Reactive « Rx.NET - github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b1dd90736103673ee5d5ef4457288c80222b18b7 (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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Reactive.Subjects;

namespace ReactiveTests.Tests
{
    class MySubject : ISubject<int>
    {
        private Dictionary<int, IDisposable> _disposeOn = new Dictionary<int, IDisposable>();

        public void DisposeOn(int value, IDisposable disposable)
        {
            _disposeOn[value] = disposable;
        }

        private IObserver<int> _observer;

        public void OnNext(int value)
        {
            _observer.OnNext(value);

            IDisposable disconnect;
            if (_disposeOn.TryGetValue(value, out disconnect))
                disconnect.Dispose();
        }

        public void OnError(Exception exception)
        {
            _observer.OnError(exception);
        }

        public void OnCompleted()
        {
            _observer.OnCompleted();
        }

        public IDisposable Subscribe(IObserver<int> observer)
        {
            _subscribeCount++;
            _observer = observer;
            return Disposable.Create(() => { _disposed = true; });
        }

        private int _subscribeCount;
        private bool _disposed;

        public int SubscribeCount { get { return _subscribeCount; } }
        public bool Disposed { get { return _disposed; } }
    }
}