From e609ae2034c1f7a486bf9a2935b6c77abac1f4ae Mon Sep 17 00:00:00 2001 From: arturovt Date: Sun, 6 Apr 2025 00:57:34 +0300 Subject: [PATCH] refactor(google-maps): tree-shake `assertInitialized` Prior to this commit, in production bundles, `assertInitialized` is an empty method (its body is guarded with `ngDevMode`) that is preserved on the class and always called. There's no reason to keep calling no-op methods in production. This also increases bundle size because the method itself is not tree-shaken. In this commit, the assertion has been moved to a separate function, which can be dropped in production due to the `ngDevMode` guard. The only downside I couldn't resolve is that assertion functions (`asserts ...`) can't be used in this setup, so I had to use the non-null assertion operator instead. --- .../deprecated-map-marker-clusterer.ts | 156 +++++++++++------- src/google-maps/google-map/google-map.ts | 117 ++++++++----- .../map-advanced-marker.ts | 25 +-- src/google-maps/map-circle/map-circle.ts | 74 +++++---- .../map-directions-renderer.ts | 42 ++--- .../map-ground-overlay/map-ground-overlay.ts | 38 +++-- .../map-heatmap-layer/map-heatmap-layer.ts | 31 ++-- .../map-info-window/map-info-window.ts | 64 ++++--- .../map-kml-layer/map-kml-layer.ts | 58 ++++--- .../map-marker-clusterer.ts | 44 ++--- src/google-maps/map-marker/map-marker.ts | 98 +++++++---- src/google-maps/map-polygon/map-polygon.ts | 62 ++++--- src/google-maps/map-polyline/map-polyline.ts | 56 ++++--- .../map-rectangle/map-rectangle.ts | 56 ++++--- .../map-traffic-layer/map-traffic-layer.ts | 24 +-- 15 files changed, 574 insertions(+), 371 deletions(-) diff --git a/src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.ts b/src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.ts index 3c84386e5750..7a80b1fd608c 100644 --- a/src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.ts +++ b/src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.ts @@ -244,7 +244,9 @@ export class DeprecatedMapMarkerClusterer return new MarkerClusterer(map, [], this._combineOptions()); }); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this._eventManager.setTarget(this.markerClusterer); this.markerClustererInitialized.emit(this.markerClusterer); }); @@ -352,103 +354,143 @@ export class DeprecatedMapMarkerClusterer } fitMapToMarkers(padding: number | google.maps.Padding) { - this._assertInitialized(); - this.markerClusterer.fitMapToMarkers(padding); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.markerClusterer!.fitMapToMarkers(padding); } getAverageCenter(): boolean { - this._assertInitialized(); - return this.markerClusterer.getAverageCenter(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getAverageCenter(); } getBatchSizeIE(): number { - this._assertInitialized(); - return this.markerClusterer.getBatchSizeIE(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getBatchSizeIE(); } getCalculator(): Calculator { - this._assertInitialized(); - return this.markerClusterer.getCalculator(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getCalculator(); } getClusterClass(): string { - this._assertInitialized(); - return this.markerClusterer.getClusterClass(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getClusterClass(); } getClusters(): Cluster[] { - this._assertInitialized(); - return this.markerClusterer.getClusters(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getClusters(); } getEnableRetinaIcons(): boolean { - this._assertInitialized(); - return this.markerClusterer.getEnableRetinaIcons(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getEnableRetinaIcons(); } getGridSize(): number { - this._assertInitialized(); - return this.markerClusterer.getGridSize(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getGridSize(); } getIgnoreHidden(): boolean { - this._assertInitialized(); - return this.markerClusterer.getIgnoreHidden(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getIgnoreHidden(); } getImageExtension(): string { - this._assertInitialized(); - return this.markerClusterer.getImageExtension(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getImageExtension(); } getImagePath(): string { - this._assertInitialized(); - return this.markerClusterer.getImagePath(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getImagePath(); } getImageSizes(): number[] { - this._assertInitialized(); - return this.markerClusterer.getImageSizes(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getImageSizes(); } getMaxZoom(): number { - this._assertInitialized(); - return this.markerClusterer.getMaxZoom(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getMaxZoom(); } getMinimumClusterSize(): number { - this._assertInitialized(); - return this.markerClusterer.getMinimumClusterSize(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getMinimumClusterSize(); } getStyles(): ClusterIconStyle[] { - this._assertInitialized(); - return this.markerClusterer.getStyles(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getStyles(); } getTitle(): string { - this._assertInitialized(); - return this.markerClusterer.getTitle(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getTitle(); } getTotalClusters(): number { - this._assertInitialized(); - return this.markerClusterer.getTotalClusters(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getTotalClusters(); } getTotalMarkers(): number { - this._assertInitialized(); - return this.markerClusterer.getTotalMarkers(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getTotalMarkers(); } getZIndex(): number { - this._assertInitialized(); - return this.markerClusterer.getZIndex(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getZIndex(); } getZoomOnClick(): boolean { - this._assertInitialized(); - return this.markerClusterer.getZoomOnClick(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.markerClusterer!.getZoomOnClick(); } private _combineOptions(): MarkerClustererOptions { @@ -477,7 +519,9 @@ export class DeprecatedMapMarkerClusterer } private _watchForMarkerChanges() { - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this._ngZone.runOutsideAngular(() => { this._getInternalMarkers(this._markers).then(markers => { @@ -486,14 +530,16 @@ export class DeprecatedMapMarkerClusterer this._currentMarkers.add(marker); initialMarkers.push(marker); } - this.markerClusterer.addMarkers(initialMarkers); + this.markerClusterer!.addMarkers(initialMarkers); }); }); this._markers.changes .pipe(takeUntil(this._destroy)) .subscribe((markerComponents: MapMarker[]) => { - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this._ngZone.runOutsideAngular(() => { this._getInternalMarkers(markerComponents).then(markers => { const newMarkers = new Set(markers); @@ -510,9 +556,9 @@ export class DeprecatedMapMarkerClusterer markersToRemove.push(marker); } } - this.markerClusterer.addMarkers(markersToAdd, true); - this.markerClusterer.removeMarkers(markersToRemove, true); - this.markerClusterer.repaint(); + this.markerClusterer!.addMarkers(markersToAdd, true); + this.markerClusterer!.removeMarkers(markersToRemove, true); + this.markerClusterer!.repaint(); for (const marker of markersToRemove) { this._currentMarkers.delete(marker); } @@ -526,15 +572,13 @@ export class DeprecatedMapMarkerClusterer ): Promise { return Promise.all(markers.map(markerComponent => markerComponent._resolveMarker())); } +} - private _assertInitialized(): asserts this is {markerClusterer: MarkerClustererInstance} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.markerClusterer) { - throw Error( - 'Cannot interact with a MarkerClusterer before it has been initialized. ' + - 'Please wait for the MarkerClusterer to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: DeprecatedMapMarkerClusterer) { + if (!ctx.markerClusterer) { + throw Error( + 'Cannot interact with a MarkerClusterer before it has been initialized. ' + + 'Please wait for the MarkerClusterer to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/google-map/google-map.ts b/src/google-maps/google-map/google-map.ts index 94b6d2dd8b9e..9c2201119bcb 100644 --- a/src/google-maps/google-map/google-map.ts +++ b/src/google-maps/google-map/google-map.ts @@ -351,8 +351,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { bounds: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral, padding?: number | google.maps.Padding, ) { - this._assertInitialized(); - this.googleMap.fitBounds(bounds, padding); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.googleMap!.fitBounds(bounds, padding); } /** @@ -360,8 +362,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.panBy */ panBy(x: number, y: number) { - this._assertInitialized(); - this.googleMap.panBy(x, y); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.googleMap!.panBy(x, y); } /** @@ -369,8 +373,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.panTo */ panTo(latLng: google.maps.LatLng | google.maps.LatLngLiteral) { - this._assertInitialized(); - this.googleMap.panTo(latLng); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.googleMap!.panTo(latLng); } /** @@ -381,8 +387,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { latLngBounds: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral, padding?: number | google.maps.Padding, ) { - this._assertInitialized(); - this.googleMap.panToBounds(latLngBounds, padding); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.googleMap!.panToBounds(latLngBounds, padding); } /** @@ -390,8 +398,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getBounds */ getBounds(): google.maps.LatLngBounds | null { - this._assertInitialized(); - return this.googleMap.getBounds() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getBounds() || null; } /** @@ -399,8 +409,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getCenter */ getCenter(): google.maps.LatLng | undefined { - this._assertInitialized(); - return this.googleMap.getCenter(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getCenter(); } /** @@ -408,8 +420,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getClickableIcons */ getClickableIcons(): boolean | undefined { - this._assertInitialized(); - return this.googleMap.getClickableIcons(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getClickableIcons(); } /** @@ -417,8 +431,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getHeading */ getHeading(): number | undefined { - this._assertInitialized(); - return this.googleMap.getHeading(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getHeading(); } /** @@ -426,8 +442,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getMapTypeId */ getMapTypeId(): google.maps.MapTypeId | string | undefined { - this._assertInitialized(); - return this.googleMap.getMapTypeId(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getMapTypeId(); } /** @@ -435,8 +453,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getProjection */ getProjection(): google.maps.Projection | null { - this._assertInitialized(); - return this.googleMap.getProjection() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getProjection() || null; } /** @@ -444,8 +464,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getStreetView */ getStreetView(): google.maps.StreetViewPanorama { - this._assertInitialized(); - return this.googleMap.getStreetView(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getStreetView(); } /** @@ -453,8 +475,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getTilt */ getTilt(): number | undefined { - this._assertInitialized(); - return this.googleMap.getTilt(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getTilt(); } /** @@ -462,8 +486,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.getZoom */ getZoom(): number | undefined { - this._assertInitialized(); - return this.googleMap.getZoom(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.getZoom(); } /** @@ -471,8 +497,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.controls */ get controls(): google.maps.MVCArray[] { - this._assertInitialized(); - return this.googleMap.controls; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.controls; } /** @@ -480,8 +508,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.data */ get data(): google.maps.Data { - this._assertInitialized(); - return this.googleMap.data; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.data; } /** @@ -489,8 +519,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.mapTypes */ get mapTypes(): google.maps.MapTypeRegistry { - this._assertInitialized(); - return this.googleMap.mapTypes; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.mapTypes; } /** @@ -498,8 +530,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { * https://developers.google.com/maps/documentation/javascript/reference/map#Map.overlayMapTypes */ get overlayMapTypes(): google.maps.MVCArray { - this._assertInitialized(); - return this.googleMap.overlayMapTypes; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.googleMap!.overlayMapTypes; } /** Returns a promise that resolves when the map has been initialized. */ @@ -533,15 +567,14 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy { mapId: this.mapId || options.mapId, }; } +} - /** Asserts that the map has been initialized. */ - private _assertInitialized(): asserts this is {googleMap: google.maps.Map} { - if (!this.googleMap && (typeof ngDevMode === 'undefined' || ngDevMode)) { - throw Error( - 'Cannot access Google Map information before the API has been initialized. ' + - 'Please wait for the API to load before trying to interact with it.', - ); - } +function assertInitialized(ctx: GoogleMap) { + if (!ctx.googleMap) { + throw Error( + 'Cannot access Google Map information before the API has been initialized. ' + + 'Please wait for the API to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-advanced-marker/map-advanced-marker.ts b/src/google-maps/map-advanced-marker/map-advanced-marker.ts index 246b955b0815..c21988d5657f 100644 --- a/src/google-maps/map-advanced-marker/map-advanced-marker.ts +++ b/src/google-maps/map-advanced-marker/map-advanced-marker.ts @@ -226,7 +226,9 @@ export class MapAdvancedMarker // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.advancedMarker = new advancedMarkerConstructor(this._combineOptions()); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.advancedMarker.map = map; this._eventManager.setTarget(this.advancedMarker); this.markerInitialized.next(this.advancedMarker); @@ -268,7 +270,9 @@ export class MapAdvancedMarker } getAnchor(): google.maps.marker.AdvancedMarkerElement { - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } return this.advancedMarker; } @@ -292,16 +296,13 @@ export class MapAdvancedMarker map: this._googleMap.googleMap, }; } +} - /** Asserts that the map has been initialized. */ - private _assertInitialized(): asserts this is {marker: google.maps.marker.AdvancedMarkerElement} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.advancedMarker) { - throw Error( - 'Cannot interact with a Google Map Marker before it has been ' + - 'initialized. Please wait for the Marker to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapAdvancedMarker) { + if (!ctx.advancedMarker) { + throw Error( + 'Cannot interact with a Google Map Marker before it has been ' + + 'initialized. Please wait for the Marker to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-circle/map-circle.ts b/src/google-maps/map-circle/map-circle.ts index 8b24fef4c73b..0fde9cfd3299 100644 --- a/src/google-maps/map-circle/map-circle.ts +++ b/src/google-maps/map-circle/map-circle.ts @@ -196,7 +196,9 @@ export class MapCircle implements OnInit, OnDestroy { // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.circle = new circleConstructor(options); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.circle.setMap(map); this._eventManager.setTarget(this.circle); this.circleInitialized.emit(this.circle); @@ -218,8 +220,10 @@ export class MapCircle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getBounds */ getBounds(): google.maps.LatLngBounds | null { - this._assertInitialized(); - return this.circle.getBounds(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.circle!.getBounds(); } /** @@ -227,8 +231,10 @@ export class MapCircle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getCenter */ getCenter(): google.maps.LatLng | null { - this._assertInitialized(); - return this.circle.getCenter(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.circle!.getCenter(); } /** @@ -236,8 +242,10 @@ export class MapCircle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getDraggable */ getDraggable(): boolean { - this._assertInitialized(); - return this.circle.getDraggable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.circle!.getDraggable(); } /** @@ -245,8 +253,10 @@ export class MapCircle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getEditable */ getEditable(): boolean { - this._assertInitialized(); - return this.circle.getEditable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.circle!.getEditable(); } /** @@ -254,8 +264,10 @@ export class MapCircle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getRadius */ getRadius(): number { - this._assertInitialized(); - return this.circle.getRadius(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.circle!.getRadius(); } /** @@ -263,8 +275,10 @@ export class MapCircle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getVisible */ getVisible(): boolean { - this._assertInitialized(); - return this.circle.getVisible(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.circle!.getVisible(); } private _combineOptions(): Observable { @@ -282,16 +296,20 @@ export class MapCircle implements OnInit, OnDestroy { private _watchForOptionsChanges() { this._options.pipe(takeUntil(this._destroyed)).subscribe(options => { - this._assertInitialized(); - this.circle.setOptions(options); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.circle!.setOptions(options); }); } private _watchForCenterChanges() { this._center.pipe(takeUntil(this._destroyed)).subscribe(center => { if (center) { - this._assertInitialized(); - this.circle.setCenter(center); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.circle!.setCenter(center); } }); } @@ -299,20 +317,20 @@ export class MapCircle implements OnInit, OnDestroy { private _watchForRadiusChanges() { this._radius.pipe(takeUntil(this._destroyed)).subscribe(radius => { if (radius !== undefined) { - this._assertInitialized(); - this.circle.setRadius(radius); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.circle!.setRadius(radius); } }); } +} - private _assertInitialized(): asserts this is {circle: google.maps.Circle} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.circle) { - throw Error( - 'Cannot interact with a Google Map Circle before it has been ' + - 'initialized. Please wait for the Circle to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapCircle) { + if (!ctx.circle) { + throw Error( + 'Cannot interact with a Google Map Circle before it has been ' + + 'initialized. Please wait for the Circle to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-directions-renderer/map-directions-renderer.ts b/src/google-maps/map-directions-renderer/map-directions-renderer.ts index f2b9d05266df..0cecfd2c35d5 100644 --- a/src/google-maps/map-directions-renderer/map-directions-renderer.ts +++ b/src/google-maps/map-directions-renderer/map-directions-renderer.ts @@ -103,7 +103,9 @@ export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy { // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.directionsRenderer = new rendererConstructor(this._combineOptions()); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.directionsRenderer.setMap(map); this._eventManager.setTarget(this.directionsRenderer); this.directionsRendererInitialized.emit(this.directionsRenderer); @@ -132,8 +134,10 @@ export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy { * #DirectionsRenderer.getDirections */ getDirections(): google.maps.DirectionsResult | null { - this._assertInitialized(); - return this.directionsRenderer.getDirections(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.directionsRenderer!.getDirections(); } /** @@ -141,8 +145,10 @@ export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy { * #DirectionsRenderer.getPanel */ getPanel(): Node | null { - this._assertInitialized(); - return this.directionsRenderer.getPanel(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.directionsRenderer!.getPanel(); } /** @@ -150,8 +156,10 @@ export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy { * #DirectionsRenderer.getRouteIndex */ getRouteIndex(): number { - this._assertInitialized(); - return this.directionsRenderer.getRouteIndex(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.directionsRenderer!.getRouteIndex(); } private _combineOptions(): google.maps.DirectionsRendererOptions { @@ -162,18 +170,14 @@ export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy { map: this._googleMap.googleMap, }; } +} - private _assertInitialized(): asserts this is { - directionsRenderer: google.maps.DirectionsRenderer; - } { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.directionsRenderer) { - throw Error( - 'Cannot interact with a Google Map Directions Renderer before it has been ' + - 'initialized. Please wait for the Directions Renderer to load before trying ' + - 'to interact with it.', - ); - } - } +function assertInitialized(ctx: MapDirectionsRenderer) { + if (!ctx.directionsRenderer) { + throw Error( + 'Cannot interact with a Google Map Directions Renderer before it has been ' + + 'initialized. Please wait for the Directions Renderer to load before trying ' + + 'to interact with it.', + ); } } diff --git a/src/google-maps/map-ground-overlay/map-ground-overlay.ts b/src/google-maps/map-ground-overlay/map-ground-overlay.ts index 88a5c6426b61..d80467cb9aa4 100644 --- a/src/google-maps/map-ground-overlay/map-ground-overlay.ts +++ b/src/google-maps/map-ground-overlay/map-ground-overlay.ts @@ -142,7 +142,9 @@ export class MapGroundOverlay implements OnInit, OnDestroy { clickable: this.clickable, opacity: this._opacity.value, }); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.groundOverlay.setMap(map); this._eventManager.setTarget(this.groundOverlay); this.groundOverlayInitialized.emit(this.groundOverlay); @@ -169,8 +171,10 @@ export class MapGroundOverlay implements OnInit, OnDestroy { * #GroundOverlay.getBounds */ getBounds(): google.maps.LatLngBounds | null { - this._assertInitialized(); - return this.groundOverlay.getBounds(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.groundOverlay!.getBounds(); } /** @@ -179,8 +183,10 @@ export class MapGroundOverlay implements OnInit, OnDestroy { * #GroundOverlay.getOpacity */ getOpacity(): number { - this._assertInitialized(); - return this.groundOverlay.getOpacity(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.groundOverlay!.getOpacity(); } /** @@ -189,8 +195,10 @@ export class MapGroundOverlay implements OnInit, OnDestroy { * #GroundOverlay.getUrl */ getUrl(): string { - this._assertInitialized(); - return this.groundOverlay.getUrl(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.groundOverlay!.getUrl(); } private _watchForOpacityChanges() { @@ -213,15 +221,13 @@ export class MapGroundOverlay implements OnInit, OnDestroy { } }); } +} - private _assertInitialized(): asserts this is {groundOverlay: google.maps.GroundOverlay} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.groundOverlay) { - throw Error( - 'Cannot interact with a Google Map GroundOverlay before it has been initialized. ' + - 'Please wait for the GroundOverlay to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapGroundOverlay) { + if (!ctx.groundOverlay) { + throw Error( + 'Cannot interact with a Google Map GroundOverlay before it has been initialized. ' + + 'Please wait for the GroundOverlay to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-heatmap-layer/map-heatmap-layer.ts b/src/google-maps/map-heatmap-layer/map-heatmap-layer.ts index ea73b8cffbc8..d2b5d76b76b3 100644 --- a/src/google-maps/map-heatmap-layer/map-heatmap-layer.ts +++ b/src/google-maps/map-heatmap-layer/map-heatmap-layer.ts @@ -116,7 +116,9 @@ export class MapHeatmapLayer implements OnInit, OnChanges, OnDestroy { // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.heatmap = new heatmapConstructor(this._combineOptions()); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.heatmap.setMap(map); this.heatmapInitialized.emit(this.heatmap); }); @@ -145,8 +147,10 @@ export class MapHeatmapLayer implements OnInit, OnChanges, OnDestroy { * See: developers.google.com/maps/documentation/javascript/reference/visualization#HeatmapLayer */ getData(): HeatmapData { - this._assertInitialized(); - return this.heatmap.getData(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.heatmap!.getData(); } /** Creates a combined options object using the passed-in options and the individual inputs. */ @@ -176,21 +180,18 @@ export class MapHeatmapLayer implements OnInit, OnChanges, OnDestroy { return result; } - - /** Asserts that the heatmap object has been initialized. */ - private _assertInitialized(): asserts this is {heatmap: google.maps.visualization.HeatmapLayer} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.heatmap) { - throw Error( - 'Cannot interact with a Google Map HeatmapLayer before it has been ' + - 'initialized. Please wait for the heatmap to load before trying to interact with it.', - ); - } - } - } } /** Asserts that an object is a `LatLngLiteral`. */ function isLatLngLiteral(value: any): value is google.maps.LatLngLiteral { return value && typeof value.lat === 'number' && typeof value.lng === 'number'; } + +function assertInitialized(ctx: MapHeatmapLayer) { + if (!ctx.heatmap) { + throw Error( + 'Cannot interact with a Google Map HeatmapLayer before it has been ' + + 'initialized. Please wait for the heatmap to load before trying to interact with it.', + ); + } +} diff --git a/src/google-maps/map-info-window/map-info-window.ts b/src/google-maps/map-info-window/map-info-window.ts index bd6d23a2bea1..9f0a8767235c 100644 --- a/src/google-maps/map-info-window/map-info-window.ts +++ b/src/google-maps/map-info-window/map-info-window.ts @@ -160,8 +160,10 @@ export class MapInfoWindow implements OnInit, OnDestroy { * See developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.close */ close() { - this._assertInitialized(); - this.infoWindow.close(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.infoWindow!.close(); } /** @@ -169,8 +171,10 @@ export class MapInfoWindow implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.getContent */ getContent(): string | Node | null { - this._assertInitialized(); - return this.infoWindow.getContent() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.infoWindow!.getContent() || null; } /** @@ -179,8 +183,10 @@ export class MapInfoWindow implements OnInit, OnDestroy { * #InfoWindow.getPosition */ getPosition(): google.maps.LatLng | null { - this._assertInitialized(); - return this.infoWindow.getPosition() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.infoWindow!.getPosition() || null; } /** @@ -188,8 +194,10 @@ export class MapInfoWindow implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.getZIndex */ getZIndex(): number { - this._assertInitialized(); - return this.infoWindow.getZIndex(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.infoWindow!.getZIndex(); } /** @@ -215,7 +223,9 @@ export class MapInfoWindow implements OnInit, OnDestroy { * then the position property of the options input is used instead. */ open(anchor?: MapAnchorPoint, shouldFocus?: boolean, content?: string | Element | Text): void { - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } if ((typeof ngDevMode === 'undefined' || ngDevMode) && anchor && !anchor.getAnchor) { throw new Error( @@ -230,14 +240,14 @@ export class MapInfoWindow implements OnInit, OnDestroy { // Note that when the window is opened for the first time, the anchor will always be // undefined. If that's the case, we have to allow it to open in order to handle the // case where the window doesn't have an anchor, but is placed at a particular position. - if (this.infoWindow.get('anchor') !== anchorObject || !anchorObject) { + if (this.infoWindow!.get('anchor') !== anchorObject || !anchorObject) { // If no explicit content is provided, it is taken from the DOM node. // If it is, we need to hide it so it doesn't take up space on the page. this._elementRef.nativeElement.style.display = content ? 'none' : ''; if (content) { - this.infoWindow.setContent(content); + this.infoWindow!.setContent(content); } - this.infoWindow.open({ + this.infoWindow!.open({ map: this._googleMap.googleMap, anchor: anchorObject, shouldFocus, @@ -260,29 +270,31 @@ export class MapInfoWindow implements OnInit, OnDestroy { private _watchForOptionsChanges() { this._options.pipe(takeUntil(this._destroy)).subscribe(options => { - this._assertInitialized(); - this.infoWindow.setOptions(options); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.infoWindow!.setOptions(options); }); } private _watchForPositionChanges() { this._position.pipe(takeUntil(this._destroy)).subscribe(position => { if (position) { - this._assertInitialized(); - this.infoWindow.setPosition(position); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.infoWindow!.setPosition(position); } }); } +} - private _assertInitialized(): asserts this is {infoWindow: google.maps.InfoWindow} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.infoWindow) { - throw Error( - 'Cannot interact with a Google Map Info Window before it has been ' + - 'initialized. Please wait for the Info Window to load before trying to interact with ' + - 'it.', - ); - } - } +function assertInitialized(ctx: MapInfoWindow) { + if (!ctx.infoWindow) { + throw Error( + 'Cannot interact with a Google Map Info Window before it has been ' + + 'initialized. Please wait for the Info Window to load before trying to interact with ' + + 'it.', + ); } } diff --git a/src/google-maps/map-kml-layer/map-kml-layer.ts b/src/google-maps/map-kml-layer/map-kml-layer.ts index 2e9f66f19432..96807b459471 100644 --- a/src/google-maps/map-kml-layer/map-kml-layer.ts +++ b/src/google-maps/map-kml-layer/map-kml-layer.ts @@ -117,7 +117,9 @@ export class MapKmlLayer implements OnInit, OnDestroy { // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.kmlLayer = new layerConstructor(options); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.kmlLayer.setMap(map); this._eventManager.setTarget(this.kmlLayer); this.kmlLayerInitialized.emit(this.kmlLayer); @@ -138,40 +140,50 @@ export class MapKmlLayer implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/kml#KmlLayer.getDefaultViewport */ getDefaultViewport(): google.maps.LatLngBounds | null { - this._assertInitialized(); - return this.kmlLayer.getDefaultViewport(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.kmlLayer!.getDefaultViewport(); } /** * See developers.google.com/maps/documentation/javascript/reference/kml#KmlLayer.getMetadata */ getMetadata(): google.maps.KmlLayerMetadata | null { - this._assertInitialized(); - return this.kmlLayer.getMetadata(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.kmlLayer!.getMetadata(); } /** * See developers.google.com/maps/documentation/javascript/reference/kml#KmlLayer.getStatus */ getStatus(): google.maps.KmlLayerStatus { - this._assertInitialized(); - return this.kmlLayer.getStatus(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.kmlLayer!.getStatus(); } /** * See developers.google.com/maps/documentation/javascript/reference/kml#KmlLayer.getUrl */ getUrl(): string { - this._assertInitialized(); - return this.kmlLayer.getUrl(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.kmlLayer!.getUrl(); } /** * See developers.google.com/maps/documentation/javascript/reference/kml#KmlLayer.getZIndex */ getZIndex(): number { - this._assertInitialized(); - return this.kmlLayer.getZIndex(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.kmlLayer!.getZIndex(); } private _combineOptions(): Observable { @@ -189,7 +201,9 @@ export class MapKmlLayer implements OnInit, OnDestroy { private _watchForOptionsChanges() { this._options.pipe(takeUntil(this._destroyed)).subscribe(options => { if (this.kmlLayer) { - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.kmlLayer.setOptions(options); } }); @@ -198,20 +212,20 @@ export class MapKmlLayer implements OnInit, OnDestroy { private _watchForUrlChanges() { this._url.pipe(takeUntil(this._destroyed)).subscribe(url => { if (url && this.kmlLayer) { - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.kmlLayer.setUrl(url); } }); } +} - private _assertInitialized(): asserts this is {kmlLayer: google.maps.KmlLayer} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.kmlLayer) { - throw Error( - 'Cannot interact with a Google Map KmlLayer before it has been ' + - 'initialized. Please wait for the KmlLayer to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapKmlLayer) { + if (!ctx.kmlLayer) { + throw Error( + 'Cannot interact with a Google Map KmlLayer before it has been ' + + 'initialized. Please wait for the KmlLayer to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-marker-clusterer/map-marker-clusterer.ts b/src/google-maps/map-marker-clusterer/map-marker-clusterer.ts index 8113ab961e9c..e1eb12da3c91 100644 --- a/src/google-maps/map-marker-clusterer/map-marker-clusterer.ts +++ b/src/google-maps/map-marker-clusterer/map-marker-clusterer.ts @@ -160,7 +160,9 @@ export class MapMarkerClusterer implements OnInit, OnChanges, OnDestroy { } private async _watchForMarkerChanges() { - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } const initialMarkers: Marker[] = []; const markers = await this._getInternalMarkers(this._markers.toArray()); @@ -168,12 +170,14 @@ export class MapMarkerClusterer implements OnInit, OnChanges, OnDestroy { this._currentMarkers.add(marker); initialMarkers.push(marker); } - this.markerClusterer.addMarkers(initialMarkers); + this.markerClusterer!.addMarkers(initialMarkers); this._markersSubscription.unsubscribe(); this._markersSubscription = this._markers.changes.subscribe( async (markerComponents: MarkerDirective[]) => { - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } const newMarkers = new Set(await this._getInternalMarkers(markerComponents)); const markersToAdd: Marker[] = []; const markersToRemove: Marker[] = []; @@ -188,9 +192,9 @@ export class MapMarkerClusterer implements OnInit, OnChanges, OnDestroy { markersToRemove.push(marker); } } - this.markerClusterer.addMarkers(markersToAdd, true); - this.markerClusterer.removeMarkers(markersToRemove, true); - this.markerClusterer.render(); + this.markerClusterer!.addMarkers(markersToAdd, true); + this.markerClusterer!.removeMarkers(markersToRemove, true); + this.markerClusterer!.render(); for (const marker of markersToRemove) { this._currentMarkers.delete(marker); } @@ -209,21 +213,19 @@ export class MapMarkerClusterer implements OnInit, OnChanges, OnDestroy { private _getInternalMarkers(markers: MarkerDirective[]): Promise { return Promise.all(markers.map(marker => marker._resolveMarker())); } +} - private _assertInitialized(): asserts this is {markerClusterer: MarkerClusterer} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this._googleMap.googleMap) { - throw Error( - 'Cannot access Google Map information before the API has been initialized. ' + - 'Please wait for the API to load before trying to interact with it.', - ); - } - if (!this.markerClusterer) { - throw Error( - 'Cannot interact with a MarkerClusterer before it has been initialized. ' + - 'Please wait for the MarkerClusterer to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapMarkerClusterer) { + if (!ctx['_googleMap'].googleMap) { + throw Error( + 'Cannot access Google Map information before the API has been initialized. ' + + 'Please wait for the API to load before trying to interact with it.', + ); + } + if (!ctx.markerClusterer) { + throw Error( + 'Cannot interact with a MarkerClusterer before it has been initialized. ' + + 'Please wait for the MarkerClusterer to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-marker/map-marker.ts b/src/google-maps/map-marker/map-marker.ts index b1d1c22fc164..e2cb748ccc01 100644 --- a/src/google-maps/map-marker/map-marker.ts +++ b/src/google-maps/map-marker/map-marker.ts @@ -312,7 +312,9 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.marker = new markerConstructor(this._combineOptions()); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.marker.setMap(map); this._eventManager.setTarget(this.marker); this.markerInitialized.next(this.marker); @@ -364,8 +366,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getAnimation */ getAnimation(): google.maps.Animation | null { - this._assertInitialized(); - return this.marker.getAnimation() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getAnimation() || null; } /** @@ -373,8 +377,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getClickable */ getClickable(): boolean { - this._assertInitialized(); - return this.marker.getClickable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getClickable(); } /** @@ -382,8 +388,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getCursor */ getCursor(): string | null { - this._assertInitialized(); - return this.marker.getCursor() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getCursor() || null; } /** @@ -391,8 +399,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getDraggable */ getDraggable(): boolean { - this._assertInitialized(); - return !!this.marker.getDraggable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return !!this.marker!.getDraggable(); } /** @@ -400,8 +410,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getIcon */ getIcon(): string | google.maps.Icon | google.maps.Symbol | null { - this._assertInitialized(); - return this.marker.getIcon() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getIcon() || null; } /** @@ -409,8 +421,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getLabel */ getLabel(): google.maps.MarkerLabel | string | null { - this._assertInitialized(); - return this.marker.getLabel() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getLabel() || null; } /** @@ -418,8 +432,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getOpacity */ getOpacity(): number | null { - this._assertInitialized(); - return this.marker.getOpacity() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getOpacity() || null; } /** @@ -427,8 +443,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getPosition */ getPosition(): google.maps.LatLng | null { - this._assertInitialized(); - return this.marker.getPosition() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getPosition() || null; } /** @@ -436,8 +454,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getShape */ getShape(): google.maps.MarkerShape | null { - this._assertInitialized(); - return this.marker.getShape() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getShape() || null; } /** @@ -445,8 +465,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getTitle */ getTitle(): string | null { - this._assertInitialized(); - return this.marker.getTitle() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getTitle() || null; } /** @@ -454,8 +476,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getVisible */ getVisible(): boolean { - this._assertInitialized(); - return this.marker.getVisible(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getVisible(); } /** @@ -463,14 +487,18 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, * developers.google.com/maps/documentation/javascript/reference/marker#Marker.getZIndex */ getZIndex(): number | null { - this._assertInitialized(); - return this.marker.getZIndex() || null; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!.getZIndex() || null; } /** Gets the anchor point that can be used to attach other Google Maps objects. */ getAnchor(): google.maps.MVCObject { - this._assertInitialized(); - return this.marker; + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.marker!; } /** Returns a promise that resolves when the marker has been initialized. */ @@ -494,15 +522,13 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint, visible: this._visible ?? options.visible, }; } +} - private _assertInitialized(): asserts this is {marker: google.maps.Marker} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.marker) { - throw Error( - 'Cannot interact with a Google Map Marker before it has been ' + - 'initialized. Please wait for the Marker to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapMarker) { + if (!ctx.marker) { + throw Error( + 'Cannot interact with a Google Map Marker before it has been ' + + 'initialized. Please wait for the Marker to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-polygon/map-polygon.ts b/src/google-maps/map-polygon/map-polygon.ts index 12642d5edbbe..4747899a393a 100644 --- a/src/google-maps/map-polygon/map-polygon.ts +++ b/src/google-maps/map-polygon/map-polygon.ts @@ -175,7 +175,9 @@ export class MapPolygon implements OnInit, OnDestroy { // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.polygon = new polygonConstructor(options); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.polygon.setMap(map); this._eventManager.setTarget(this.polygon); this.polygonInitialized.emit(this.polygon); @@ -196,40 +198,50 @@ export class MapPolygon implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Polygon.getDraggable */ getDraggable(): boolean { - this._assertInitialized(); - return this.polygon.getDraggable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polygon!.getDraggable(); } /** * See developers.google.com/maps/documentation/javascript/reference/polygon#Polygon.getEditable */ getEditable(): boolean { - this._assertInitialized(); - return this.polygon.getEditable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polygon!.getEditable(); } /** * See developers.google.com/maps/documentation/javascript/reference/polygon#Polygon.getPath */ getPath(): google.maps.MVCArray { - this._assertInitialized(); - return this.polygon.getPath(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polygon!.getPath(); } /** * See developers.google.com/maps/documentation/javascript/reference/polygon#Polygon.getPaths */ getPaths(): google.maps.MVCArray> { - this._assertInitialized(); - return this.polygon.getPaths(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polygon!.getPaths(); } /** * See developers.google.com/maps/documentation/javascript/reference/polygon#Polygon.getVisible */ getVisible(): boolean { - this._assertInitialized(); - return this.polygon.getVisible(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polygon!.getVisible(); } private _combineOptions(): Observable { @@ -246,28 +258,30 @@ export class MapPolygon implements OnInit, OnDestroy { private _watchForOptionsChanges() { this._options.pipe(takeUntil(this._destroyed)).subscribe(options => { - this._assertInitialized(); - this.polygon.setOptions(options); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.polygon!.setOptions(options); }); } private _watchForPathChanges() { this._paths.pipe(takeUntil(this._destroyed)).subscribe(paths => { if (paths) { - this._assertInitialized(); - this.polygon.setPaths(paths); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.polygon!.setPaths(paths); } }); } +} - private _assertInitialized(): asserts this is {polygon: google.maps.Polygon} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.polygon) { - throw Error( - 'Cannot interact with a Google Map Polygon before it has been ' + - 'initialized. Please wait for the Polygon to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapPolygon) { + if (!ctx.polygon) { + throw Error( + 'Cannot interact with a Google Map Polygon before it has been ' + + 'initialized. Please wait for the Polygon to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-polyline/map-polyline.ts b/src/google-maps/map-polyline/map-polyline.ts index 373467691b59..7b621554d9fc 100644 --- a/src/google-maps/map-polyline/map-polyline.ts +++ b/src/google-maps/map-polyline/map-polyline.ts @@ -173,7 +173,9 @@ export class MapPolyline implements OnInit, OnDestroy { // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.polyline = new polylineConstructor(options); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.polyline.setMap(map); this._eventManager.setTarget(this.polyline); this.polylineInitialized.emit(this.polyline); @@ -194,32 +196,40 @@ export class MapPolyline implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Polyline.getDraggable */ getDraggable(): boolean { - this._assertInitialized(); - return this.polyline.getDraggable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polyline!.getDraggable(); } /** * See developers.google.com/maps/documentation/javascript/reference/polygon#Polyline.getEditable */ getEditable(): boolean { - this._assertInitialized(); - return this.polyline.getEditable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polyline!.getEditable(); } /** * See developers.google.com/maps/documentation/javascript/reference/polygon#Polyline.getPath */ getPath(): google.maps.MVCArray { - this._assertInitialized(); - return this.polyline.getPath(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polyline!.getPath(); } /** * See developers.google.com/maps/documentation/javascript/reference/polygon#Polyline.getVisible */ getVisible(): boolean { - this._assertInitialized(); - return this.polyline.getVisible(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.polyline!.getVisible(); } private _combineOptions(): Observable { @@ -236,28 +246,30 @@ export class MapPolyline implements OnInit, OnDestroy { private _watchForOptionsChanges() { this._options.pipe(takeUntil(this._destroyed)).subscribe(options => { - this._assertInitialized(); - this.polyline.setOptions(options); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.polyline!.setOptions(options); }); } private _watchForPathChanges() { this._path.pipe(takeUntil(this._destroyed)).subscribe(path => { if (path) { - this._assertInitialized(); - this.polyline.setPath(path); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.polyline!.setPath(path); } }); } +} - private _assertInitialized(): asserts this is {polyline: google.maps.Polyline} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.polyline) { - throw Error( - 'Cannot interact with a Google Map Polyline before it has been ' + - 'initialized. Please wait for the Polyline to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapPolyline) { + if (!ctx.polyline) { + throw Error( + 'Cannot interact with a Google Map Polyline before it has been ' + + 'initialized. Please wait for the Polyline to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-rectangle/map-rectangle.ts b/src/google-maps/map-rectangle/map-rectangle.ts index 29495a9f89ff..96a08d6e0df8 100644 --- a/src/google-maps/map-rectangle/map-rectangle.ts +++ b/src/google-maps/map-rectangle/map-rectangle.ts @@ -182,7 +182,9 @@ export class MapRectangle implements OnInit, OnDestroy { // user has subscribed to. this._ngZone.runOutsideAngular(() => { this.rectangle = new rectangleConstructor(options); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.rectangle.setMap(map); this._eventManager.setTarget(this.rectangle); this.rectangleInitialized.emit(this.rectangle); @@ -203,8 +205,10 @@ export class MapRectangle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Rectangle.getBounds */ getBounds(): google.maps.LatLngBounds | null { - this._assertInitialized(); - return this.rectangle.getBounds(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.rectangle!.getBounds(); } /** @@ -212,8 +216,10 @@ export class MapRectangle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Rectangle.getDraggable */ getDraggable(): boolean { - this._assertInitialized(); - return this.rectangle.getDraggable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.rectangle!.getDraggable(); } /** @@ -221,8 +227,10 @@ export class MapRectangle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Rectangle.getEditable */ getEditable(): boolean { - this._assertInitialized(); - return this.rectangle.getEditable(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.rectangle!.getEditable(); } /** @@ -230,8 +238,10 @@ export class MapRectangle implements OnInit, OnDestroy { * developers.google.com/maps/documentation/javascript/reference/polygon#Rectangle.getVisible */ getVisible(): boolean { - this._assertInitialized(); - return this.rectangle.getVisible(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + return this.rectangle!.getVisible(); } private _combineOptions(): Observable { @@ -248,28 +258,30 @@ export class MapRectangle implements OnInit, OnDestroy { private _watchForOptionsChanges() { this._options.pipe(takeUntil(this._destroyed)).subscribe(options => { - this._assertInitialized(); - this.rectangle.setOptions(options); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.rectangle!.setOptions(options); }); } private _watchForBoundsChanges() { this._bounds.pipe(takeUntil(this._destroyed)).subscribe(bounds => { if (bounds) { - this._assertInitialized(); - this.rectangle.setBounds(bounds); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.rectangle!.setBounds(bounds); } }); } +} - private _assertInitialized(): asserts this is {rectangle: google.maps.Rectangle} { - if (typeof ngDevMode === 'undefined' || ngDevMode) { - if (!this.rectangle) { - throw Error( - 'Cannot interact with a Google Map Rectangle before it has been initialized. ' + - 'Please wait for the Rectangle to load before trying to interact with it.', - ); - } - } +function assertInitialized(ctx: MapRectangle) { + if (!ctx.rectangle) { + throw Error( + 'Cannot interact with a Google Map Rectangle before it has been initialized. ' + + 'Please wait for the Rectangle to load before trying to interact with it.', + ); } } diff --git a/src/google-maps/map-traffic-layer/map-traffic-layer.ts b/src/google-maps/map-traffic-layer/map-traffic-layer.ts index 87994a311f75..1004be8af389 100644 --- a/src/google-maps/map-traffic-layer/map-traffic-layer.ts +++ b/src/google-maps/map-traffic-layer/map-traffic-layer.ts @@ -88,7 +88,9 @@ export class MapTrafficLayer implements OnInit, OnDestroy { ) { this._ngZone.runOutsideAngular(() => { this.trafficLayer = new layerConstructor(options); - this._assertInitialized(); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } this.trafficLayer.setMap(map); this.trafficLayerInitialized.emit(this.trafficLayer); this._watchForAutoRefreshChanges(); @@ -114,17 +116,19 @@ export class MapTrafficLayer implements OnInit, OnDestroy { this._combineOptions() .pipe(takeUntil(this._destroyed)) .subscribe(options => { - this._assertInitialized(); - this.trafficLayer.setOptions(options); + if (typeof ngDevMode === 'undefined' || ngDevMode) { + assertInitialized(this); + } + this.trafficLayer!.setOptions(options); }); } +} - private _assertInitialized(): asserts this is {trafficLayer: google.maps.TrafficLayer} { - if (!this.trafficLayer) { - throw Error( - 'Cannot interact with a Google Map Traffic Layer before it has been initialized. ' + - 'Please wait for the Traffic Layer to load before trying to interact with it.', - ); - } +function assertInitialized(ctx: MapTrafficLayer) { + if (!ctx.trafficLayer) { + throw Error( + 'Cannot interact with a Google Map Traffic Layer before it has been initialized. ' + + 'Please wait for the Traffic Layer to load before trying to interact with it.', + ); } }