MapComplete/src/Logic/Actors/PendingChangesUploader.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

64 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Changes } from "../Osm/Changes"
import Constants from "../../Models/Constants"
import { UIEventSource } from "../UIEventSource"
import { Utils } from "../../Utils"
2023-03-28 05:13:48 +02:00
import { Feature } from "geojson"
export default class PendingChangesUploader {
2023-03-28 05:13:48 +02:00
constructor(changes: Changes, selectedFeature: UIEventSource<Feature>) {
2023-10-16 14:27:05 +02:00
changes.pendingChanges
.stabilized(Constants.updateTimeoutSec * 1000)
.addCallback(() => changes.flushChanges("Flushing changes due to timeout"))
2023-10-16 13:38:11 +02:00
selectedFeature.stabilized(1000).addCallback((feature) => {
if (feature === undefined) {
// The popup got closed - we flush
changes.flushChanges("Flushing changes due to popup closed")
}
})
2021-11-07 16:34:51 +01:00
if (Utils.runningFromConsole) {
return
}
2021-11-07 16:34:51 +01:00
document.addEventListener("mouseout", (e) => {
// @ts-ignore
if (!e.toElement && !e.relatedTarget) {
changes.flushChanges("Flushing changes due to focus lost")
}
})
document.onfocus = () => {
changes.flushChanges("OnFocus")
}
document.onblur = () => {
changes.flushChanges("OnFocus")
}
try {
document.addEventListener(
"visibilitychange",
() => {
changes.flushChanges("Visibility change")
},
false
2022-09-08 21:40:48 +02:00
)
} catch (e) {
console.warn("Could not register visibility change listener", e)
}
function onunload(e) {
if (changes.pendingChanges.data.length == 0) {
return
}
changes.flushChanges("onbeforeunload - probably closing or something similar")
e.preventDefault()
return "Saving your last changes..."
}
window.onbeforeunload = onunload
// https://stackoverflow.com/questions/3239834/window-onbeforeunload-not-working-on-the-ipad#4824156
window.addEventListener("pagehide", onunload)
}
}