Update captions in sequence to avoid concurrence issues

pull/1156/head
Chocobozzz 2018-10-01 08:51:26 +02:00
parent 6360e12616
commit ed4c3c0910
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 5 additions and 11 deletions

View File

@ -1,7 +1,7 @@
import { catchError, map, switchMap } from 'rxjs/operators' import { catchError, map, switchMap } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http' import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core' import { Injectable } from '@angular/core'
import { forkJoin, Observable, of } from 'rxjs' import { Observable, of } from 'rxjs'
import { peertubeTranslate, ResultList } from '../../../../../shared' import { peertubeTranslate, ResultList } from '../../../../../shared'
import { RestExtractor } from '../rest' import { RestExtractor } from '../rest'
import { VideoService } from '@app/shared/video/video.service' import { VideoService } from '@app/shared/video/video.service'
@ -61,22 +61,16 @@ export class VideoCaptionService {
} }
updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) { updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) {
const observables: Observable<any>[] = [] let obs = of(true)
for (const videoCaption of videoCaptions) { for (const videoCaption of videoCaptions) {
if (videoCaption.action === 'CREATE') { if (videoCaption.action === 'CREATE') {
observables.push( obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)))
this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)
)
} else if (videoCaption.action === 'REMOVE') { } else if (videoCaption.action === 'REMOVE') {
observables.push( obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id)))
this.removeCaption(videoId, videoCaption.language.id)
)
} }
} }
if (observables.length === 0) return of(undefined) return obs
return forkJoin(observables)
} }
} }