Skip to content

Commit

Permalink
channel: Remove the WithTrigger wrapper type.
Browse files Browse the repository at this point in the history
At one point I needed this for something, but I can no longer find any use of
it so I am removing it from the package.
  • Loading branch information
creachadair committed Aug 25, 2021
1 parent 65bf9ad commit a5d1a82
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 67 deletions.
43 changes: 0 additions & 43 deletions channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,46 +200,3 @@ func TestHeaderTypeMismatch(t *testing.T) {
}
}
}

func TestWithTrigger(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r, w := io.Pipe()
triggered := false
ch := WithTrigger(test.framing(r, w), func() {
triggered = true
})

// Send a message to the channel, then close it.
const message = `["fools", "rush", "in"]`
done := make(chan struct{})
go func() {
defer close(done)
t.Log("Sending...")
if err := ch.Send([]byte(message)); err != nil {
t.Errorf("Send failed: %v", err)
}
t.Logf("Close: err=%v", ch.Close())
}()

// Read messages from the channel till it closes, then check that
// the trigger was correctly invoked.
for {
msg, err := ch.Recv()
if err == io.EOF {
t.Log("Recv: returned io.EOF")
break
} else if err != nil {
t.Errorf("Recv: unexpected error: %v", err)
break
}
t.Logf("Recv: msg=%q", string(msg))
}

<-done
if !triggered {
t.Error("After channel close: trigger not called")
}
})
}
}
24 changes: 0 additions & 24 deletions channel/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,6 @@ import (
// message-framing discipline.
type Framing func(io.Reader, io.WriteCloser) Channel

// WithTrigger returns a Channel that delegates I/O operations to ch, and when
// a Recv operation on ch returns io.EOF it synchronously calls the trigger.
func WithTrigger(ch Channel, trigger func()) Channel {
return triggered{ch: ch, trigger: trigger}
}

type triggered struct {
ch Channel
trigger func()
}

// Recv implements part of the channel.Channel interface. It delegates to the
// wrapped channel and calls the trigger when the delegate returns io.EOF.
func (c triggered) Recv() ([]byte, error) {
msg, err := c.ch.Recv()
if err == io.EOF {
c.trigger()
}
return msg, err
}

func (c triggered) Send(msg []byte) error { return c.ch.Send(msg) }
func (c triggered) Close() error { return c.ch.Close() }

type direct struct {
send chan<- []byte
recv <-chan []byte
Expand Down

0 comments on commit a5d1a82

Please sign in to comment.