| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  |  * Every previously added point is remembered, but new points are added. | 
					
						
							|  |  |  |  * Data coming from upstream will always overwrite a previous value | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import FeatureSource, { Tiled } from "../FeatureSource" | 
					
						
							|  |  |  | import { Store, UIEventSource } from "../../UIEventSource" | 
					
						
							|  |  |  | import { BBox } from "../../BBox" | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | export default class RememberingSource implements FeatureSource, Tiled { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public readonly features: Store<{ feature: any; freshness: Date }[]> | 
					
						
							|  |  |  |     public readonly name | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     public readonly tileIndex: number | 
					
						
							|  |  |  |     public readonly bbox: BBox | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |     constructor(source: FeatureSource & Tiled) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const self = this | 
					
						
							|  |  |  |         this.name = "RememberingSource of " + source.name | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         this.tileIndex = source.tileIndex | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.bbox = source.bbox | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const empty = [] | 
					
						
							|  |  |  |         const featureSource = new UIEventSource<{ feature: any; freshness: Date }[]>(empty) | 
					
						
							| 
									
										
										
										
											2022-06-06 19:37:22 +02:00
										 |  |  |         this.features = featureSource | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         source.features.addCallbackAndRunD((features) => { | 
					
						
							|  |  |  |             const oldFeatures = self.features?.data ?? empty | 
					
						
							| 
									
										
										
										
											2021-01-04 22:59:11 +01:00
										 |  |  |             // Then new ids
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const ids = new Set<string>( | 
					
						
							|  |  |  |                 features.map((f) => f.feature.properties.id + f.feature.geometry.type) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-01-04 22:59:11 +01:00
										 |  |  |             // the old data
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const oldData = oldFeatures.filter( | 
					
						
							|  |  |  |                 (old) => !ids.has(old.feature.properties.id + old.feature.geometry.type) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-06-06 19:37:22 +02:00
										 |  |  |             featureSource.setData([...features, ...oldData]) | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |