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,7 +30,9 @@ export class UploadDocumentsService {
status.message = $localize`Connecting...` status.message = $localize`Connecting...`
this.documentService.uploadDocument(formData).subscribe({ this.uploadSubscriptions[file.name] = this.documentService
.uploadDocument(formData)
.subscribe({
next: (event) => { next: (event) => {
if (event.type == HttpEventType.UploadProgress) { if (event.type == HttpEventType.UploadProgress) {
status.updateProgress( status.updateProgress(
@ -39,12 +44,16 @@ export class UploadDocumentsService {
} else if (event.type == HttpEventType.Response) { } else if (event.type == HttpEventType.Response) {
status.taskId = event.body['task_id'] status.taskId = event.body['task_id']
status.message = $localize`Upload complete, waiting...` status.message = $localize`Upload complete, waiting...`
this.uploadSubscriptions[file.name]?.complete()
} }
}, },
error: (error) => { error: (error) => {
switch (error.status) { switch (error.status) {
case 400: { case 400: {
this.consumerStatusService.fail(status, error.error.document) this.consumerStatusService.fail(
status,
error.error.document
)
break break
} }
default: { default: {
@ -55,6 +64,7 @@ export class UploadDocumentsService {
break break
} }
} }
this.uploadSubscriptions[file.name]?.complete()
}, },
}) })
}) })