Skip to content

Commit

Permalink
chore: store handlers and simplify teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Oct 7, 2022
1 parent 76ce669 commit 7c4961f
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"os/signal"
"runtime/debug"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -178,6 +179,7 @@ func (p *Program) handleSignals() chan struct{} {
select {
case <-p.ctx.Done():
return

case <-sig:
if !p.ignoreSignals {
p.msgs <- quitMsg{}
Expand Down Expand Up @@ -332,6 +334,7 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {

// StartReturningModel initializes the program. Returns the final model.
func (p *Program) StartReturningModel() (Model, error) {
var handlers []chan struct{}
cmds := make(chan Cmd)
p.errs = make(chan error)

Expand Down Expand Up @@ -360,7 +363,6 @@ func (p *Program) StartReturningModel() (Model, error) {
if !isFile {
break
}

if isatty.IsTerminal(f.Fd()) {
break
}
Expand All @@ -376,11 +378,8 @@ func (p *Program) StartReturningModel() (Model, error) {
}

// Handle signals.
sigintLoopDone := make(chan struct{})
if !p.startupOptions.has(withoutSignalHandler) {
sigintLoopDone = p.handleSignals()
} else {
close(sigintLoopDone)
handlers = append(handlers, p.handleSignals())
}

// Recover from panics.
Expand Down Expand Up @@ -417,18 +416,19 @@ func (p *Program) StartReturningModel() (Model, error) {
}

// Initialize the program.
initSignalDone := make(chan struct{})
model := p.initialModel
if initCmd := model.Init(); initCmd != nil {
ch := make(chan struct{})
handlers = append(handlers, ch)

go func() {
defer close(initSignalDone)
defer close(ch)

select {
case cmds <- initCmd:
case <-p.ctx.Done():
}
}()
} else {
close(initSignalDone)
}

// Start the renderer.
Expand All @@ -442,16 +442,16 @@ func (p *Program) StartReturningModel() (Model, error) {
if err := p.initCancelReader(); err != nil {
return model, err
}
} else {
defer close(p.readLoopDone)
defer p.cancelReader.Close() //nolint:errcheck

handlers = append(handlers, p.readLoopDone)
}
defer p.cancelReader.Close() //nolint:errcheck

// Handle resize events.
resizeLoopDone := p.handleResize()
handlers = append(handlers, p.handleResize())

// Process commands.
cmdLoopDone := p.handleCommands(cmds)
handlers = append(handlers, p.handleCommands(cmds))

// Run event loop, handle updates and draw.
model, err := p.eventLoop(model, cmds)
Expand All @@ -463,10 +463,19 @@ func (p *Program) StartReturningModel() (Model, error) {
if p.cancelReader.Cancel() {
p.waitForReadLoop()
}
<-cmdLoopDone
<-resizeLoopDone
<-sigintLoopDone
<-initSignalDone

// Wait for all handlers to finish.
var wg sync.WaitGroup
for _, ch := range handlers {
wg.Add(1)
go func(ch chan struct{}) {
<-ch
wg.Done()
}(ch)
}
wg.Wait()

// Restore terminal state.
p.shutdown(false)

return model, err
Expand Down

0 comments on commit 7c4961f

Please sign in to comment.