mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
feat: tea.WithContext ProgramOption to supply a context
WithContext lets you specify a context in which to run the Program. This is useful if you want to cancel the execution from outside. When a Program gets cancelled it will exit with an error ErrProgramKilled.
This commit is contained in:
23
tea_test.go
23
tea_test.go
@@ -2,6 +2,7 @@ package tea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -97,6 +98,28 @@ func TestTeaKill(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTeaContext(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var buf bytes.Buffer
|
||||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithContext(ctx), WithInput(&in), WithOutput(&buf))
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
if m.executed.Load() != nil {
|
||||
cancel()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := p.Run(); err != ErrProgramKilled {
|
||||
t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTeaBatchMsg(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
var in bytes.Buffer
|
||||
|
||||
Reference in New Issue
Block a user