Skip to content

Commit

Permalink
feat: add ctx ready key for answer
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Nov 2, 2023
1 parent cb76edc commit 9c89dd0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ Features:
- [ ] DTMF passing


Checkout `echome` example to see more.


14 changes: 14 additions & 0 deletions phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ type AnswerOptions struct {
// Answer will answer call
// Closing ansCtx will close listeners or it will be closed on BYE
// TODO: reusing listener

var (
// You can use this key with AnswerReadyCtxValue to get signal when
// Answer is ready to receive traffic
AnswerReadyCtxKey = "AnswerReadyCtxKey"
)

type AnswerReadyCtxValue chan struct{}

func (p *Phone) Answer(ansCtx context.Context, opts AnswerOptions) (*DialDialog, error) {
ringtime := opts.Ringtime

Expand Down Expand Up @@ -786,10 +795,15 @@ func (p *Phone) Answer(ansCtx context.Context, opts AnswerOptions) (*DialDialog,
go l.Listen()
}

if v := ctx.Value(AnswerReadyCtxKey); v != nil {
close(v.(AnswerReadyCtxValue))
}

select {
case d = <-waitDialog:
return d, nil
case <-ctx.Done():
stopAnswer()
return nil, ctx.Err()
}
}
Expand Down

0 comments on commit 9c89dd0

Please sign in to comment.