diff --git a/README.md b/README.md index 7ef037a5..51460eed 100644 --- a/README.md +++ b/README.md @@ -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 } diff --git a/concurrency.go b/concurrency.go index d7fc26a1..036ee9c6 100644 --- a/concurrency.go +++ b/concurrency.go @@ -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 } @@ -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 } diff --git a/concurrency_test.go b/concurrency_test.go index f0f22718..9b9e8386 100644 --- a/concurrency_test.go +++ b/concurrency_test.go @@ -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() @@ -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 } @@ -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