1
0

Scheduler.Recursive.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Disposables;
  5. namespace System.Reactive.Concurrency
  6. {
  7. public static partial class Scheduler
  8. {
  9. /// <summary>
  10. /// Schedules an action to be executed recursively.
  11. /// </summary>
  12. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  13. /// <param name="action">Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action.</param>
  14. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  15. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  16. public static IDisposable Schedule(this IScheduler scheduler, Action<Action> action)
  17. {
  18. if (scheduler == null)
  19. {
  20. throw new ArgumentNullException(nameof(scheduler));
  21. }
  22. if (action == null)
  23. {
  24. throw new ArgumentNullException(nameof(action));
  25. }
  26. return scheduler.Schedule(action, (_action, self) => _action(() => self(_action)));
  27. }
  28. /// <summary>
  29. /// Schedules an action to be executed recursively.
  30. /// </summary>
  31. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  32. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  33. /// <param name="state">State passed to the action to be executed.</param>
  34. /// <param name="action">Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state.</param>
  35. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  36. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  37. public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, Action<TState, Action<TState>> action)
  38. {
  39. if (scheduler == null)
  40. {
  41. throw new ArgumentNullException(nameof(scheduler));
  42. }
  43. if (action == null)
  44. {
  45. throw new ArgumentNullException(nameof(action));
  46. }
  47. return scheduler.Schedule((state, action), (s, p) => InvokeRec1(s, p));
  48. }
  49. private static IDisposable InvokeRec1<TState>(IScheduler scheduler, (TState state, Action<TState, Action<TState>> action) tuple)
  50. {
  51. var recursiveInvoker = new InvokeRec1State<TState>(scheduler, tuple.action);
  52. recursiveInvoker.InvokeFirst(tuple.state);
  53. return recursiveInvoker;
  54. }
  55. /// <summary>
  56. /// Schedules an action to be executed recursively after a specified relative due time.
  57. /// </summary>
  58. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  59. /// <param name="action">Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified relative time.</param>
  60. /// <param name="dueTime">Relative time after which to execute the action for the first time.</param>
  61. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  62. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  63. public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action<Action<TimeSpan>> action)
  64. {
  65. if (scheduler == null)
  66. {
  67. throw new ArgumentNullException(nameof(scheduler));
  68. }
  69. if (action == null)
  70. {
  71. throw new ArgumentNullException(nameof(action));
  72. }
  73. return scheduler.Schedule(action, dueTime, (_action, self) => _action(dt => self(_action, dt)));
  74. }
  75. /// <summary>
  76. /// Schedules an action to be executed recursively after a specified relative due time.
  77. /// </summary>
  78. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  79. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  80. /// <param name="state">State passed to the action to be executed.</param>
  81. /// <param name="action">Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.</param>
  82. /// <param name="dueTime">Relative time after which to execute the action for the first time.</param>
  83. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  84. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  85. public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Action<TState, Action<TState, TimeSpan>> action)
  86. {
  87. if (scheduler == null)
  88. {
  89. throw new ArgumentNullException(nameof(scheduler));
  90. }
  91. if (action == null)
  92. {
  93. throw new ArgumentNullException(nameof(action));
  94. }
  95. return scheduler.Schedule((state, action), dueTime, (s, p) => InvokeRec2(s, p));
  96. }
  97. private static IDisposable InvokeRec2<TState>(IScheduler scheduler, (TState state, Action<TState, Action<TState, TimeSpan>> action) tuple)
  98. {
  99. var recursiveInvoker = new InvokeRec2State<TState>(scheduler, tuple.action);
  100. recursiveInvoker.InvokeFirst(tuple.state);
  101. return recursiveInvoker;
  102. }
  103. /// <summary>
  104. /// Schedules an action to be executed recursively at a specified absolute due time.
  105. /// </summary>
  106. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  107. /// <param name="action">Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified absolute time.</param>
  108. /// <param name="dueTime">Absolute time at which to execute the action for the first time.</param>
  109. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  110. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  111. public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action<Action<DateTimeOffset>> action)
  112. {
  113. if (scheduler == null)
  114. {
  115. throw new ArgumentNullException(nameof(scheduler));
  116. }
  117. if (action == null)
  118. {
  119. throw new ArgumentNullException(nameof(action));
  120. }
  121. return scheduler.Schedule(action, dueTime, (_action, self) => _action(dt => self(_action, dt)));
  122. }
  123. /// <summary>
  124. /// Schedules an action to be executed recursively at a specified absolute due time.
  125. /// </summary>
  126. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  127. /// <param name="scheduler">Scheduler to execute the recursive action on.</param>
  128. /// <param name="state">State passed to the action to be executed.</param>
  129. /// <param name="action">Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.</param>
  130. /// <param name="dueTime">Absolute time at which to execute the action for the first time.</param>
  131. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  132. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  133. public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Action<TState, Action<TState, DateTimeOffset>> action)
  134. {
  135. if (scheduler == null)
  136. {
  137. throw new ArgumentNullException(nameof(scheduler));
  138. }
  139. if (action == null)
  140. {
  141. throw new ArgumentNullException(nameof(action));
  142. }
  143. return scheduler.Schedule((state, action), dueTime, (s, p) => InvokeRec3(s, p));
  144. }
  145. private static IDisposable InvokeRec3<TState>(IScheduler scheduler, (TState state, Action<TState, Action<TState, DateTimeOffset>> action) tuple)
  146. {
  147. var recursiveInvoker = new InvokeRec3State<TState>(scheduler, tuple.action);
  148. recursiveInvoker.InvokeFirst(tuple.state);
  149. return recursiveInvoker;
  150. }
  151. private abstract class InvokeRecBaseState<TState> : IDisposable
  152. {
  153. protected readonly IScheduler Scheduler;
  154. protected readonly CompositeDisposable Group;
  155. public InvokeRecBaseState(IScheduler scheduler)
  156. {
  157. Scheduler = scheduler;
  158. Group = new CompositeDisposable();
  159. }
  160. public void Dispose()
  161. {
  162. Group.Dispose();
  163. }
  164. }
  165. private sealed class InvokeRec1State<TState> : InvokeRecBaseState<TState>
  166. {
  167. private readonly Action<TState, Action<TState>> _action;
  168. private readonly Action<TState> _recurseCallback;
  169. public InvokeRec1State(IScheduler scheduler, Action<TState, Action<TState>> action) : base(scheduler)
  170. {
  171. _action = action;
  172. _recurseCallback = state => InvokeNext(state);
  173. }
  174. internal void InvokeNext(TState state)
  175. {
  176. var sad = new SingleAssignmentDisposable();
  177. Group.Add(sad);
  178. sad.Disposable = Scheduler.Schedule((state, sad, @this: this), (_, nextState) =>
  179. {
  180. [email protected](nextState.sad);
  181. [email protected](nextState.state);
  182. return Disposable.Empty;
  183. });
  184. }
  185. internal void InvokeFirst(TState state)
  186. {
  187. _action(state, _recurseCallback);
  188. }
  189. }
  190. private sealed class InvokeRec2State<TState> : InvokeRecBaseState<TState>
  191. {
  192. private readonly Action<TState, Action<TState, TimeSpan>> _action;
  193. private readonly Action<TState, TimeSpan> _recurseCallback;
  194. public InvokeRec2State(IScheduler scheduler, Action<TState, Action<TState, TimeSpan>> action) : base(scheduler)
  195. {
  196. _action = action;
  197. _recurseCallback = (state, time) => InvokeNext(state, time);
  198. }
  199. internal void InvokeNext(TState state, TimeSpan time)
  200. {
  201. var sad = new SingleAssignmentDisposable();
  202. Group.Add(sad);
  203. sad.Disposable = Scheduler.Schedule((state, sad, @this: this), time, (_, nextState) =>
  204. {
  205. [email protected](nextState.sad);
  206. [email protected](nextState.state);
  207. return Disposable.Empty;
  208. });
  209. }
  210. internal void InvokeFirst(TState state)
  211. {
  212. _action(state, _recurseCallback);
  213. }
  214. }
  215. private sealed class InvokeRec3State<TState> : InvokeRecBaseState<TState>
  216. {
  217. private readonly Action<TState, Action<TState, DateTimeOffset>> _action;
  218. private readonly Action<TState, DateTimeOffset> _recurseCallback;
  219. public InvokeRec3State(IScheduler scheduler, Action<TState, Action<TState, DateTimeOffset>> action) : base(scheduler)
  220. {
  221. _action = action;
  222. _recurseCallback = (state, dtOffset) => InvokeNext(state, dtOffset);
  223. }
  224. internal void InvokeNext(TState state, DateTimeOffset dtOffset)
  225. {
  226. var sad = new SingleAssignmentDisposable();
  227. Group.Add(sad);
  228. sad.Disposable = Scheduler.Schedule((state, sad, @this: this), dtOffset, (_, nextState) =>
  229. {
  230. [email protected](nextState.sad);
  231. [email protected](nextState.state);
  232. return Disposable.Empty;
  233. });
  234. }
  235. internal void InvokeFirst(TState state)
  236. {
  237. _action(state, _recurseCallback);
  238. }
  239. }
  240. }
  241. }