From 08e28c8c1a7ed3f6d9513e927935774104d60a6c Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Thu, 17 Feb 2022 09:17:23 +0100 Subject: [PATCH 1/2] ofDelayedFunc improved --- of.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/of.go b/of.go index 4ec9492..f3a259e 100644 --- a/of.go +++ b/of.go @@ -12,19 +12,7 @@ func Of[T any](values ...T) <-chan T { // OfDelayed behaves like Of but with a pre-defined delay between each value func OfDelayed[T any](delay time.Duration, values ...T) <-chan T { - out := make(chan T, len(values)) - - go func(out chan T, values []T) { - for i, value := range values { - out <- value - if i < len(values)-1 { - time.Sleep(delay) - } - } - close(out) - }(out, values) - - return out + return OfDelayedFunc(func(value T) time.Duration { return delay }, values...) } // OfDelayedFunc behaves like OfDelayed but accepts a function to determine the delay From d5afefc540f877153f0e88f66e82b9a78cd062d3 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Thu, 17 Feb 2022 09:18:37 +0100 Subject: [PATCH 2/2] ofDelayedFunc further improved --- of.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/of.go b/of.go index f3a259e..d14e687 100644 --- a/of.go +++ b/of.go @@ -20,13 +20,13 @@ func OfDelayedFunc[T any](delayFunc func(value T) time.Duration, values ...T) <- out := make(chan T, len(values)) go func(out chan T, values []T) { + defer close(out) for i, value := range values { out <- value if i < len(values)-1 { time.Sleep(delayFunc(value)) } } - close(out) }(out, values) return out