Comparing version 0.0.2-0 to 0.0.3-0
@@ -67,2 +67,4 @@ window["get"] = | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _util = __webpack_require__(/*! ./util */ 2); | ||
@@ -72,29 +74,46 @@ | ||
function isResolved(src) { | ||
return resolved[src] === true || !!document.querySelector('script[src="' + src + '"]'); | ||
} | ||
function loadScript(url, callback, errorCallback) { | ||
var invokeCallback = function invokeCallback() { | ||
resolved[url] = true; | ||
function loadScript(src) { | ||
if (isResolved(src)) { | ||
return Promise.resolve(true); | ||
if ((0, _util.isFunction)(callback)) { | ||
callback(); | ||
} | ||
}; | ||
if (resolved[url]) { | ||
invokeCallback(); | ||
return; | ||
} | ||
var s = document.createElement('script'); | ||
var script = document.createElement('script'); | ||
script.type = 'text/javascript'; | ||
var promise = new Promise(function (resolve, reject) { | ||
s.src = src; | ||
s.async = false; | ||
s.type = 'text/javascript'; | ||
s.onload = function () { | ||
resolve(resolved[src] = true); | ||
if (script.readyState) { | ||
//IE | ||
script.onreadystatechange = function () { | ||
if (script.readyState == 'loaded' || script.readyState == 'complete') { | ||
script.onreadystatechange = null; | ||
invokeCallback(); | ||
} | ||
}; | ||
} else { | ||
//Others | ||
script.onload = function () { | ||
invokeCallback(); | ||
}; | ||
} | ||
s.onerror = reject; | ||
}); | ||
script.onerror = function (e) { | ||
resolved[url] = false; | ||
console.log('error', e); | ||
if ((0, _util.isFunction)(errorCallback)) { | ||
errorCallback(); | ||
} | ||
}; | ||
script.src = url; | ||
var parent = document.body || document.head || document; | ||
parent.appendChild(s); | ||
return promise; | ||
parent.appendChild(script); | ||
} | ||
@@ -104,14 +123,28 @@ | ||
if ((0, _util.isString)(src)) { | ||
return loadScript(src); | ||
} else if ((0, _util.isArray)(src) && src.length > 0) { | ||
var p; | ||
src.forEach(function (url) { | ||
p = p ? p.then(get(url)) : get(url); | ||
return new Promise(function (resolve, reject) { | ||
loadScript(src, function () { | ||
return resolve(true); | ||
}, function () { | ||
return reject(); | ||
}); | ||
}); | ||
} else if ((0, _util.isArray)(src)) { | ||
var _ret = function () { | ||
var p = Promise.resolve(true); | ||
return p; | ||
} else { | ||
throw new Error('Invalid argument for get()'); | ||
src.forEach(function (url) { | ||
p = p.then(function () { | ||
return get(url); | ||
}); | ||
}); | ||
return { | ||
v: p | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
} | ||
throw new Error('Invalid argument for get()'); | ||
} | ||
@@ -140,3 +173,3 @@ | ||
var isFunction = function isFunction(v) { | ||
return typeof c === 'function'; | ||
return typeof v === 'function'; | ||
}; | ||
@@ -143,0 +176,0 @@ |
@@ -1,1 +0,1 @@ | ||
window.get=function(t){function r(n){if(e[n])return e[n].exports;var o=e[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,r),o.loaded=!0,o.exports}var e={};return r.m=t,r.c=e,r.p="",r(0)}([function(t,r,e){t.exports=e(1)["default"]},function(t,r,e){"use strict";function n(t){return c[t]===!0||!!document.querySelector('script[src="'+t+'"]')}function o(t){if(n(t))return Promise.resolve(!0);var r=document.createElement("script"),e=new Promise(function(e,n){r.src=t,r.async=!1,r.type="text/javascript",r.onload=function(){e(c[t]=!0)},r.onerror=n}),o=document.body||document.head||document;return o.appendChild(r),e}function i(t,r){if((0,u.isString)(t))return o(t);if((0,u.isArray)(t)&&t.length>0){var e;return t.forEach(function(t){e=e?e.then(i(t)):i(t)}),e}throw new Error("Invalid argument for get()")}Object.defineProperty(r,"__esModule",{value:!0});var u=e(2),c={};r["default"]=i},function(t,r){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var e=function(t){return"[object Array]"===Object.prototype.toString.call(t)},n=function(t){return"string"==typeof t},o=function(t){return"function"==typeof c};r.isArray=e,r.isString=n,r.isFunction=o}]); | ||
window.get=function(t){function n(r){if(e[r])return e[r].exports;var o=e[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,n),o.loaded=!0,o.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){t.exports=e(1)["default"]},function(t,n,e){"use strict";function r(t,n,e){var r=function(){c[t]=!0,(0,u.isFunction)(n)&&n()};if(c[t])return void r();var o=document.createElement("script");o.type="text/javascript",o.readyState?o.onreadystatechange=function(){"loaded"!=o.readyState&&"complete"!=o.readyState||(o.onreadystatechange=null,r())}:o.onload=function(){r()},o.onerror=function(n){c[t]=!1,console.log("error",n),(0,u.isFunction)(e)&&e()},o.src=t;var i=document.body||document.head||document;i.appendChild(o)}function o(t,n){if((0,u.isString)(t))return new Promise(function(n,e){r(t,function(){return n(!0)},function(){return e()})});if((0,u.isArray)(t)){var e=function(){var n=Promise.resolve(!0);return t.forEach(function(t){n=n.then(function(){return o(t)})}),{v:n}}();if("object"===("undefined"==typeof e?"undefined":i(e)))return e.v}throw new Error("Invalid argument for get()")}Object.defineProperty(n,"__esModule",{value:!0});var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t},u=e(2),c={};n["default"]=o},function(t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var e=function(t){return"[object Array]"===Object.prototype.toString.call(t)},r=function(t){return"string"==typeof t},o=function(t){return"function"==typeof t};n.isArray=e,n.isString=r,n.isFunction=o}]); |
@@ -7,2 +7,4 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _util = require('./util'); | ||
@@ -12,29 +14,46 @@ | ||
function isResolved(src) { | ||
return resolved[src] === true || !!document.querySelector('script[src="' + src + '"]'); | ||
} | ||
function loadScript(url, callback, errorCallback) { | ||
var invokeCallback = function invokeCallback() { | ||
resolved[url] = true; | ||
function loadScript(src) { | ||
if (isResolved(src)) { | ||
return Promise.resolve(true); | ||
if ((0, _util.isFunction)(callback)) { | ||
callback(); | ||
} | ||
}; | ||
if (resolved[url]) { | ||
invokeCallback(); | ||
return; | ||
} | ||
var s = document.createElement('script'); | ||
var script = document.createElement('script'); | ||
script.type = 'text/javascript'; | ||
var promise = new Promise(function (resolve, reject) { | ||
s.src = src; | ||
s.async = false; | ||
s.type = 'text/javascript'; | ||
s.onload = function () { | ||
resolve(resolved[src] = true); | ||
if (script.readyState) { | ||
//IE | ||
script.onreadystatechange = function () { | ||
if (script.readyState == 'loaded' || script.readyState == 'complete') { | ||
script.onreadystatechange = null; | ||
invokeCallback(); | ||
} | ||
}; | ||
} else { | ||
//Others | ||
script.onload = function () { | ||
invokeCallback(); | ||
}; | ||
} | ||
s.onerror = reject; | ||
}); | ||
script.onerror = function (e) { | ||
resolved[url] = false; | ||
console.log('error', e); | ||
if ((0, _util.isFunction)(errorCallback)) { | ||
errorCallback(); | ||
} | ||
}; | ||
script.src = url; | ||
var parent = document.body || document.head || document; | ||
parent.appendChild(s); | ||
return promise; | ||
parent.appendChild(script); | ||
} | ||
@@ -44,16 +63,30 @@ | ||
if ((0, _util.isString)(src)) { | ||
return loadScript(src); | ||
} else if ((0, _util.isArray)(src) && src.length > 0) { | ||
var p; | ||
src.forEach(function (url) { | ||
p = p ? p.then(get(url)) : get(url); | ||
return new Promise(function (resolve, reject) { | ||
loadScript(src, function () { | ||
return resolve(true); | ||
}, function () { | ||
return reject(); | ||
}); | ||
}); | ||
} else if ((0, _util.isArray)(src)) { | ||
var _ret = function () { | ||
var p = Promise.resolve(true); | ||
return p; | ||
} else { | ||
throw new Error('Invalid argument for get()'); | ||
src.forEach(function (url) { | ||
p = p.then(function () { | ||
return get(url); | ||
}); | ||
}); | ||
return { | ||
v: p | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
} | ||
throw new Error('Invalid argument for get()'); | ||
} | ||
exports.default = get; |
@@ -13,3 +13,3 @@ 'use strict'; | ||
var isFunction = function isFunction(v) { | ||
return typeof c === 'function'; | ||
return typeof v === 'function'; | ||
}; | ||
@@ -16,0 +16,0 @@ |
{ | ||
"name": "get-js", | ||
"version": "0.0.2-0", | ||
"description": "A Promise based package to load scripts dynamically", | ||
"version": "0.0.3-0", | ||
"description": "A lightweight promise based package to load scripts on the fly", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "dependencies": {}, |
@@ -6,3 +6,3 @@ # Get JS | ||
A lightweight Promise based package to load scripts dynamically in the browser. | ||
A lightweight promise based package to load scripts on the fly. | ||
@@ -9,0 +9,0 @@ ## Installation |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18008
237