1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 11:12:28 +08:00

feat: add Repeate function to generate a chan of repeate values

This commit is contained in:
dudaodong
2022-04-15 16:08:14 +08:00
parent dc25bdab2f
commit a4900fecb4
2 changed files with 41 additions and 4 deletions

View File

@@ -14,14 +14,14 @@ func NewChannel() *Channel {
return &Channel{}
}
// Generate a data of type any chan
func (c *Channel) Generate(done <-chan any, datas ...any) <-chan any {
// Generate a data of type any chan, put param `values` into the chan
func (c *Channel) Generate(done <-chan any, values ...any) <-chan any {
dataStream := make(chan any)
go func() {
defer close(dataStream)
for _, v := range datas {
for _, v := range values {
select {
case <-done:
return
@@ -32,3 +32,23 @@ func (c *Channel) Generate(done <-chan any, datas ...any) <-chan any {
return dataStream
}
// Repeat return a data of type any chan, put param `values` into the chan repeatly,
// until close the `done` chan
func (c *Channel) Repeat(done <-chan any, values ...any) <-chan any {
dataStream := make(chan any)
go func() {
defer close(dataStream)
for {
for _, v := range values {
select {
case <-done:
return
case dataStream <- v:
}
}
}
}()
return dataStream
}