Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
const API_URL = 'https://maps.googleapis.com/maps/api/js'
const CALLBACK_NAME = '__googleMapsApiOnLoadCallback'
const GOOGLE_SCRIPT_ID = 'google-map-script'

const optionsKeys = ['channel', 'client', 'key', 'language', 'region', 'v']

let promise = null
let preOptions = null

function isOptEq (preOptions, options) {
return JSON.stringify(preOptions) !== JSON.stringify(options)
}

module.exports = function (options = {}) {
if (isOptEq(preOptions, options) && promise !== null) {
promise = null
preOptions = options
const child = document.getElementById(GOOGLE_SCRIPT_ID)
if (child) {
child.parentNode.removeChild(child)
delete window.google.maps
}
}
promise =
promise ||
new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -36,6 +51,7 @@ module.exports = function (options = {}) {
params.push(`libraries=${options.libraries.join(',')}`)
}
scriptElement.src = `${options.apiUrl || API_URL}?${params.join('&')}`
scriptElement.id = GOOGLE_SCRIPT_ID

// Insert the `script` tag
document.body.appendChild(scriptElement)
Expand Down