Comments (4)
See commit b08852a
from dynamically-polyfill-features-for-a-script.
#11 sort of caters for this, but there's an issue... the init functions appear to execute before we've gotten the necessary third party scripts (using quicklink()
as an example).
One option:
Extract the loadMyScript()
function out into a couple of small helper functions:
function loadMyScript(url, functionToRunonLoad) {
if(Array.isArray(url)) {
var urlen = url.length;
for (var u = 0; u < urlen; u++) {
var uri = url[u];
if(uri !== null && uri !== '') {
nonblankURL(uri).then(function(){initialiseMyScript(functionToRunonLoad)});
}
}
} else if (!Array.isArray(url) && url !== null && url !== '') {
return nonblankURL(url).then(function(){initialiseMyScript(functionToRunonLoad)});
} else {
return initialiseMyScript(functionToRunonLoad); //straight to init because no dependency
}
}
function nonblankURL(uri){
return new Promise(
function(resolve, reject) {
var thescript = document.createElement('script');
thescript.src = encodeURI(uri);
document.body.appendChild(thescript);
thescript.onerror = function(response) {
return reject("Loading the script failed!", response);
}
thescript.onload = function() {
return resolve("Script has loaded!");
}
}
)
}
This would also require refactoring of pageLoaded()
:
function pageLoaded(polyfillFeatures, scriptToPolyfill, functionToRunonLoad) {
checkNativeSupport(polyfillFeatures)
.then(
function() {
loadMyScript(scriptToPolyfill, functionToRunonLoad)
}
).catch(function(error){return error})
}
But is this the best route to follow?
from dynamically-polyfill-features-for-a-script.
The only problem with the above is that iterating through the arrays requires both to be aligned.
i.e. [Script1, Script2], [initScript1, initScript2]
If a user were to, for instance, order the arrays differently, things would fail because the dependency may not have loaded yet.
i.e. [Script1, Script2], [initScript2, initScript1]
Furthermore, what happens if we:
[Script1, Script2], initScripts
Where initScripts has a dependency on both scripts?
from dynamically-polyfill-features-for-a-script.
ONLY GONE AND FOUND A WAY!!!!
Array.forEach()
from dynamically-polyfill-features-for-a-script.
Related Issues (15)
- [Enhancement] Reliant on promises... which aren't fully supported in browsers that need polyfills (IE) HOT 6
- [Review] Optimal performance & supportability HOT 3
- [Bug] Safari throws a bunch of errors initially HOT 2
- [Enhancement] checkNative if statement could be better
- [Bug] Chrome not executing init functions
- [Enhancement] Create a version of the script with inline comments
- [Bug] Polyfill.io is still being called when no polyfill required
- [Bug] Multiple polyfill.io requests rather than just one
- [Feature Request] typeOf item != ‘function’ missing as a native support check HOT 1
- [Enhancement] Code Cognitive Complexity HOT 1
- [Bug] Uglifying JS breaks... everything!
- [Enhancement] Further native feature checks
- Whole script needs to be a bit more "dynamic" HOT 3
- [Feature Request] Allow array of scripts to load HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dynamically-polyfill-features-for-a-script.