From 5781b44b25f84181ca1137fcdb3a8c415ca283b3 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Tue, 15 Nov 2022 18:59:03 +0100 Subject: [PATCH] Revert "feat(concurrency): adding lo.WaitFor" This reverts commit 2f6f28e6fc887957991e165d0ca10a1dc136d71e. --- concurrency.go | 37 +------------------------------------ concurrency_test.go | 29 ----------------------------- 2 files changed, 1 insertion(+), 65 deletions(-) diff --git a/concurrency.go b/concurrency.go index 98bd0e25..2d6fd871 100644 --- a/concurrency.go +++ b/concurrency.go @@ -1,9 +1,6 @@ package lo -import ( - "sync" - "time" -) +import "sync" type synchronize struct { locker sync.Locker @@ -96,35 +93,3 @@ func Async6[A any, B any, C any, D any, E any, F any](f func() (A, B, C, D, E, F }() return ch } - -// WaitFor runs periodically until a condition is validated. -func WaitFor(condition func(i int) bool, maxDuration time.Duration, tick time.Duration) bool { - ch := make(chan bool, 1) - - timer := time.NewTimer(maxDuration) - defer timer.Stop() - - ticker := time.NewTicker(tick) - defer ticker.Stop() - - i := 0 - - for tick := ticker.C; ; { - select { - case <-timer.C: - return false - case <-tick: - tick = nil - currentIndex := i - i++ - go func() { ch <- condition(currentIndex) }() - case v := <-ch: - if v { - return true - } - - tick = ticker.C - } - - } -} diff --git a/concurrency_test.go b/concurrency_test.go index 953fcacb..ae65efdd 100644 --- a/concurrency_test.go +++ b/concurrency_test.go @@ -212,32 +212,3 @@ func TestAsyncX(t *testing.T) { } } } - -func TestWaitFor(t *testing.T) { - t.Parallel() - testWithTimeout(t, 100*time.Millisecond) - is := assert.New(t) - - alwaysTrue := func(i int) bool { return true } - alwaysFalse := func(i int) bool { return false } - - is.True(WaitFor(alwaysTrue, 10*time.Millisecond, time.Millisecond)) - is.False(WaitFor(alwaysFalse, 10*time.Millisecond, time.Millisecond)) - - laterTrue := func(i int) bool { - return i > 5 - } - - is.True(WaitFor(laterTrue, 10*time.Millisecond, time.Millisecond)) - is.False(WaitFor(laterTrue, 10*time.Millisecond, 5*time.Millisecond)) - - counter := 0 - - alwaysFalse = func(i int) bool { - is.Equal(counter, i) - counter++ - return false - } - - is.False(WaitFor(alwaysFalse, 10*time.Millisecond, time.Millisecond)) -}