1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00
lazygit/pkg/utils/once_writer.go
Jesse Duffield 755ae0ef84 add deadlock mutex package
write to deadlock stderr after closing gocui

more deadlock checking
2022-08-07 11:16:14 +10:00

32 lines
509 B
Go

package utils
import (
"io"
"sync"
)
// This wraps a writer and ensures that before we actually write anything we call a given function first
type OnceWriter struct {
writer io.Writer
once sync.Once
f func()
}
var _ io.Writer = &OnceWriter{}
func NewOnceWriter(writer io.Writer, f func()) *OnceWriter {
return &OnceWriter{
writer: writer,
f: f,
}
}
func (self *OnceWriter) Write(p []byte) (n int, err error) {
self.once.Do(func() {
self.f()
})
return self.writer.Write(p)
}