Needed to load some external scripts.
Like it to be in one location, for the entire story. Avoid duplication.
$.getScript(url) in Story Javascript
Unfortunately, you run into race conditions all over the place.
Both in the Story Javascript, and in passages. It'd be nice to either make this easier(story html so you could use <script> tags?), or have a tutorial on how to do this(these issues are related to snowman's structure, not js in general).
//This just loads them in parallel
f.ExtLibs = function (libs) {
return new Promise(function(resolve, reject) {
var toReturn = [];
var previousError = false;
libs.map(function(url) {
$.getScript(url)
.done(function( script, textStatus ) {
if (previousError) { return; }
toReturn.push(url +":" + textStatus);
if (toReturn.length === libs.length) {
resolve(null, toReturn);
}
})
.fail(function( jqxhr, settings, exception ) {
previousError = true;
reject('Error' + exception);
});
});
});
};
//this fixes story js race conditions, and does the actual loading
f.load = async function(){
f.loaded = f.ExtLibs(ext_libs);
await f.loaded;
<code using external libs within story javascript.>
}
f.load();
//this fixes the passage race conditions
window.story.oldshow = window.story.show;
window.story.show = async function (idOrName, noHistory = false) {
await f.loaded;
window.story.oldshow(idOrName, noHistory);
};