forked from MapComplete/MapComplete
Feature: stabilize saved history, add code to cleanup old preferences, make loading preferences faster (which prevents a 'hang') when just logged in
This commit is contained in:
parent
dc1e582664
commit
c1d3f35d30
10 changed files with 249 additions and 146 deletions
|
@ -26,10 +26,12 @@ class RoundRobinStore<T> {
|
|||
private readonly _index: UIEventSource<number>
|
||||
private readonly _value: UIEventSource<T[]>
|
||||
public readonly value: Store<T[]>
|
||||
private readonly _maxCount: number
|
||||
|
||||
constructor(store: UIEventSource<T[]>, index: UIEventSource<number>) {
|
||||
constructor(store: UIEventSource<T[]>, index: UIEventSource<number>, maxCount: number) {
|
||||
this._store = store
|
||||
this._index = index
|
||||
this._maxCount = maxCount
|
||||
this._value = new UIEventSource([])
|
||||
this.value = this._value
|
||||
this._store.addCallbackD(() => this.set())
|
||||
|
@ -40,6 +42,9 @@ class RoundRobinStore<T> {
|
|||
const v = this._store.data
|
||||
const i = this._index.data
|
||||
const newList = Utils.NoNull(v.slice(i + 1, v.length).concat(v.slice(0, i + 1)))
|
||||
if (newList.length === 0) {
|
||||
this._index.set(0)
|
||||
}
|
||||
this._value.set(newList)
|
||||
}
|
||||
|
||||
|
@ -49,7 +54,7 @@ class RoundRobinStore<T> {
|
|||
*/
|
||||
public add(t: T) {
|
||||
const i = this._index.data
|
||||
this._index.set((i + 1) % this._store.data.length)
|
||||
this._index.set((i + 1) % this._maxCount)
|
||||
this._store.data[i] = t
|
||||
this._store.ping()
|
||||
|
||||
|
@ -78,15 +83,21 @@ export class OptionallySyncedHistory<T extends object | string> {
|
|||
this.osmconnection = osmconnection
|
||||
this._maxHistory = maxHistory
|
||||
this._isSame = isSame
|
||||
this.syncPreference = osmconnection.getPreference("preference-" + key + "-history", "sync")
|
||||
this.syncPreference = osmconnection.getPreference("preference-" + key + "-history", {
|
||||
defaultValue: "sync"
|
||||
})
|
||||
|
||||
this.syncedBackingStore = Stores.fromArray(
|
||||
Utils.TimesT(maxHistory, (i) =>
|
||||
UIEventSource.asObject<T>(osmconnection.getPreference(key + "-history-" + i), undefined)
|
||||
))
|
||||
this.syncedOrdered = new RoundRobinStore<T>(this.syncedBackingStore,
|
||||
UIEventSource.asInt(osmconnection.getPreference(key + "-history-round-robin", "0"))
|
||||
)
|
||||
Utils.TimesT(maxHistory, (i) => {
|
||||
const pref = osmconnection.getPreference(key + "-hist-" + i + "-")
|
||||
pref.addCallbackAndRun(v => console.trace(">>> pref", pref.tag, " is now ", v))
|
||||
return UIEventSource.asObject<T>(pref, undefined)
|
||||
}))
|
||||
|
||||
const ringIndex = UIEventSource.asInt(osmconnection.getPreference(key + "-hist-round-robin", {
|
||||
defaultValue: "0"
|
||||
}))
|
||||
this.syncedOrdered = new RoundRobinStore<T>(this.syncedBackingStore, ringIndex, 10)
|
||||
const local = (this.local = LocalStorageSource.getParsed<T[]>(key + "-history", []))
|
||||
const thisSession = (this.thisSession = new UIEventSource<T[]>(
|
||||
[],
|
||||
|
@ -250,33 +261,33 @@ export default class UserRelatedState {
|
|||
this.osmConnection = osmConnection
|
||||
|
||||
this.showAllQuestionsAtOnce = UIEventSource.asBoolean(
|
||||
this.osmConnection.getPreference("show-all-questions", "false")
|
||||
this.osmConnection.getPreference("show-all-questions", { defaultValue: "false" })
|
||||
)
|
||||
this.language = this.osmConnection.getPreference("language")
|
||||
this.showTags = this.osmConnection.getPreference("show_tags")
|
||||
this.showCrosshair = this.osmConnection.getPreference("show_crosshair")
|
||||
this.fixateNorth = this.osmConnection.getPreference("fixate-north")
|
||||
this.morePrivacy = this.osmConnection.getPreference("more_privacy", "no")
|
||||
this.morePrivacy = this.osmConnection.getPreference("more_privacy", { defaultValue: "no" })
|
||||
|
||||
this.a11y = this.osmConnection.getPreference("a11y")
|
||||
|
||||
this.mangroveIdentity = new MangroveIdentity(
|
||||
this.osmConnection.getPreference("identity", undefined, "mangrove"),
|
||||
this.osmConnection.getPreference("identity-creation-date", undefined, "mangrove")
|
||||
)
|
||||
this.preferredBackgroundLayer = this.osmConnection.getPreference(
|
||||
"preferred-background-layer"
|
||||
this.osmConnection.getPreference("identity", { defaultValue: undefined, prefix: "mangrove" }),
|
||||
this.osmConnection.getPreference("identity-creation-date", {
|
||||
defaultValue: undefined,
|
||||
prefix: "mangrove"
|
||||
})
|
||||
)
|
||||
this.preferredBackgroundLayer = this.osmConnection.getPreference("preferred-background-layer")
|
||||
|
||||
this.addNewFeatureMode = this.osmConnection.getPreference(
|
||||
"preferences-add-new-mode",
|
||||
"button_click_right"
|
||||
this.addNewFeatureMode = this.osmConnection.getPreference("preferences-add-new-mode",
|
||||
{ defaultValue: "button_click_right" }
|
||||
)
|
||||
this.showScale = UIEventSource.asBoolean(
|
||||
this.osmConnection.GetPreference("preference-show-scale", "false")
|
||||
this.osmConnection.getPreference("preference-show-scale", { defaultValue: "false" })
|
||||
)
|
||||
|
||||
this.imageLicense = this.osmConnection.getPreference("pictures-license", "CC0")
|
||||
this.imageLicense = this.osmConnection.getPreference("pictures-license", { defaultValue: "CC0" })
|
||||
this.installedUserThemes = UserRelatedState.initInstalledUserThemes(osmConnection)
|
||||
this.translationMode = this.initTranslationMode()
|
||||
this.homeLocation = this.initHomeLocation()
|
||||
|
@ -309,7 +320,7 @@ export default class UserRelatedState {
|
|||
|
||||
private initTranslationMode(): UIEventSource<"false" | "true" | "mobile" | undefined | string> {
|
||||
const translationMode: UIEventSource<undefined | "true" | "false" | "mobile" | string> =
|
||||
this.osmConnection.getPreference("translation-mode", "false")
|
||||
this.osmConnection.getPreference("translation-mode", { defaultValue: "false" })
|
||||
translationMode.addCallbackAndRunD((mode) => {
|
||||
mode = mode.toLowerCase()
|
||||
if (mode === "true" || mode === "yes") {
|
||||
|
@ -358,7 +369,7 @@ export default class UserRelatedState {
|
|||
try {
|
||||
return JSON.parse(str)
|
||||
} catch (e) {
|
||||
e.warn(
|
||||
console.warn(
|
||||
"Removing theme " +
|
||||
id +
|
||||
" as it could not be parsed from the preferences; the content is:",
|
||||
|
@ -639,7 +650,7 @@ export default class UserRelatedState {
|
|||
public getThemeDisabled(themeId: string, layerId: string): UIEventSource<string[]> {
|
||||
const flatSource = this.osmConnection.getPreference(
|
||||
"disabled-questions-" + themeId + "-" + layerId,
|
||||
"[]"
|
||||
{ defaultValue: "[]" }
|
||||
)
|
||||
return UIEventSource.asObject<string[]>(flatSource, [])
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue