local-server/vendor/github.com/markbates/oncer/oncer.go

27 lines
366 B
Go
Raw Normal View History

2018-11-13 03:44:15 +00:00
package oncer
import (
"sync"
)
var onces = &sync.Map{}
func Do(name string, fn func()) {
o, _ := onces.LoadOrStore(name, &sync.Once{})
if once, ok := o.(*sync.Once); ok {
once.Do(fn)
}
}
func Reset(names ...string) {
if len(names) == 0 {
onces = &sync.Map{}
return
}
for _, n := range names {
onces.Delete(n)
onces.Delete(deprecated + n)
}
}