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

fix: emit error on timeout request and disconnect #42

Closed
Closed
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
20 changes: 9 additions & 11 deletions src/Ember/Client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,9 @@ export class EmberClient extends EventEmitter<EmberClientEvents> {
this._client.on('error', (e) => this.emit('error', e))
this._client.on('connected', () => this.emit('connected'))
this._client.on('disconnected', () => {
this._requests.forEach((req) => {
req.reject(new Error('Socket was disconnected'))
this._requests.delete(req.reqId)
})
this.emit('disconnected')
// Clanup and Emit disconnected event, and let the client handle that the connection is lost
this._requests.clear() // Just clean up the requests
this.emit('disconnected') // Notify listeners about the disconnection
})
}

Expand Down Expand Up @@ -172,10 +170,9 @@ export class EmberClient extends EventEmitter<EmberClientEvents> {
this._client.removeAllListeners()
// @ts-expect-error: after using this method, properties are no longer expected to always exist
delete this._client
this._requests.forEach((req) => {
req.reject(new Error('Socket was disconnected'))
this._requests.delete(req.reqId)
})
this._requests.clear() // Just clean up the requests
this.emit('disconnected') // Notify listeners about the disconnection

clearInterval(this._timer)
}

Expand Down Expand Up @@ -722,11 +719,12 @@ export class EmberClient extends EventEmitter<EmberClientEvents> {
if (sent) {
req.lastSent = Date.now()
} else {
req.reject(new Error('Request was not sent correctly'))
this.emit('error', new Error('Request was not sent correctly'))
}
}
if (sinceFirstSent >= this._timeout) {
req.reject(new Error('Request timed out'))
// Emit error and remove request
this.emit('error', new Error('Request timed out'))
this._requests.delete(req.reqId)
}
})
Expand Down
Loading