Skip to content

Commit

Permalink
feat: WaitForWithContext: apply code review suggestion
Browse files Browse the repository at this point in the history
the context should be passed to the condition checker
  • Loading branch information
ccoVeille committed Jun 30, 2024
1 parent 8f1405f commit 0af804c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3063,12 +3063,14 @@ iterations, duration, ok := lo.WaitFor(laterTrue, 10*time.Millisecond, 5*time.Mi

Runs periodically until a condition is validated or context is invalid.

The condition receives also the context, so it can invalidate the process in the condition checker

```go
ctx := context.Background()

alwaysTrue := func(i int) bool { return true }
alwaysFalse := func(i int) bool { return false }
laterTrue := func(i int) bool {
alwaysTrue := func(_ context.Context, i int) bool { return true }
alwaysFalse := func(_ context.Context, i int) bool { return false }
laterTrue := func(_ context.Context, i int) bool {
return i > 5
}

Expand Down
12 changes: 8 additions & 4 deletions concurrency.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ func Async6[A, B, C, D, E, F any](f func() (A, B, C, D, E, F)) <-chan Tuple6[A,

// WaitFor runs periodically until a condition is validated.
func WaitFor(condition func(i int) bool, maxDuration time.Duration, tick time.Duration) (int, time.Duration, bool) {
return WaitForWithContext(context.Background(), condition, maxDuration, tick)
// the context is not used, but WaitForWithContext signature needs it
conditionWithContext := func(_ context.Context, i int) bool {
return condition(i)
}
return WaitForWithContext(context.Background(), conditionWithContext, maxDuration, tick)
}

// WaitForWithContext runs periodically until a condition is validated or context is canceled.
func WaitForWithContext(ctx context.Context, condition func(i int) bool, maxDuration time.Duration, tick time.Duration) (int, time.Duration, bool) {
if condition(0) {
func WaitForWithContext(ctx context.Context, condition func(ctx context.Context, i int) bool, maxDuration time.Duration, tick time.Duration) (int, time.Duration, bool) {
if condition(ctx, 0) {
return 1, 0, true
}

Expand All @@ -128,7 +132,7 @@ func WaitForWithContext(ctx context.Context, condition func(i int) bool, maxDura
case <-timer.C:
return i, time.Since(start), false
case <-ticker.C:
if condition(i) {
if condition(ctx, i) {
return i + 1, time.Since(start), true
}

Expand Down
8 changes: 4 additions & 4 deletions concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ func TestWaitForWithContext(t *testing.T) {
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)

alwaysTrue := func(i int) bool { return true }
alwaysFalse := func(i int) bool { return false }
alwaysTrue := func(_ context.Context, _ int) bool { return true }
alwaysFalse := func(_ context.Context, _ int) bool { return false }

ctx := context.Background()

Expand All @@ -277,7 +277,7 @@ func TestWaitForWithContext(t *testing.T) {
is.InEpsilon(10*time.Millisecond, duration, float64(500*time.Microsecond))
is.False(ok)

laterTrue := func(i int) bool {
laterTrue := func(_ context.Context, i int) bool {
return i >= 5
}

Expand All @@ -291,7 +291,7 @@ func TestWaitForWithContext(t *testing.T) {
is.False(ok)

counter := 0
alwaysFalse = func(i int) bool {
alwaysFalse = func(_ context.Context, i int) bool {
is.Equal(counter, i)
counter++
return false
Expand Down

0 comments on commit 0af804c

Please sign in to comment.