github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/natives/src/sync/waitgroup.go (about)

     1  // +build js
     2  
     3  package sync
     4  
     5  type WaitGroup struct {
     6  	counter int
     7  	ch      chan struct{}
     8  
     9  	state1 [3]uint32
    10  }
    11  
    12  func (wg *WaitGroup) Add(delta int) {
    13  	wg.counter += delta
    14  	if wg.counter < 0 {
    15  		panic("sync: negative WaitGroup counter")
    16  	}
    17  	if wg.counter > 0 && wg.ch == nil {
    18  		wg.ch = make(chan struct{})
    19  	}
    20  	if wg.counter == 0 && wg.ch != nil {
    21  		close(wg.ch)
    22  		wg.ch = nil
    23  	}
    24  }
    25  
    26  func (wg *WaitGroup) Wait() {
    27  	if wg.counter > 0 {
    28  		<-wg.ch
    29  	}
    30  }