forked from MapComplete/MapComplete
		
	Small performance issues, improvement of #579
This commit is contained in:
		
							parent
							
								
									4fd30d1a62
								
							
						
					
					
						commit
						ee962f4ba3
					
				
					 2 changed files with 17 additions and 9 deletions
				
			
		|  | @ -281,10 +281,12 @@ export class UIEventSource<T> { | |||
|      * @param f: The transforming function | ||||
|      * @param extraSources: also trigger the update if one of these sources change | ||||
|      * @param g: a 'backfunction to let the sync run in two directions. (data of the new UIEVEntSource, currentData) => newData | ||||
|      * @param allowUnregister: if set, the update will be halted if no listeners are registered | ||||
|      */ | ||||
|     public map<J>(f: ((t: T) => J), | ||||
|                   extraSources: UIEventSource<any>[] = [], | ||||
|                   g: ((j: J, t: T) => T) = undefined): UIEventSource<J> { | ||||
|                   g: ((j: J, t: T) => T) = undefined, | ||||
|                   allowUnregister = false): UIEventSource<J> { | ||||
|         const self = this; | ||||
| 
 | ||||
|         const stack = new Error().stack.split("\n"); | ||||
|  | @ -297,6 +299,7 @@ export class UIEventSource<T> { | |||
| 
 | ||||
|         const update = function () { | ||||
|             newSource.setData(f(self.data)); | ||||
|             return allowUnregister && newSource._callbacks.length === 0 | ||||
|         } | ||||
| 
 | ||||
|         this.addCallback(update); | ||||
|  |  | |||
|  | @ -146,6 +146,7 @@ export default class ShowDataLayer { | |||
|             onEachFeature: (feature, leafletLayer) => self.postProcessFeature(feature, leafletLayer) | ||||
|         }); | ||||
|          | ||||
|         const selfLayer = this.geoLayer; | ||||
|         const allFeats = this._features.features.data; | ||||
|         for (const feat of allFeats) { | ||||
|             if (feat === undefined) { | ||||
|  | @ -153,12 +154,11 @@ export default class ShowDataLayer { | |||
|             } | ||||
|             try { | ||||
|                 if (feat.geometry.type === "LineString") { | ||||
|                     const self = this; | ||||
|                     const coords = L.GeoJSON.coordsToLatLngs(feat.geometry.coordinates) | ||||
|                     const tagsSource = this.allElements?.addOrGetElement(feat) ?? new UIEventSource<any>(feat.properties); | ||||
|                     let offsettedLine; | ||||
|                     tagsSource | ||||
|                         .map(tags => this._layerToShow.lineRendering[feat.lineRenderingIndex].GenerateLeafletStyle(tags)) | ||||
|                         .map(tags => this._layerToShow.lineRendering[feat.lineRenderingIndex].GenerateLeafletStyle(tags), [], undefined, true) | ||||
|                         .withEqualityStabilized((a, b) => { | ||||
|                             if (a === b) { | ||||
|                                 return true | ||||
|  | @ -176,6 +176,9 @@ export default class ShowDataLayer { | |||
|                             offsettedLine = L.polyline(coords, lineStyle); | ||||
|                             this.postProcessFeature(feat, offsettedLine) | ||||
|                             offsettedLine.addTo(this.geoLayer) | ||||
|                              | ||||
|                             // If 'self.geoLayer' is not the same as the layer the feature is added to, we can safely remove this callback
 | ||||
|                             return self.geoLayer !== selfLayer | ||||
|                         }) | ||||
|                 } else { | ||||
|                     this.geoLayer.addData(feat); | ||||
|  | @ -186,6 +189,7 @@ export default class ShowDataLayer { | |||
|         } | ||||
| 
 | ||||
|         if (options.zoomToFeatures ?? false) { | ||||
|             if(this.geoLayer.getLayers().length > 0){ | ||||
|                 try { | ||||
|                     const bounds = this.geoLayer.getBounds() | ||||
|                     mp.fitBounds(bounds, {animate: false}) | ||||
|  | @ -193,6 +197,7 @@ export default class ShowDataLayer { | |||
|                     console.debug("Invalid bounds", e) | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         if (options.doShowLayer?.data ?? true) { | ||||
|             mp.addLayer(this.geoLayer) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue