Repository URL to install this package:
|
Version:
0.0.13 ▾
|
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