Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onceAsync: reject with custom error on disconnect #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ export class FreeSwitchTimeout extends Error {
}
}

export class FreeSwitchDisconnect extends Error {
public readonly text: string
constructor (text: string) {
super()
this.text = text
}

toString (): string {
return `FreeSwitchDisconnect: Disconnected while waiting for ${this.text}`
}
}

// List from https://github.com/signalwire/freeswitch/blob/master/src/switch_event.c#L137
type EventName =
| 'CUSTOM'
Expand Down Expand Up @@ -605,13 +617,23 @@ export class FreeSwitchResponse extends FreeSwitchEventEmitter<keyof FreeSwitchR
cleanup()
reject(new Error(`end() called (${this.__ref}) while waiting for ${event} in ${comment}`))
}
const on_disconnect = (): void => {
this.logger.error('FreeSwitchResponse: onceAsync: on_disconnect', {
event,
comment,
ref: this.__ref
})
cleanup()
reject(new FreeSwitchDisconnect(`(${this.__ref}) ${event} in ${comment}`))
}
let timer: ReturnType<typeof setTimeout> | undefined
const cleanup = (): void => {
this.removeListener(event, on_event)
this.removeListener('socket.error', on_error)
this.removeListener('socket.close', on_close)
this.removeListener('socket.write', on_error)
this.removeListener('socket.end', on_end)
this.removeListener('freeswitch_disconnect', on_disconnect)
clearTimeout(timer)
}

Expand Down Expand Up @@ -640,6 +662,7 @@ export class FreeSwitchResponse extends FreeSwitchEventEmitter<keyof FreeSwitchR
this.once('socket.close', on_close)
this.once('socket.write', on_error)
this.once('socket.end', on_end)
this.once('freeswitch_disconnect', on_disconnect)
if (timeout != null) {
const on_timeout = (): void => {
this.logger.error('FreeSwitchResponse: onceAsync: on_timeout', {
Expand Down