import { Injectable } from '@angular/core' ; import { AppData } from '../app.data' ; import { ActivatedRouteSnapshot , Resolve } from '@angular/router' ; import { IBaseResponse , IPlay } from '../app.interface' ; import { PlayService } from './play.service' ; @ Injectable ( ) export class PlayResolver implements Resolve < IPlay > { constructor ( private appData , private playService ) { } resolve ( route ) { return new Promise < IPlay > ( async ( resolve , reject ) => { const id = route . paramMap . get ( 'id' ) || '' ; const type = route . paramMap . get ( 'type' ) || '' ; const movie = this . appData . data . movie . find ( i => i . id === id ) ; const handleResponse = ( res ) => { if ( res && res . success ) { resolve ( res . data ) ; } else { reject ( ) ; } } ; let response ; if ( type === 'hls' ) { try { response = await this . playService . hls ( { id , download : ( movie ) ? movie . download : null } ) ; } finally { handleResponse ( response ) ; } } else if ( type === 'file' ) { try { response = await this . playService . file ( id ) ; } finally { handleResponse ( response ) ; } } else { reject ( ) ; } } ) ; } }