forked from MapComplete/MapComplete
Refactoring: re-enable caching
This commit is contained in:
parent
59544ec073
commit
476423f9d1
5 changed files with 19 additions and 3 deletions
|
@ -11,6 +11,7 @@ import { UIEventSource } from "../../UIEventSource"
|
|||
export default class TileLocalStorage<T> {
|
||||
private static perLayer: Record<string, TileLocalStorage<any>> = {}
|
||||
private readonly _layername: string
|
||||
private readonly inUse = new UIEventSource(false)
|
||||
private readonly cachedSources: Record<number, UIEventSource<T> & { flush: () => void }> = {}
|
||||
|
||||
private constructor(layername: string) {
|
||||
|
@ -49,7 +50,10 @@ export default class TileLocalStorage<T> {
|
|||
|
||||
private async SetIdb(tileIndex: number, data: any): Promise<void> {
|
||||
try {
|
||||
await this.inUse.AsPromise((inUse) => !inUse)
|
||||
this.inUse.setData(true)
|
||||
await IdbLocalStorage.SetDirectly(this._layername + "_" + tileIndex, data)
|
||||
this.inUse.setData(false)
|
||||
} catch (e) {
|
||||
console.error(
|
||||
"Could not save tile to indexed-db: ",
|
||||
|
|
|
@ -240,6 +240,12 @@ export abstract class Store<T> implements Readable<T> {
|
|||
return newSource
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the uiEventSource into a promise.
|
||||
* The promise will return the value of the store if the given condition evaluates to true
|
||||
* @param condition: an optional condition, default to 'store.value !== undefined'
|
||||
* @constructor
|
||||
*/
|
||||
public AsPromise(condition?: (t: T) => boolean): Promise<T> {
|
||||
const self = this
|
||||
condition = condition ?? ((t) => t !== undefined)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue