From de782d73561c6ba17b0368b541f33f985fa760da Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Fri, 13 Dec 2024 14:13:28 +0100 Subject: [PATCH] Chore: use strict on two classes --- src/Logic/Actors/GeoLocationHandler.ts | 26 ++++++++-------- .../Sources/StaticFeatureSource.ts | 30 +++++++++++++++++-- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/Logic/Actors/GeoLocationHandler.ts b/src/Logic/Actors/GeoLocationHandler.ts index 1909daa52..c97a75c8e 100644 --- a/src/Logic/Actors/GeoLocationHandler.ts +++ b/src/Logic/Actors/GeoLocationHandler.ts @@ -8,10 +8,11 @@ import { FeatureSource, WritableFeatureSource } from "../FeatureSource/FeatureSo import { LocalStorageSource } from "../Web/LocalStorageSource" import { GeoOperations } from "../GeoOperations" import { OsmTags } from "../../Models/OsmFeature" -import StaticFeatureSource from "../FeatureSource/Sources/StaticFeatureSource" +import StaticFeatureSource, { WritableStaticFeatureSource } from "../FeatureSource/Sources/StaticFeatureSource" import { MapProperties } from "../../Models/MapProperties" import { Orientation } from "../../Sensors/Orientation" +"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 @@ -43,13 +44,13 @@ export default class GeoLocationHandler { public readonly mapHasMoved: UIEventSource = new UIEventSource< Date | undefined >(undefined) - private readonly selectedElement: UIEventSource + private readonly selectedElement: UIEventSource private readonly mapProperties?: MapProperties private readonly gpsLocationHistoryRetentionTime?: UIEventSource constructor( geolocationState: GeoLocationState, - selectedElement: UIEventSource, + selectedElement: UIEventSource, mapProperties?: MapProperties, gpsLocationHistoryRetentionTime?: UIEventSource ) { @@ -59,13 +60,12 @@ export default class GeoLocationHandler { this.mapProperties = mapProperties this.gpsLocationHistoryRetentionTime = gpsLocationHistoryRetentionTime // Did an interaction move the map? - let self = this - let initTime = new Date() - mapLocation.addCallbackD((_) => { + const initTime = new Date() + mapLocation.addCallbackD(() => { if (new Date().getTime() - initTime.getTime() < 250) { return } - self.mapHasMoved.setData(new Date()) + this.mapHasMoved.setData(new Date()) return true // Unsubscribe }) @@ -76,12 +76,12 @@ export default class GeoLocationHandler { this.mapHasMoved.setData(new Date()) } - this.geolocationState.currentGPSLocation.addCallbackAndRunD((_) => { + this.geolocationState.currentGPSLocation.addCallbackAndRunD(() => { const timeSinceLastRequest = (new Date().getTime() - geolocationState.requestMoment.data?.getTime() ?? 0) / 1000 if (!this.mapHasMoved.data) { // The map hasn't moved yet; we received our first coordinates, so let's move there! - self.MoveMapToCurrentLocation() + this.MoveMapToCurrentLocation() } if ( timeSinceLastRequest < Constants.zoomToLocationTimeout && @@ -90,12 +90,12 @@ export default class GeoLocationHandler { geolocationState.requestMoment.data?.getTime()) ) { // still within request time and the map hasn't moved since requesting to jump to the current location - self.MoveMapToCurrentLocation() + this.MoveMapToCurrentLocation() } if (!this.geolocationState.allowMoving.data) { // Jup, the map is locked to the bound location: move automatically - self.MoveMapToCurrentLocation(0) + this.MoveMapToCurrentLocation(0) return } }) @@ -183,7 +183,7 @@ export default class GeoLocationHandler { } private initUserLocationTrail() { - const features = LocalStorageSource.getParsed("gps_location_history", []) + const features = LocalStorageSource.getParsed[]>("gps_location_history", []) const now = new Date().getTime() features.data = features.data.filter((ff) => { if (ff.properties === undefined) { @@ -230,7 +230,7 @@ export default class GeoLocationHandler { features.ping() }) - this.historicalUserLocations = new StaticFeatureSource(features) + this.historicalUserLocations = new WritableStaticFeatureSource>(features) const asLine = features.map((allPoints) => { if (allPoints === undefined || allPoints.length < 2) { diff --git a/src/Logic/FeatureSource/Sources/StaticFeatureSource.ts b/src/Logic/FeatureSource/Sources/StaticFeatureSource.ts index df1fe8f49..dcfeada40 100644 --- a/src/Logic/FeatureSource/Sources/StaticFeatureSource.ts +++ b/src/Logic/FeatureSource/Sources/StaticFeatureSource.ts @@ -1,7 +1,8 @@ -import { FeatureSource } from "../FeatureSource" -import { ImmutableStore, Store } from "../../UIEventSource" +import { FeatureSource, WritableFeatureSource } from "../FeatureSource" +import { ImmutableStore, Store, UIEventSource } from "../../UIEventSource" import { Feature } from "geojson" +"use strict" /** * A simple, read only feature store. */ @@ -30,3 +31,28 @@ export default class StaticFeatureSource implements return new StaticFeatureSource(geojson) } } + +export class WritableStaticFeatureSource implements WritableFeatureSource { + public readonly features: UIEventSource = undefined + + constructor(features: UIEventSource | T[] | { features: T[] } | { features: Store }) { + if (features === undefined) { + throw "Static feature source received undefined as source" + } + + let feats: T[] | UIEventSource + + if (features["features"]) { + feats = features["features"] + } else { + feats = >features + } + + if (Array.isArray(feats)) { + this.features = new UIEventSource(feats) + } else { + this.features = feats + } + + } +}