(withCredentials, method, headers)
| 181 | // ======================================== |
| 182 | |
| 183 | function createStub(withCredentials, method, headers) { |
| 184 | var stub = { |
| 185 | listeners: [], |
| 186 | retryCount: 0, |
| 187 | closed: false, |
| 188 | abortController: null, |
| 189 | reader: null, |
| 190 | lastEventId: null, |
| 191 | reconnectTimeout: null, |
| 192 | url: null, |
| 193 | withCredentials: withCredentials, |
| 194 | method: method, |
| 195 | headers: headers, |
| 196 | |
| 197 | open: function (url) { |
| 198 | if (url == undefined) { |
| 199 | if (stub.url != null) { |
| 200 | url = stub.url; |
| 201 | } else { |
| 202 | throw new Error("no url defined for EventSource."); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // If already connected to same URL, skip |
| 207 | if (stub.url === url && stub.abortController && !stub.abortController.signal.aborted) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | // Close existing connection if URL changed or reconnecting |
| 212 | if (stub.abortController) { |
| 213 | stub.abortController.abort(); |
| 214 | } |
| 215 | |
| 216 | stub.closed = false; |
| 217 | stub.url = url; |
| 218 | startConnection(stub); |
| 219 | }, |
| 220 | |
| 221 | close: function () { |
| 222 | stub.closed = true; |
| 223 | if (stub.reconnectTimeout) { |
| 224 | clearTimeout(stub.reconnectTimeout); |
| 225 | stub.reconnectTimeout = null; |
| 226 | } |
| 227 | if (stub.abortController) { |
| 228 | stub.abortController.abort(); |
| 229 | stub.abortController = null; |
| 230 | } |
| 231 | stub.retryCount = 0; |
| 232 | dispatch(stub, 'close', { type: 'close' }); |
| 233 | }, |
| 234 | |
| 235 | addEventListener: function (type, handler, options) { |
| 236 | stub.listeners.push({ type: type, handler: handler, options: options }); |
| 237 | }, |
| 238 | }; |
| 239 | |
| 240 | return stub; |
no test coverage detected