github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/natives/src/sync/waitgroup.go (about)

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