load-google-maps-api
Advanced tools
Comparing version 1.3.0 to 1.3.1
# Changelog | ||
## 1.3.1 | ||
- Support repeated/duplicate calls by caching and returning the initial Promise | ||
## 1.3.0 | ||
@@ -4,0 +8,0 @@ |
65
index.js
@@ -5,43 +5,44 @@ var CALLBACK_NAME = '__googleMapsApiOnLoadCallback' | ||
var promise = null | ||
module.exports = function (options) { | ||
options = options || {} | ||
return new Promise(function (resolve, reject) { | ||
// Check if the Google Maps API has already been loaded | ||
if (window.google && window.google.maps) { | ||
return resolve(window.google.maps) | ||
} | ||
if (!promise) { | ||
promise = new Promise(function (resolve, reject) { | ||
// Reject the promise after a timeout | ||
var timeoutId = setTimeout(function () { | ||
window[CALLBACK_NAME] = function () {} // Set the on load callback to a no-op | ||
reject(new Error('Could not load the Google Maps API')) | ||
}, options.timeout || 10000) | ||
// Reject the promise after a timeout | ||
var timeoutId = setTimeout(function () { | ||
window[CALLBACK_NAME] = function () {} // Set the on load callback to a no-op | ||
reject(new Error('Could not load the Google Maps API')) | ||
}, options.timeout || 10000) | ||
// Hook up the on load callback | ||
window[CALLBACK_NAME] = function () { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId) | ||
} | ||
resolve(window.google.maps) | ||
delete window[CALLBACK_NAME] | ||
} | ||
// Hook up the on load callback | ||
window[CALLBACK_NAME] = function () { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId) | ||
// Prepare the `script` tag to be inserted into the page | ||
var scriptElement = document.createElement('script') | ||
var params = ['callback=' + CALLBACK_NAME] | ||
OPTIONS_KEYS.forEach(function (key) { | ||
if (options[key]) { | ||
params.push(key + '=' + options[key]) | ||
} | ||
}) | ||
if (options.libraries && options.libraries.length) { | ||
params.push('libraries=' + options.libraries.join(',')) | ||
} | ||
resolve(window.google.maps) | ||
delete window[CALLBACK_NAME] | ||
} | ||
scriptElement.src = | ||
'https://maps.googleapis.com/maps/api/js?' + params.join('&') | ||
// Prepare the `script` tag to be inserted into the page | ||
var scriptElement = document.createElement('script') | ||
var params = ['callback=' + CALLBACK_NAME] | ||
OPTIONS_KEYS.forEach(function (key) { | ||
if (options[key]) { | ||
params.push(key + '=' + options[key]) | ||
} | ||
// Insert the `script` tag | ||
document.body.appendChild(scriptElement) | ||
}) | ||
if (options.libraries && options.libraries.length) { | ||
params.push('libraries=' + options.libraries.join(',')) | ||
} | ||
scriptElement.src = | ||
'https://maps.googleapis.com/maps/api/js?' + params.join('&') | ||
} | ||
// Insert the `script` tag | ||
document.body.appendChild(scriptElement) | ||
}) | ||
return promise | ||
} |
{ | ||
"name": "load-google-maps-api", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "A thin, Promise-returning helper for loading the Google Maps JavaScript API.", | ||
@@ -14,2 +14,4 @@ "author": "Lim Yuan Qing", | ||
"gzip-size-cli": "^2.1.0", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^7.1.0", | ||
"prettier-standard": "^8.0.0", | ||
@@ -29,2 +31,8 @@ "rimraf": "^2.6.2", | ||
}, | ||
"lint-staged": { | ||
"*.js": [ | ||
"standard", | ||
"git add" | ||
] | ||
}, | ||
"files": [ | ||
@@ -31,0 +39,0 @@ "index.js" |
@@ -7,3 +7,3 @@ # load-google-maps-api [![npm Version](http://img.shields.io/npm/v/load-google-maps-api.svg?style=flat)](https://www.npmjs.com/package/load-google-maps-api) [![Build Status](https://img.shields.io/travis/yuanqing/load-google-maps-api.svg?branch=master&style=flat)](https://travis-ci.org/yuanqing/load-google-maps-api) | ||
- Optionally set a timeout, an API key, the language, [and more](#loadgooglemapsapioptions) | ||
- 429 bytes gzipped | ||
- 424 bytes gzipped | ||
@@ -10,0 +10,0 @@ ## Usage |
7857
10