import { Observable } from 'babylonjs/Misc/observable' ; import { AbstractViewer } from './viewer' ; /**
 * The viewer manager is the container for all viewers currently registered on this page.
 * It is possible to have more than one viewer on a single page.
 */ export class ViewerManager { private _viewers : { [ key ] : AbstractViewer ; } ; /**
     * A callback that will be triggered when a new viewer was added
     */ public onViewerAdded : ( viewer : AbstractViewer ) => void ; /**
     * Will notify when a new viewer was added
     */ public onViewerAddedObservable : Observable < AbstractViewer > ; /**
     * Will notify when a viewer was removed (disposed)
     */ public onViewerRemovedObservable : Observable < string > ; constructor ( ) { this . _viewers = { } ; this . onViewerAddedObservable = new Observable ( ) ; this . onViewerRemovedObservable = new Observable ( ) ; } param viewer public addViewer ( viewer ) { this . _viewers [ viewer . getBaseId ( ) ] = viewer ; this . _onViewerAdded ( viewer ) ; } param viewer public removeViewer ( viewer ) { let id = viewer . getBaseId ( ) ; delete this . _viewers [ id ] ; this . onViewerRemovedObservable . notifyObservers ( id ) ; } param id public getViewerById ( id ) { return this . _viewers [ id ] ; } param element public getViewerByHTMLElement ( element ) { for ( let id in this . _viewers ) { if ( this . _viewers [ id ] . containerElement === element ) { return this . getViewerById ( id ) ; } } } param id public getViewerPromiseById ( id ) { return new Promise ( ( resolve , reject ) => { let localViewer = this . getViewerById ( id ) ; if ( localViewer ) { return resolve ( localViewer ) ; } let viewerFunction = ( viewer ) => { if ( viewer . getBaseId ( ) === id ) { resolve ( viewer ) ; this . onViewerAddedObservable . removeCallback ( viewerFunction ) ; } } ; this . onViewerAddedObservable . add ( viewerFunction ) ; } ) ; } private _onViewerAdded ( viewer ) { this . onViewerAdded && this . onViewerAdded ( viewer ) ; this . onViewerAddedObservable . notifyObservers ( viewer ) ; } /**
     * dispose the manager and all of its associated viewers
     */ public dispose ( ) { for ( let id in this . _viewers ) { this . _viewers [ id ] . dispose ( ) ; } this . onViewerAddedObservable . clear ( ) ; this . onViewerRemovedObservable . clear ( ) ; } } export let viewerManager = new ViewerManager ( ) ;