@braintree/asset-loader
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -0,1 +1,5 @@ | ||
# 0.2.1 | ||
* Set `loadScript` to cache the promise used to load the script to eliiminate a race condition where the script could be on the page, but not ready to use | ||
# 0.2.0 | ||
@@ -2,0 +6,0 @@ |
'use strict'; | ||
var Promise = require('./lib/promise'); | ||
var scriptPromiseCache = {}; | ||
module.exports = function loadScript(options) { | ||
var attrs, container, script; | ||
function loadScript(options) { | ||
var attrs, container, script, scriptLoadPromise; | ||
var stringifiedOptions = JSON.stringify(options); | ||
if (!options.forceScriptReload) { | ||
script = document.querySelector('script[src="' + options.src + '"]'); | ||
scriptLoadPromise = scriptPromiseCache[stringifiedOptions]; | ||
if (script) { | ||
return Promise.resolve(script); | ||
if (scriptLoadPromise) { | ||
return scriptLoadPromise; | ||
} | ||
@@ -28,3 +30,3 @@ } | ||
return new Promise(function (resolve, reject) { | ||
scriptLoadPromise = new Promise(function (resolve, reject) { | ||
script.addEventListener('load', function () { | ||
@@ -41,2 +43,12 @@ resolve(script); | ||
}); | ||
scriptPromiseCache[stringifiedOptions] = scriptLoadPromise; | ||
return scriptLoadPromise; | ||
} | ||
loadScript.clearCache = function () { | ||
scriptPromiseCache = {}; | ||
}; | ||
module.exports = loadScript; |
{ | ||
"name": "@braintree/asset-loader", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "A module to load frontend assets.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
6392
71