mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 17:44:59 +09:00
24 lines
693 B
Go
24 lines
693 B
Go
package contexthelper
|
|
|
|
import "context"
|
|
|
|
// ContextWithCloseChan returns a context that is canceled when either the parent context
|
|
// is canceled or when the provided close channel is closed.
|
|
func ContextWithCloseChan(ctx context.Context, closeChan <-chan struct{}) (context.Context, context.CancelFunc) {
|
|
// Create a new context that can be canceled
|
|
newCtx, cancel := context.WithCancel(ctx)
|
|
|
|
// Start a goroutine that waits for either the closeChan to be closed or
|
|
// the new context to be canceled
|
|
go func() {
|
|
select {
|
|
case <-closeChan:
|
|
cancel()
|
|
case <-newCtx.Done():
|
|
// newCtx is canceled, goroutine exits
|
|
}
|
|
}()
|
|
|
|
// Return the cancel function
|
|
return newCtx, cancel
|
|
}
|