refactor: use sync.OnceValue

This commit is contained in:
oliverpool
2025-09-22 09:33:32 +02:00
committed by Vincent Bernat
parent d2536398e9
commit f3ebff6ce5

View File

@@ -14,28 +14,18 @@ import (
"sync" "sync"
) )
var ( //go:embed data/embed.zip
//go:embed data/embed.zip var embeddedZip []byte
embeddedZip []byte
data fs.FS var dataOnce = sync.OnceValue(func() *zip.Reader {
dataOnce sync.Once r, err := zip.NewReader(bytes.NewReader(embeddedZip), int64(len(embeddedZip)))
dataReady chan struct{} if err != nil {
) panic(fmt.Sprintf("cannot read embedded archive: %s", err))
}
return r
})
// Data returns a filesystem with the files contained in the embedded archive. // Data returns a filesystem with the files contained in the embedded archive.
func Data() fs.FS { func Data() fs.FS {
dataOnce.Do(func() { return dataOnce()
r, err := zip.NewReader(bytes.NewReader(embeddedZip), int64(len(embeddedZip)))
if err != nil {
panic(fmt.Sprintf("cannot read embedded archive: %s", err))
}
data = r
close(dataReady)
})
<-dataReady
return data
}
func init() {
dataReady = make(chan struct{})
} }