Skip to content

Commit

Permalink
refactor(google-maps): switch to inject function
Browse files Browse the repository at this point in the history
Reworks the Google Maps module to use the `inject` function instead of constructor-based injection.
  • Loading branch information
crisbeto committed Sep 21, 2024
1 parent 9b4085c commit db7d560
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 94 deletions.
14 changes: 7 additions & 7 deletions src/google-maps/google-map/google-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
OnInit,
Output,
ViewEncapsulation,
Inject,
PLATFORM_ID,
NgZone,
SimpleChanges,
Expand Down Expand Up @@ -63,7 +62,9 @@ export const DEFAULT_WIDTH = '500px';
encapsulation: ViewEncapsulation.None,
})
export class GoogleMap implements OnChanges, OnInit, OnDestroy {
private _eventManager: MapEventManager = new MapEventManager(inject(NgZone));
private readonly _elementRef = inject(ElementRef);
private _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));
private _mapEl: HTMLElement;
private _existingAuthFailureCallback: GoogleMapsWindow['gm_authFailure'];

Expand Down Expand Up @@ -250,11 +251,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
@Output() readonly zoomChanged: Observable<void> =
this._eventManager.getLazyEmitter<void>('zoom_changed');

constructor(
private readonly _elementRef: ElementRef,
private _ngZone: NgZone,
@Inject(PLATFORM_ID) platformId: Object,
) {
constructor(...args: unknown[]);

constructor() {
const platformId = inject<Object>(PLATFORM_ID);
this._isBrowser = isPlatformBrowser(platformId);

if (this._isBrowser) {
Expand Down
8 changes: 4 additions & 4 deletions src/google-maps/map-advanced-marker/map-advanced-marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export const DEFAULT_MARKER_OPTIONS = {
standalone: true,
})
export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
private readonly _googleMap = inject(GoogleMap);
private _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));

/**
Expand Down Expand Up @@ -186,10 +188,8 @@ export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy, MapAncho
*/
advancedMarker: google.maps.marker.AdvancedMarkerElement;

constructor(
private readonly _googleMap: GoogleMap,
private _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (!this._googleMap._isBrowser) {
Expand Down
11 changes: 6 additions & 5 deletions src/google-maps/map-base-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
/// <reference types="google.maps" preserve="true" />

import {Directive, NgZone, OnDestroy, OnInit} from '@angular/core';
import {Directive, NgZone, OnDestroy, OnInit, inject} from '@angular/core';

import {GoogleMap} from './google-map/google-map';

Expand All @@ -19,10 +19,11 @@ import {GoogleMap} from './google-map/google-map';
standalone: true,
})
export class MapBaseLayer implements OnInit, OnDestroy {
constructor(
protected readonly _map: GoogleMap,
protected readonly _ngZone: NgZone,
) {}
protected readonly _map = inject(GoogleMap);
protected readonly _ngZone = inject(NgZone);

constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._map._isBrowser) {
Expand Down
9 changes: 4 additions & 5 deletions src/google-maps/map-circle/map-circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ import {MapEventManager} from '../map-event-manager';
standalone: true,
})
export class MapCircle implements OnInit, OnDestroy {
private readonly _map = inject(GoogleMap);
private readonly _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));
private readonly _options = new BehaviorSubject<google.maps.CircleOptions>({});
private readonly _center = new BehaviorSubject<
google.maps.LatLng | google.maps.LatLngLiteral | undefined
>(undefined);
private readonly _radius = new BehaviorSubject<number | undefined>(undefined);

private readonly _destroyed = new Subject<void>();

/**
Expand Down Expand Up @@ -161,10 +162,8 @@ export class MapCircle implements OnInit, OnDestroy {
@Output() readonly circleInitialized: EventEmitter<google.maps.Circle> =
new EventEmitter<google.maps.Circle>();

constructor(
private readonly _map: GoogleMap,
private readonly _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (!this._map._isBrowser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {MapEventManager} from '../map-event-manager';
standalone: true,
})
export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy {
private readonly _googleMap = inject(GoogleMap);
private _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));

/**
Expand Down Expand Up @@ -74,10 +76,8 @@ export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy {
/** The underlying google.maps.DirectionsRenderer object. */
directionsRenderer?: google.maps.DirectionsRenderer;

constructor(
private readonly _googleMap: GoogleMap,
private _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._googleMap._isBrowser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
/// <reference types="google.maps" preserve="true" />

import {Injectable, NgZone} from '@angular/core';
import {Injectable, NgZone, inject} from '@angular/core';
import {Observable} from 'rxjs';

export interface MapDirectionsResponse {
Expand All @@ -25,9 +25,11 @@ export interface MapDirectionsResponse {
*/
@Injectable({providedIn: 'root'})
export class MapDirectionsService {
private readonly _ngZone = inject(NgZone);
private _directionsService: google.maps.DirectionsService | undefined;

constructor(private readonly _ngZone: NgZone) {}
constructor(...args: unknown[]);
constructor() {}

/**
* See
Expand Down
6 changes: 4 additions & 2 deletions src/google-maps/map-geocoder/map-geocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
/// <reference types="google.maps" preserve="true" />

import {Injectable, NgZone} from '@angular/core';
import {Injectable, NgZone, inject} from '@angular/core';
import {Observable} from 'rxjs';

export interface MapGeocoderResponse {
Expand All @@ -23,9 +23,11 @@ export interface MapGeocoderResponse {
*/
@Injectable({providedIn: 'root'})
export class MapGeocoder {
private readonly _ngZone = inject(NgZone);
private _geocoder: google.maps.Geocoder | undefined;

constructor(private readonly _ngZone: NgZone) {}
constructor(...args: unknown[]);
constructor() {}

/**
* See developers.google.com/maps/documentation/javascript/reference/geocoder#Geocoder.geocode
Expand Down
9 changes: 4 additions & 5 deletions src/google-maps/map-ground-overlay/map-ground-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ import {MapEventManager} from '../map-event-manager';
standalone: true,
})
export class MapGroundOverlay implements OnInit, OnDestroy {
private readonly _map = inject(GoogleMap);
private readonly _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));

private readonly _opacity = new BehaviorSubject<number>(1);
private readonly _url = new BehaviorSubject<string>('');
private readonly _bounds = new BehaviorSubject<
Expand Down Expand Up @@ -96,10 +97,8 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
@Output() readonly groundOverlayInitialized: EventEmitter<google.maps.GroundOverlay> =
new EventEmitter<google.maps.GroundOverlay>();

constructor(
private readonly _map: GoogleMap,
private readonly _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._map._isBrowser) {
Expand Down
10 changes: 6 additions & 4 deletions src/google-maps/map-heatmap-layer/map-heatmap-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SimpleChanges,
Output,
EventEmitter,
inject,
} from '@angular/core';

import {GoogleMap} from '../google-map/google-map';
Expand All @@ -41,6 +42,9 @@ export type HeatmapData =
standalone: true,
})
export class MapHeatmapLayer implements OnInit, OnChanges, OnDestroy {
private readonly _googleMap = inject(GoogleMap);
private _ngZone = inject(NgZone);

/**
* Data shown on the heatmap.
* See: https://developers.google.com/maps/documentation/javascript/reference/visualization
Expand Down Expand Up @@ -72,10 +76,8 @@ export class MapHeatmapLayer implements OnInit, OnChanges, OnDestroy {
@Output() readonly heatmapInitialized: EventEmitter<google.maps.visualization.HeatmapLayer> =
new EventEmitter<google.maps.visualization.HeatmapLayer>();

constructor(
private readonly _googleMap: GoogleMap,
private _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._googleMap._isBrowser) {
Expand Down
10 changes: 5 additions & 5 deletions src/google-maps/map-info-window/map-info-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import {MapAnchorPoint} from '../map-anchor-point';
host: {'style': 'display: none'},
})
export class MapInfoWindow implements OnInit, OnDestroy {
private readonly _googleMap = inject(GoogleMap);
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
private _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));
private readonly _options = new BehaviorSubject<google.maps.InfoWindowOptions>({});
private readonly _position = new BehaviorSubject<
Expand Down Expand Up @@ -105,11 +108,8 @@ export class MapInfoWindow implements OnInit, OnDestroy {
@Output() readonly infoWindowInitialized: EventEmitter<google.maps.InfoWindow> =
new EventEmitter<google.maps.InfoWindow>();

constructor(
private readonly _googleMap: GoogleMap,
private _elementRef: ElementRef<HTMLElement>,
private _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._googleMap._isBrowser) {
Expand Down
8 changes: 4 additions & 4 deletions src/google-maps/map-kml-layer/map-kml-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {MapEventManager} from '../map-event-manager';
standalone: true,
})
export class MapKmlLayer implements OnInit, OnDestroy {
private readonly _map = inject(GoogleMap);
private _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));
private readonly _options = new BehaviorSubject<google.maps.KmlLayerOptions>({});
private readonly _url = new BehaviorSubject<string>('');
Expand Down Expand Up @@ -83,10 +85,8 @@ export class MapKmlLayer implements OnInit, OnDestroy {
@Output() readonly kmlLayerInitialized: EventEmitter<google.maps.KmlLayer> =
new EventEmitter<google.maps.KmlLayer>();

constructor(
private readonly _map: GoogleMap,
private _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._map._isBrowser) {
Expand Down
14 changes: 6 additions & 8 deletions src/google-maps/map-marker-clusterer/map-marker-clusterer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ declare const MarkerClusterer: typeof MarkerClustererInstance;
exportAs: 'mapMarkerClusterer',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
template: '<ng-content />',
template: '<ng-content/>',
encapsulation: ViewEncapsulation.None,
})
export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges, OnDestroy {
private readonly _googleMap = inject(GoogleMap);
private readonly _ngZone = inject(NgZone);
private readonly _currentMarkers = new Set<google.maps.Marker>();
private readonly _eventManager = new MapEventManager(inject(NgZone));
private readonly _destroy = new Subject<void>();

/** Whether the clusterer is allowed to be initialized. */
private readonly _canInitialize: boolean;
private readonly _canInitialize = this._googleMap._isBrowser;

@Input()
ariaLabelFn: AriaLabelFn = () => '';
Expand Down Expand Up @@ -212,12 +214,8 @@ export class MapMarkerClusterer implements OnInit, AfterContentInit, OnChanges,
@Output() readonly markerClustererInitialized: EventEmitter<MarkerClustererInstance> =
new EventEmitter<MarkerClustererInstance>();

constructor(
private readonly _googleMap: GoogleMap,
private readonly _ngZone: NgZone,
) {
this._canInitialize = _googleMap._isBrowser;
}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._canInitialize) {
Expand Down
8 changes: 4 additions & 4 deletions src/google-maps/map-marker/map-marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export const DEFAULT_MARKER_OPTIONS = {
standalone: true,
})
export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
private readonly _googleMap = inject(GoogleMap);
private _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));

/**
Expand Down Expand Up @@ -277,10 +279,8 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
*/
marker?: google.maps.Marker;

constructor(
private readonly _googleMap: GoogleMap,
private _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (!this._googleMap._isBrowser) {
Expand Down
8 changes: 4 additions & 4 deletions src/google-maps/map-polygon/map-polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {MapEventManager} from '../map-event-manager';
standalone: true,
})
export class MapPolygon implements OnInit, OnDestroy {
private readonly _map = inject(GoogleMap);
private readonly _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));
private readonly _options = new BehaviorSubject<google.maps.PolygonOptions>({});
private readonly _paths = new BehaviorSubject<
Expand Down Expand Up @@ -141,10 +143,8 @@ export class MapPolygon implements OnInit, OnDestroy {
@Output() readonly polygonInitialized: EventEmitter<google.maps.Polygon> =
new EventEmitter<google.maps.Polygon>();

constructor(
private readonly _map: GoogleMap,
private readonly _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._map._isBrowser) {
Expand Down
8 changes: 4 additions & 4 deletions src/google-maps/map-polyline/map-polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {MapEventManager} from '../map-event-manager';
standalone: true,
})
export class MapPolyline implements OnInit, OnDestroy {
private readonly _map = inject(GoogleMap);
private _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));
private readonly _options = new BehaviorSubject<google.maps.PolylineOptions>({});
private readonly _path = new BehaviorSubject<
Expand Down Expand Up @@ -139,10 +141,8 @@ export class MapPolyline implements OnInit, OnDestroy {
@Output() readonly polylineInitialized: EventEmitter<google.maps.Polyline> =
new EventEmitter<google.maps.Polyline>();

constructor(
private readonly _map: GoogleMap,
private _ngZone: NgZone,
) {}
constructor(...args: unknown[]);
constructor() {}

ngOnInit() {
if (this._map._isBrowser) {
Expand Down
Loading

0 comments on commit db7d560

Please sign in to comment.