Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Concurrency/CatchScheduler.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.Reactive.Disposables;
5
6 #if !NO_WEAKTABLE
7 using System.Runtime.CompilerServices;
8 #endif
9
10 namespace System.Reactive.Concurrency
11 {
12 class CatchScheduler<TException> : SchedulerWrapper
13 where TException : Exception
14 {
15 private readonly Func<TException, bool> _handler;
16
17 public CatchScheduler(IScheduler scheduler, Func<TException, bool> handler)
18 : base(scheduler)
19 {
20 _handler = handler;
21 }
22
23 protected override Func<IScheduler, TState, IDisposable> Wrap<TState>(Func<IScheduler, TState, IDisposable> action)
24 {
25 return (self, state) =>
26 {
27 try
28 {
29 return action(GetRecursiveWrapper(self), state);
30 }
31 catch (TException exception)
32 {
33 if (!_handler(exception))
34 throw;
35
36 return Disposable.Empty;
37 }
38 };
39 }
40
41 #if !NO_WEAKTABLE
42 public CatchScheduler(IScheduler scheduler, Func<TException, bool> handler, ConditionalWeakTable<IScheduler, IScheduler> cache)
43 : base(scheduler, cache)
44 {
45 _handler = handler;
46 }
47
48 protected override SchedulerWrapper Clone(IScheduler scheduler, ConditionalWeakTable<IScheduler, IScheduler> cache)
49 {
50 return new CatchScheduler<TException>(scheduler, _handler, cache);
51 }
52 #else
53 protected override SchedulerWrapper Clone(IScheduler scheduler)
54 {
55 return new CatchScheduler<TException>(scheduler, _handler);
56 }
57 #endif
58
59 protected override bool TryGetService(IServiceProvider provider, Type serviceType, out object service)
60 {
61 service = provider.GetService(serviceType);
62
63 if (service != null)
64 {
65 if (serviceType == typeof(ISchedulerLongRunning))
66 service = new CatchSchedulerLongRunning((ISchedulerLongRunning)service, _handler);
67 else if (serviceType == typeof(ISchedulerPeriodic))
68 service = new CatchSchedulerPeriodic((ISchedulerPeriodic)service, _handler);
69 }
70
71 return true;
72 }
73
74 class CatchSchedulerLongRunning : ISchedulerLongRunning
75 {
76 private readonly ISchedulerLongRunning _scheduler;
77 private readonly Func<TException, bool> _handler;
78
79 public CatchSchedulerLongRunning(ISchedulerLongRunning scheduler, Func<TException, bool> handler)
80 {
81 _scheduler = scheduler;
82 _handler = handler;
83 }
84
85 public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action)
86 {
87 return _scheduler.ScheduleLongRunning(state, (state_, cancel) =>
88 {
89 try
90 {
91 action(state_, cancel);
92 }
93 catch (TException exception)
94 {
95 if (!_handler(exception))
96 throw;
97 }
98 });
99 }
100 }
101
102 class CatchSchedulerPeriodic : ISchedulerPeriodic
103 {
104 private readonly ISchedulerPeriodic _scheduler;
105 private readonly Func<TException, bool> _handler;
106
107 public CatchSchedulerPeriodic(ISchedulerPeriodic scheduler, Func<TException, bool> handler)
108 {
109 _scheduler = scheduler;
110 _handler = handler;
111 }
112
113 public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
114 {
115 var failed = false;
116
117 var d = new SingleAssignmentDisposable();
118
119 d.Disposable = _scheduler.SchedulePeriodic(state, period, state_ =>
120 {
121 //
122 // Cancellation may not be granted immediately; prevent from running user
123 // code in that case. Periodic schedulers are assumed to introduce some
124 // degree of concurrency, so we should return from the SchedulePeriodic
125 // call eventually, allowing the d.Dispose() call in the catch block to
126 // take effect.
127 //
128 if (failed)
129 return default(TState);
130
131 try
132 {
133 return action(state_);
134 }
135 catch (TException exception)
136 {
137 failed = true;
138
139 if (!_handler(exception))
140 throw;
141
142 d.Dispose();
143 return default(TState);
144 }
145 });
146
147 return d;
148 }
149 }
150 }
151 }