Repository URL to install this package:
Version:
0.9.6 ▾
|
import React from 'react'
import { isObj } from 'exotic'
import { createScriptTag } from '@skava/modules/___dist/utils/createScriptTag'
const IS_BROWSER = typeof window === 'object'
// process.env.GOOGLE_API_KEY
// For temporary fix.. Needs to move this in @skava/modules/___dist config
const GOOGLE_API_KEY = 'AIzaSyA08efSv82XGllA0frxNT08-_W7sdGgA5A'
const sleep = () => new Promise(resolve => setTimeout(resolve, 0))
// @todo >>>>>>>>>>>>> CLEANUP, rewrite
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(): Promise {
// 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(
`https://maps.googleapis.com/maps/api/js?key=${GOOGLE_API_KEY}&callback=onGoogleApisLoad&libraries=places`
)
return handleGoogleApisLoad()
}
export { loadGoogle }