package sync

import 

// OnceErr wraps the behavior of recording an error
// once and signal on a channel when this has occurred.
// Signaling is done by closing of the channel.
//
// Type is safe for concurrent usage.
type OnceErr struct {
	mu  sync.RWMutex
	err error
	ch  chan struct{}
}

// NewOnceErr return a new OnceErr
func () *OnceErr {
	return &OnceErr{
		ch: make(chan struct{}, 1),
	}
}

// Err acquires a read-lock and returns an
// error if one has been set.
func ( *OnceErr) () error {
	.mu.RLock()
	 := .err
	.mu.RUnlock()

	return 
}

// SetError acquires a write-lock and will set
// the underlying error value if one has not been set.
func ( *OnceErr) ( error) {
	if  == nil {
		return
	}

	.mu.Lock()
	if .err == nil {
		.err = 
		close(.ch)
	}
	.mu.Unlock()
}

// ErrorSet returns a channel that will be used to signal
// that an error has been set. This channel will be closed
// when the error value has been set for OnceErr.
func ( *OnceErr) () <-chan struct{} {
	return .ch
}