aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/recommendations/recommended-videos.store.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/videos/recommendations/recommended-videos.store.ts')
-rw-r--r--client/src/app/videos/recommendations/recommended-videos.store.ts32
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 @@
1import { Inject, Injectable } from '@angular/core'
2import { Observable, ReplaySubject } from 'rxjs'
3import { Video } from '@app/shared/video/video.model'
4import { RecentVideosRecommendationService } from '@app/videos/recommendations/recent-videos-recommendation.service'
5import { RecommendationService, UUID } from '@app/videos/recommendations/recommendations.service'
6import { map, switchMap, take } from 'rxjs/operators'
7
8/**
9 * This store is intended to provide data for the RecommendedVideosComponent.
10 */
11@Injectable()
12export 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}