1/** 2 * MIT License 3 * 4 * Copyright (c) 2018 cryptostorage 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 25// global loader instance 26var LOADER = new DependencyLoader(); 27 28/** 29 * Loads dependencies. 30 * 31 * Requires loadjs.js. 32 */ 33function DependencyLoader() { 34 35 var loadedImages = []; 36 37 /** 38 * Loads the given paths. 39 * 40 * @param paths is one or more paths to load as a string or string[] 41 * @param onDone(err) is invoked when the paths are loaded or fail 42 * @param maxThreads specifies the maximum number of parallel fetch requests (default 10) 43 */ 44 this.load = function(paths, onDone, maxThreads) { 45 maxThreads = maxThreads || 10; 46 47 // listify paths 48 if (!isArray(paths)) { 49 assertTrue(isString(paths)); 50 paths = [paths]; 51 } 52 53 // collect images and scripts that aren't loaded 54 var imagesToLoad = []; 55 var scriptsToLoad = []; 56 for (var i = 0; i < paths.length; i++) { 57 var path = paths[i]; 58 assertDefined(path); 59 if (path.endsWith(".png") || path.endsWith(".jpg") || path.endsWith(".gif")) { 60 if (!arrayContains(loadedImages, path)) imagesToLoad.push(path); 61 } else { 62 if (!loadjs.isDefined(path)) { 63 scriptsToLoad.push(path); 64 loadjs(path, path); 65 } 66 } 67 } 68 69 // done if everything loaded 70 if (!imagesToLoad.length && !scriptsToLoad.length) { 71 if (onDone) onDone(); 72 return; 73 } 74 75 // simulate load time 76 if (AppUtils.SIMULATED_LOAD_TIME) { 77 setTimeout(function() { loadAsync(); }, AppUtils.SIMULATED_LOAD_TIME); 78 } else loadAsync(); 79 80 // executes functions to fetch scripts and images 81 function loadAsync() { 82 var funcs = [getScriptsFunc(scriptsToLoad), getImagesFunc(imagesToLoad)]; 83 async.parallelLimit(funcs, maxThreads, function(err, result) { 84 if (onDone) onDone(err); 85 }); 86 } 87 88 function getScriptsFunc(paths) { 89 return function(onDone) { 90 if (!paths.length) onDone(); 91 else { 92 loadjs.ready(paths, { 93 success: onDone, 94 error: function() { onDone(new Error("Failed to load dependencies: " + paths)); } 95 }); 96 } 97 } 98 } 99 100 function getImagesFunc(paths) { 101 return function(onDone) { 102 getImages(paths, function(err) { 103 if (err) onDone(new Error("Failed to load images: " + paths)); 104 else { 105 loadedImages = loadedImages.concat(paths); 106 onDone(); 107 } 108 }); 109 } 110 } 111 } 112 113 /** 114 * Determines if the given paths are loaded. 115 * 116 * @param paths is one or more paths to check if loaded 117 */ 118 this.isLoaded = function(paths) { 119 120 // listify paths 121 if (!isArray(paths)) { 122 assertTrue(isString(paths)); 123 paths = [paths]; 124 } 125 126 // check if each path is loaded 127 for (var i = 0; i < paths.length; i++) { 128 if (!arrayContains(loadedImages, paths[i]) && !loadjs.isDefined(paths[i])) return false; 129 } 130 131 // all paths loaded 132 return true; 133 } 134}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.