Skip to content

Commit e10eefa

Browse files
committed
add: client.Done()
1 parent 50f1453 commit e10eefa

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Client struct {
1818
chErr chan error
1919
chResp chan response
2020
closer io.Closer
21+
done chan struct{}
2122
}
2223

2324
// Default timeout options.
@@ -72,6 +73,7 @@ func NewClient(rwc io.ReadWriteCloser, password string, opts ...Option) (*Client
7273
chErr: make(chan error, 1),
7374
chResp: make(chan response),
7475
closer: rwc,
76+
done: make(chan struct{}),
7577
}
7678

7779
go client.runReader(cfg.events, cfg.autoClose)
@@ -87,6 +89,11 @@ func (c *Client) Close() error {
8789
return c.closer.Close() //nolint:wrapcheck
8890
}
8991

92+
// Done returns a channel that will be closed when the client connection is closed.
93+
func (c *Client) Done() <-chan struct{} {
94+
return c.done
95+
}
96+
9097
// API sends a command to the API and returns the response body or an error.
9198
//
9299
// Send a FreeSWITCH API command, blocking mode. That is, the FreeSWITCH
@@ -265,6 +272,7 @@ func (c *Client) runReader(events chan<- Event, autoClose bool) {
265272
defer func() {
266273
close(c.chResp)
267274
close(c.chErr)
275+
close(c.done)
268276

269277
if autoClose && events != nil {
270278
close(events)

example_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/mdigger/esl"
77
)
88

9+
//nolint:testableexamples
910
func Example() {
1011
// initialize buffered events channel
1112
events := make(chan esl.Event, 1)
@@ -37,14 +38,7 @@ func Example() {
3738
}
3839

3940
// read events
40-
go func() {
41-
for ev := range events {
42-
fmt.Println(ev.Name(), ev.Get("Job-UUID"))
43-
}
44-
}()
45-
46-
//Output:
47-
// 0 total.
48-
//
49-
// BACKGROUND_JOB test-xxx
41+
for ev := range events {
42+
fmt.Println(ev.Name(), ev.Get("Job-UUID"))
43+
}
5044
}

0 commit comments

Comments
 (0)