| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  | import FeatureSource from "./FeatureSource"; | 
					
						
							|  |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class FeatureSourceMerger implements FeatureSource { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public features: UIEventSource<{ feature: any; freshness: Date }[]> = new UIEventSource<{feature: any; freshness: Date}[]>([]); | 
					
						
							|  |  |  |     private readonly _sources: FeatureSource[]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(sources: FeatureSource[]) { | 
					
						
							|  |  |  |         this._sources = sources; | 
					
						
							|  |  |  |         const self = this; | 
					
						
							| 
									
										
										
										
											2021-01-15 00:29:07 +01:00
										 |  |  |         for (let i = 0; i < sources.length; i++){ | 
					
						
							|  |  |  |             let source = sources[i]; | 
					
						
							|  |  |  |             source.features.addCallback(() => { | 
					
						
							|  |  |  |                 self.Update(); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-01-15 00:29:07 +01:00
										 |  |  |         this.Update(); | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private Update() { | 
					
						
							|  |  |  |         let all = {}; // Mapping 'id' -> {feature, freshness}
 | 
					
						
							|  |  |  |         for (const source of this._sources) { | 
					
						
							| 
									
										
										
										
											2021-01-04 22:59:11 +01:00
										 |  |  |             if(source?.features?.data === undefined){ | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |             for (const f of source.features.data) { | 
					
						
							| 
									
										
										
										
											2021-01-04 04:06:21 +01:00
										 |  |  |                 const id = f.feature.properties.id+f.feature.geometry.type+f.feature._matching_layer_id; | 
					
						
							| 
									
										
										
										
											2021-01-03 03:09:52 +01:00
										 |  |  |                 const oldV = all[id]; | 
					
						
							|  |  |  |                 if(oldV === undefined){ | 
					
						
							|  |  |  |                     all[id] = f; | 
					
						
							|  |  |  |                 }else{ | 
					
						
							|  |  |  |                     if(oldV.freshness < f.freshness){ | 
					
						
							|  |  |  |                         all[id]=f; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const newList = []; | 
					
						
							|  |  |  |         for (const id in all) { | 
					
						
							|  |  |  |             newList.push(all[id]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this.features.setData(newList); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |