All files / app/address/googleAutocomplete GoogleAutocompleteService.ts

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 4525x     25x         10x 20x     25x                             25x                               25x  
import getGoogleAutocompleteScriptLoader from './getGoogleAutocompleteScriptLoader';
import GoogleAutocompleteScriptLoader from './GoogleAutocompleteScriptLoader';
 
export default class GoogleAutocompleteService {
    private _autocompletePromise?: Promise<google.maps.places.AutocompleteService>;
    private _placesPromise?: Promise<google.maps.places.PlacesService>;
 
    constructor(
        private _apiKey: string,
        private E_scriptLoader: GoogleAutocompleteScriptLoader = getGoogleAutocompleteScriptLoader()
    ) {}
 
    getAutocompleteService(): Promise<google.maps.places.AutocompleteService> {
        if (!this._autocompletePromise) {
            this._autocompletePromise = this._scriptLoader.loadMapsSdk(this._apiKey)
                .then(googleMapsSdk => {
                    if (!googleMapsSdk.places.AutocompleteService) {
                        throw new Error('`AutocompleteService` is undefined');
                    }
 
                    return new googleMapsSdk.places.AutocompleteService();
                });
        }
 
        return this._autocompletePromise;
    }
 
    getPlacesServices(): Promise<google.maps.places.PlacesService> {
        const node = document.createElement('div');
 
        if (!this._placesPromise) {
            this._placesPromise = this._scriptLoader.loadMapsSdk(this._apiKey)
                .then(googleMapsSdk => {
                    if (!googleMapsSdk.places.PlacesService) {
                        throw new Error('`PlacesService` is undefined');
                    }
 
                    return new googleMapsSdk.places.PlacesService(node);
                });
        }
 
        return this._placesPromise;
    }
}