mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-13 21:48:02 +08:00
32 lines
487 B
Go
32 lines
487 B
Go
|
package contextutils
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
func afterFunc(ctx context.Context, f func()) (stop func() bool) {
|
||
|
stopc := make(chan struct{})
|
||
|
once := sync.Once{} // either starts running f or stops f from running
|
||
|
if ctx.Done() != nil {
|
||
|
go func() {
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
once.Do(func() {
|
||
|
go f()
|
||
|
})
|
||
|
case <-stopc:
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
return func() bool {
|
||
|
stopped := false
|
||
|
once.Do(func() {
|
||
|
stopped = true
|
||
|
close(stopc)
|
||
|
})
|
||
|
return stopped
|
||
|
}
|
||
|
}
|