Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/packages / core / location / google / loadGoogle.js
Size: Mime:
import { isObj } from 'exotic';
import { createScriptTag } from '@skava/utils';
import { GOOGLE_API_KEY as apiKey } from 'src/bootstrap/api/config';
import { IS_BROWSER, sleep } from './deps';
let attempts = 0;
let timeoutId = false;
// @todo - ref dom node or?
const ref = {};
function handleGoogleApisLoad() {
    attempts += 1;
    if (attempts >= 10) {
        return new Error('failed to load google');
    }
    else if (typeof google === 'undefined') {
        // can be awaited
        return new Promise(resolve => {
            timeoutId = setInterval(() => {
                if (typeof google === 'undefined') {
                    //
                }
                else {
                    clearInterval(timeoutId);
                    timeoutId = false;
                    resolve();
                }
            }, 1000);
            // if (timeoutId !== false) {
            //   clearTimeout(timeoutId)
            // }
            // timeoutId = setTimeout(handleGoogleApisLoad, 500)
            // resolve(timeoutId)
        });
    }
    else {
        return Promise.resolve(ref);
    }
    // const nodeForGoogle = (this.references.map)
}
function loadGoogle(url = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&callback=onGoogleApisLoad&libraries=places`) {
    // console.log('loadGoogle')
    if (!IS_BROWSER) {
        return sleep();
    }
    if (isObj(window.google)) {
        // we already loaded it
        return sleep();
    }
    window.onGoogleApisLoad = handleGoogleApisLoad;
    // @see https://developers.google.com/maps/documentation/javascript/libraries
    // async load the Google Maps script
    // passing in the callback reference/name
    createScriptTag(url);
    return handleGoogleApisLoad();
}
export { loadGoogle };
//# sourceMappingURL=loadGoogle.js.map