comparison 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
comparison
equal deleted inserted replaced
485:5f3e1b275e18 486:c9f12a9c1d5c
58 sanitizer.bypassSecurityTrustResourceUrl('assets/duck.svg') 58 sanitizer.bypassSecurityTrustResourceUrl('assets/duck.svg')
59 ); 59 );
60 60
61 this.onAudioDataSubscription = this.audioService.audioLoaded$.subscribe( 61 this.onAudioDataSubscription = this.audioService.audioLoaded$.subscribe(
62 resource => { 62 resource => {
63 const findCurrentAudio =
64 val => isPendingRootAudioItem(val) && val.uri === getRootAudioItem(
65 this.analyses.get(0)
66 ).uri;
63 const wasError = (resource as AudioResourceError).message != null; 67 const wasError = (resource as AudioResourceError).message != null;
64 if (wasError) { 68 if (wasError) {
65 this.analyses.shift(); 69 this.analyses.findIndexAndUse(
70 findCurrentAudio,
71 index => this.analyses.remove(index)
72 );
66 this.canExtract = false; 73 this.canExtract = false;
67 } else { 74 } else {
68 const audioData = (resource as AudioResource).samples; 75 const audioData = (resource as AudioResource).samples;
69 if (audioData) { 76 if (audioData) {
70 const rootAudio = getRootAudioItem(this.analyses.get(0));
71 this.canExtract = true; 77 this.canExtract = true;
72 const currentRootIndex = this.analyses.findIndex(val => { 78 this.analyses.findIndexAndUse(
73 return isPendingRootAudioItem(val) && val.uri === rootAudio.uri; 79 findCurrentAudio,
74 }); 80 currentRootIndex => this.analyses.set(
75 if (currentRootIndex !== -1) {
76 this.analyses.set(
77 currentRootIndex, 81 currentRootIndex,
78 Object.assign( 82 Object.assign(
79 {}, 83 {},
80 this.analyses.get(currentRootIndex), 84 this.analyses.get(currentRootIndex),
81 {audioData} 85 {audioData}
82 ) 86 )
83 ); 87 ));
84 }
85 } 88 }
86 } 89 }
87 } 90 }
88 ); 91 );
89 this.onProgressUpdated = this.featureService.progressUpdated$.subscribe( 92 this.onProgressUpdated = this.featureService.progressUpdated$.subscribe(
90 progress => { 93 progress => {
91 const index = this.analyses.findIndex(val => val.id === progress.id); 94 this.analyses.findIndexAndUse(
92 if (index === -1) { 95 val => val.id === progress.id,
93 return; 96 index => this.analyses.setMutating(
94 } 97 index,
95 98 Object.assign(
96 this.analyses.setMutating( 99 {},
97 index, 100 this.analyses.get(index),
98 Object.assign( 101 {progress: progress.value}
99 {}, 102 )
100 this.analyses.get(index),
101 {progress: progress.value}
102 ) 103 )
103 ); 104 );
104 } 105 }
105 ); 106 );
106 } 107 }
181 } 182 }
182 183
183 private sendExtractionRequest(analysis: AnalysisItem): Promise<void> { 184 private sendExtractionRequest(analysis: AnalysisItem): Promise<void> {
184 const findAndUpdateItem = (result: ExtractionResult): void => { 185 const findAndUpdateItem = (result: ExtractionResult): void => {
185 // TODO subscribe to the extraction service instead 186 // TODO subscribe to the extraction service instead
186 const i = this.analyses.findIndex(val => val.id === result.id); 187 this.analyses.findIndexAndUse(
187 this.canExtract = true; 188 val => val.id === result.id,
188 if (i !== -1) { 189 (index) => this.analyses.set(
189 this.analyses.set( 190 index,
190 i,
191 Object.assign( 191 Object.assign(
192 {}, 192 {},
193 this.analyses.get(i), 193 this.analyses.get(index),
194 result.result, 194 result.result,
195 result.unit ? {unit: result.unit} : {} 195 result.unit ? {unit: result.unit} : {}
196 ) 196 )
197 ); 197 )
198 } // TODO else remove the item? 198 );
199 this.canExtract = true;
199 }; 200 };
200 return this.featureService.extract( 201 return this.featureService.extract(
201 analysis.id, 202 analysis.id,
202 createExtractionRequest(analysis)) 203 createExtractionRequest(analysis))
203 .then(findAndUpdateItem) 204 .then(findAndUpdateItem)
204 .catch(err => { 205 .catch(err => {
205 this.canExtract = true; 206 this.canExtract = true;
206 this.analyses.shift(); 207 this.analyses.findIndexAndUse(
208 val => val.id === analysis.id,
209 index => this.analyses.remove(index)
210 );
207 console.error(`Error whilst extracting: ${err}`); 211 console.error(`Error whilst extracting: ${err}`);
208 }); 212 });
209 } 213 }
210 } 214 }