Skip to content

Commit

Permalink
Fix: fix loading of preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
pietervdvn committed Oct 8, 2024
1 parent f8ef32f commit 7060f7c
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/Logic/Osm/OsmPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class OsmPreferences {

private initPreference(key: string, value: string = undefined): UIEventSource<string> {
if (this.preferences[key] !== undefined) {
if(value !== undefined){
if (value !== undefined) {
this.preferences[key].set(value)
}
return this.preferences[key]
Expand All @@ -62,6 +62,9 @@ export class OsmPreferences {
this.seenKeys = Object.keys(prefs)
const legacy = OsmPreferences.getLegacyCombinedItems(prefs)
const merged = OsmPreferences.mergeDict(prefs)
if(Object.keys(legacy).length > 0){
await this.removeLegacy(legacy)
}
for (const key in merged) {
this.initPreference(key, prefs[key])
}
Expand Down Expand Up @@ -119,6 +122,15 @@ export class OsmPreferences {
this.removeAllWithPrefix("")
}

public async removeLegacy(legacyDict: Record<string, string>) {
for (const k in legacyDict) {
const v = legacyDict[k]
console.log("Upgrading legacy preference",k )
await this.removeAllWithPrefix(k)
this.osmConnection.getPreference(k).set(v)
}
}

/**
*
* OsmPreferences.mergeDict({abc: "123", def: "123", "def:0": "456", "def:1":"789"}) // => {abc: "123", def: "123456789"}
Expand All @@ -129,7 +141,7 @@ export class OsmPreferences {
const allKeys: string[] = Object.keys(dict)
const normalKeys = allKeys.filter(k => !k.match(/[a-z-_0-9A-Z]*:[0-9]+/))
for (const normalKey of normalKeys) {
if(normalKey.match(/-combined-[0-9]*$/) || normalKey.match(/-combined-length$/)){
if (normalKey.match(/-combined-[0-9]*$/) || normalKey.match(/-combined-length$/)) {
// Ignore legacy keys
continue
}
Expand All @@ -140,6 +152,7 @@ export class OsmPreferences {
return newDict
}


/**
* Gets all items which have a 'combined'-string, the legacy long preferences
*
Expand All @@ -158,7 +171,7 @@ export class OsmPreferences {
public static getLegacyCombinedItems(dict: Record<string, string>): Record<string, string> {
const merged: Record<string, string> = {}
const keys = Object.keys(dict)
const toCheck =Utils.NoNullInplace( Utils.Dedup(keys.map(k => k.match(/(.*)-combined-[0-9]+$/)?.[1])))
const toCheck = Utils.NoNullInplace(Utils.Dedup(keys.map(k => k.match(/(.*)-combined-[0-9]+$/)?.[1])))
for (const key of toCheck) {
let i = 0
let str = ""
Expand All @@ -170,11 +183,10 @@ export class OsmPreferences {
} while (v !== undefined)
merged[key] = str
}


return merged
}


/**
* Bulk-downloads all preferences
* @private
Expand Down Expand Up @@ -217,7 +229,7 @@ export class OsmPreferences {
*
*/
private static keysStartingWith(allKeys: string[], key: string): string[] {
const keys = allKeys.filter(k => k === key || k.match(new RegExp(key + ":[0-9]+")))
const keys = allKeys.filter(k => k === key || k.match(new RegExp(key + ":[0-9]+")))
keys.sort()
return keys
}
Expand All @@ -242,6 +254,7 @@ export class OsmPreferences {
while (v.length > 0) {
await this.uploadKeyDirectly(`${k}:${i}`, v.slice(0, 255))
v = v.slice(255)
i++
}

}
Expand Down Expand Up @@ -333,12 +346,5 @@ export class OsmPreferences {
}
}

getExistingPreference(key: string, defaultValue: undefined, prefix: string): UIEventSource<string> {
if (prefix) {
key = prefix + key
}
key = key.replace(/[:/"' {}.%\\]/g, "")
return this.preferences[key]

}
}

0 comments on commit 7060f7c

Please sign in to comment.