1 import { VideoPlaylist, VideoPlaylistElement } from '../../../../../shared/models'
2 import { logger } from '../../../root-helpers'
4 export class PlaylistTracker {
5 private currentPlaylistElement: VideoPlaylistElement
8 private readonly playlist: VideoPlaylist,
9 private readonly playlistElements: VideoPlaylistElement[]
18 getPlaylistElements () {
19 return this.playlistElements
22 hasNextPlaylistElement (position?: number) {
23 return !!this.getNextPlaylistElement(position)
26 getNextPlaylistElement (position?: number): VideoPlaylistElement {
27 if (!position) position = this.currentPlaylistElement.position + 1
29 if (position > this.playlist.videosLength) {
33 const next = this.playlistElements.find(e => e.position === position)
36 return this.getNextPlaylistElement(position + 1)
42 hasPreviousPlaylistElement (position?: number) {
43 return !!this.getPreviousPlaylistElement(position)
46 getPreviousPlaylistElement (position?: number): VideoPlaylistElement {
47 if (!position) position = this.currentPlaylistElement.position - 1
53 const prev = this.playlistElements.find(e => e.position === position)
56 return this.getNextPlaylistElement(position - 1)
63 const next = this.getNextPlaylistElement()
66 return next.video.name
69 setPosition (position: number) {
70 this.currentPlaylistElement = this.playlistElements.find(e => e.position === position)
71 if (!this.currentPlaylistElement?.video) {
72 logger.error('Current playlist element is not valid.', this.currentPlaylistElement)
73 this.currentPlaylistElement = this.getNextPlaylistElement()
76 if (!this.currentPlaylistElement) {
77 throw new Error('This playlist does not have any valid element')
81 setCurrentElement (playlistElement: VideoPlaylistElement) {
82 this.currentPlaylistElement = playlistElement
85 getCurrentElement () {
86 return this.currentPlaylistElement
89 getCurrentPosition () {
90 if (!this.currentPlaylistElement) return -1
92 return this.currentPlaylistElement.position