Skip to content

Commit

Permalink
@uppy/transloadit: implement Server-sent event API
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Sep 12, 2022
1 parent 952fb8d commit e885b2a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/@uppy/transloadit/src/Assembly.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class TransloaditAssembly extends Emitter {

#previousFetchStatusStillPending = false

#sse

constructor (assembly, rateLimitedQueue) {
super()

Expand All @@ -53,6 +55,7 @@ class TransloaditAssembly extends Emitter {
}

connect () {
this.#connectServerEvent()
this.#connectSocket()
this.#beginPolling()
}
Expand All @@ -62,6 +65,49 @@ class TransloaditAssembly extends Emitter {
this.close()
}

#connectServerEvent () {
this.#sse = new EventSource(this.status.assembly_ssl_url)

this.#sse.addEventListener('open', () => {
if (this.socket) {
this.socket.disconnect()
this.socket = null
}
clearInterval(this.pollInterval)
this.pollInterval = null
})

/*
* This will listen only for events
* similar to the following:
*
* event: notice
* data: useful data
* id: someid
*/
this.#sse.addEventListener('notice', (e) => {
console.log(e.data)
})

/*
* Similarly, this will listen for events
* with the field event: update
*/
this.#sse.addEventListener('update', (e) => {
console.log(e.data)
})

/*
* The event "message" is a special case, as it
* will capture events without an event field
* as well as events that have the specific type
* other event type.
*/
this.#sse.addEventListener('message', (e) => {
console.log(e.data)
})
}

#connectSocket () {
const parsed = parseUrl(this.status.websocket_url)
const socket = io(parsed.origin, {
Expand Down Expand Up @@ -262,6 +308,10 @@ class TransloaditAssembly extends Emitter {
*/
close () {
this.closed = true
if (this.#sse) {
this.#sse.close()
this.#sse = null
}
if (this.socket) {
this.socket.disconnect()
this.socket = null
Expand Down

0 comments on commit e885b2a

Please sign in to comment.