@@ -15,6 +15,7 @@ const {
15
15
16
16
let evtSource = null ;
17
17
let sseReconnectFrequencySeconds = 1 ;
18
+ let shouldCloseEvtSource = false ;
18
19
19
20
const axiosInstance = axios . create ( {
20
21
baseURL : `${ apiEndpoint } /${ apiVersion } ` ,
@@ -39,15 +40,18 @@ const ApiService = {
39
40
40
41
if ( process . type === "main" ) {
41
42
ipcMain . on ( "CONNECT_EVENT_STREAM" , ( evt , args ) => this . onConnectEventStream ( ) ) ;
42
- ipcMain . on ( "CLOSE_EVENT_STREAM" , ( evt , args ) => {
43
- if ( evtSource ) evtSource . close ( ) ;
44
- } ) ;
43
+ ipcMain . on ( "CLOSE_EVENT_STREAM" , ( evt , args ) => this . onCloseEventStream ( ) ) ;
45
44
}
46
45
47
- if ( store . getters . activeSession . id !== "" )
48
- this . connectEventStream ( ) ;
46
+ if ( store . getters . activeSession . id !== "" ) this . connectEventStream ( ) ;
49
47
} ,
50
48
49
+ /**
50
+ * Create a new session on the server
51
+ *
52
+ * @param {{id}[] } votingOptions for the sessions
53
+ * @returns {{sessionId:String, accessToken:{}} } session data
54
+ */
51
55
async joinSession ( votingOptions ) {
52
56
const { data } = await axiosInstance . post ( "sessions" , {
53
57
votingOptions : votingOptions . map ( x => x . id ) ,
@@ -56,6 +60,11 @@ const ApiService = {
56
60
return data ;
57
61
} ,
58
62
63
+ /**
64
+ * Abandon a session on the server
65
+ *
66
+ * @param {String } sessionId
67
+ */
59
68
async leaveSession ( sessionId ) {
60
69
await axiosInstance . delete ( `sessions/${ sessionId } ` , {
61
70
headers : { Authorization : `Bearer ${ store . getters . accessToken } ` } ,
@@ -78,22 +87,32 @@ const ApiService = {
78
87
return ;
79
88
}
80
89
90
+ this . onCloseEventStream ( ) ;
91
+ } ,
92
+
93
+ onCloseEventStream ( ) {
94
+ shouldCloseEvtSource = true ;
81
95
if ( evtSource ) evtSource . close ( ) ;
96
+ store . dispatch ( "updateSessionStreamState" , null ) ;
97
+ console . log ( "stream closed" ) ;
82
98
} ,
83
99
84
100
onConnectEventStream ( ) {
85
101
console . log ( "Connecting eventstream" ) ;
102
+ shouldCloseEvtSource = false ;
86
103
const waitFunc = ( ) => sseReconnectFrequencySeconds * 1000 ;
87
104
const tryToSetupFunc = ( ) => {
88
- setupEventSource ( ) ;
89
- sseReconnectFrequencySeconds *= 2 ;
90
- if ( sseReconnectFrequencySeconds >= 64 ) {
91
- sseReconnectFrequencySeconds = 64 ;
105
+ if ( ! shouldCloseEvtSource ) {
106
+ setupEventSource ( ) ;
107
+ sseReconnectFrequencySeconds *= 2 ;
108
+ if ( sseReconnectFrequencySeconds >= 64 ) {
109
+ sseReconnectFrequencySeconds = 64 ;
110
+ }
92
111
}
93
112
} ;
94
113
95
114
const setupEventSource = ( ) => {
96
- if ( evtSource ) evtSource . close ( ) ;
115
+ if ( evtSource ) evtSource . close ( ) ;
97
116
98
117
evtSource = new EventSource ( `${ streamEndpoint } ?access_token=${ store . getters . accessToken } ` , {
99
118
withCredentials : true ,
@@ -113,12 +132,12 @@ const ApiService = {
113
132
this . closeEventStream ( ) ;
114
133
} ) ;
115
134
116
- evtSource . onopen = ( e ) => {
135
+ evtSource . onopen = e => {
117
136
sseReconnectFrequencySeconds = 1 ;
118
- console . log ( "Eventstream opened" , { e } ) ;
137
+ console . log ( "Eventstream opened" , { e } ) ;
119
138
} ;
120
- evtSource . onerror = ( e ) => {
121
- console . log ( "Eventstream error" , { e } ) ;
139
+ evtSource . onerror = e => {
140
+ console . log ( "Eventstream error" , { e } ) ;
122
141
evtSource . close ( ) ;
123
142
store . dispatch ( "updateSessionStreamState" , false ) ;
124
143
setTimeout ( tryToSetupFunc , waitFunc ( ) ) ;
0 commit comments