aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+videos/+video-watch/recommendations/recommended-videos.store.ts
blob: 8c3fb648042f7cbd84268beb119df8d530669f15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Observable, ReplaySubject } from 'rxjs'
import { map, shareReplay, switchMap, take } from 'rxjs/operators'
import { Inject, Injectable } from '@angular/core'
import { Video } from '@app/shared/shared-main'
import { RecentVideosRecommendationService } from './recent-videos-recommendation.service'
import { RecommendationInfo } from './recommendation-info.model'
import { RecommendationService } from './recommendations.service'

/**
 * This store is intended to provide data for the RecommendedVideosComponent.
 */
@Injectable()
export class RecommendedVideosStore {
  public readonly recommendations$: Observable<Video[]>
  public readonly hasRecommendations$: Observable<boolean>
  private readonly requestsForLoad$$ = new ReplaySubject<RecommendationInfo>(1)

  constructor (
    @Inject(RecentVideosRecommendationService) private recommendations: RecommendationService
  ) {
    this.recommendations$ = this.requestsForLoad$$.pipe(
      switchMap(requestedRecommendation => {
        return this.recommendations.getRecommendations(requestedRecommendation)
                              .pipe(take(1))
      }),
      shareReplay()
    )

    this.hasRecommendations$ = this.recommendations$.pipe(
      map(otherVideos => otherVideos.length > 0)
    )
  }

  requestNewRecommendations (recommend: RecommendationInfo) {
    this.requestsForLoad$$.next(recommend)
  }
}