import { ImageItem , TexturizableImage } from "./Image" ; /**
 * Combines an  ImageItem with a list of owners (which can be any object) that reference it.
  class ImageCacheItem { /**
     * The list of owners referencing the  ImageItem .
      owners : any [ ] = [ ] ; param imageItem - The  ImageItem referenced by the associated owners.
     *  param owner - First owner referencing the  ImageItem .
      constructor ( public imageItem , owner ) { this . owners . push ( owner ) ; } } internal remarks export class ImageCache { /**
     * Returns the singleton `instance` of the `ImageCache`.
     */ static get instance ( ) : ImageCache { if ( ImageCache . m_instance === undefined ) { ImageCache . m_instance = new ImageCache ( ) ; } return ImageCache . m_instance ; } remarks static dispose ( ) { ImageCache . m_instance = undefined ; } private static m_instance : ImageCache | undefined ; private readonly m_images : Map < string , ImageCacheItem > = new Map ( ) ; param owner - Specify which  any requests the image.
     *  param url param image - Optional  TexturizableImage .
      registerImage ( owner , url , image ? ) { let imageCacheItem = this . findImageCacheItem ( url ) ; if ( imageCacheItem ) { if ( owner !== undefined && ! imageCacheItem . owners . includes ( owner ) ) { imageCacheItem . owners . push ( owner ) ; } return imageCacheItem . imageItem ; } imageCacheItem = { imageItem : new ImageItem ( url , image ) , owners : [ owner ] } ; this . m_images . set ( url , imageCacheItem ) ; return imageCacheItem . imageItem ; } param url param owner returns removeImage ( url , owner ) { const cacheItem = this . m_images . get ( url ) ; if ( cacheItem !== undefined ) { this . unlinkCacheItem ( cacheItem , owner ) ; return true ; } return false ; } /**
     * Find  ImageItem for the specified URL.
     *
     *  param url returns findImage ( url ) { const cacheItem = this . m_images . get ( url ) ; if ( cacheItem !== undefined ) { return cacheItem . imageItem ; } return undefined ; } /**
     * Clear all  ImageItem s belonging to an owner.
     *
     *  remarks param owner - specify to remove all items registered by  any .
     *  returns clear ( owner ) { this . m_images . forEach ( cacheItem => { this . unlinkCacheItem ( cacheItem , owner ) ; } ) ; } /**
     * Returns the number of all cached  ImageItem s.
      get size ( ) : number { return this . m_images . size ; } /**
     * Find the cached  ImageItem by URL.
     *
     *  param url private findImageCacheItem ( url ) { return this . m_images . get ( url ) ; } param imageItem private cancelLoading ( imageItem ) { if ( imageItem . loading ) { imageItem . cancelled = true ; } } param cacheItem param owner private unlinkCacheItem ( cacheItem , owner ) { const ownerIndex = cacheItem . owners . indexOf ( owner ) ; if ( ownerIndex >= 0 ) { cacheItem . owners . splice ( ownerIndex , 1 ) ; } if ( cacheItem . owners . length === 0 ) { this . m_images . delete ( cacheItem . imageItem . url ) ; this . cancelLoading ( cacheItem . imageItem ) ; } } }