diff --git a/Logic/SimpleMetaTagger.ts b/Logic/SimpleMetaTagger.ts index 7b38d85bc..faddf2906 100644 --- a/Logic/SimpleMetaTagger.ts +++ b/Logic/SimpleMetaTagger.ts @@ -80,20 +80,25 @@ export class ReferencingWaysMetaTagger extends SimpleMetaTagger { if (!id.startsWith("node/")) { return false } - console.trace("Downloading referencing ways for", feature.properties.id) - OsmObject.DownloadReferencingWays(id).then((referencingWays) => { - const currentTagsSource = state.allElements?.getEventSourceById(id) ?? [] - const wayIds = referencingWays.map((w) => "way/" + w.id) - wayIds.sort() - const wayIdsStr = wayIds.join(";") - if ( - wayIdsStr !== "" && - currentTagsSource.data["_referencing_ways"] !== wayIdsStr - ) { - currentTagsSource.data["_referencing_ways"] = wayIdsStr - currentTagsSource.ping() + + const currentTagsSource = state.allElements?.getEventSourceById(id) + if (currentTagsSource === undefined) { + return + } + Utils.AddLazyPropertyAsync( + currentTagsSource.data, + "_referencing_ways", + async () => { + const referencingWays = await OsmObject.DownloadReferencingWays(id) + const wayIds = referencingWays.map((w) => "way/" + w.id) + wayIds.sort() + const wayIdsStr = wayIds.join(";") + if (wayIdsStr !== "" && currentTagsSource.data[""] !== wayIdsStr) { + currentTagsSource.data["_referencing_ways"] = wayIdsStr + currentTagsSource.ping() + } } - }) + ) return true } @@ -282,16 +287,9 @@ export default class SimpleMetaTaggers { }, }) - Object.defineProperty(feature.properties, "_surface:ha", { - enumerable: false, - configurable: true, - get: () => { - const sqMeters = GeoOperations.surfaceAreaInSqMeters(feature) - const sqMetersHa = "" + Math.floor(sqMeters / 1000) / 10 - delete feature.properties["_surface:ha"] - feature.properties["_surface:ha"] = sqMetersHa - return sqMetersHa - }, + Utils.AddLazyProperty(feature.properties, "_surface:ha", () => { + const sqMeters = GeoOperations.surfaceAreaInSqMeters(feature) + return "" + Math.floor(sqMeters / 1000) / 10 }) return true @@ -443,8 +441,6 @@ export default class SimpleMetaTaggers { } }, }) - - const tagsSource = state.allElements.getEventSourceById(feature.properties.id) } ) private static directionSimplified = new SimpleMetaTagger( diff --git a/Utils.ts b/Utils.ts index 8bc0e011c..7e8fdff24 100644 --- a/Utils.ts +++ b/Utils.ts @@ -300,6 +300,49 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be return str.substr(0, l - 3) + "..." } + /** + * Adds a property to the given object, but the value will _only_ be calculated when it is actually requested + * @param object + * @param name + * @param init + * @constructor + */ + public static AddLazyProperty(object: any, name: string, init: () => any) { + Object.defineProperty(object, name, { + enumerable: false, + configurable: true, + get: () => { + delete object[name] + object[name] = init() + return object[name] + }, + }) + } + + /** + * Adds a property to the given object, but the value will _only_ be calculated when it is actually requested + */ + public static AddLazyPropertyAsync( + object: any, + name: string, + init: () => Promise, + whenDone?: () => void + ) { + Object.defineProperty(object, name, { + enumerable: false, + configurable: true, + get: () => { + init().then((r) => { + delete object[name] + object[name] = r + if (whenDone) { + whenDone() + } + }) + }, + }) + } + public static FixedLength(str: string, l: number) { str = Utils.EllipsesAfter(str, l) while (str.length < l) { @@ -1281,13 +1324,6 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be return d } - private static colorDiff( - c0: { r: number; g: number; b: number }, - c1: { r: number; g: number; b: number } - ) { - return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b) - } - static toIdRecord(ts: T[]): Record { const result: Record = {} for (const t of ts) { @@ -1317,4 +1353,11 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be // If the element has a parent, repeat the process for the parent element return Utils.findParentWithScrolling(element.parentElement) } + + private static colorDiff( + c0: { r: number; g: number; b: number }, + c1: { r: number; g: number; b: number } + ) { + return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b) + } }