mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
chore: group handler type and methods together
This commit is contained in:
44
tea.go
44
tea.go
@@ -58,8 +58,6 @@ type Model interface {
|
||||
// update function.
|
||||
type Cmd func() Msg
|
||||
|
||||
type handlers []chan struct{}
|
||||
|
||||
type inputType int
|
||||
|
||||
const (
|
||||
@@ -102,6 +100,29 @@ const (
|
||||
withoutCatchPanics
|
||||
)
|
||||
|
||||
// handlers manages series of channels returned by various processes. It allows
|
||||
// us to wait for those processes to terminate before exiting the program.
|
||||
type handlers []chan struct{}
|
||||
|
||||
// Adds a channel to the list of handlers. We wait for all handlers to terminate
|
||||
// gracefully on shutdown.
|
||||
func (h *handlers) add(ch chan struct{}) {
|
||||
*h = append(*h, ch)
|
||||
}
|
||||
|
||||
// shutdown waits for all handlers to terminate.
|
||||
func (h handlers) shutdown() {
|
||||
var wg sync.WaitGroup
|
||||
for _, ch := range h {
|
||||
wg.Add(1)
|
||||
go func(ch chan struct{}) {
|
||||
<-ch
|
||||
wg.Done()
|
||||
}(ch)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// Program is a terminal user interface.
|
||||
type Program struct {
|
||||
initialModel Model
|
||||
@@ -678,22 +699,3 @@ func (p *Program) Printf(template string, args ...interface{}) {
|
||||
messageBody: fmt.Sprintf(template, args...),
|
||||
}
|
||||
}
|
||||
|
||||
// Adds a handler to the list of handlers. We wait for all handlers to terminate
|
||||
// gracefully on shutdown.
|
||||
func (h *handlers) add(ch chan struct{}) {
|
||||
*h = append(*h, ch)
|
||||
}
|
||||
|
||||
// Shutdown waits for all handlers to terminate.
|
||||
func (h handlers) shutdown() {
|
||||
var wg sync.WaitGroup
|
||||
for _, ch := range h {
|
||||
wg.Add(1)
|
||||
go func(ch chan struct{}) {
|
||||
<-ch
|
||||
wg.Done()
|
||||
}(ch)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user