import { Subject , Subscription } from 'rxjs' ; import { Tracker } from '../shared/tracker' ; import { PinLike } from './pin-like' ; import { Pin } from './pin' ; export type PinMapFactory = ( label : string ) => PinLike ; export type PinMapSusbcriber = ( label : string , pin : PinLike ) => void ; /**
 *
 * Represents a map of labelled pins. The labelled pins are created
 * first time they are requested, allowing for possibly huge
 * maps without high memory cost.
 *
 */ export class PinMap extends Tracker { private _pins : { [ label ] : PinLike ; } = { } ; private _subject : Subject < [ string , PinLike ] > | undefined ; param factory constructor ( readonly factory = ( ) => new Pin ( ) ) { super ( ) ; } param label public get ( label ) { if ( ! ( label in this . _pins ) ) { let _pin = this . factory ( label ) ; this . _pins [ label ] = _pin ; if ( this . _subject ) this . _subject . next ( [ label , _pin ] ) ; return _pin ; } return this . _pins [ label ] ; } param label public instantiated ( label ) { return label in this . _pins ; } returns public get pins ( ) : PinLike [ ] { return Object . values ( this . _pins ) ; } returns public get entries ( ) : [ string , PinLike ] [ ] { return Object . entries ( this . _pins ) ; } param subscriber returns public subscribe ( subscriber ) { if ( ! this . _subject ) this . _subject = new Subject < [ string , PinLike ] > ( ) ; this . entries . forEach ( entry => subscriber ( ... entry ) ) ; return this . track ( this . _subject . subscribe ( entry => subscriber ( ... entry ) ) ) ; } /**
     *
     * Clears all the created pins and remove references to them,
     * also will remove all subscriptions.
     *
     */ public clear ( ) { this . pins . forEach ( pin => pin . clear ( ) ) ; this . _pins = { } ; if ( this . _subject ) { this . _subject . complete ( ) ; this . _subject = undefined ; } return super . clear ( ) ; } }