github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/utils/batch/entry.go (about)

     1  package batch
     2  
     3  type Entry[IN, OUT any] interface {
     4  	wait()
     5  	done()
     6  	Value() IN
     7  	Callback(out OUT)
     8  }
     9  
    10  type entry[IN, OUT any] struct {
    11  	v  IN
    12  	ch chan struct{}
    13  	cb func(OUT)
    14  }
    15  
    16  func NewEntry[IN, OUT any](v IN, callbackFn func(out OUT)) Entry[IN, OUT] {
    17  	return &entry[IN, OUT]{
    18  		v:  v,
    19  		cb: callbackFn,
    20  		ch: make(chan struct{}, 1),
    21  	}
    22  }
    23  
    24  func (e *entry[IN, OUT]) wait() {
    25  	<-e.ch
    26  }
    27  
    28  func (e *entry[IN, OUT]) done() {
    29  	e.ch <- struct{}{}
    30  }
    31  
    32  func (e *entry[IN, OUT]) Value() IN {
    33  	return e.v
    34  }
    35  
    36  func (e *entry[IN, OUT]) Callback(out OUT) {
    37  	if e.cb != nil {
    38  		e.cb(out)
    39  	}
    40  }