File tree Expand file tree Collapse file tree 1 file changed +20
-3
lines changed
packages/vue-apollo-option/src Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Original file line number Diff line number Diff line change 1
1
import SmartApollo from './smart-apollo'
2
2
3
+ const MAX_RETRIES = 5
4
+ const DELAY_MS = 500
5
+
3
6
export default class SmartSubscription extends SmartApollo {
4
7
type = 'subscription'
5
8
vueApolloSpecialKeys = [
@@ -14,6 +17,8 @@ export default class SmartSubscription extends SmartApollo {
14
17
constructor ( vm , key , options , autostart = true ) {
15
18
super ( vm , key , options )
16
19
20
+ this . attempts = 0
21
+
17
22
if ( autostart ) {
18
23
this . autostart ( )
19
24
}
@@ -73,6 +78,8 @@ export default class SmartSubscription extends SmartApollo {
73
78
nextResult ( data ) {
74
79
super . nextResult ( data )
75
80
81
+ this . attempts = 0
82
+
76
83
if ( typeof this . options . result === 'function' ) {
77
84
this . options . result . call ( this . vm , data , this . key )
78
85
}
@@ -81,9 +88,19 @@ export default class SmartSubscription extends SmartApollo {
81
88
catchError ( error ) {
82
89
super . catchError ( error )
83
90
// Restart the subscription
84
- if ( ! this . skip ) {
85
- this . stop ( )
86
- this . start ( )
91
+ if ( this . skip || this . attempts >= MAX_RETRIES ) {
92
+ return
87
93
}
94
+
95
+ this . stop ( )
96
+
97
+ // Restart the subscription with exponential backoff
98
+ this . retryTimeout = setTimeout ( this . start . bind ( this ) , Math . pow ( 2 , this . attempts ) * DELAY_MS )
99
+ this . attempts ++
100
+ }
101
+
102
+ stop ( ) {
103
+ super . stop ( )
104
+ clearTimeout ( this . retryTimeout )
88
105
}
89
106
}
You can’t perform that action at this time.
0 commit comments