import { Observable } from '../../esl-utils/abstract/observable' ; import type { BaseProvider , ProviderType } from './esl-media-provider' ; import type { ESLMedia } from './esl-media' ; let evRegistryInstance = null ; author Yuliya Adamskaya, Natallia Harshunova
  export class ESLMediaProviderRegistry extends Observable < ( name : string , provider : ProviderType ) => void > { private providersMap : Map < string , ProviderType > = new Map ( ) ; public static get instance ( ) : ESLMediaProviderRegistry { if ( ! evRegistryInstance ) { evRegistryInstance = new ESLMediaProviderRegistry ( ) ; } return evRegistryInstance ; } /** List of registered providers */ public get providers ( ) : ProviderType [ ] { const list = [ ] ; this . providersMap . forEach ( ( provider ) => list . push ( provider ) ) ; return list ; } /** Register provider */ public register ( provider ) { if ( ! provider . providerName ) throw new Error ( 'Provider should have a name' ) ; this . providersMap . set ( provider . providerName , provider ) ; this . fire ( provider . providerName , provider ) ; } /** Check that provider is registered for passed name */ public has ( name ) { return this . providersMap . has ( name ) ; } /** Find provider by name */ public findByName ( name ) { if ( ! name || name === 'auto' ) return null ; return this . providersMap . get ( name . toLowerCase ( ) ) || null ; } /** Create provider instance for passed ESLMedia instance */ public createFor ( media ) { return this . createByType ( media ) || this . createBySrc ( media ) ; } /** Create provider instance for passed ESLMedia instance via provider name */ private createByType ( media ) { const provider = this . findByName ( media . mediaType ) ; return provider ? ESLMediaProviderRegistry . _create ( provider , media ) : null ; } /** Create provider instance for passed ESLMedia instance via url */ private createBySrc ( media ) { for ( const provider of this . providers . reverse ( ) ) { const cfg = provider . parseUrl ( media . mediaSrc ) ; if ( cfg ) return ESLMediaProviderRegistry . _create ( provider , media , cfg ) ; } return null ; } /** Create provider instance for passed configuration */ private static _create ( provider , media , cfg = provider . parseUrl ( media . mediaSrc ) ) { const config = Object . assign ( { } , cfg || { } , provider . parseConfig ( media ) ) ; return new provider ( media , config ) ; } }