diff options
Diffstat (limited to 'client/src/app/videos/recommendations/recommended-videos.store.ts')
-rw-r--r-- | client/src/app/videos/recommendations/recommended-videos.store.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/client/src/app/videos/recommendations/recommended-videos.store.ts b/client/src/app/videos/recommendations/recommended-videos.store.ts new file mode 100644 index 000000000..689adeb1f --- /dev/null +++ b/client/src/app/videos/recommendations/recommended-videos.store.ts | |||
@@ -0,0 +1,32 @@ | |||
1 | import { Inject, Injectable } from '@angular/core' | ||
2 | import { Observable, ReplaySubject } from 'rxjs' | ||
3 | import { Video } from '@app/shared/video/video.model' | ||
4 | import { RecentVideosRecommendationService } from '@app/videos/recommendations/recent-videos-recommendation.service' | ||
5 | import { RecommendationService, UUID } from '@app/videos/recommendations/recommendations.service' | ||
6 | import { map, switchMap, take } from 'rxjs/operators' | ||
7 | |||
8 | /** | ||
9 | * This store is intended to provide data for the RecommendedVideosComponent. | ||
10 | */ | ||
11 | @Injectable() | ||
12 | export class RecommendedVideosStore { | ||
13 | public readonly recommendations$: Observable<Video[]> | ||
14 | public readonly hasRecommendations$: Observable<boolean> | ||
15 | private readonly requestsForLoad$$ = new ReplaySubject<UUID>(1) | ||
16 | |||
17 | constructor ( | ||
18 | @Inject(RecentVideosRecommendationService) private recommendations: RecommendationService | ||
19 | ) { | ||
20 | this.recommendations$ = this.requestsForLoad$$.pipe( | ||
21 | switchMap(requestedUUID => recommendations.getRecommendations(requestedUUID) | ||
22 | .pipe(take(1)) | ||
23 | )) | ||
24 | this.hasRecommendations$ = this.recommendations$.pipe( | ||
25 | map(otherVideos => otherVideos.length > 0) | ||
26 | ) | ||
27 | } | ||
28 | |||
29 | requestNewRecommendations (videoUUID: string) { | ||
30 | this.requestsForLoad$$.next(videoUUID) | ||
31 | } | ||
32 | } | ||