diff src/app/app.component.ts @ 486:c9f12a9c1d5c

Actually find the corresponding analysis instead of assuming its at the front.
author Lucas Thompson <dev@lucas.im>
date Mon, 03 Jul 2017 20:34:59 +0100
parents 8820a133bcf5
children f52eb1b422f5
line wrap: on
line diff
--- a/src/app/app.component.ts	Mon Jul 03 20:33:49 2017 +0100
+++ b/src/app/app.component.ts	Mon Jul 03 20:34:59 2017 +0100
@@ -60,45 +60,46 @@
 
     this.onAudioDataSubscription = this.audioService.audioLoaded$.subscribe(
       resource => {
+        const findCurrentAudio =
+          val => isPendingRootAudioItem(val) && val.uri === getRootAudioItem(
+            this.analyses.get(0)
+          ).uri;
         const wasError = (resource as AudioResourceError).message != null;
         if (wasError) {
-          this.analyses.shift();
+          this.analyses.findIndexAndUse(
+            findCurrentAudio,
+            index => this.analyses.remove(index)
+          );
           this.canExtract = false;
         } else {
           const audioData = (resource as AudioResource).samples;
           if (audioData) {
-            const rootAudio = getRootAudioItem(this.analyses.get(0));
             this.canExtract = true;
-            const currentRootIndex = this.analyses.findIndex(val => {
-              return isPendingRootAudioItem(val) && val.uri === rootAudio.uri;
-            });
-            if (currentRootIndex !== -1) {
-              this.analyses.set(
+            this.analyses.findIndexAndUse(
+              findCurrentAudio,
+              currentRootIndex => this.analyses.set(
                 currentRootIndex,
                 Object.assign(
                   {},
                   this.analyses.get(currentRootIndex),
                   {audioData}
                 )
-              );
-            }
+              ));
           }
         }
       }
     );
     this.onProgressUpdated = this.featureService.progressUpdated$.subscribe(
       progress => {
-        const index = this.analyses.findIndex(val => val.id === progress.id);
-        if (index === -1) {
-          return;
-        }
-
-        this.analyses.setMutating(
-          index,
-          Object.assign(
-            {},
-            this.analyses.get(index),
-            {progress: progress.value}
+        this.analyses.findIndexAndUse(
+          val => val.id === progress.id,
+          index => this.analyses.setMutating(
+            index,
+            Object.assign(
+              {},
+              this.analyses.get(index),
+              {progress: progress.value}
+            )
           )
         );
       }
@@ -183,19 +184,19 @@
   private sendExtractionRequest(analysis: AnalysisItem): Promise<void> {
     const findAndUpdateItem = (result: ExtractionResult): void => {
       // TODO subscribe to the extraction service instead
-      const i = this.analyses.findIndex(val => val.id === result.id);
-      this.canExtract = true;
-      if (i !== -1) {
-        this.analyses.set(
-          i,
+      this.analyses.findIndexAndUse(
+        val => val.id === result.id,
+        (index) => this.analyses.set(
+          index,
           Object.assign(
             {},
-            this.analyses.get(i),
+            this.analyses.get(index),
             result.result,
             result.unit ? {unit: result.unit} : {}
           )
-        );
-      }  // TODO else remove the item?
+        )
+      );
+      this.canExtract = true;
     };
     return this.featureService.extract(
       analysis.id,
@@ -203,7 +204,10 @@
       .then(findAndUpdateItem)
       .catch(err => {
         this.canExtract = true;
-        this.analyses.shift();
+        this.analyses.findIndexAndUse(
+          val => val.id === analysis.id,
+          index => this.analyses.remove(index)
+        );
         console.error(`Error whilst extracting: ${err}`);
       });
   }