Skip to content

Commit

Permalink
chore: group handler type and methods together
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Jun 20, 2023
1 parent b1e7f42 commit f75684c
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ type Model interface {
// update function.
type Cmd func() Msg

type handlers []chan struct{}

type inputType int

const (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

0 comments on commit f75684c

Please sign in to comment.