Cleanup file upload susbcriptions

This commit is contained in:
Michael Shamoon 2022-04-05 13:29:51 -07:00
parent fb76b72787
commit f2c83f51de

View File

@ -6,11 +6,14 @@ import {
FileStatusPhase, FileStatusPhase,
} from './consumer-status.service' } from './consumer-status.service'
import { DocumentService } from './rest/document.service' import { DocumentService } from './rest/document.service'
import { Subscription } from 'rxjs'
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class UploadDocumentsService { export class UploadDocumentsService {
private uploadSubscriptions: Array<Subscription> = []
constructor( constructor(
private documentService: DocumentService, private documentService: DocumentService,
private consumerStatusService: ConsumerStatusService private consumerStatusService: ConsumerStatusService
@ -27,36 +30,43 @@ export class UploadDocumentsService {
status.message = $localize`Connecting...` status.message = $localize`Connecting...`
this.documentService.uploadDocument(formData).subscribe({ this.uploadSubscriptions[file.name] = this.documentService
next: (event) => { .uploadDocument(formData)
if (event.type == HttpEventType.UploadProgress) { .subscribe({
status.updateProgress( next: (event) => {
FileStatusPhase.UPLOADING, if (event.type == HttpEventType.UploadProgress) {
event.loaded, status.updateProgress(
event.total FileStatusPhase.UPLOADING,
) event.loaded,
status.message = $localize`Uploading...` event.total
} else if (event.type == HttpEventType.Response) {
status.taskId = event.body['task_id']
status.message = $localize`Upload complete, waiting...`
}
},
error: (error) => {
switch (error.status) {
case 400: {
this.consumerStatusService.fail(status, error.error.document)
break
}
default: {
this.consumerStatusService.fail(
status,
$localize`HTTP error: ${error.status} ${error.statusText}`
) )
break status.message = $localize`Uploading...`
} else if (event.type == HttpEventType.Response) {
status.taskId = event.body['task_id']
status.message = $localize`Upload complete, waiting...`
this.uploadSubscriptions[file.name]?.complete()
} }
} },
}, error: (error) => {
}) switch (error.status) {
case 400: {
this.consumerStatusService.fail(
status,
error.error.document
)
break
}
default: {
this.consumerStatusService.fail(
status,
$localize`HTTP error: ${error.status} ${error.statusText}`
)
break
}
}
this.uploadSubscriptions[file.name]?.complete()
},
})
}) })
} }
} }