mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-26 12:57:44 +08:00
31 lines
660 B
Go
31 lines
660 B
Go
package async
|
|
|
|
import (
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
|
"github.com/jbenet/go-ipfs/blocks"
|
|
)
|
|
|
|
// ForwardN forwards up to |num| blocks to the returned channel.
|
|
func ForwardN(ctx context.Context, in <-chan *blocks.Block, num int) <-chan *blocks.Block {
|
|
out := make(chan *blocks.Block)
|
|
go func() {
|
|
defer close(out)
|
|
for i := 0; i < num; i++ {
|
|
select {
|
|
case block, ok := <-in:
|
|
if !ok {
|
|
return // otherwise nil value is forwarded to output
|
|
}
|
|
select {
|
|
case out <- block:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
return out
|
|
}
|