Debian Codegraph @

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

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_PERF
4 using System;
5
6 namespace System.Reactive.Linq.ObservableImpl
7 {
8 class Synchronize<TSource> : Producer<TSource>
9 {
10 private readonly IObservable<TSource> _source;
11 private readonly object _gate;
12
13 public Synchronize(IObservable<TSource> source, object gate)
14 {
15 _source = source;
16 _gate = gate;
17 }
18
19 public Synchronize(IObservable<TSource> source)
20 {
21 _source = source;
22 }
23
24 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
25 {
26 var sink = new _(this, observer, cancel);
27 setSink(sink);
28 return _source.Subscribe(sink);
29 }
30
31 class _ : Sink<TSource>, IObserver<TSource>
32 {
33 private readonly Synchronize<TSource> _parent;
34 private object _gate;
35
36 public _(Synchronize<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
37 : base(observer, cancel)
38 {
39 _parent = parent;
40 _gate = _parent._gate ?? new object();
41 }
42
43 public void OnNext(TSource value)
44 {
45 lock (_gate)
46 {
47 base._observer.OnNext(value);
48 }
49 }
50
51 public void OnError(Exception error)
52 {
53 lock (_gate)
54 {
55 base._observer.OnError(error);
56 base.Dispose();
57 }
58 }
59
60 public void OnCompleted()
61 {
62 lock (_gate)
63 {
64 base._observer.OnCompleted();
65 base.Dispose();
66 }
67 }
68 }
69 }
70 }
71 #endif