github.com/blend/go-sdk@v1.20220411.3/async/signal_group.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package async
     9  
    10  import "sync"
    11  
    12  // SignalGroup is a wait group but it awaits on a signal.
    13  type SignalGroup struct {
    14  	wg sync.WaitGroup
    15  }
    16  
    17  // Add adds delta.
    18  func (sg *SignalGroup) Add(delta int) {
    19  	sg.wg.Add(delta)
    20  }
    21  
    22  // Done decrements delta.
    23  func (sg *SignalGroup) Done() {
    24  	sg.wg.Done()
    25  }
    26  
    27  // Wait returns a channel you can select from.
    28  func (sg *SignalGroup) Wait() <-chan struct{} {
    29  	finished := make(chan struct{})
    30  	go func() {
    31  		sg.wg.Wait()
    32  		close(finished)
    33  	}()
    34  	return finished
    35  }