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:
Pieter Vander Vennet 2025-04-06 19:17:19 +02:00
parent dc1e582664
commit c1d3f35d30
10 changed files with 249 additions and 146 deletions

View file

@ -1,19 +1,18 @@
import { QueryParameters } from "../Web/QueryParameters"
import { BBox } from "../BBox"
import Constants from "../../Models/Constants"
import { GeoLocationState } from "../State/GeoLocationState"
import { GeoLocationPointProperties, GeoLocationState } from "../State/GeoLocationState"
import { Store, UIEventSource } from "../UIEventSource"
import { Feature, LineString, Point } from "geojson"
import { FeatureSource, WritableFeatureSource } from "../FeatureSource/FeatureSource"
import { LocalStorageSource } from "../Web/LocalStorageSource"
import { GeoOperations } from "../GeoOperations"
import { OsmTags } from "../../Models/OsmFeature"
import StaticFeatureSource, {
WritableStaticFeatureSource,
} from "../FeatureSource/Sources/StaticFeatureSource"
import StaticFeatureSource, { WritableStaticFeatureSource } from "../FeatureSource/Sources/StaticFeatureSource"
import { MapProperties } from "../../Models/MapProperties"
import { Orientation } from "../../Sensors/Orientation"
;("use strict")
("use strict")
/**
* The geolocation-handler takes a map-location and a geolocation state.
* It'll move the map as appropriate given the state of the geolocation-API
@ -25,12 +24,12 @@ export default class GeoLocationHandler {
/**
* The location as delivered by the GPS, wrapped as FeatureSource
*/
public currentUserLocation: FeatureSource
public currentUserLocation: FeatureSource<Feature<Point, GeoLocationPointProperties>>
/**
* All previously visited points (as 'Point'-objects), with their metadata
*/
public historicalUserLocations: WritableFeatureSource<Feature<Point>>
public historicalUserLocations: WritableFeatureSource<Feature<Point, GeoLocationPointProperties>>
/**
* A featureSource containing a single linestring which has the GPS-history of the user.
@ -151,27 +150,29 @@ export default class GeoLocationHandler {
}
private CopyGeolocationIntoMapstate() {
const features: UIEventSource<Feature[]> = new UIEventSource<Feature[]>([])
const features: UIEventSource<Feature<Point, GeoLocationPointProperties>[]> = new UIEventSource<Feature<Point, GeoLocationPointProperties>[]>([])
this.currentUserLocation = new StaticFeatureSource(features)
let i = 0
this.geolocationState.currentGPSLocation.addCallbackAndRunD((location) => {
const properties = {
const properties: GeoLocationPointProperties = {
id: "gps-" + i,
"user:location": "yes",
date: new Date().toISOString(),
// GeolocationObject behaves really weird when indexing, so copying it one by one is the most stable
accuracy: location.accuracy,
speed: location.speed,
latitude: location.latitude,
longitude: location.longitude,
altitude: location.altitude,
altitudeAccuracy: location.altitudeAccuracy,
heading: location.heading,
alpha: Orientation.singleton.gotMeasurement.data
? "" + Orientation.singleton.alpha.data
? ("" + Orientation.singleton.alpha.data)
: undefined,
}
i++
const feature = <Feature>{
const feature = <Feature<Point, GeoLocationPointProperties>>{
type: "Feature",
properties,
geometry: {
@ -184,7 +185,7 @@ export default class GeoLocationHandler {
}
private initUserLocationTrail() {
const features = LocalStorageSource.getParsed<Feature<Point>[]>("gps_location_history", [])
const features = LocalStorageSource.getParsed<Feature<Point, GeoLocationPointProperties>[]>("gps_location_history", [])
const now = new Date().getTime()
features.data = features.data.filter((ff) => {
if (ff.properties === undefined) {
@ -197,7 +198,7 @@ export default class GeoLocationHandler {
)
})
features.ping()
this.currentUserLocation?.features?.addCallbackAndRunD(([location]: [Feature<Point>]) => {
this.currentUserLocation?.features?.addCallbackAndRunD(([location]: [Feature<Point, GeoLocationPointProperties>]) => {
if (location === undefined) {
return
}
@ -231,7 +232,7 @@ export default class GeoLocationHandler {
features.ping()
})
this.historicalUserLocations = new WritableStaticFeatureSource<Feature<Point>>(features)
this.historicalUserLocations = new WritableStaticFeatureSource<Feature<Point, GeoLocationPointProperties>>(features)
const asLine = features.map((allPoints) => {
if (allPoints === undefined || allPoints.length < 2) {

View file

@ -27,8 +27,7 @@ export default class PendingChangesUploader {
}
document.addEventListener("mouseout", (e) => {
// @ts-ignore
if (!e.toElement && !e.relatedTarget) {
if (!e["toElement"] && !e.relatedTarget) {
changes.flushChanges("Flushing changes due to focus lost")
}
})

View file

@ -17,6 +17,7 @@ import ChangeTagAction from "./Actions/ChangeTagAction"
import DeleteAction from "./Actions/DeleteAction"
import MarkdownUtils from "../../Utils/MarkdownUtils"
import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore"
import { Feature, Point } from "geojson"
/**
* Handles all changes made to OSM.
@ -37,7 +38,7 @@ export class Changes {
public readonly backend: string
public readonly isUploading = new UIEventSource(false)
public readonly errors = new UIEventSource<string[]>([], "upload-errors")
private readonly historicalUserLocations?: FeatureSource
private readonly historicalUserLocations?: FeatureSource<Feature<Point, GeoLocationPointProperties>>
private _nextId: number = 0 // Newly assigned ID's are negative
private readonly previouslyCreated: OsmObject[] = []
private readonly _leftRightSensitive: boolean
@ -53,7 +54,7 @@ export class Changes {
osmConnection: OsmConnection
reportError?: (error: string) => void
featureProperties?: FeaturePropertiesStore
historicalUserLocations?: FeatureSource
historicalUserLocations?: FeatureSource<Feature<Point, GeoLocationPointProperties>>
allElements?: IndexedFeatureSource
},
leftRightSensitive: boolean = false
@ -66,7 +67,7 @@ export class Changes {
if (isNaN(this._nextId) && state.reportError !== undefined) {
state.reportError(
"Got a NaN as nextID. Pending changes IDs are:" +
this.pendingChanges.data?.map((pch) => pch?.id).join(".")
this.pendingChanges.data?.map((pch) => pch?.id).join(".")
)
this._nextId = -100
}
@ -90,8 +91,8 @@ export class Changes {
return new Changes({
osmConnection: new OsmConnection(),
featureSwitches: {
featureSwitchIsTesting: new ImmutableStore(true),
},
featureSwitchIsTesting: new ImmutableStore(true)
}
})
}
@ -178,50 +179,50 @@ export class Changes {
[
{
key: "comment",
docs: "The changeset comment. Will be a fixed string, mentioning the theme",
docs: "The changeset comment. Will be a fixed string, mentioning the theme"
},
{
key: "theme",
docs: "The name of the theme that was used to create this change. ",
docs: "The name of the theme that was used to create this change. "
},
{
key: "source",
value: "survey",
docs: "The contributor had their geolocation enabled while making changes",
docs: "The contributor had their geolocation enabled while making changes"
},
{
key: "change_within_{distance}",
docs: "If the contributor enabled their geolocation, this will hint how far away they were from the objects they edited. This gives an indication of proximity and if they truly surveyed or were armchair-mapping",
docs: "If the contributor enabled their geolocation, this will hint how far away they were from the objects they edited. This gives an indication of proximity and if they truly surveyed or were armchair-mapping"
},
{
key: "change_over_{distance}",
docs: "If the contributor enabled their geolocation, this will hint how far away they were from the objects they edited. If they were over 5000m away, the might have been armchair-mapping",
docs: "If the contributor enabled their geolocation, this will hint how far away they were from the objects they edited. If they were over 5000m away, the might have been armchair-mapping"
},
{
key: "created_by",
value: "MapComplete <version>",
docs: "The piece of software used to create this changeset; will always start with MapComplete, followed by the version number",
docs: "The piece of software used to create this changeset; will always start with MapComplete, followed by the version number"
},
{
key: "locale",
value: "en|nl|de|...",
docs: "The code of the language that the contributor used MapComplete in. Hints what language the user speaks.",
docs: "The code of the language that the contributor used MapComplete in. Hints what language the user speaks."
},
{
key: "host",
value: "https://mapcomplete.org/<theme>",
docs: "The URL that the contributor used to make changes. One can see the used instance with this",
docs: "The URL that the contributor used to make changes. One can see the used instance with this"
},
{
key: "imagery",
docs: "The identifier of the used background layer, this will probably be an identifier from the [editor layer index](https://github.com/osmlab/editor-layer-index)",
},
docs: "The identifier of the used background layer, this will probably be an identifier from the [editor layer index](https://github.com/osmlab/editor-layer-index)"
}
],
"default"
),
...addSource(ChangeTagAction.metatags, "ChangeTag"),
...addSource(ChangeLocationAction.metatags, "ChangeLocation"),
...addSource(DeleteAction.metatags, "DeleteAction"),
...addSource(DeleteAction.metatags, "DeleteAction")
// TODO
/*
...DeleteAction.metatags,
@ -243,11 +244,11 @@ export class Changes {
docs,
specialMotivation
? "This might give a reason per modified node or way"
: "",
: ""
].join("\n"),
source,
source
])
),
)
].join("\n\n")
}
@ -266,7 +267,7 @@ export class Changes {
this._changesetHandler._remappings.has("node/" + this._nextId) ||
this._changesetHandler._remappings.has("way/" + this._nextId) ||
this._changesetHandler._remappings.has("relation/" + this._nextId)
)
)
return this._nextId
}
@ -333,6 +334,7 @@ export class Changes {
this.previouslyCreated
)
}
public static createChangesetObjectsStatic(
changes: ChangeDescription[],
downloadedOsmObjects: OsmObject[],
@ -502,7 +504,7 @@ export class Changes {
const result = {
newObjects: [],
modifiedObjects: [],
deletedObjects: [],
deletedObjects: []
}
objects.forEach((v, id) => {
@ -559,9 +561,7 @@ export class Changes {
const recentLocationPoints = locations
.filter((feat) => feat.geometry.type === "Point")
.filter((feat) => {
const visitTime = new Date(
(<GeoLocationPointProperties>(<any>feat.properties)).date
)
const visitTime = new Date(feat.properties.date)
// In seconds
const diff = (now.getTime() - visitTime.getTime()) / 1000
return diff < Constants.nearbyVisitTime
@ -665,7 +665,7 @@ export class Changes {
} else {
this._reportError(
`Got an orphaned change. The 'creation'-change description for ${c.type}/${c.id} got lost. Permanently dropping this change:` +
JSON.stringify(c)
JSON.stringify(c)
)
}
return
@ -676,10 +676,10 @@ export class Changes {
} else {
console.log(
"Refusing change about " +
c.type +
"/" +
c.id +
" as not in the objects. No internet?"
c.type +
"/" +
c.id +
" as not in the objects. No internet?"
)
refused.push(c)
}
@ -694,7 +694,7 @@ export class Changes {
*/
private async flushSelectChanges(
pending: ChangeDescription[],
openChangeset: UIEventSource<number>
openChangeset: UIEventSource<{ id: number, opened: number }>
): Promise<ChangeDescription[]> {
const neededIds = Changes.GetNeededIds(pending)
/* Download the latest version of the OSM-objects
@ -775,14 +775,14 @@ export class Changes {
([key, count]) => ({
key: key,
value: count,
aggregate: true,
aggregate: true
})
)
const motivations = pending
.filter((descr) => descr.meta.specialMotivation !== undefined)
.map((descr) => ({
key: descr.meta.changeType + ":" + descr.type + "/" + descr.id,
value: descr.meta.specialMotivation,
value: descr.meta.specialMotivation
}))
const distances = Utils.NoNull(pending.map((descr) => descr.meta.distanceToObject))
@ -813,7 +813,7 @@ export class Changes {
return {
key,
value: count,
aggregate: true,
aggregate: true
}
})
)
@ -828,19 +828,20 @@ export class Changes {
const metatags: ChangesetTag[] = [
{
key: "comment",
value: comment,
value: comment
},
{
key: "theme",
value: theme,
value: theme
},
...perType,
...motivations,
...perBinMessage,
...perBinMessage
]
return metatags
}
private async flushChangesAsync(): Promise<void> {
try {
// At last, we build the changeset and upload
@ -858,16 +859,12 @@ export class Changes {
const refusedChanges: ChangeDescription[][] = await Promise.all(
Array.from(pendingPerTheme, async ([theme, pendingChanges]) => {
try {
const openChangeset = UIEventSource.asInt(
this.state.osmConnection.GetPreference(
"current-open-changeset-" + theme
)
)
const openChangeset = this.state.osmConnection.getCurrentChangesetFor(theme)
console.log(
"Using current-open-changeset-" +
theme +
" from the preferences, got " +
openChangeset.data
theme +
" from the preferences, got " +
openChangeset.data
)
const refused = await this.flushSelectChanges(pendingChanges, openChangeset)

View file

@ -113,11 +113,11 @@ export class ChangesetHandler {
private async UploadWithNew(
generateChangeXML: (csid: number, remappings: Map<string, string>) => string,
openChangeset: UIEventSource<number>,
openChangeset: UIEventSource<{ id: number, opened: number }>,
extraMetaTags: ChangesetTag[]
) {
const csId = await this.OpenChangeset(extraMetaTags)
openChangeset.setData(csId)
openChangeset.setData({ id: csId, opened: new Date().getTime() })
const changeset = generateChangeXML(csId, this._remappings)
console.log(
"Opened a new changeset (openChangeset.data is undefined):",
@ -145,7 +145,7 @@ export class ChangesetHandler {
public async UploadChangeset(
generateChangeXML: (csid: number, remappings: Map<string, string>) => string,
extraMetaTags: ChangesetTag[],
openChangeset: UIEventSource<number>
openChangeset: UIEventSource<{ id: number, opened: number }>
): Promise<void> {
if (
!extraMetaTags.some((tag) => tag.key === "comment") ||
@ -169,18 +169,21 @@ export class ChangesetHandler {
}
console.log("Trying to reuse changeset", openChangeset.data)
if (openChangeset.data) {
const now = new Date()
const changesetIsUsable = openChangeset.data !== undefined &&
(now.getTime() - openChangeset.data.opened < 24 * 60 * 60 * 1000)
if (changesetIsUsable) {
try {
const csId = openChangeset.data
const oldChangesetMeta = await this.GetChangesetMeta(csId)
const oldChangesetMeta = await this.GetChangesetMeta(csId.id)
console.log("Got metadata:", oldChangesetMeta, "isopen", oldChangesetMeta?.open)
if (oldChangesetMeta.open) {
// We can hopefully reuse the changeset
try {
const rewritings = await this.UploadChange(
csId,
generateChangeXML(csId, this._remappings)
csId.id,
generateChangeXML(csId.id, this._remappings)
)
const rewrittenTags = this.RewriteTagsOf(
@ -188,7 +191,7 @@ export class ChangesetHandler {
rewritings,
oldChangesetMeta
)
await this.UpdateTags(csId, rewrittenTags)
await this.UpdateTags(csId.id, rewrittenTags)
return // We are done!
} catch (e) {
this._reportError(e, "While reusing a changeset " + openChangeset.data)
@ -236,9 +239,9 @@ export class ChangesetHandler {
/**
* Given an existing changeset with metadata and extraMetaTags to add, will fuse them to a new set of metatags
* Does not yet send data
* @param extraMetaTags: new changeset tags to add/fuse with this changeset
* @param rewriteIds: the mapping of ids
* @param oldChangesetMeta: the metadata-object of the already existing changeset
* @param extraMetaTags new changeset tags to add/fuse with this changeset
* @param rewriteIds the mapping of ids
* @param oldChangesetMeta the metadata-object of the already existing changeset
*
* @public for testing purposes
*/
@ -250,7 +253,7 @@ export class ChangesetHandler {
id: number
uid: number // User ID
changes_count: number
tags: any
tags: Record<string, string>
}
): ChangesetTag[] {
// Note: extraMetaTags is where all the tags are collected into
@ -300,11 +303,11 @@ export class ChangesetHandler {
/**
* Updates the id in the AllElements store, returns the new ID
* @param node: the XML-element, e.g. <node old_id="-1" new_id="9650458521" new_version="1"/>
* @param node the XML-element, e.g. <node old_id="-1" new_id="9650458521" new_version="1"/>
* @param type
* @private
*/
private static parseIdRewrite(node: any, type: string): [string, string] {
private static parseIdRewrite(node: any, type: "node" | "way" | "relation"): [string, string] {
const oldId = parseInt(node.attributes.old_id.value)
if (node.attributes.new_id === undefined) {
return [type + "/" + oldId, undefined]

View file

@ -246,11 +246,13 @@ export class OsmConnection {
}
public getPreference<T extends string = string>(
key: string,
defaultValue: string = undefined,
prefix: string = "mapcomplete-"
key: string, options?: {
defaultValue?: string,
prefix?: "mapcomplete-" | string,
saveToLocalStorage?: true | boolean
}
): UIEventSource<T | undefined> {
return <UIEventSource<T>>this.preferencesHandler.getPreference(key, defaultValue, prefix)
return <UIEventSource<T>>this.preferencesHandler.getPreference(key, options?.defaultValue, options?.prefix ?? "mapcomplete-")
}
public LogOut() {
@ -731,4 +733,24 @@ export class OsmConnection {
return { api: "offline", gpx: "offline", database: "online" }
}
}
public getCurrentChangesetFor(theme: string) {
return UIEventSource.asObject<{ id: number, opened: number }>(
this.GetPreference(
"current-changeset-" + theme
),
undefined
)
}
/**
* Returns the names of the themes that have an open changeset
*/
public getAllOpenChangesetsPreferences(): Store<string[]> {
const prefix = "current-changeset-"
return this.preferencesHandler.allPreferences.map(dict =>
Object.keys(dict)
.filter(k => k.startsWith(prefix))
.map(k => k.substring(prefix.length)))
}
}

View file

@ -14,6 +14,7 @@ export class OsmPreferences {
private localStorageInited: Set<string> = new Set()
/**
* Contains all the keys as returned by the OSM-preferences.
* This includes combined preferences, such as: pref, pref:0, pref:1
* Used to clean up old preferences
*/
private seenKeys: string[] = []
@ -59,18 +60,18 @@ export class OsmPreferences {
value: string = undefined,
deferPing = false
): UIEventSource<string> {
if (this.preferences[key] !== undefined) {
const cached = this.preferences[key]
if (cached !== undefined) {
if (value !== undefined) {
this.preferences[key].set(value)
cached.set(value)
}
return this.preferences[key]
return cached
}
const pref = (this.preferences[key] = new UIEventSource(value, "preference: " + key))
if (value) {
this.setPreferencesAll(key, value, deferPing)
}
pref.addCallback((v) => {
console.log("Got an update:", key, "--->", v)
this.uploadKvSplit(key, v)
this.setPreferencesAll(key, v, deferPing)
})
@ -82,13 +83,16 @@ export class OsmPreferences {
this.seenKeys = Object.keys(prefs)
const merged = OsmPreferences.mergeDict(prefs)
for (const key in merged) {
this.initPreference(key, prefs[key], true)
this.initPreference(key, merged[key], true)
}
this._allPreferences.ping()
if (this.osmConnection.isLoggedIn.data) {
await this.cleanup()
}
}
public getPreference(key: string, defaultValue: string = undefined, prefix?: string) {
return this.getPreferenceSeedFromlocal(key, defaultValue, { prefix })
public getPreference(key: string, defaultValue: string = undefined, prefix?: string, saveLocally = true) {
return this.getPreferenceSeedFromlocal(key, defaultValue, { prefix, saveToLocalStorage: saveLocally })
}
/**
@ -139,23 +143,52 @@ export class OsmPreferences {
* OsmPreferences.mergeDict({abc: "123", def: "123", "def:0": "456", "def:1":"789"}) // => {abc: "123", def: "123456789"}
*/
private static mergeDict(dict: Record<string, string>): Record<string, string> {
const newDict = {}
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$/)) {
const keyParts: Record<string, Record<number, string>> = {}
const endsWithNumber = /:[0-9]+$/
for (const key of Object.keys(dict)) {
if (key.match(/-combined-[0-9]*$/) || key.match(/-combined-length$/)) {
continue
}
const partKeys = OsmPreferences.keysStartingWith(allKeys, normalKey)
const parts = partKeys.map((k) => dict[k])
newDict[normalKey] = parts.join("")
const nr = key.match(endsWithNumber)
if (nr) {
const i = Number(nr[0].substring(1))
const k = key.substring(0, key.length - nr[0].length)
let subparts = keyParts[k]
if (!subparts) {
subparts = {}
keyParts[k] = subparts
}
subparts[i] = dict[key]
} else {
let subparts = keyParts[key]
if (!subparts) {
subparts = keyParts[key] = {}
}
subparts[""] = dict[key]
}
}
const newDict = {}
for (const key in keyParts) {
const subparts = keyParts[key]
let i = 0
let v = subparts[""] ?? ""
while (subparts[i]) {
v += subparts[i]
i++
}
newDict[key] = v
}
return newDict
}
/**
* Bulk-downloads all preferences, creates a simple record from all
* Bulk-downloads all preferences, creates a simple record from all preferences.
* This should still be merged!
* @private
*/
private async getPreferencesDictDirectly(): Promise<Record<string, string>> {
@ -166,7 +199,7 @@ export class OsmPreferences {
this.auth.xhr(
{
method: "GET",
path: "/api/0.6/user/preferences",
path: "/api/0.6/user/preferences"
},
(error, value: XMLDocument) => {
if (error) {
@ -187,6 +220,9 @@ export class OsmPreferences {
})
}
private static readonly endsWithNumber = /:[0-9]+$/
/**
* Returns all keys matching `k:[number]`
* Split separately for test
@ -198,7 +234,24 @@ 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) => {
if (k === key) {
return true
}
if (!k.startsWith(key)) {
return false
}
const match = k.match(OsmPreferences.endsWithNumber)
if (!match) {
return false
}
const matchLength = match[0].length
if (key.length + matchLength !== k.length) {
return false
}
return true
})
keys.sort()
return keys
}
@ -247,7 +300,7 @@ export class OsmPreferences {
{
method: "DELETE",
path: "/api/0.6/user/preferences/" + encodeURIComponent(k),
headers: { "Content-Type": "text/plain" },
headers: { "Content-Type": "text/plain" }
},
(error) => {
if (error) {
@ -255,7 +308,6 @@ export class OsmPreferences {
reject(error)
return
}
console.debug("Preference ", k, "removed!")
resolve()
}
)
@ -289,33 +341,50 @@ export class OsmPreferences {
throw "Preference too long, at most 255 characters are supported"
}
return new Promise<void>((resolve, reject) => {
this.auth.xhr(
{
method: "PUT",
path: "/api/0.6/user/preferences/" + encodeURIComponent(k),
headers: { "Content-Type": "text/plain" },
content: v,
},
(error) => {
if (error) {
console.warn(`Could not set preference "${k}"'`, error)
reject(error)
return
}
resolve()
}
)
})
try {
return this.osmConnection.interact("user/preferences/" + encodeURIComponent(k),
"PUT", { "Content-Type": "text/plain" }, v)
} catch (e) {
console.error("Could not upload preference due to", e)
}
}
async removeAllWithPrefix(prefix: string) {
const keys = this.seenKeys
let somethingChanged = false
for (const key of keys) {
if (!key.startsWith(prefix)) {
continue
}
console.log("Cleaning up preference", key)
await this.deleteKeyDirectly(key)
somethingChanged = true
}
return somethingChanged
}
private async cleanup() {
const prefixesToClean = ["mapcomplete-mapcomplete-", "mapcomplete-places-history", "unofficial-theme-", "mapcompleteplaces", "mapcompletethemes"] // TODO enable this one once the new system is in prod "mapcomplete-current-open-changeset-"]
let somethingChanged = false
for (const prefix of prefixesToClean) {
const hasChange = await this.removeAllWithPrefix(prefix) // Don't inline - short-circuiting
somethingChanged ||= hasChange
}
if (somethingChanged) {
this._allPreferences.ping()
}
const themes = this.osmConnection.getAllOpenChangesetsPreferences()
const now = new Date()
for (const theme of themes.data) {
const cs = this.osmConnection.getCurrentChangesetFor(theme)
if (now.getTime() - cs.data.opened > 24 * 60 * 60 * 1000) {
console.log("Clearing 'open changeset' for theme", theme, "; definitively expired by now")
cs.set(undefined)
}
}
}
}

View file

@ -8,9 +8,10 @@ import { AndroidPolyfill } from "../Web/AndroidPolyfill"
export type GeolocationPermissionState = "prompt" | "requested" | "granted" | "denied"
export interface GeoLocationPointProperties extends GeolocationCoordinates {
id: "gps"
id: "gps" | string
"user:location": "yes"
date: string
date: string,
alpha?: string
}
/**

View file

@ -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, [])
}

View file

@ -38,11 +38,11 @@ export class IdbLocalStorage {
return src
}
public static SetDirectly(key: string, value: any): Promise<void> {
public static SetDirectly<T>(key: string, value: T): Promise<void> {
return idb.set(key, value)
}
static GetDirectly(key: string): Promise<any> {
static GetDirectly<T>(key: string): Promise<T> {
return idb.get(key)
}

View file

@ -1,7 +1,7 @@
import ThemeConfig from "../ThemeConfig/ThemeConfig"
import { Store, UIEventSource } from "../../Logic/UIEventSource"
import { Map as MlMap } from "maplibre-gl"
import { GeoLocationState } from "../../Logic/State/GeoLocationState"
import { GeoLocationPointProperties, GeoLocationState } from "../../Logic/State/GeoLocationState"
import InitialMapPositioning from "../../Logic/Actors/InitialMapPositioning"
import { MapLibreAdaptor } from "../../UI/Map/MapLibreAdaptor"
import { ExportableMap, MapProperties } from "../MapProperties"
@ -43,7 +43,7 @@ export class UserMapFeatureswitchState extends WithUserRelatedState {
readonly geolocationState: GeoLocationState
readonly geolocation: GeoLocationHandler
readonly geolocationControl: GeolocationControlState
readonly historicalUserLocations: WritableFeatureSource<Feature<Point>>
readonly historicalUserLocations: WritableFeatureSource<Feature<Point, GeoLocationPointProperties>>
readonly availableLayers: { store: Store<RasterLayerPolygon[]> }
readonly currentView: FeatureSource<Feature<Polygon>>