forked from MapComplete/MapComplete
		
	Chore: use strict on two classes
This commit is contained in:
		
							parent
							
								
									fe0929ef6f
								
							
						
					
					
						commit
						de782d7356
					
				
					 2 changed files with 41 additions and 15 deletions
				
			
		|  | @ -8,10 +8,11 @@ import { FeatureSource, WritableFeatureSource } from "../FeatureSource/FeatureSo | ||||||
| import { LocalStorageSource } from "../Web/LocalStorageSource" | import { LocalStorageSource } from "../Web/LocalStorageSource" | ||||||
| import { GeoOperations } from "../GeoOperations" | import { GeoOperations } from "../GeoOperations" | ||||||
| import { OsmTags } from "../../Models/OsmFeature" | import { OsmTags } from "../../Models/OsmFeature" | ||||||
| import StaticFeatureSource from "../FeatureSource/Sources/StaticFeatureSource" | import StaticFeatureSource, { WritableStaticFeatureSource } from "../FeatureSource/Sources/StaticFeatureSource" | ||||||
| import { MapProperties } from "../../Models/MapProperties" | import { MapProperties } from "../../Models/MapProperties" | ||||||
| import { Orientation } from "../../Sensors/Orientation" | import { Orientation } from "../../Sensors/Orientation" | ||||||
| 
 | 
 | ||||||
|  | "use strict" | ||||||
| /** | /** | ||||||
|  * The geolocation-handler takes a map-location and a geolocation state. |  * 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 |  * 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<Date | undefined> = new UIEventSource< |     public readonly mapHasMoved: UIEventSource<Date | undefined> = new UIEventSource< | ||||||
|         Date | undefined |         Date | undefined | ||||||
|     >(undefined) |     >(undefined) | ||||||
|     private readonly selectedElement: UIEventSource<any> |     private readonly selectedElement: UIEventSource<Feature> | ||||||
|     private readonly mapProperties?: MapProperties |     private readonly mapProperties?: MapProperties | ||||||
|     private readonly gpsLocationHistoryRetentionTime?: UIEventSource<number> |     private readonly gpsLocationHistoryRetentionTime?: UIEventSource<number> | ||||||
| 
 | 
 | ||||||
|     constructor( |     constructor( | ||||||
|         geolocationState: GeoLocationState, |         geolocationState: GeoLocationState, | ||||||
|         selectedElement: UIEventSource<any>, |         selectedElement: UIEventSource<Feature>, | ||||||
|         mapProperties?: MapProperties, |         mapProperties?: MapProperties, | ||||||
|         gpsLocationHistoryRetentionTime?: UIEventSource<number> |         gpsLocationHistoryRetentionTime?: UIEventSource<number> | ||||||
|     ) { |     ) { | ||||||
|  | @ -59,13 +60,12 @@ export default class GeoLocationHandler { | ||||||
|         this.mapProperties = mapProperties |         this.mapProperties = mapProperties | ||||||
|         this.gpsLocationHistoryRetentionTime = gpsLocationHistoryRetentionTime |         this.gpsLocationHistoryRetentionTime = gpsLocationHistoryRetentionTime | ||||||
|         // Did an interaction move the map?
 |         // Did an interaction move the map?
 | ||||||
|         let self = this |         const initTime = new Date() | ||||||
|         let initTime = new Date() |         mapLocation.addCallbackD(() => { | ||||||
|         mapLocation.addCallbackD((_) => { |  | ||||||
|             if (new Date().getTime() - initTime.getTime() < 250) { |             if (new Date().getTime() - initTime.getTime() < 250) { | ||||||
|                 return |                 return | ||||||
|             } |             } | ||||||
|             self.mapHasMoved.setData(new Date()) |             this.mapHasMoved.setData(new Date()) | ||||||
|             return true // Unsubscribe
 |             return true // Unsubscribe
 | ||||||
|         }) |         }) | ||||||
| 
 | 
 | ||||||
|  | @ -76,12 +76,12 @@ export default class GeoLocationHandler { | ||||||
|             this.mapHasMoved.setData(new Date()) |             this.mapHasMoved.setData(new Date()) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         this.geolocationState.currentGPSLocation.addCallbackAndRunD((_) => { |         this.geolocationState.currentGPSLocation.addCallbackAndRunD(() => { | ||||||
|             const timeSinceLastRequest = |             const timeSinceLastRequest = | ||||||
|                 (new Date().getTime() - geolocationState.requestMoment.data?.getTime() ?? 0) / 1000 |                 (new Date().getTime() - geolocationState.requestMoment.data?.getTime() ?? 0) / 1000 | ||||||
|             if (!this.mapHasMoved.data) { |             if (!this.mapHasMoved.data) { | ||||||
|                 // The map hasn't moved yet; we received our first coordinates, so let's move there!
 |                 // The map hasn't moved yet; we received our first coordinates, so let's move there!
 | ||||||
|                 self.MoveMapToCurrentLocation() |                 this.MoveMapToCurrentLocation() | ||||||
|             } |             } | ||||||
|             if ( |             if ( | ||||||
|                 timeSinceLastRequest < Constants.zoomToLocationTimeout && |                 timeSinceLastRequest < Constants.zoomToLocationTimeout && | ||||||
|  | @ -90,12 +90,12 @@ export default class GeoLocationHandler { | ||||||
|                         geolocationState.requestMoment.data?.getTime()) |                         geolocationState.requestMoment.data?.getTime()) | ||||||
|             ) { |             ) { | ||||||
|                 // still within request time and the map hasn't moved since requesting to jump to the current location
 |                 // 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) { |             if (!this.geolocationState.allowMoving.data) { | ||||||
|                 // Jup, the map is locked to the bound location: move automatically
 |                 // Jup, the map is locked to the bound location: move automatically
 | ||||||
|                 self.MoveMapToCurrentLocation(0) |                 this.MoveMapToCurrentLocation(0) | ||||||
|                 return |                 return | ||||||
|             } |             } | ||||||
|         }) |         }) | ||||||
|  | @ -183,7 +183,7 @@ export default class GeoLocationHandler { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private initUserLocationTrail() { |     private initUserLocationTrail() { | ||||||
|         const features = LocalStorageSource.getParsed<Feature[]>("gps_location_history", []) |         const features = LocalStorageSource.getParsed<Feature<Point>[]>("gps_location_history", []) | ||||||
|         const now = new Date().getTime() |         const now = new Date().getTime() | ||||||
|         features.data = features.data.filter((ff) => { |         features.data = features.data.filter((ff) => { | ||||||
|             if (ff.properties === undefined) { |             if (ff.properties === undefined) { | ||||||
|  | @ -230,7 +230,7 @@ export default class GeoLocationHandler { | ||||||
|             features.ping() |             features.ping() | ||||||
|         }) |         }) | ||||||
| 
 | 
 | ||||||
|         this.historicalUserLocations = <any>new StaticFeatureSource(features) |         this.historicalUserLocations = new WritableStaticFeatureSource<Feature<Point>>(features) | ||||||
| 
 | 
 | ||||||
|         const asLine = features.map((allPoints) => { |         const asLine = features.map((allPoints) => { | ||||||
|             if (allPoints === undefined || allPoints.length < 2) { |             if (allPoints === undefined || allPoints.length < 2) { | ||||||
|  |  | ||||||
|  | @ -1,7 +1,8 @@ | ||||||
| import { FeatureSource } from "../FeatureSource" | import { FeatureSource, WritableFeatureSource } from "../FeatureSource" | ||||||
| import { ImmutableStore, Store } from "../../UIEventSource" | import { ImmutableStore, Store, UIEventSource } from "../../UIEventSource" | ||||||
| import { Feature } from "geojson" | import { Feature } from "geojson" | ||||||
| 
 | 
 | ||||||
|  | "use strict" | ||||||
| /** | /** | ||||||
|  * A simple, read only feature store. |  * A simple, read only feature store. | ||||||
|  */ |  */ | ||||||
|  | @ -30,3 +31,28 @@ export default class StaticFeatureSource<T extends Feature = Feature> implements | ||||||
|         return new StaticFeatureSource(geojson) |         return new StaticFeatureSource(geojson) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export class WritableStaticFeatureSource<T extends Feature = Feature> implements WritableFeatureSource<T> { | ||||||
|  |     public readonly features: UIEventSource<T[]> = undefined | ||||||
|  | 
 | ||||||
|  |     constructor(features: UIEventSource<T[]> | T[] | { features: T[] } | { features: Store<T[]> }) { | ||||||
|  |         if (features === undefined) { | ||||||
|  |             throw "Static feature source received undefined as source" | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         let feats: T[] | UIEventSource<T[]> | ||||||
|  | 
 | ||||||
|  |         if (features["features"]) { | ||||||
|  |             feats = features["features"] | ||||||
|  |         } else { | ||||||
|  |             feats = <T[] | UIEventSource<T[]>>features | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (Array.isArray(feats)) { | ||||||
|  |             this.features = new UIEventSource<T[]>(feats) | ||||||
|  |         } else { | ||||||
|  |             this.features = feats | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue