Skip to content

Commit

Permalink
feat: added dispatcher in eventSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Mert Can Altin committed Apr 14, 2024
1 parent 7a94682 commit 8f6cf8b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/web/eventsource/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ class EventSource extends EventTarget {
* Creates a new EventSource object.
* @param {string} url
* @param {EventSourceInit} [eventSourceInitDict]
* @param {dispatcher} [dispatcher] - WebSocket dispatcher option
* @param {Object} [customHeaders] - Custom HTTP headers to be included in the request
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html#the-eventsource-interface
*/
constructor (url, eventSourceInitDict = {}) {
constructor (url, eventSourceInitDict = {}, dispatcher = {}, customHeaders = {}) {
// 1. Let ev be a new EventSource object.
super()

Expand All @@ -132,7 +134,8 @@ class EventSource extends EventTarget {
referrerPolicy: 'no-referrer'
},
lastEventId: '',
reconnectionTime: defaultReconnectionTime
reconnectionTime: defaultReconnectionTime,
dispatcher
}

let urlRecord
Expand Down Expand Up @@ -179,15 +182,20 @@ class EventSource extends EventTarget {
// 10. User agents may set (`Accept`, `text/event-stream`) in request's header list.
initRequest.headersList = [['accept', { name: 'accept', value: 'text/event-stream' }]]

// 11. Set request's cache mode to "no-store".
// 11. Add custom headers to the request's header list
for (const [headerName, headerValue] of Object.entries(customHeaders)) {
initRequest.headersList.push([headerName.toLowerCase(), { name: headerName.toLowerCase(), value: headerValue }])
}

// 12. Set request's cache mode to "no-store".
initRequest.cache = 'no-store'

// 12. Set request's initiator type to "other".
// 13. Set request's initiator type to "other".
initRequest.initiator = 'other'

initRequest.urlList = [new URL(this.#url)]

// 13. Set ev's request to request.
// 14. Set ev's request to request.
this.#request = makeRequest(initRequest)

this.#connect()
Expand Down

0 comments on commit 8f6cf8b

Please sign in to comment.