import { assert } from "@here/harp-utils" ; import { ImageItem , TexturizableImage } from "./Image" ; import { ImageCache } from "./ImageCache" ; /**
 * Cache images wrapped into  ImageItem s for a  MapView .
 *
 *  remarks An image may have multiple names in a theme, the `MapViewImageCache` maps different names to the
 * same image URL, and allows to share the image by URL to different MapViews.
 * Within a MapView instance, the (optional) name is unique, so registering multiple images with the
 * same name is invalid.
 *
 * The `MapViewImageCache` uses a global  ImageCache to actually store (and generate) the
 * image data.
  export class MapViewImageCache { private readonly m_name2Url : Map < string , string > = new Map ( ) ; private readonly m_urlNameCount : Map < string , number > = new Map ( ) ; /**
     * Add an image from an URL and optionally start loading it, storing the resulting
     *  TexturizableImage in a  ImageItem .
     *
     *  remarks Names are unique within a  MapView . URLs are not unique, multiple images with
     * different names can have the same URL. Still, URLs are are loaded only once.
     * If an image with the same name is already registered an error is thrown.
     *
     *  param name param url param startLoading returns The resulting  ImageItem or a promise for it if it starts loading.
      addImage ( name , url , startLoading ? ) ; /**
     * Add an image storing it in a  ImageItem .
     *
     *  remarks Names are unique within a  MapView . If an image with the same name is already
     * registered an error is thrown.
     *
     *  param name param image returns The resulting  ImageItem addImage ( name , image ) ; addImage ( name , urlOrImage , startLoading = true ) { if ( typeof urlOrImage === "string" ) { const url = urlOrImage ; const imageItem = this . registerImage ( name , url ) ; return startLoading ? imageItem . loadImage ( ) : imageItem ; } const image = urlOrImage ; return this . registerImage ( name , undefined , image ) ; } param name returns removeImage ( name ) { const url = this . m_name2Url . get ( name ) ; if ( url !== undefined ) { this . m_name2Url . delete ( name ) ; let nameCount = 1 ; if ( name !== url ) { const result = this . m_urlNameCount . get ( url ) ; assert ( result !== undefined ) ; nameCount = result ! ; assert ( nameCount > 0 ) ; } if ( nameCount > 1 ) { this . m_urlNameCount . set ( url , nameCount - 1 ) ; } else { this . m_urlNameCount . delete ( url ) ; ImageCache . instance . removeImage ( url , this ) ; } return true ; } return false ; } /**
     * Find  ImageItem by its name.
     *
     *  param name findImageByName ( name ) { const url = this . m_name2Url . get ( name ) ; if ( url === undefined ) { return undefined ; } return ImageCache . instance . findImage ( url ) ; } /**
     * Remove all  ImageItem s from the cache.
     *
     *  remarks Also removes all  ImageItem s that belong to this
     *  MapView from the global  ImageCache .
     *  returns clear ( ) { ImageCache . instance . clear ( this ) ; this . m_name2Url . clear ( ) ; this . m_urlNameCount . clear ( ) ; } param name param url param image - Optional  TexturizableImage .
      private registerImage ( name , url ? , image ? ) { if ( this . hasName ( name ) ) { throw new Error ( "duplicate name in cache" ) ; } if ( url === undefined ) { assert ( image !== undefined ) ; url = name ; } if ( url !== name ) { const nameCount = this . m_urlNameCount . get ( url ) ?? 0 ; this . m_urlNameCount . set ( url , nameCount + 1 ) ; } this . m_name2Url . set ( name , url ) ; return ImageCache . instance . registerImage ( this , url , image ) ; } private hasName ( name ) { return this . m_name2Url . get ( name ) !== undefined ; } }