import "@here/harp-fetch" ; import { DecodedTile , ITileDecoder , RequestController , TileInfo } from "@here/harp-datasource-protocol" ; import { TileKey } from "@here/harp-geoutils" ; import { BaseTileLoader , DataSource } from "@here/harp-mapview" ; import { TileLoaderState } from "@here/harp-mapview/lib/ITileLoader" ; import { LoggerManager } from "@here/harp-utils" ; import { DataProvider } from "./DataProvider" ; /**
 * Logger to write to console etc.
 */ const logger = LoggerManager . instance . create ( "TileLoader" ) ; /**
 * The [[TileLoader]] manages the different states of loading and decoding for a [[Tile]]. Used by
 * the [[TileDataSource]].
 */ export class TileLoader extends BaseTileLoader { /**
     * The binary data in form of [[ArrayBufferLike]], or any object.
     */ payload ? : ArrayBufferLike | { } ; /**
     * The result of decoding the `payload`: The [[DecodedTile]].
     */ decodedTile ? : DecodedTile ; /**
     * The  notifying the [[ITileDecoder]] to cancel decoding.
     */ protected requestController ? : RequestController ; param dataSource param tileKey param dataProvider param tileDecoder constructor ( protected dataSource , protected tileKey , protected dataProvider , protected tileDecoder ) { super ( dataSource , tileKey ) ; } @override
      get priority ( ) : number { return this . m_priority ; } @override
      set priority ( priority ) { this . m_priority = priority ; if ( this . requestController !== undefined ) { this . requestController . priority = priority ; } } @override
      protected loadImpl ( abortSignal , onDone , onError ) { this . dataProvider . getTile ( this . tileKey , abortSignal ) . then ( payload => { if ( abortSignal . aborted ) { const err = new Error ( "Aborted" ) ; err . name = "AbortError" ; throw err ; } this . onLoaded ( payload , onDone , onError ) ; } ) . catch ( error => { if ( error . name === "AbortError" || error . message === "AbortError: Aborted" ) { return ; } onError ( error ) ; } ) ; } @override
      protected cancelImpl ( ) { if ( this . state === TileLoaderState . Decoding && this . requestController ) { this . requestController . abort ( ) ; this . requestController = undefined ; } } /**
     * Start decoding the payload.
     */ protected startDecodeTile ( onDone , onError ) { const payload = this . payload ; if ( payload === undefined ) { logger . error ( "TileLoader#startDecodeTile: Cannot decode without payload" ) ; return ; } this . state = TileLoaderState . Decoding ; this . payload = undefined ; const requestController = new RequestController ( this . priority ) ; this . requestController = requestController ; const dataSource = this . dataSource ; this . tileDecoder . decodeTile ( payload , this . tileKey , dataSource . projection , requestController ) . then ( decodedTile => { if ( requestController . signal . aborted ) { return ; } this . onDecoded ( decodedTile , onDone ) ; } ) . catch ( error => { if ( error . name === "AbortError" || error . message === "AbortError: Aborted" ) { return ; } onError ( error ) ; } ) ; } param payload private onLoaded ( payload , onDone , onError ) { this . state = TileLoaderState . Loaded ; this . payload = payload ; const byteLength = ( payload as ArrayBufferLike ) . byteLength ; if ( byteLength === 0 || ( payload . constructor === Object && Object . keys ( payload ) . length === 0 ) ) { this . onDecoded ( { geometries : [ ] , techniques : [ ] } , onDone ) ; return ; } this . startDecodeTile ( onDone , onError ) ; } param decodedTile private onDecoded ( decodedTile , onDone ) { this . decodedTile = decodedTile ; onDone ( TileLoaderState . Ready ) ; } } /**
 * Subclass of [[TileLoader]] which is used by [[TileDataSource]] to load the [[TileInfo]] meta
 * data, not the tile data itself.
 */ export class TileInfoLoader extends TileLoader { tileInfo ? : TileInfo ; @override  protected startDecodeTile ( onDone , onError ) { const payload = this . payload ; if ( payload === undefined ) { logger . error ( "TileInfoLoader#startDecodeTile: Cannot decode without payload" ) ; return ; } this . state = TileLoaderState . Decoding ; this . payload = undefined ; const requestController = new RequestController ( this . priority ) ; this . requestController = requestController ; const dataSource = this . dataSource ; this . tileDecoder . getTileInfo ( payload , this . tileKey , dataSource . projection , requestController ) . then ( tileInfo => { if ( requestController . signal . aborted ) { return ; } this . tileInfo = tileInfo ; onDone ( TileLoaderState . Ready ) ; } ) . catch ( error => { if ( error . name === "AbortError" || error . message === "AbortError: Aborted" ) { return ; } onError ( error ) ; } ) ; } }