Is there a way to ensure that your api pages are loaded in a certain order? I have a potential race condition in a series of scripts I am using. Page A: General Commands General = General || {};
General.CreateCharacter = function(characterName){
return createObj('character', { name : characterName });
} Page B: Initializer Initializer = Initializer || {};
Initializer.ToDoList = Initializer.ToDoList || [];
Initializer.Installer = Initializer.Installer || {};
Initializer.Installer.ToDoList = Initializer.Installer.ToDoList || [];
Initializer.Initialize = function(){
var LoadItems = [];
var installedListHandout = General.GetOrCreateHandout('Installed Modules');
installedListHandout.get('notes',function(installedTable){
var installedArray = [];
if(installedTable != ''){
installedArray = General.TableToJSONArray(installedTable);
}
_.each(Initializer.Installer.ToDoList,function(pendingModule){
var triggerAction = "Install";
_.each(installedArray,function(installedModule){
if(installedModule.Name == pendingModule.Name && installedModule.Version == pendingModule.Version){
triggerAction = "None";
} else if(installedModule.Name == pendingModule.Name && installedModule.Version > pendingModule.Version){
triggerAction = "Version Rollback";
} else if(installedModule.Name == pendingModule.Name && installedModule.Version < pendingModule.Version){
triggerAction = "Version Update";
}
});
if(triggerAction == "Install"){
LoadItems.push(pendingModule);
}
});
});
_.each(Initializer.ToDoList,function(pendingModule){
pendingModule.LoadOrder = parseFloat(pendingModule.LoadOrder) + parseFloat(0.1);
LoadItems.push(pendingModule);
});
LoadItems = LoadItems.sort(function(a,b){ return parseFloat(a.LoadOrder) - parseFloat(b.LoadOrder)});
_.each(LoadItems,function(loadItem){
loadItem.Execute();
});
}
on('ready',function(){ Initializer.Initialize(); sendChat('API','/desc The API is ready.'});
Page C: DataStore DataStore = DataStore || {};
DataStore.Key = 'DataStore';
DataStore.Version = 1;
DataStore.LoadOrder = 1;
DataStore.Repository = {};
DataStore.Initializer = function(){
DataStore.Repository = findObjs({ type: 'character', name: 'Data Store'})[0];
}
DataStore.Installer = function(){
General.CreateCharacter('Data Store');
}
Initializer.ToDoList = Initializer.ToDoList || [];
Initializer.Installer = Initializer.Installer || {};
Initializer.Installer.ToDoList = Initializer.Installer.ToDoList || [];
Initializer.Installer.ToDoList.push({ 'Name' : DataStore.Key, 'Version' : DataStore.Version, 'LoadOrder' : DataStore.LoadOrder, 'Execute' : DataStore.Installer });
Initializer.ToDoList.push({ 'Name' : DataStore.Key, 'Version' : DataStore.Version, 'LoadOrder' : DataStore.LoadOrder, 'Execute' : DataStore.Initializer });
The problem arises if Page C somehow gets loaded before Page A. The General.CreateCharacter function will not be declared when the DataStore.Installer function is declared, and may not even be declared when the DataStore.Installer function is pushed to the Initializer.Installer.ToDoList. Normally, I would add the script tags to an HTML page in the order I wish them to be loaded, but I don't have that option with Roll20. Do pages run in the order they are tabbed? Is there some other way to ensure they load in a specific order?