Source File
context_watcher.go
Belonging Package
github.com/jackc/pgconn/internal/ctxwatch
package ctxwatch
import (
)
type ContextWatcher struct {
onCancel func()
onUnwatchAfterCancel func()
unwatchChan chan struct{}
watchInProgress bool
onCancelWasCalled bool
}
func ( func(), func()) *ContextWatcher {
:= &ContextWatcher{
onCancel: ,
onUnwatchAfterCancel: ,
unwatchChan: make(chan struct{}),
}
return
}
func ( *ContextWatcher) ( context.Context) {
if .watchInProgress {
panic("Watch already in progress")
}
.onCancelWasCalled = false
if .Done() != nil {
.watchInProgress = true
go func() {
select {
case <-.Done():
.onCancel()
.onCancelWasCalled = true
<-.unwatchChan
case <-.unwatchChan:
}
}()
} else {
.watchInProgress = false
}
}
func ( *ContextWatcher) () {
if .watchInProgress {
.unwatchChan <- struct{}{}
if .onCancelWasCalled {
.onUnwatchAfterCancel()
}
.watchInProgress = false
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |