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    
ui-component-library / src / components / atoms / Map / deps / loadGoogle.ts
Size: Mime:
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 }