Exemplo n.º 1
0
 public static Task DelayAsync(this IMomentClock clock, long dueInMilliseconds, CancellationToken cancellationToken = default)
 {
     if (dueInMilliseconds == Timeout.Infinite)
     {
         return(clock.DelayAsync(Timeout.InfiniteTimeSpan, cancellationToken));
     }
     if (dueInMilliseconds < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(dueInMilliseconds));
     }
     return(clock.DelayAsync(TimeSpan.FromMilliseconds(dueInMilliseconds), cancellationToken));
 }
Exemplo n.º 2
0
        public static IObservable <long> Interval(this IMomentClock clock, IEnumerable <TimeSpan> intervals)
        {
            var e = intervals.GetEnumerator();

            return(Observable.Create <long>(async(observer, ct) => {
                var completed = false;
                try {
                    var index = 0L;
                    while (e.MoveNext())
                    {
                        var dueAt = clock.Now + e.Current;
                        await clock.DelayAsync(dueAt, ct).SuppressCancellation().ConfigureAwait(false);
                        if (ct.IsCancellationRequested)
                        {
                            break;
                        }
                        observer.OnNext(index++);
                    }
                    completed = true;
                    observer.OnCompleted();
                }
                catch (Exception e) {
                    if (!completed)
                    {
                        observer.OnError(e);
                    }
                }
                finally {
                    e.Dispose();
                }
            }));
        }
Exemplo n.º 3
0
 public static IObservable <long> Timer(this IMomentClock clock, TimeSpan dueIn)
 {
     if (clock is SystemClock)
     {
         return(Observable.Timer(dueIn)); // Perf. optimization
     }
     return(Observable.Create <long>(async observer => {
         var completed = false;
         try {
             await clock.DelayAsync(dueIn).ConfigureAwait(false);
             observer.OnNext(0);
             completed = true;
             observer.OnCompleted();
         }
         catch (Exception e) {
             if (!completed)
             {
                 observer.OnError(e);
             }
         }
     }));
 }
Exemplo n.º 4
0
 public static Task DelayAsync(this IMomentClock clock, Moment dueAt, CancellationToken cancellationToken = default)
 => clock.DelayAsync((dueAt - clock.Now).NonNegative(), cancellationToken);