2021-09-20 17:14:55 +02:00
|
|
|
/***
|
|
|
|
* Saves all the features that are passed in to localstorage, so they can be retrieved on the next run
|
|
|
|
*
|
|
|
|
* Technically, more an Actor then a featuresource, but it fits more neatly this ay
|
|
|
|
*/
|
2021-11-15 11:51:32 +01:00
|
|
|
import FeatureSource, {Tiled} from "../FeatureSource";
|
2021-10-25 21:08:44 +02:00
|
|
|
import {Tiles} from "../../../Models/TileRange";
|
2021-11-15 11:51:32 +01:00
|
|
|
import {IdbLocalStorage} from "../../Web/IdbLocalStorage";
|
|
|
|
import {UIEventSource} from "../../UIEventSource";
|
2021-09-21 02:10:42 +02:00
|
|
|
|
2021-09-22 05:02:09 +02:00
|
|
|
export default class SaveTileToLocalStorageActor {
|
2021-11-15 11:51:32 +01:00
|
|
|
private readonly visitedTiles: UIEventSource<Map<number, Date>>
|
|
|
|
private readonly _layerId: string;
|
|
|
|
static storageKey: string = "";
|
|
|
|
|
|
|
|
constructor(layerId: string) {
|
|
|
|
this._layerId = layerId;
|
|
|
|
this.visitedTiles = IdbLocalStorage.Get("visited_tiles_" + layerId,
|
|
|
|
{defaultValue: new Map<number, Date>(), })
|
|
|
|
}
|
2021-09-30 04:13:23 +02:00
|
|
|
|
2021-11-15 11:51:32 +01:00
|
|
|
public loadAvailableTiles(){
|
|
|
|
this.visitedTiles.addCallbackAndRunD()
|
|
|
|
}
|
2021-10-25 21:08:44 +02:00
|
|
|
|
2021-11-15 11:51:32 +01:00
|
|
|
public addTile(tile: FeatureSource & Tiled){
|
|
|
|
tile.features.addCallbackAndRunD(features => {
|
2021-09-30 04:13:23 +02:00
|
|
|
const now = new Date()
|
2021-09-20 17:14:55 +02:00
|
|
|
|
2021-11-15 11:51:32 +01:00
|
|
|
if (features.length > 0) {
|
|
|
|
IdbLocalStorage.SetDirectly(this._layerId+"_"+tile.tileIndex, features)
|
2021-09-20 17:14:55 +02:00
|
|
|
}
|
2021-11-15 11:51:32 +01:00
|
|
|
// We _still_ write the time to know that this tile is empty!
|
|
|
|
this.MarkVisited(tile.tileIndex, now)
|
2021-09-20 17:14:55 +02:00
|
|
|
})
|
|
|
|
}
|
2021-11-15 11:51:32 +01:00
|
|
|
|
|
|
|
public poison(lon: number, lat: number) {
|
2021-10-25 21:08:44 +02:00
|
|
|
for (let z = 0; z < 25; z++) {
|
|
|
|
const {x, y} = Tiles.embedded_tile(lat, lon, z)
|
|
|
|
const tileId = Tiles.tile_index(z, x, y)
|
2021-11-15 11:51:32 +01:00
|
|
|
this.visitedTiles.data.delete(tileId)
|
2021-10-25 21:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-11-15 11:51:32 +01:00
|
|
|
|
|
|
|
public MarkVisited(tileId: number, freshness: Date) {
|
|
|
|
this.visitedTiles.data.set(tileId, freshness)
|
|
|
|
this.visitedTiles.ping()
|
|
|
|
}
|
2021-09-20 17:14:55 +02:00
|
|
|
}
|