2016-08-22 3 views
0

をセルフキャンセル:ソースがあるときは、常に偽である配列を生成するが、ソースシーケンスが真滞在したときに真行くReactiveX私は、フォームの拡張メソッドを作成したいタイマー

IObservable<bool> CancellableTimer(this IObservable source, TimeSpan delay) 
{ 
... 
} 

遅延、tで定義された期間のために:

source: 0---1---------0--1-0-1-0-1-0-1----------0 
      t------>     t------> 
result: 0----------1--0---------------------1---0 

私は確信しているRxのプリミティブを使用してこれを行う方法がなければならないが、私はRXに新たなんだと悩み、それの周りに私の頭を取得しました。どんなアイデアですか?

答えて

0

これは私が思いついたものです。

[Fact] 
    public static void Test_AsymetricDelay() 
    { 
     var scheduler = new TestScheduler(); 

     var xs = scheduler.CreateHotObservable(
      new Recorded<Notification<bool>>(10000000, Notification.CreateOnNext(true)), 
      new Recorded<Notification<bool>>(60000000, Notification.CreateOnNext(false)), 
      new Recorded<Notification<bool>>(70000000, Notification.CreateOnNext(true)), 
      new Recorded<Notification<bool>>(80000000, Notification.CreateOnNext(false)), 
      new Recorded<Notification<bool>>(100000000, Notification.CreateOnCompleted<bool>()) 
     ); 

     var dest = xs.DelayOn(TimeSpan.FromSeconds(2), scheduler); 

     var testObserver = scheduler.Start(
      () => dest,    
      0, 
      0, 
      TimeSpan.FromSeconds(10).Ticks); 


     testObserver.Messages.AssertEqual(
      new Recorded<Notification<bool>>(30000000, Notification.CreateOnNext(true)), 
      new Recorded<Notification<bool>>(60000000, Notification.CreateOnNext(false)), 
      new Recorded<Notification<bool>>(100000000, Notification.CreateOnCompleted<bool>()) 

     ); 
    } 
:その動作を確認するために

static public IObservable<bool> AsymetricDelay(this IObservable<bool> source, TimeSpan delay, IScheduler scheduler) 
    { 
     var distinct = source.DistinctUntilChanged(); 
     return distinct. 
      Throttle(delay, scheduler) // Delay both trues and falses 
      .Where(x => x)    // But we only want trues to be delayed 
      .Merge(     // Merge the trues with... 
       distinct.Where(x=>!x) // non delayed falses 
      ) 
      .DistinctUntilChanged(); // Get rid of any repeated values 

    } 

をそしてここでユニットテストです:より適切な名前のように思えるように私もAsymetricDelay()メソッドへと改名しました

関連する問題