github.com/x-oss-byte/git-lfs@v2.5.2+incompatible/tools/channels.go (about)

     1  package tools
     2  
     3  import "fmt"
     4  
     5  // Interface for all types of wrapper around a channel of results and an error channel
     6  // Implementors will expose a type-specific channel for results
     7  // Call the Wait() function after processing the results channel to catch any errors
     8  // that occurred during the async processing
     9  type ChannelWrapper interface {
    10  	// Call this after processing results channel to check for async errors
    11  	Wait() error
    12  }
    13  
    14  // Base implementation of channel wrapper to just deal with errors
    15  type BaseChannelWrapper struct {
    16  	errorChan <-chan error
    17  }
    18  
    19  func (w *BaseChannelWrapper) Wait() error {
    20  	var err error
    21  	for e := range w.errorChan {
    22  		if err != nil {
    23  			// Combine in case multiple errors
    24  			err = fmt.Errorf("%v\n%v", err, e)
    25  
    26  		} else {
    27  			err = e
    28  		}
    29  	}
    30  
    31  	return err
    32  }
    33  
    34  func NewBaseChannelWrapper(errChan <-chan error) *BaseChannelWrapper {
    35  	return &BaseChannelWrapper{errorChan: errChan}
    36  }