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

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

#if HAS_AWAIT
using System.Threading;
using System.Reactive.Disposables;
using System.Reactive.Subjects;

namespace System.Reactive.Linq
{
    internal partial class QueryLanguage
    {
        public virtual AsyncSubject<TSource> GetAwaiter<TSource>(IObservable<TSource> source)
        {
            var s = new AsyncSubject<TSource>();
            source.SubscribeSafe(s);
            return s;
        }

        public virtual AsyncSubject<TSource> GetAwaiter<TSource>(IConnectableObservable<TSource> source)
        {
            var s = new AsyncSubject<TSource>();
            source.SubscribeSafe(s);
            source.Connect();
            return s;
        }

        public virtual AsyncSubject<TSource> RunAsync<TSource>(IObservable<TSource> source, CancellationToken cancellationToken)
        {
            var s = new AsyncSubject<TSource>();

            var cancel = new Action(() => s.OnError(new OperationCanceledException()));
            if (cancellationToken.IsCancellationRequested)
            {
                cancel();
                return s;
            }

            var d = source.SubscribeSafe(s);
            cancellationToken.Register(d.Dispose);
            cancellationToken.Register(cancel);

            return s;
        }

        public virtual AsyncSubject<TSource> RunAsync<TSource>(IConnectableObservable<TSource> source, CancellationToken cancellationToken)
        {
            var s = new AsyncSubject<TSource>();

            var cancel = new Action(() => s.OnError(new OperationCanceledException()));
            if (cancellationToken.IsCancellationRequested)
            {
                cancel();
                return s;
            }

            var d = new CompositeDisposable(source.SubscribeSafe(s), source.Connect());
            cancellationToken.Register(d.Dispose);
            cancellationToken.Register(cancel);

            return s;
        }
    }
}
#endif