From 64f3f9720cf0b3937abd359c375e5c4f74639115 Mon Sep 17 00:00:00 2001 From: Wykks Date: Wed, 18 Oct 2017 20:26:52 +0200 Subject: [PATCH] fix(sources): add a guard to ensure that the source is added before onChanges --- src/app/lib/source/canvas-source.component.ts | 20 ++++++++++------ .../geojson/geojson-source.component.ts | 13 ++++++---- src/app/lib/source/image-source.component.ts | 6 +++++ src/app/lib/source/raster-source.component.ts | 24 ++++++++++++------- src/app/lib/source/vector-source.component.ts | 6 +++++ src/app/lib/source/video-source.component.ts | 6 +++++ 6 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/app/lib/source/canvas-source.component.ts b/src/app/lib/source/canvas-source.component.ts index 687d01a03..e89d8c26d 100644 --- a/src/app/lib/source/canvas-source.component.ts +++ b/src/app/lib/source/canvas-source.component.ts @@ -17,24 +17,30 @@ export class CanvasSourceComponent implements OnInit, OnDestroy, OnChanges, Canv @Input() contextType: '2d' | 'webgl' | 'experimental-webgl' | 'webgl2'; @Input() animate?: boolean; + private sourceAdded = false; + constructor( private MapService: MapService ) { } ngOnInit() { - const source = { - type: 'canvas', - coordinates: this.coordinates, - canvas: this.canvas, - animate: this.animate, - contextType: this.contextType - }; this.MapService.mapLoaded$.subscribe(() => { + const source = { + type: 'canvas', + coordinates: this.coordinates, + canvas: this.canvas, + animate: this.animate, + contextType: this.contextType + }; this.MapService.addSource(this.id, source); + this.sourceAdded = true; }); } ngOnChanges(changes: SimpleChanges) { + if (!this.sourceAdded) { + return; + } if ( changes.coordinates && !changes.coordinates.isFirstChange() || changes.canvas && !changes.canvas.isFirstChange() || diff --git a/src/app/lib/source/geojson/geojson-source.component.ts b/src/app/lib/source/geojson/geojson-source.component.ts index b6e3ab537..7d38af283 100644 --- a/src/app/lib/source/geojson/geojson-source.component.ts +++ b/src/app/lib/source/geojson/geojson-source.component.ts @@ -25,17 +25,18 @@ export class GeoJSONSourceComponent implements OnInit, OnDestroy, OnChanges, Geo private updateFeatureData = new Subject(); private sub: Subscription; + private sourceAdded = false; constructor( private MapService: MapService ) { } ngOnInit() { - this.data = this.data ? this.data : { - type: 'FeatureCollection', - features: [] - }; this.MapService.mapLoaded$.subscribe(() => { + this.data = this.data ? this.data : { + type: 'FeatureCollection', + features: [] + }; this.MapService.addSource(this.id, { type: 'geojson', data: this.data, @@ -50,10 +51,14 @@ export class GeoJSONSourceComponent implements OnInit, OnDestroy, OnChanges, Geo const source = this.MapService.getSource(this.id); source.setData(this.data!); }); + this.sourceAdded = true; }); } ngOnChanges(changes: SimpleChanges) { + if (!this.sourceAdded) { + return; + } if ( changes.maxzoom && !changes.maxzoom.isFirstChange() || changes.buffer && !changes.buffer.isFirstChange() || diff --git a/src/app/lib/source/image-source.component.ts b/src/app/lib/source/image-source.component.ts index e4c64a1a7..f3463e0e1 100644 --- a/src/app/lib/source/image-source.component.ts +++ b/src/app/lib/source/image-source.component.ts @@ -15,6 +15,8 @@ export class ImageSourceComponent implements OnInit, OnDestroy, OnChanges, Image @Input() url: string; @Input() coordinates: number[][]; + private sourceAdded = false; + constructor( private MapService: MapService ) { } @@ -26,10 +28,14 @@ export class ImageSourceComponent implements OnInit, OnDestroy, OnChanges, Image url: this.url, coordinates: this.coordinates }); + this.sourceAdded = true; }); } ngOnChanges(changes: SimpleChanges) { + if (!this.sourceAdded) { + return; + } if ( changes.url && !changes.url.isFirstChange() || changes.coordinates && !changes.coordinates.isFirstChange() diff --git a/src/app/lib/source/raster-source.component.ts b/src/app/lib/source/raster-source.component.ts index 8f628f91c..141aa2702 100644 --- a/src/app/lib/source/raster-source.component.ts +++ b/src/app/lib/source/raster-source.component.ts @@ -21,26 +21,32 @@ export class RasterSourceComponent implements OnInit, OnDestroy, OnChanges, Rast type: 'raster' = 'raster'; // Just to make ts happy + private sourceAdded = false; + constructor( private MapService: MapService ) { } ngOnInit() { - const source = { - type: this.type, - url: this.url, - tiles: this.tiles, - bounds: this.bounds, - minzoom: this.minzoom, - maxzoom: this.maxzoom, - tileSize: this.tileSize - }; this.MapService.mapLoaded$.subscribe(() => { + const source = { + type: this.type, + url: this.url, + tiles: this.tiles, + bounds: this.bounds, + minzoom: this.minzoom, + maxzoom: this.maxzoom, + tileSize: this.tileSize + }; this.MapService.addSource(this.id, source); + this.sourceAdded = true; }); } ngOnChanges(changes: SimpleChanges) { + if (!this.sourceAdded) { + return; + } if ( changes.url && !changes.url.isFirstChange() || changes.tiles && !changes.tiles.isFirstChange() || diff --git a/src/app/lib/source/vector-source.component.ts b/src/app/lib/source/vector-source.component.ts index dd6c77615..5ae2f4370 100644 --- a/src/app/lib/source/vector-source.component.ts +++ b/src/app/lib/source/vector-source.component.ts @@ -19,6 +19,8 @@ export class VectorSourceComponent implements OnInit, OnDestroy, OnChanges, Vect type: 'vector' = 'vector'; // Just to make ts happy + private sourceAdded = false; + constructor( private MapService: MapService ) { } @@ -32,10 +34,14 @@ export class VectorSourceComponent implements OnInit, OnDestroy, OnChanges, Vect minzoom: this.minzoom, maxzoom: this.maxzoom, }); + this.sourceAdded = true; }); } ngOnChanges(changes: SimpleChanges) { + if (!this.sourceAdded) { + return; + } if ( changes.url && !changes.url.isFirstChange() || changes.tiles && !changes.tiles.isFirstChange() || diff --git a/src/app/lib/source/video-source.component.ts b/src/app/lib/source/video-source.component.ts index 7b7b6c384..378341884 100644 --- a/src/app/lib/source/video-source.component.ts +++ b/src/app/lib/source/video-source.component.ts @@ -15,6 +15,8 @@ export class VideoSourceComponent implements OnInit, OnDestroy, OnChanges, Video @Input() urls: string[]; @Input() coordinates: number[][]; + private sourceAdded = false; + constructor( private MapService: MapService ) { } @@ -26,10 +28,14 @@ export class VideoSourceComponent implements OnInit, OnDestroy, OnChanges, Video urls: this.urls, coordinates: this.coordinates }); + this.sourceAdded = true; }); } ngOnChanges(changes: SimpleChanges) { + if (!this.sourceAdded) { + return; + } if ( changes.urls && !changes.urls.isFirstChange() || changes.coordinates && !changes.coordinates.isFirstChange()