GithubHelp home page GithubHelp logo

Comments (4)

willstocks avatar willstocks commented on May 31, 2024

See commit b08852a

from dynamically-polyfill-features-for-a-script.

willstocks avatar willstocks commented on May 31, 2024

#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.

willstocks avatar willstocks commented on May 31, 2024

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.

willstocks avatar willstocks commented on May 31, 2024

ONLY GONE AND FOUND A WAY!!!!

Array.forEach()

from dynamically-polyfill-features-for-a-script.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.