fix(options): use exponential backoff on subscribe error retry

This commit is contained in:
Guillaume Chau
2024-01-15 10:26:13 +01:00
parent 72d4df5cce
commit b17817e9c9
@@ -1,5 +1,8 @@
import SmartApollo from './smart-apollo'
const MAX_RETRIES = 5
const DELAY_MS = 500
export default class SmartSubscription extends SmartApollo {
type = 'subscription'
vueApolloSpecialKeys = [
@@ -14,6 +17,8 @@ export default class SmartSubscription extends SmartApollo {
constructor (vm, key, options, autostart = true) {
super(vm, key, options)
this.attempts = 0
if (autostart) {
this.autostart()
}
@@ -73,6 +78,8 @@ export default class SmartSubscription extends SmartApollo {
nextResult (data) {
super.nextResult(data)
this.attempts = 0
if (typeof this.options.result === 'function') {
this.options.result.call(this.vm, data, this.key)
}
@@ -81,9 +88,19 @@ export default class SmartSubscription extends SmartApollo {
catchError (error) {
super.catchError(error)
// Restart the subscription
if (!this.skip) {
this.stop()
this.start()
if (this.skip || this.attempts >= MAX_RETRIES) {
return
}
this.stop()
// Restart the subscription with exponential backoff
this.retryTimeout = setTimeout(this.start.bind(this), Math.pow(2, this.attempts) * DELAY_MS)
this.attempts++
}
stop () {
super.stop()
clearTimeout(this.retryTimeout)
}
}