2021-09-20 17:14:55 +02:00
import FilteredLayer from "../../../Models/FilteredLayer" ;
2021-09-21 02:10:42 +02:00
import { FeatureSourceForLayer , Tiled } from "../FeatureSource" ;
2021-09-20 17:14:55 +02:00
import { UIEventSource } from "../../UIEventSource" ;
2021-09-21 02:10:42 +02:00
import TileHierarchy from "./TileHierarchy" ;
2021-09-22 05:02:09 +02:00
import SaveTileToLocalStorageActor from "../Actors/SaveTileToLocalStorageActor" ;
2021-09-26 17:36:39 +02:00
import { Tiles } from "../../../Models/TileRange" ;
2021-09-28 17:30:48 +02:00
import { BBox } from "../../BBox" ;
2021-09-21 02:10:42 +02:00
export default class TiledFromLocalStorageSource implements TileHierarchy < FeatureSourceForLayer & Tiled > {
2021-10-11 22:30:22 +02:00
public readonly loadedTiles : Map < number , FeatureSourceForLayer & Tiled > = new Map < number , FeatureSourceForLayer & Tiled > ( ) ;
private readonly layer : FilteredLayer ;
private readonly handleFeatureSource : ( src : FeatureSourceForLayer & Tiled , index : number ) = > void ;
private readonly undefinedTiles : Set < number > ;
2021-09-30 04:13:23 +02:00
public static GetFreshnesses ( layerId : string ) : Map < number , Date > {
const prefix = SaveTileToLocalStorageActor . storageKey + "-" + layerId + "-"
const freshnesses = new Map < number , Date > ( )
for ( const key of Object . keys ( localStorage ) ) {
if ( ! ( key . startsWith ( prefix ) && key . endsWith ( "-time" ) ) ) {
continue
}
const index = Number ( key . substring ( prefix . length , key . length - "-time" . length ) )
const time = Number ( localStorage . getItem ( key ) )
const freshness = new Date ( )
freshness . setTime ( time )
freshnesses . set ( index , freshness )
}
return freshnesses
}
2021-09-20 17:14:55 +02:00
constructor ( layer : FilteredLayer ,
2021-09-21 02:10:42 +02:00
handleFeatureSource : ( src : FeatureSourceForLayer & Tiled , index : number ) = > void ,
2021-09-20 17:14:55 +02:00
state : {
2021-10-11 22:30:22 +02:00
currentBounds : UIEventSource < BBox >
2021-09-20 17:14:55 +02:00
} ) {
2021-10-11 22:30:22 +02:00
this . layer = layer ;
this . handleFeatureSource = handleFeatureSource ;
2021-09-21 02:10:42 +02:00
2021-10-11 22:30:22 +02:00
this . undefinedTiles = new Set < number > ( )
2021-09-22 05:02:09 +02:00
const prefix = SaveTileToLocalStorageActor . storageKey + "-" + layer . layerDef . id + "-"
2021-10-11 22:30:22 +02:00
const knownTiles : number [ ] = Object . keys ( localStorage )
2021-09-21 02:10:42 +02:00
. filter ( key = > {
2021-09-29 23:56:59 +02:00
return key . startsWith ( prefix ) && ! key . endsWith ( "-time" ) && ! key . endsWith ( "-format" ) ;
2021-09-21 02:10:42 +02:00
} )
. map ( key = > {
return Number ( key . substring ( prefix . length ) ) ;
} )
2021-09-29 23:56:59 +02:00
. filter ( i = > ! isNaN ( i ) )
2021-09-21 02:10:42 +02:00
2021-10-11 22:30:22 +02:00
console . debug ( "Layer" , layer . layerDef . id , "has following tiles in available in localstorage" , knownTiles . map ( i = > Tiles . tile_from_index ( i ) . join ( "/" ) ) . join ( ", " ) )
for ( const index of knownTiles ) {
2021-09-30 04:13:23 +02:00
const prefix = SaveTileToLocalStorageActor . storageKey + "-" + layer . layerDef . id + "-" + index ;
const version = localStorage . getItem ( prefix + "-format" )
if ( version === undefined || version !== SaveTileToLocalStorageActor . formatVersion ) {
2021-09-29 19:56:59 +02:00
// Invalid version! Remove this tile from local storage
localStorage . removeItem ( prefix )
2021-09-30 04:13:23 +02:00
localStorage . removeItem ( prefix + "-time" )
localStorage . removeItem ( prefix + "-format" )
2021-10-11 22:30:22 +02:00
this . undefinedTiles . add ( index )
2021-09-29 19:56:59 +02:00
console . log ( "Dropped old format tile" , prefix )
}
2021-09-28 17:30:48 +02:00
}
2021-09-21 02:10:42 +02:00
2021-10-11 22:30:22 +02:00
const self = this
state . currentBounds . map ( bounds = > {
2021-09-21 02:10:42 +02:00
2021-10-11 22:30:22 +02:00
if ( bounds === undefined ) {
return ;
}
for ( const knownTile of knownTiles ) {
if ( this . loadedTiles . has ( knownTile ) ) {
continue ;
2021-09-21 02:10:42 +02:00
}
2021-10-11 22:30:22 +02:00
if ( this . undefinedTiles . has ( knownTile ) ) {
continue ;
2021-09-21 02:10:42 +02:00
}
2021-10-11 22:30:22 +02:00
if ( ! bounds . overlapsWith ( BBox . fromTileIndex ( knownTile ) ) ) {
continue ;
2021-09-21 02:10:42 +02:00
}
2021-10-11 22:30:22 +02:00
self . loadTile ( knownTile )
2021-09-21 02:10:42 +02:00
}
} )
2021-09-20 17:14:55 +02:00
}
2021-10-11 22:30:22 +02:00
private loadTile ( neededIndex : number ) {
try {
const key = SaveTileToLocalStorageActor . storageKey + "-" + this . layer . layerDef . id + "-" + neededIndex
const data = localStorage . getItem ( key )
const features = JSON . parse ( data )
const src = {
layer : this.layer ,
features : new UIEventSource < { feature : any ; freshness : Date } [ ] > ( features ) ,
name : "FromLocalStorage(" + key + ")" ,
tileIndex : neededIndex ,
bbox : BBox.fromTileIndex ( neededIndex )
}
this . handleFeatureSource ( src , neededIndex )
this . loadedTiles . set ( neededIndex , src )
} catch ( e ) {
console . error ( "Could not load data tile from local storage due to" , e )
this . undefinedTiles . add ( neededIndex )
}
}
2021-09-20 17:14:55 +02:00
2021-09-21 02:10:42 +02:00
2021-09-20 17:14:55 +02:00
}