github.com/jwowillo/pipe@v1.2.0/consume.go (about)

     1  package pipe
     2  
     3  import "sync"
     4  
     5  // Consumer consumes Items received from Pipes.
     6  type Consumer interface {
     7  	Consume(Item)
     8  }
     9  
    10  // ConsumerFunc converts a function to a Consumer.
    11  type ConsumerFunc func(Item)
    12  
    13  // Consume applies the function to the Item.
    14  func (f ConsumerFunc) Consume(x Item) {
    15  	f(x)
    16  }
    17  
    18  // ProcessAndConsume processes all the Items with the Pipe and gives them to the
    19  // Consumer as they are delivered.
    20  func ProcessAndConsume(p *Pipe, c Consumer, xs ...Item) {
    21  	for _, x := range xs {
    22  		p.Receive(x)
    23  	}
    24  	var wg sync.WaitGroup
    25  	wg.Add(len(xs))
    26  	for i := 0; i < len(xs); i++ {
    27  		go func() {
    28  			c.Consume(p.Deliver())
    29  			wg.Done()
    30  		}()
    31  	}
    32  	wg.Wait()
    33  }