MCPcopy Create free account
hub / github.com/nodejs/node / #reconnect

Method #reconnect

deps/undici/src/lib/web/eventsource/eventsource.js:311–350  ·  view source on GitHub ↗

* @see https://html.spec.whatwg.org/multipage/server-sent-events.html#sse-processing-model * @returns {void}

()

Source from the content-addressed store, hash-verified

309 * @returns {void}
310 */
311 #reconnect () {
312 // When a user agent is to reestablish the connection, the user agent must
313 // run the following steps. These steps are run in parallel, not as part of
314 // a task. (The tasks that it queues, of course, are run like normal tasks
315 // and not themselves in parallel.)
316
317 // 1. Queue a task to run the following steps:
318
319 // 1. If the readyState attribute is set to CLOSED, abort the task.
320 if (this.#readyState === CLOSED) return
321
322 // 2. Set the readyState attribute to CONNECTING.
323 this.#readyState = CONNECTING
324
325 // 3. Fire an event named error at the EventSource object.
326 this.dispatchEvent(new Event('error'))
327
328 // 2. Wait a delay equal to the reconnection time of the event source.
329 setTimeout(() => {
330 // 5. Queue a task to run the following steps:
331
332 // 1. If the EventSource object's readyState attribute is not set to
333 // CONNECTING, then return.
334 if (this.#readyState !== CONNECTING) return
335
336 // 2. Let request be the EventSource object's request.
337 // 3. If the EventSource object's last event ID string is not the empty
338 // string, then:
339 // 1. Let lastEventIDValue be the EventSource object's last event ID
340 // string, encoded as UTF-8.
341 // 2. Set (`Last-Event-ID`, lastEventIDValue) in request's header
342 // list.
343 if (this.#state.lastEventId.length) {
344 this.#request.headersList.set('last-event-id', this.#state.lastEventId, true)
345 }
346
347 // 4. Fetch request and process the response obtained in this fashion, if any, as described earlier in this section.
348 this.#connect()
349 }, this.#state.reconnectionTime)?.unref()
350 }
351
352 /**
353 * Closes the connection, if any, and sets the readyState attribute to

Callers 2

#connectMethod · 0.95

Calls 5

#connectMethod · 0.95
dispatchEventMethod · 0.80
setTimeoutFunction · 0.50
unrefMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected