github.com/lingyao2333/mo-zero@v1.4.1/core/syncx/donechan.go (about)

     1  package syncx
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/lingyao2333/mo-zero/core/lang"
     7  )
     8  
     9  // A DoneChan is used as a channel that can be closed multiple times and wait for done.
    10  type DoneChan struct {
    11  	done chan lang.PlaceholderType
    12  	once sync.Once
    13  }
    14  
    15  // NewDoneChan returns a DoneChan.
    16  func NewDoneChan() *DoneChan {
    17  	return &DoneChan{
    18  		done: make(chan lang.PlaceholderType),
    19  	}
    20  }
    21  
    22  // Close closes dc, it's safe to close more than once.
    23  func (dc *DoneChan) Close() {
    24  	dc.once.Do(func() {
    25  		close(dc.done)
    26  	})
    27  }
    28  
    29  // Done returns a channel that can be notified on dc closed.
    30  func (dc *DoneChan) Done() chan lang.PlaceholderType {
    31  	return dc.done
    32  }