public void BuildDownFromTop(ScriptsBag scriptsBag, TreeSharedDependencyMap sharedDependencies) { // Loop through its dependencies, add to (create?) layer below int targetLayer = 0; var currentScript = Layers[0].Scripts.Values.First(); var scriptsForLayer = new HashSet<string>(currentScript.Dependencies); bool layerAdded = true; for (targetLayer++; layerAdded; targetLayer++) { if (scriptsForLayer.Count == 0) { break; } // This layer may have already been added by PushDown; if not, add it EnsureLayer(targetLayer); foreach (string scriptName in scriptsForLayer) { // Has this script already been added to this tree? if (!ScriptLayerMap.ContainsKey(scriptName)) { // No AddScriptToLayer(targetLayer, scriptName, scriptsBag, sharedDependencies); continue; } // Yes MaybePushDown(scriptName, targetLayer, scriptsBag, sharedDependencies); } if (!layerAdded) continue; scriptsForLayer = Layers[targetLayer].Scripts.Values.SelectMany(s => s.Dependencies).ToHashSet(); } }
public HashSet<ScriptLoadGroup> MapScripts(HashSet<ScriptResource> pageScripts, Dictionary<string, ScriptResource> appScripts) { ScriptsBag = new ScriptsBag(appScripts); // First, start the trees, and map the page scripts into the source startTrees(pageScripts); // Now build trees downwards in isolation buildTreesDownward(); consolidateSharedDependencies(); // Merge trees where we find shared dependencies while (mergeTrees()) {// mergeTrees does the work and returns false when it's done } var groupChainRootsFromTrees = new HashSet<ScriptLoadGroup>(); foreach(var tree in Trees) groupChainRootsFromTrees.Add(tree.ToScriptLoadGroupChain()); return groupChainRootsFromTrees; }
// Recursive public void MaybePushDown(string scriptName, int targetLayer, ScriptsBag scriptsBag, TreeSharedDependencyMap sharedDependencies) { //if (ScriptLayerMap[scriptName] == targetLayer) //{ // We don't need to do anything, the script is already here - skip it //} // The dependency script may not have been added yet in a recursive PushDown, even if it would have been had we waited - // check and allow for it (it needs to be added instead of pushed down) if (!ScriptLayerMap.ContainsKey(scriptName) || ScriptLayerMap[scriptName] < targetLayer) { // The script is already added higher in the tree, preventing it from loading before it's needed by the script // calling this as a dependency. That means it needs to be pushed lower in the tree, to the targetLayer. PushDown(scriptName, targetLayer, scriptsBag, sharedDependencies); } }
public void MoveScriptInTree(int targetLayer, string scriptName, ScriptsBag scriptsBag) { Layers[ScriptLayerMap[scriptName]].Scripts.Remove(scriptName); ScriptLayerMap[scriptName] = targetLayer; Layers[targetLayer].AddScript(scriptName, scriptsBag[scriptName]); }
public void AddScriptToLayer(int layerNumber, string scriptName, ScriptsBag scriptsBag, TreeSharedDependencyMap sharedDependencies) { ScriptLayerMap.Add(scriptName, layerNumber); Layers[layerNumber].AddScript(scriptName, scriptsBag[scriptName]); sharedDependencies.ScriptIsInTree(scriptName, this); }
// Recursive public void PushDown(string scriptName, int targetLayer, ScriptsBag scriptsBag, TreeSharedDependencyMap sharedDependencies) { EnsureLayer(targetLayer); if (ScriptLayerMap.ContainsKey(scriptName)) // Push script from current to target layer MoveScriptInTree(targetLayer, scriptName, scriptsBag); else AddScriptToLayer(targetLayer, scriptName, scriptsBag, sharedDependencies); // then loop through its dependencies // and proceed pushing them down recursively targetLayer++; var dependencies = scriptsBag[scriptName].Dependencies; foreach (string dependency in dependencies) { MaybePushDown(dependency, targetLayer, scriptsBag, sharedDependencies); } }