forked from MapComplete/MapComplete
refactoring
This commit is contained in:
parent
b94a8f5745
commit
5d0fe31c41
114 changed files with 2412 additions and 2958 deletions
63
Logic/FeatureSource/Actors/TileLocalStorage.ts
Normal file
63
Logic/FeatureSource/Actors/TileLocalStorage.ts
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import { IdbLocalStorage } from "../../Web/IdbLocalStorage"
|
||||
import { UIEventSource } from "../../UIEventSource"
|
||||
|
||||
/**
|
||||
* A class which allows to read/write a tile to local storage.
|
||||
*
|
||||
* Does the heavy lifting for LocalStorageFeatureSource and SaveFeatureToLocalStorage
|
||||
*/
|
||||
export default class TileLocalStorage<T> {
|
||||
private static perLayer: Record<string, TileLocalStorage<any>> = {}
|
||||
private readonly _layername: string
|
||||
private readonly cachedSources: Record<number, UIEventSource<T>> = {}
|
||||
|
||||
private constructor(layername: string) {
|
||||
this._layername = layername
|
||||
}
|
||||
|
||||
public static construct<T>(layername: string): TileLocalStorage<T> {
|
||||
const cached = TileLocalStorage.perLayer[layername]
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
|
||||
const tls = new TileLocalStorage<T>(layername)
|
||||
TileLocalStorage.perLayer[layername] = tls
|
||||
return tls
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a UIEventSource element which is synced with localStorage
|
||||
* @param layername
|
||||
* @param tileIndex
|
||||
*/
|
||||
public getTileSource(tileIndex: number): UIEventSource<T> {
|
||||
const cached = this.cachedSources[tileIndex]
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
const src = UIEventSource.FromPromise(this.GetIdb(tileIndex))
|
||||
src.addCallbackD((data) => this.SetIdb(tileIndex, data))
|
||||
this.cachedSources[tileIndex] = src
|
||||
return src
|
||||
}
|
||||
|
||||
private SetIdb(tileIndex: number, data): void {
|
||||
try {
|
||||
IdbLocalStorage.SetDirectly(this._layername + "_" + tileIndex, data)
|
||||
} catch (e) {
|
||||
console.error(
|
||||
"Could not save tile to indexed-db: ",
|
||||
e,
|
||||
"tileIndex is:",
|
||||
tileIndex,
|
||||
"for layer",
|
||||
this._layername
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private GetIdb(tileIndex: number): Promise<any> {
|
||||
return IdbLocalStorage.GetDirectly(this._layername + "_" + tileIndex)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue