import { Inject , Injectable , Optional } from '@angular/core' ; import DPlayer , { DPlayerOptions } from 'dplayer' ; import { Util } from '../util' ; import { Observable , from , of } from 'rxjs' ; import { filter , mapTo , mergeMap , switchMap , tap } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' } ) export class DPlayerService { private _pid = - 0x1 ; public set pid ( id ) { this . _pid = id ; } public get pid ( ) : number { return this . _pid ; } private readonly _dp : { id : number ; dp : DPlayer ; } [ ] = [ ] ; private set dp ( _p ) { this . _dp . push ( { id : this . pid , dp : _p } ) ; } private get dp ( ) : DPlayer { return this . _dp . find ( _p => { return this . pid === _p . id ; } ) . dp ; } private readonly _dpOptions : { id : number ; op : DPlayerOptions ; } [ ] = [ ] ; private set dpOptions ( _o ) { this . _dpOptions . push ( { id : ++ this . pid , op : Util . safeSet ( _o ) } ) ; } private get dpOptions ( ) : DPlayerOptions { return this . _dpOptions . find ( _o => { return this . pid === _o . id ; } ) . op ; } constructor ( @ Optional ( ) @ Inject ( 'config' ) private config ) { Object . freeze ( this . config ) ; } createPlayer ( o ) { this . dpOptions = o ; return of ( this . dpOptions ) . pipe ( mergeMap ( _o => of ( this . config ? Object . assign ( { } , this . config , _o ) : _o ) ) , switchMap ( _o => of ( new DPlayer ( _o ) ) ) ) . pipe ( tap ( _dp => { this . dp = _dp ; } ) ) ; } destroyPlayer ( p ) { return from ( this . _dp ) . pipe ( filter ( _p => _p . dp === p ) ) . pipe ( tap ( _p => _p . dp . destroy ( ) ) , switchMap ( _p => of ( this . _dp . indexOf ( _p ) ) ) ) . pipe ( tap ( _i => { this . _dp . splice ( _i , 1 ) ; this . _dpOptions . splice ( _i , 1 ) ; } ) , mapTo ( this . pid ) ) ; } findInstance ( id ) { return this . _dp . find ( _p => { return id === _p . id ; } ) . dp ; } findOptions ( id ) { return this . _dpOptions . find ( _o => { return id === _o . id ; } ) . op ; } }