This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit 3d5e397b authored by Michael Breu's avatar Michael Breu
Browse files

excercises are now cached

parent cfa1bfe5
2 merge requests!231New Deployment into production and update gitlab,!225Resolve "Collections mehr hervorheben"
......@@ -304,13 +304,13 @@ export class ExerciseBodyComponent implements OnInit, OnDestroy, AfterViewInit {
}
updateParent(exercise: ExtendedSearchResultDTO): void {
if (this.exercise!.file.parentId) {
this.exerciseService.loadExercise(this.exercise!.file.parentId).subscribe({
if (exercise.file.parentId) {
this.exerciseService.loadExercise(exercise.file.parentId).subscribe({
next: searchResult => {
this.parent = searchResult;
},
error: () => {
alert(`exercise ${this.exercise!.file.parentId} cannot be loaded`);
alert(`exercise ${exercise.file.parentId} cannot be loaded`);
},
});
}
......
......@@ -6,7 +6,7 @@ import { StatisticsService } from 'app/entities/statistics/statistics.service';
import { SearchService } from 'app/search/service/search-service';
import { ArtemisExerciseInfo } from 'app/shared/model/artemis-exercise-info.model';
import { ChildInfo, ExtendedSearchResultDTO, hasChildren, SearchResultDTO } from 'app/shared/model/search/search-result-dto.model';
import { Observable } from 'rxjs';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class ExerciseService {
......@@ -34,10 +34,42 @@ export class ExerciseService {
});
}
private exerciseCache: { [id: string]: BehaviorSubject<SearchResultDTO> } = {};
public loadExercise(exerciseId: string): Observable<SearchResultDTO> {
if (this.exerciseCache[exerciseId]) {
return this.exerciseCache[exerciseId];
} else {
const call = this.loadExerciseFromServer(exerciseId);
this.exerciseCache[exerciseId] = new BehaviorSubject<SearchResultDTO>(undefined!);
call.subscribe({
next: (data: SearchResultDTO) => {
if (this.exerciseCache[exerciseId]) {
this.exerciseCache[exerciseId].next(data);
} else {
// this should never happen, but just in case
// we loose all previous listeners to the observable
this.exerciseCache[exerciseId] = new BehaviorSubject<SearchResultDTO>(undefined!);
this.exerciseCache[exerciseId].next(data);
}
},
error: () => console.warn('Could not load exercise'),
});
return call;
}
}
private loadExerciseFromServer(exerciseId: string): Observable<SearchResultDTO> {
return this.http.get<SearchResultDTO>(this.exerciseUrl + encodeURIforExerciseId(exerciseId));
}
/**
* resets all cached exercises
*/
resetExcerciseCache() {
this.exerciseCache = {};
}
public loadChildInfos(exerciseId: string): Observable<ChildInfo[]> {
return this.http.get<ChildInfo[]>(this.exerciseChildrenUrl + encodeURIforExerciseId(exerciseId));
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment