fix: allow Program.Run to be called again after the program exits (#1890)
Signed-off-by: simpleqt <2015900818@qq.com>
This commit is contained in:
parent
5982a61ef5
commit
55ba0f6de3
2 changed files with 85 additions and 0 deletions
17
tea.go
17
tea.go
|
|
@ -526,6 +526,10 @@ type Program struct {
|
|||
runMu sync.Mutex
|
||||
shutdownOnce sync.Once
|
||||
|
||||
// ranOnce reports whether [Program.Run] has been called before, so its
|
||||
// per-run state can be reinitialized on subsequent runs.
|
||||
ranOnce bool
|
||||
|
||||
profile *colorprofile.Profile // the terminal color profile
|
||||
|
||||
// where to send output, this will usually be os.Stdout.
|
||||
|
|
@ -1051,6 +1055,19 @@ func (p *Program) Run() (returnModel Model, returnErr error) {
|
|||
}()
|
||||
|
||||
// Initialize context and teardown channel.
|
||||
//
|
||||
// When a previous run already tore down its per-run state, recreate it so
|
||||
// the Program can be run again. The context is only recreated after an
|
||||
// actual run: replacing it unconditionally would swallow a
|
||||
// [Program.Kill] issued before the program started, which works by
|
||||
// cancelling the context created in NewProgram.
|
||||
if p.ranOnce {
|
||||
p.ctx, p.cancel = context.WithCancel(p.externalCtx)
|
||||
p.errs = make(chan error, 1)
|
||||
p.shutdownOnce = sync.Once{}
|
||||
}
|
||||
p.ranOnce = true
|
||||
|
||||
p.handlers = channelHandlers{}
|
||||
cmds := make(chan Cmd)
|
||||
|
||||
|
|
|
|||
68
tea_test.go
68
tea_test.go
|
|
@ -572,6 +572,74 @@ func TestTeaNoRun(t *testing.T) {
|
|||
)
|
||||
}
|
||||
|
||||
// runTwiceModel waits for a WindowSizeMsg before quitting, and reports whether
|
||||
// that message ever reached Update, so we can tell a real run from a
|
||||
// torn-down one.
|
||||
type runTwiceModel struct {
|
||||
updated atomic.Bool
|
||||
}
|
||||
|
||||
// Init returns no command on purpose: the program exits from Update once it
|
||||
// has seen a WindowSizeMsg, so the quit is ordered after the observation
|
||||
// rather than racing it.
|
||||
func (m *runTwiceModel) Init() Cmd { return nil }
|
||||
|
||||
func (m *runTwiceModel) Update(msg Msg) (Model, Cmd) {
|
||||
if _, ok := msg.(WindowSizeMsg); ok {
|
||||
m.updated.Store(true)
|
||||
return m, Quit
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *runTwiceModel) View() View { return NewView("hello") }
|
||||
|
||||
// TestTeaRunTwice is a regression test for running a Program a second time
|
||||
// after the first Run returned. Run resets its per-run state (handlers,
|
||||
// finished channel) at the top of every call, but before the fix the run
|
||||
// context, error channel, and shutdown guard stayed behind: the second run
|
||||
// tore down immediately with ErrProgramKilled and its shutdown was skipped,
|
||||
// leaving the terminal state it changed unrestored.
|
||||
func TestTeaRunTwice(t *testing.T) {
|
||||
t.Parallel()
|
||||
var buf bytes.Buffer
|
||||
var in bytes.Buffer
|
||||
|
||||
m := &runTwiceModel{}
|
||||
p := NewProgram(m,
|
||||
WithInput(&in),
|
||||
WithOutput(&buf),
|
||||
)
|
||||
|
||||
// The program quits from Update once it has observed a WindowSizeMsg, so
|
||||
// every run returns on its own; a bounded wait keeps a genuinely broken
|
||||
// run from hanging the test instead of failing it.
|
||||
run := func(which string) {
|
||||
t.Helper()
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := p.Run()
|
||||
done <- err
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("%s run: unexpected error: %v", which, err)
|
||||
}
|
||||
case <-time.After(30 * time.Second):
|
||||
t.Fatalf("%s run: Run did not return; the program never observed a WindowSizeMsg", which)
|
||||
}
|
||||
}
|
||||
|
||||
run("first")
|
||||
m.updated.Store(false)
|
||||
|
||||
run("second")
|
||||
if !m.updated.Load() {
|
||||
t.Error("second run never delivered messages to Update")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTeaPanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
var buf bytes.Buffer
|
||||
|
|
|
|||
Loading…
Reference in a new issue