Skip to content

Commit

Permalink
pass events from event stream to JS
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Oct 13, 2022
1 parent c7b4c64 commit e1983ef
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions packages/@uppy/transloadit/src/Assembly.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,44 @@ class TransloaditAssembly extends Emitter {
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)
if (e.data === 'assembly_finished') {
this.#onFinished()
}

if (e.data.startsWith('assembly_upload_finished:')) {
const file = JSON.parse(e.data.slice('assembly_upload_finished:'.length))
this.emit('upload', file)
this.status.uploads.push(file)
}

if (e.data === 'assembly_uploading_finished') {
this.emit('executing')
}

if (e.data === 'assembly_upload_meta_data_extracted') {
this.emit('metadata')
this.#fetchStatus({ diff: false })
}

if (e.data.startsWith('assembly_result_finished:')) {
const [stepName, result] = JSON.parse(e.data.slice('assembly_result_finished:'.length))
this.emit('result', stepName, result)
;(this.status.results[stepName] ??= []).push(result)
}

if (e.data === 'assembly_error') {
const err = null
this.#onError(err)
// Refetch for updated status code
this.#fetchStatus({ diff: false })
}
})
}

Expand Down Expand Up @@ -148,10 +158,7 @@ class TransloaditAssembly extends Emitter {

socket.on('assembly_result_finished', (stepName, result) => {
this.emit('result', stepName, result)
if (!this.status.results[stepName]) {
this.status.results[stepName] = []
}
this.status.results[stepName].push(result)
;(this.status.results[stepName] ??= []).push(result)
})

socket.on('assembly_error', (err) => {
Expand Down

0 comments on commit e1983ef

Please sign in to comment.