Repository navigation
Expand file tree
/
Copy pathInternalEx.cs
More file actions
98 lines (90 loc) · 3.41 KB
/
Copy pathInternalEx.cs
File metadata and controls
98 lines (90 loc) · 3.41 KB
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
using System;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using DynamicData.Annotations;
namespace DynamicData.Kernel
{
/// <summary>
///
/// </summary>
public static class InternalEx
{
[StringFormatMethod("parameters")]
internal static string FormatWith(this string source, params object[] parameters)
{
return string.Format(source, parameters);
}
/// <summary>
/// Retries the with back off.
/// </summary>
/// <remarks>
/// With big thanks. I took this from
/// http://social.msdn.microsoft.com/Forums/en-US/af43b14e-fb00-42d4-8fb1-5c45862f7796/recursive-async-web-requestresponse-what-is-best-practice-3rd-try
/// </remarks>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TException">The type of the exception.</typeparam>
/// <param name="source">The source.</param>
/// <param name="backOffStrategy">The back off strategy.</param>
/// <returns></returns>
public static IObservable<TSource> RetryWithBackOff<TSource, TException>(this IObservable<TSource> source, Func<TException, int, TimeSpan?> backOffStrategy)
where TException : Exception
{
Func<int, IObservable<TSource>> retry = null;
retry = (failureCount) => source.Catch<TSource, TException>(error =>
{
TimeSpan? delay = backOffStrategy(error, failureCount);
if (!delay.HasValue)
return Observable.Throw<TSource>(error);
return Observable.Timer(delay.Value).SelectMany(retry(failureCount + 1));
});
return retry(0);
}
internal static IObservable<Unit> ToUnit<T>(this IObservable<T> source)
{
return source.Select(_ => Unit.Default);
}
/// <summary>
/// Schedules a recurring action.
/// </summary>
/// <remarks>
/// I took this from
/// http://www.zerobugbuild.com/?p=259
/// </remarks>
/// <param name="scheduler">The scheduler.</param>
/// <param name="interval">The interval.</param>
/// <param name="action">The action.</param>
/// <returns></returns>
public static IDisposable ScheduleRecurringAction(this IScheduler scheduler, TimeSpan interval, Action action)
{
return scheduler.Schedule(interval, scheduleNext =>
{
action();
scheduleNext(interval);
});
}
/// <summary>
/// Schedules a recurring action.
/// </summary>
/// <remarks>
/// I took this from
///
/// http://www.zerobugbuild.com/?p=259
///
/// and adapted it to receive
/// </remarks>
/// <param name="scheduler">The scheduler.</param>
/// <param name="interval">The interval.</param>
/// <param name="action">The action.</param>
/// <returns></returns>
public static IDisposable ScheduleRecurringAction(this IScheduler scheduler, Func<TimeSpan> interval, Action action)
{
return scheduler.Schedule(interval(), scheduleNext =>
{
action();
var next = interval();
scheduleNext(next);
});
}
}
}