Skip to content

Commit

Permalink
Timer operator
Browse files Browse the repository at this point in the history
  • Loading branch information
teivah committed Dec 6, 2018
1 parent 169479b commit 8f4a263
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions observablecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,19 @@ func Never() Observable {
out := make(chan interface{})
return newObservableFromChannel(out)
}

// Timer returns an Observable that emits the zeroed value of a float64 after a
// specified delay, and then completes.
func Timer(d Duration) Observable {
out := make(chan interface{})
go func() {
if d == nil {
time.Sleep(0)
} else {
time.Sleep(d.duration())
}
out <- 0.
close(out)
}()
return newObservableFromChannel(out)
}
16 changes: 16 additions & 0 deletions observablecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,19 @@ func TestRangeWithMaximumExceeded(t *testing.T) {
assert.NotNil(t, err)
assert.Nil(t, r)
}

func TestTimer(t *testing.T) {
d := new(mockDuration)
d.On("duration").Return(1 * time.Millisecond)

obs := Timer(d)

AssertThatObservable(t, obs, HasItems(float64(0)))
d.AssertCalled(t, "duration")
}

func TestTimerWithNilDuration(t *testing.T) {
obs := Timer(nil)

AssertThatObservable(t, obs, HasItems(float64(0)))
}

0 comments on commit 8f4a263

Please sign in to comment.