1 import { Inject, Injectable } from '@angular/core'
2 import { Observable, ReplaySubject } from 'rxjs'
3 import { Video } from '@app/shared/video/video.model'
4 import { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
5 import { RecentVideosRecommendationService } from '@app/videos/recommendations/recent-videos-recommendation.service'
6 import { RecommendationService } from '@app/videos/recommendations/recommendations.service'
7 import { map, shareReplay, switchMap, take } from 'rxjs/operators'
10 * This store is intended to provide data for the RecommendedVideosComponent.
13 export class RecommendedVideosStore {
14 public readonly recommendations$: Observable<Video[]>
15 public readonly hasRecommendations$: Observable<boolean>
16 private readonly requestsForLoad$$ = new ReplaySubject<RecommendationInfo>(1)
19 @Inject(RecentVideosRecommendationService) private recommendations: RecommendationService
21 this.recommendations$ = this.requestsForLoad$$.pipe(
22 switchMap(requestedRecommendation => {
23 return recommendations.getRecommendations(requestedRecommendation)
29 this.hasRecommendations$ = this.recommendations$.pipe(
30 map(otherVideos => otherVideos.length > 0)
34 requestNewRecommendations (recommend: RecommendationInfo) {
35 this.requestsForLoad$$.next(recommend)