mirror of
https://github.com/mum4k/termdash.git
synced 2025-04-27 13:48:49 +08:00
Errors are no longer a special case in the EDS.
And changing event queue so that Pullers don't have to process an error.
This commit is contained in:
parent
36bf99ebc7
commit
286e5abd2f
@ -73,21 +73,8 @@ func newSubscriber(filter []terminalapi.Event, cb Callback) *subscriber {
|
|||||||
// Terminates when the context expires.
|
// Terminates when the context expires.
|
||||||
func (s *subscriber) run(ctx context.Context) {
|
func (s *subscriber) run(ctx context.Context) {
|
||||||
for {
|
for {
|
||||||
e, err := s.queue.Pull(ctx)
|
ev := s.queue.Pull(ctx)
|
||||||
if err != nil {
|
if ev != nil {
|
||||||
e = terminalapi.NewErrorf("failed to pull event off the queue: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ev := e.(type) {
|
|
||||||
case *terminalapi.Error:
|
|
||||||
// Don't forward the error if the context is closed.
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
default:
|
|
||||||
s.cb(ev)
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
s.cb(ev)
|
s.cb(ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,9 +92,8 @@ func (s *subscriber) event(ev terminalapi.Event) {
|
|||||||
s.queue.Push(ev)
|
s.queue.Push(ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
var errT *terminalapi.Error
|
|
||||||
t := reflect.TypeOf(ev)
|
t := reflect.TypeOf(ev)
|
||||||
if t == reflect.TypeOf(errT) || s.filter[t] {
|
if s.filter[t] {
|
||||||
s.queue.Push(ev)
|
s.queue.Push(ev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,9 +153,6 @@ type StopFunc func()
|
|||||||
// all kinds. If the filter is non-empty, only events of the provided type will
|
// all kinds. If the filter is non-empty, only events of the provided type will
|
||||||
// be sent to the subscriber.
|
// be sent to the subscriber.
|
||||||
// Returns a function that allows the subscriber to unsubscribe.
|
// Returns a function that allows the subscriber to unsubscribe.
|
||||||
//
|
|
||||||
// Subscribers cannot filter out event type terminalapi.Error which is used to
|
|
||||||
// forward errors to the Callback.
|
|
||||||
func (eds *DistributionSystem) Subscribe(filter []terminalapi.Event, cb Callback) StopFunc {
|
func (eds *DistributionSystem) Subscribe(filter []terminalapi.Event, cb Callback) StopFunc {
|
||||||
eds.mu.Lock()
|
eds.mu.Lock()
|
||||||
defer eds.mu.Unlock()
|
defer eds.mu.Unlock()
|
||||||
|
@ -187,7 +187,7 @@ func TestDistributionSystem(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "single subscriber, errors are always received",
|
desc: "single subscriber, wants errors only",
|
||||||
events: []terminalapi.Event{
|
events: []terminalapi.Event{
|
||||||
&terminalapi.Keyboard{Key: keyboard.KeyEnter},
|
&terminalapi.Keyboard{Key: keyboard.KeyEnter},
|
||||||
&terminalapi.Mouse{Position: image.Point{1, 1}},
|
&terminalapi.Mouse{Position: image.Point{1, 1}},
|
||||||
@ -197,14 +197,11 @@ func TestDistributionSystem(t *testing.T) {
|
|||||||
subCase: []*subscriberCase{
|
subCase: []*subscriberCase{
|
||||||
{
|
{
|
||||||
filter: []terminalapi.Event{
|
filter: []terminalapi.Event{
|
||||||
&terminalapi.Keyboard{},
|
terminalapi.NewError(""),
|
||||||
&terminalapi.Mouse{},
|
|
||||||
},
|
},
|
||||||
rec: newReceiver(receiverModeReceive),
|
rec: newReceiver(receiverModeReceive),
|
||||||
want: map[terminalapi.Event]bool{
|
want: map[terminalapi.Event]bool{
|
||||||
&terminalapi.Keyboard{Key: keyboard.KeyEnter}: true,
|
terminalapi.NewError("error"): true,
|
||||||
&terminalapi.Mouse{Position: image.Point{1, 1}}: true,
|
|
||||||
terminalapi.NewError("error"): true,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -230,8 +227,6 @@ func TestDistributionSystem(t *testing.T) {
|
|||||||
want: map[terminalapi.Event]bool{
|
want: map[terminalapi.Event]bool{
|
||||||
&terminalapi.Keyboard{Key: keyboard.KeyEnter}: true,
|
&terminalapi.Keyboard{Key: keyboard.KeyEnter}: true,
|
||||||
&terminalapi.Keyboard{Key: keyboard.KeyEsc}: true,
|
&terminalapi.Keyboard{Key: keyboard.KeyEsc}: true,
|
||||||
terminalapi.NewError("error1"): true,
|
|
||||||
terminalapi.NewError("error2"): true,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -245,8 +240,6 @@ func TestDistributionSystem(t *testing.T) {
|
|||||||
&terminalapi.Mouse{Position: image.Point{1, 1}}: true,
|
&terminalapi.Mouse{Position: image.Point{1, 1}}: true,
|
||||||
&terminalapi.Resize{Size: image.Point{1, 1}}: true,
|
&terminalapi.Resize{Size: image.Point{1, 1}}: true,
|
||||||
&terminalapi.Resize{Size: image.Point{2, 2}}: true,
|
&terminalapi.Resize{Size: image.Point{2, 2}}: true,
|
||||||
terminalapi.NewError("error1"): true,
|
|
||||||
terminalapi.NewError("error2"): true,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -268,8 +261,6 @@ func TestDistributionSystem(t *testing.T) {
|
|||||||
want: map[terminalapi.Event]bool{
|
want: map[terminalapi.Event]bool{
|
||||||
&terminalapi.Keyboard{Key: keyboard.KeyEnter}: true,
|
&terminalapi.Keyboard{Key: keyboard.KeyEnter}: true,
|
||||||
&terminalapi.Keyboard{Key: keyboard.KeyEsc}: true,
|
&terminalapi.Keyboard{Key: keyboard.KeyEsc}: true,
|
||||||
terminalapi.NewError("error1"): true,
|
|
||||||
terminalapi.NewError("error2"): true,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -280,8 +271,6 @@ func TestDistributionSystem(t *testing.T) {
|
|||||||
want: map[terminalapi.Event]bool{
|
want: map[terminalapi.Event]bool{
|
||||||
&terminalapi.Keyboard{Key: keyboard.KeyEnter}: true,
|
&terminalapi.Keyboard{Key: keyboard.KeyEnter}: true,
|
||||||
&terminalapi.Keyboard{Key: keyboard.KeyEsc}: true,
|
&terminalapi.Keyboard{Key: keyboard.KeyEsc}: true,
|
||||||
terminalapi.NewError("error1"): true,
|
|
||||||
terminalapi.NewError("error2"): true,
|
|
||||||
},
|
},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
|
@ -124,10 +124,10 @@ func (u *Unbound) Pop() terminalapi.Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pull is like Pop(), but blocks until an item is available or the context
|
// Pull is like Pop(), but blocks until an item is available or the context
|
||||||
// expires.
|
// expires. Returns a nil event if the context expired.
|
||||||
func (u *Unbound) Pull(ctx context.Context) (terminalapi.Event, error) {
|
func (u *Unbound) Pull(ctx context.Context) terminalapi.Event {
|
||||||
if e := u.Pop(); e != nil {
|
if e := u.Pop(); e != nil {
|
||||||
return e, nil
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
u.cond.L.Lock()
|
u.cond.L.Lock()
|
||||||
@ -135,12 +135,12 @@ func (u *Unbound) Pull(ctx context.Context) (terminalapi.Event, error) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil, ctx.Err()
|
return nil
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if e := u.Pop(); e != nil {
|
if e := u.Pop(); e != nil {
|
||||||
return e, nil
|
return e
|
||||||
}
|
}
|
||||||
u.cond.Wait()
|
u.cond.Wait()
|
||||||
}
|
}
|
||||||
|
@ -84,10 +84,7 @@ func TestPullEventAvailable(t *testing.T) {
|
|||||||
q.Push(want)
|
q.Push(want)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
got, err := q.Pull(ctx)
|
got := q.Pull(ctx)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Pull => unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
if diff := pretty.Compare(want, got); diff != "" {
|
if diff := pretty.Compare(want, got); diff != "" {
|
||||||
t.Errorf("Pull => unexpected diff (-want, +got):\n%s", diff)
|
t.Errorf("Pull => unexpected diff (-want, +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
@ -107,16 +104,12 @@ func TestPullBlocksUntilAvailable(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if _, err := q.Pull(ctx); err == nil {
|
if got := q.Pull(ctx); got != nil {
|
||||||
t.Fatal("Pull => expected timeout error, got nil")
|
t.Fatalf("Pull => %v, want <nil>", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
close(ch)
|
close(ch)
|
||||||
got, err := q.Pull(context.Background())
|
got := q.Pull(context.Background())
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Pull => unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := pretty.Compare(want, got); diff != "" {
|
if diff := pretty.Compare(want, got); diff != "" {
|
||||||
t.Errorf("Pull => unexpected diff (-want, +got):\n%s", diff)
|
t.Errorf("Pull => unexpected diff (-want, +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
|
@ -195,9 +195,9 @@ func (t *Terminal) Event(ctx context.Context) terminalapi.Event {
|
|||||||
return terminalapi.NewErrorf("no event queue provided, use the WithEventQueue option when creating the fake terminal")
|
return terminalapi.NewErrorf("no event queue provided, use the WithEventQueue option when creating the fake terminal")
|
||||||
}
|
}
|
||||||
|
|
||||||
ev, err := t.events.Pull(ctx)
|
ev := t.events.Pull(ctx)
|
||||||
if err != nil {
|
if ev == nil {
|
||||||
return terminalapi.NewErrorf("unable to pull the next event: %v", err)
|
return terminalapi.NewError("unable to pull the next event")
|
||||||
}
|
}
|
||||||
|
|
||||||
if res, ok := ev.(*terminalapi.Resize); ok {
|
if res, ok := ev.(*terminalapi.Resize); ok {
|
||||||
|
@ -148,9 +148,9 @@ func (t *Terminal) pollEvents() {
|
|||||||
|
|
||||||
// Event implements terminalapi.Terminal.Event.
|
// Event implements terminalapi.Terminal.Event.
|
||||||
func (t *Terminal) Event(ctx context.Context) terminalapi.Event {
|
func (t *Terminal) Event(ctx context.Context) terminalapi.Event {
|
||||||
ev, err := t.events.Pull(ctx)
|
ev := t.events.Pull(ctx)
|
||||||
if err != nil {
|
if ev == nil {
|
||||||
return terminalapi.NewErrorf("unable to pull the next event: %v", err)
|
return terminalapi.NewError("unable to pull the next event")
|
||||||
}
|
}
|
||||||
return ev
|
return ev
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user