Skip to content

Commit 2ba1041

Browse files
committed
feat(uploader): emit proper events for changed state
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 9e0f249 commit 2ba1041

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

lib/upload/uploader/Uploader.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,31 @@ interface BatchUploadOptions extends UploadOptions {
5656
}
5757

5858
interface UploaderEventsMap {
59+
/**
60+
* Dispatched when the uploader is paused
61+
*/
5962
paused: CustomEvent
63+
/**
64+
* Dispatched when the uploader is resumed
65+
*/
6066
resumed: CustomEvent
61-
progress: CustomEvent
67+
/**
68+
* Dispatched when the uploader has finished all uploads (successfully, failed or cancelled)
69+
*/
6270
finished: CustomEvent
71+
72+
/**
73+
* Dispatched when a new upload has been started.
74+
*/
75+
uploadStarted: CustomEvent<IUpload>
76+
/**
77+
* Dispatched when an upload has made progress (e.g. a chunk has been uploaded).
78+
*/
79+
uploadProgress: CustomEvent<IUpload>
80+
/**
81+
* Dispatched when an upload has finished (successfully, failed or cancelled).
82+
*/
83+
uploadFinished: CustomEvent<IUpload>
6384
}
6485

6586
export class Uploader extends TypedEventTarget<UploaderEventsMap> {
@@ -262,6 +283,7 @@ export class Uploader extends TypedEventTarget<UploaderEventsMap> {
262283
this.#attachEventListeners(upload)
263284
}
264285
this.#uploadQueue.push(...uploads)
286+
this.dispatchTypedEvent('uploadStarted', new CustomEvent('uploadStarted', { detail: upload }))
265287
await upload.start(this.#jobQueue)
266288
return uploads
267289
}
@@ -283,29 +305,35 @@ export class Uploader extends TypedEventTarget<UploaderEventsMap> {
283305

284306
this.#attachEventListeners(upload)
285307
this.#uploadQueue.push(upload)
308+
this.dispatchTypedEvent('uploadStarted', new CustomEvent('uploadStarted', { detail: upload }))
286309
await upload.start(this.#jobQueue)
287310
return upload
288311
}
289312

290313
/**
291314
* Handle the progress event of an upload.
292315
* Update the ETA and dispatch a progress event for the uploader.
316+
*
317+
* @param event - The progress event of an upload
293318
*/
294-
#onProgress() {
319+
#onProgress(event: CustomEvent<IUpload>) {
295320
const totalBytes = this.#uploadQueue.reduce((acc, upload) => acc + upload.totalBytes, 0)
296321
const uploadedBytes = this.#uploadQueue.reduce((acc, upload) => acc + upload.uploadedBytes, 0)
297322
this.#eta.update(uploadedBytes, totalBytes)
298-
this.dispatchTypedEvent('progress', new CustomEvent('progress'))
323+
this.dispatchTypedEvent('uploadProgress', new CustomEvent('uploadProgress', { detail: event.detail }))
299324
}
300325

301326
/**
302327
* Handle the finished event of an upload.
303328
*
304329
* 1. Update the progress
305330
* 2. if all uploads are finished dispatch a finished event for the uploader and clear the queue
331+
*
332+
* @param event - The finished event of an upload
306333
*/
307-
async #onFinished() {
308-
this.#onProgress()
334+
async #onFinished(event: CustomEvent<IUpload>) {
335+
this.#onProgress(event)
336+
this.dispatchTypedEvent('uploadFinished', new CustomEvent('uploadFinished', { detail: event.detail }))
309337

310338
const finalStates = [
311339
UploadStatus.FINISHED,

0 commit comments

Comments
 (0)